Compilation clean up
[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         (void) size;
57         (void) write_request;
58
59         if(!addr)
60                 return -1;
61         return 0;
62 }
63
64 /*
65  * yaffsfs_Lock()
66  * yaffsfs_Unlock()
67  * A single mechanism to lock and unlock yaffs. Hook up to a mutex or whatever.
68  * Here are two examples, one using POSIX pthreads, the other doing nothing.
69  *
70  * If we use pthreads then we also start a background gc thread.
71  */
72
73 #if 1
74
75 #include <pthread.h>
76
77 static pthread_mutex_t mutex1;
78 static pthread_t bc_gc_thread;
79
80 void yaffsfs_Lock(void)
81 {
82         pthread_mutex_lock( &mutex1 );
83 }
84
85 void yaffsfs_Unlock(void)
86 {
87         pthread_mutex_unlock( &mutex1 );
88 }
89
90 static void *bg_gc_func(void *dummy)
91 {
92         struct yaffs_dev *dev;
93         int urgent = 0;
94         int result;
95         int next_urgent;
96
97         (void)dummy;
98
99         /* Sleep for a bit to allow start up */
100         sleep(2);
101
102
103         while (1) {
104                 /* Iterate through devices, do bg gc updating ungency */
105                 yaffs_dev_rewind();
106                 next_urgent = 0;
107
108                 while ((dev = yaffs_next_dev()) != NULL) {
109                         result = yaffs_do_background_gc_reldev(dev, urgent);
110                         if (result > 0)
111                                 next_urgent = 1;
112                 }
113
114                 urgent = next_urgent;
115
116                 if (next_urgent)
117                         sleep(1);
118                 else
119                         sleep(5);
120         }
121
122         /* Don't ever return. */
123         return NULL;
124 }
125
126 void yaffsfs_LockInit(void)
127 {
128         /* Initialise lock */
129         pthread_mutex_init(&mutex1, NULL);
130
131         /* Sneak in starting a background gc thread too */
132         pthread_create(&bc_gc_thread, NULL, bg_gc_func, NULL);
133 }
134
135 #else
136
137 void yaffsfs_Lock(void)
138 {
139 }
140
141 void yaffsfs_Unlock(void)
142 {
143 }
144
145 void yaffsfs_LockInit(void)
146 {
147 }
148 #endif
149
150 /*
151  * yaffsfs_CurrentTime() retrns a 32-bit timestamp.
152  *
153  * Can return 0 if your system does not care about time.
154  */
155
156 u32 yaffsfs_CurrentTime(void)
157 {
158         return time(NULL);
159 }
160
161
162 /*
163  * yaffsfs_malloc()
164  * yaffsfs_free()
165  *
166  * Functions to allocate and free memory.
167  */
168
169 #ifdef CONFIG_YAFFS_TEST_MALLOC
170
171 static int yaffs_kill_alloc = 0;
172 static size_t total_malloced = 0;
173 static size_t malloc_limit = 0 & 6000000;
174
175 void *yaffsfs_malloc(size_t size)
176 {
177         void * this;
178         if(yaffs_kill_alloc)
179                 return NULL;
180         if(malloc_limit && malloc_limit <(total_malloced + size) )
181                 return NULL;
182
183         this = malloc(size);
184         if(this)
185                 total_malloced += size;
186         return this;
187 }
188
189 #else
190
191 void *yaffsfs_malloc(size_t size)
192 {
193         return malloc(size);
194 }
195
196 #endif
197
198 void yaffsfs_free(void *ptr)
199 {
200         free(ptr);
201 }
202
203 void yaffsfs_OSInitialisation(void)
204 {
205         yaffsfs_LockInit();
206 }
207
208 /*
209  * yaffs_bug_fn()
210  * Function to report a bug.
211  */
212
213 void yaffs_bug_fn(const char *file_name, int line_no)
214 {
215         printf("yaffs bug detected %s:%d\n",
216                 file_name, line_no);
217         assert(0);
218 }