2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2018 Aleph One Ltd.
6 * Created by Charles Manning <charles@aleph1.co.uk>
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.
14 * Example OS glue functions for running on a Linux/POSIX system.
18 #include "yaffs_guts.h"
20 #include "yaffs_trace.h"
27 * yaffsfs_SetError() and yaffsfs_GetError()
28 * Do whatever to set the system error.
29 * yaffsfs_GetError() just fetches the last error.
32 static int yaffsfs_lastError;
34 void yaffsfs_SetError(int err)
36 //Do whatever to set error
37 yaffsfs_lastError = err;
41 int yaffsfs_GetLastError(void)
43 return yaffsfs_lastError;
47 * yaffsfs_CheckMemRegion()
48 * Check that access to an address is valid.
49 * This can check memory is in bounds and is writable etc.
51 * Returns 0 if ok, negative if not.
53 int yaffsfs_CheckMemRegion(const void *addr, size_t size, int write_request)
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.
69 * If we use pthreads then we also start a background gc thread.
76 static pthread_mutex_t mutex1;
77 static pthread_t bc_gc_thread;
79 void yaffsfs_Lock(void)
81 pthread_mutex_lock( &mutex1 );
84 void yaffsfs_Unlock(void)
86 pthread_mutex_unlock( &mutex1 );
89 static void *bg_gc_func(void *dummy)
91 struct yaffs_dev *dev;
98 /* Sleep for a bit to allow start up */
103 /* Iterate through devices, do bg gc updating ungency */
107 while ((dev = yaffs_next_dev()) != NULL) {
108 result = yaffs_do_background_gc_reldev(dev, urgent);
110 /* result is 1 if more than half the free space is
112 * If less than half the free space is erased then it is
113 * worth doing another background_gc operation sooner.
119 urgent = next_urgent;
127 /* Don't ever return. */
131 void yaffsfs_LockInit(void)
133 /* Initialise lock */
134 pthread_mutex_init(&mutex1, NULL);
136 /* Sneak in starting a background gc thread too */
137 pthread_create(&bc_gc_thread, NULL, bg_gc_func, NULL);
142 void yaffsfs_Lock(void)
146 void yaffsfs_Unlock(void)
150 void yaffsfs_LockInit(void)
156 * yaffsfs_CurrentTime() retrns a 32-bit timestamp.
158 * Can return 0 if your system does not care about time.
161 u32 yaffsfs_CurrentTime(void)
171 * Functions to allocate and free memory.
175 static unsigned malloc_high_water;
176 static unsigned malloc_currently_allocated;
178 #ifdef CONFIG_YAFFS_MONITOR_MALLOC
181 static void yaffsfs_malloc_add(void *ptr)
183 unsigned this_size = malloc_usable_size(ptr);
184 malloc_currently_allocated += this_size;
186 if (malloc_currently_allocated > malloc_high_water)
187 malloc_high_water = malloc_currently_allocated;
190 static void yaffsfs_malloc_remove(void *ptr)
192 unsigned this_size = malloc_usable_size(ptr);
193 malloc_currently_allocated -= this_size;
196 static void yaffsfs_malloc_add(void *ptr)
201 static void yaffsfs_malloc_remove(void *ptr)
207 void yaffsfs_get_malloc_values(unsigned *current, unsigned *high_water)
210 *current = malloc_currently_allocated;
212 *high_water = malloc_high_water;
216 #ifdef CONFIG_YAFFS_TEST_MALLOC
218 static int yaffs_kill_alloc = 0;
219 static size_t total_malloced = 0;
220 static size_t malloc_limit = 0 & 6000000;
222 void *yaffsfs_malloc(size_t size)
227 if(malloc_limit && malloc_limit <(total_malloced + size) )
232 total_malloced += size;
233 yaffsfs_malloc_add(this);
239 void *yaffsfs_malloc(size_t size)
241 void *this = malloc(size);
242 yaffsfs_malloc_add(this);
251 void yaffsfs_free(void *ptr)
253 yaffsfs_malloc_remove(ptr);
257 void yaffsfs_OSInitialisation(void)
264 * Function to report a bug.
267 void yaffs_bug_fn(const char *file_name, int line_no)
269 printf("yaffs bug detected %s:%d\n",