Fix copyright
[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 #ifdef CONFIG_YAFFS_TEST_MALLOC
169
170 static int yaffs_kill_alloc = 0;
171 static size_t total_malloced = 0;
172 static size_t malloc_limit = 0 & 6000000;
173
174 void *yaffsfs_malloc(size_t size)
175 {
176         void * this;
177         if(yaffs_kill_alloc)
178                 return NULL;
179         if(malloc_limit && malloc_limit <(total_malloced + size) )
180                 return NULL;
181
182         this = malloc(size);
183         if(this)
184                 total_malloced += size;
185         return this;
186 }
187
188 #else
189
190 void *yaffsfs_malloc(size_t size)
191 {
192         return malloc(size);
193 }
194
195 #endif
196
197 void yaffsfs_free(void *ptr)
198 {
199         free(ptr);
200 }
201
202 void yaffsfs_OSInitialisation(void)
203 {
204         yaffsfs_LockInit();
205 }
206
207 /*
208  * yaffs_bug_fn()
209  * Function to report a bug.
210  */
211
212 void yaffs_bug_fn(const char *file_name, int line_no)
213 {
214         printf("yaffs bug detected %s:%d\n",
215                 file_name, line_no);
216         assert(0);
217 }