e31ad222a99572c4a6c0ec5204dc0ff51decdce9
[yaffs2.git] / direct / test-framework / yaffs_osglue.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2011 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 /*
15  * Example OS glue functions for running on a Linux/POSIX system.
16  */
17
18 #include "yaffscfg.h"
19 #include "yaffs_guts.h"
20 #include "yaffsfs.h"
21 #include "yaffs_trace.h"
22 #include <assert.h>
23
24 #include <errno.h>
25 #include <unistd.h>
26
27 /*
28  * yaffsfs_SetError() and yaffsfs_GetError()
29  * Do whatever to set the system error.
30  * yaffsfs_GetError() just fetches the last error.
31  */
32
33 static int yaffsfs_lastError;
34
35 void yaffsfs_SetError(int err)
36 {
37         //Do whatever to set error
38         yaffsfs_lastError = err;
39         errno = err;
40 }
41
42 int yaffsfs_GetLastError(void)
43 {
44         return yaffsfs_lastError;
45 }
46
47 /*
48  * yaffsfs_CheckMemRegion()
49  * Check that access to an address is valid.
50  * This can check memory is in bounds and is writable etc.
51  *
52  * Returns 0 if ok, negative if not.
53  */
54 int yaffsfs_CheckMemRegion(const void *addr, size_t size, int write_request)
55 {
56         if(!addr)
57                 return -1;
58         return 0;
59 }
60
61 /*
62  * yaffsfs_Lock()
63  * yaffsfs_Unlock()
64  * A single mechanism to lock and unlock yaffs. Hook up to a mutex or whatever.
65  * Here are two examples, one using POSIX pthreads, the other doing nothing.
66  *
67  * If we use pthreads then we also start a background gc thread.
68  */
69
70 #if 1
71
72 #include <pthread.h>
73
74 static pthread_mutex_t mutex1;
75 static pthread_t bc_gc_thread;
76
77 void yaffsfs_Lock(void)
78 {
79         pthread_mutex_lock( &mutex1 );
80 }
81
82 void yaffsfs_Unlock(void)
83 {
84         pthread_mutex_unlock( &mutex1 );
85 }
86
87 static void *bg_gc_func(void *dummy)
88 {
89         struct yaffs_dev *dev;
90         int urgent = 0;
91         int result;
92         int next_urgent;
93
94         /* Sleep for a bit to allow start up */
95         sleep(2);
96
97
98         while (1) {
99                 /* Iterate through devices, do bg gc updating ungency */
100                 yaffs_dev_rewind();
101                 next_urgent = 0;
102
103                 while ((dev = yaffs_next_dev()) != NULL) {
104                         result = yaffs_do_background_gc_reldev(dev, urgent);
105                         if (result > 0)
106                                 next_urgent = 1;
107                 }
108
109                 urgent = next_urgent;
110
111                 if (next_urgent)
112                         sleep(1);
113                 else
114                         sleep(5);
115         }
116
117         /* Don't ever return. */
118         return NULL;
119 }
120
121 void yaffsfs_LockInit(void)
122 {
123         /* Initialise lock */
124         pthread_mutex_init(&mutex1, NULL);
125
126         /* Sneak in starting a background gc thread too */
127         pthread_create(&bc_gc_thread, NULL, bg_gc_func, NULL);
128 }
129
130 #else
131
132 void yaffsfs_Lock(void)
133 {
134 }
135
136 void yaffsfs_Unlock(void)
137 {
138 }
139
140 void yaffsfs_LockInit(void)
141 {
142 }
143 #endif
144
145 /*
146  * yaffsfs_CurrentTime() retrns a 32-bit timestamp.
147  *
148  * Can return 0 if your system does not care about time.
149  */
150
151 u32 yaffsfs_CurrentTime(void)
152 {
153         return time(NULL);
154 }
155
156
157 /*
158  * yaffsfs_malloc()
159  * yaffsfs_free()
160  *
161  * Functions to allocate and free memory.
162  */
163
164 #ifdef CONFIG_YAFFS_TEST_MALLOC
165
166 static int yaffs_kill_alloc = 0;
167 static size_t total_malloced = 0;
168 static size_t malloc_limit = 0 & 6000000;
169
170 void *yaffsfs_malloc(size_t size)
171 {
172         void * this;
173         if(yaffs_kill_alloc)
174                 return NULL;
175         if(malloc_limit && malloc_limit <(total_malloced + size) )
176                 return NULL;
177
178         this = malloc(size);
179         if(this)
180                 total_malloced += size;
181         return this;
182 }
183
184 #else
185
186 void *yaffsfs_malloc(size_t size)
187 {
188         return malloc(size);
189 }
190
191 #endif
192
193 void yaffsfs_free(void *ptr)
194 {
195         free(ptr);
196 }
197
198 void yaffsfs_OSInitialisation(void)
199 {
200         yaffsfs_LockInit();
201 }
202
203 /*
204  * yaffs_bug_fn()
205  * Function to report a bug.
206  */
207
208 void yaffs_bug_fn(const char *file_name, int line_no)
209 {
210         printf("yaffs bug detected %s:%d\n",
211                 file_name, line_no);
212         assert(0);
213 }