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