X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs2.git;a=blobdiff_plain;f=direct%2Ftest-framework%2Fyaffs_osglue.c;h=94ca24dfd225a73bde9924f6c51a73672ed1c89b;hp=40e405209fa1641e6472de0710c928aedfc7326f;hb=HEAD;hpb=c08faae4258b29a794ad55ca160c5a247145c838 diff --git a/direct/test-framework/yaffs_osglue.c b/direct/test-framework/yaffs_osglue.c index 40e4052..94ca24d 100644 --- a/direct/test-framework/yaffs_osglue.c +++ b/direct/test-framework/yaffs_osglue.c @@ -1,8 +1,7 @@ /* * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. * - * Copyright (C) 2002-2011 Aleph One Ltd. - * for Toby Churchill Ltd and Brightstar Engineering + * Copyright (C) 2002-2018 Aleph One Ltd. * * Created by Charles Manning * @@ -11,6 +10,9 @@ * published by the Free Software Foundation. */ +/* + * Example OS glue functions for running on a Linux/POSIX system. + */ #include "yaffscfg.h" #include "yaffs_guts.h" @@ -19,7 +21,13 @@ #include #include +#include +/* + * yaffsfs_SetError() and yaffsfs_GetError() + * Do whatever to set the system error. + * yaffsfs_GetError() just fetches the last error. + */ static int yaffsfs_lastError; @@ -27,6 +35,7 @@ void yaffsfs_SetError(int err) { //Do whatever to set error yaffsfs_lastError = err; + errno = err; } int yaffsfs_GetLastError(void) @@ -34,18 +43,38 @@ int yaffsfs_GetLastError(void) return yaffsfs_lastError; } -int yaffsfs_CheckMemRegion(const void *addr, size_t size, int writeable) +/* + * yaffsfs_CheckMemRegion() + * Check that access to an address is valid. + * This can check memory is in bounds and is writable etc. + * + * Returns 0 if ok, negative if not. + */ +int yaffsfs_CheckMemRegion(const void *addr, size_t size, int write_request) { + (void) size; + (void) write_request; + if(!addr) return -1; return 0; } +/* + * yaffsfs_Lock() + * yaffsfs_Unlock() + * A single mechanism to lock and unlock yaffs. Hook up to a mutex or whatever. + * Here are two examples, one using POSIX pthreads, the other doing nothing. + * + * If we use pthreads then we also start a background gc thread. + */ + +#if 1 -#ifdef CONFIG_YAFFS_USE_PTHREADS #include -static pthread_mutex_t mutex1; +static pthread_mutex_t mutex1; +static pthread_t bc_gc_thread; void yaffsfs_Lock(void) { @@ -57,9 +86,55 @@ void yaffsfs_Unlock(void) pthread_mutex_unlock( &mutex1 ); } +static void *bg_gc_func(void *dummy) +{ + struct yaffs_dev *dev; + int urgent = 0; + int result; + int next_urgent; + + (void)dummy; + + /* Sleep for a bit to allow start up */ + sleep(2); + + + while (1) { + /* Iterate through devices, do bg gc updating ungency */ + yaffs_dev_rewind(); + next_urgent = 0; + + while ((dev = yaffs_next_dev()) != NULL) { + result = yaffs_do_background_gc_reldev(dev, urgent); + + /* result is 1 if more than half the free space is + * erased. + * If less than half the free space is erased then it is + * worth doing another background_gc operation sooner. + */ + if (result == 0) + next_urgent = 1; + } + + urgent = next_urgent; + + if (next_urgent) + sleep(1); + else + sleep(5); + } + + /* Don't ever return. */ + return NULL; +} + void yaffsfs_LockInit(void) { - pthread_mutex_init( &mutex1, NULL); + /* Initialise lock */ + pthread_mutex_init(&mutex1, NULL); + + /* Sneak in starting a background gc thread too */ + pthread_create(&bc_gc_thread, NULL, bg_gc_func, NULL); } #else @@ -77,12 +152,69 @@ void yaffsfs_LockInit(void) } #endif +/* + * yaffsfs_CurrentTime() retrns a 32-bit timestamp. + * + * Can return 0 if your system does not care about time. + */ + u32 yaffsfs_CurrentTime(void) { return time(NULL); } +/* + * yaffsfs_malloc() + * yaffsfs_free() + * + * Functions to allocate and free memory. + */ + + +static unsigned malloc_high_water; +static unsigned malloc_currently_allocated; + +#ifdef CONFIG_YAFFS_MONITOR_MALLOC + +#include +static void yaffsfs_malloc_add(void *ptr) +{ + unsigned this_size = malloc_usable_size(ptr); + malloc_currently_allocated += this_size; + + if (malloc_currently_allocated > malloc_high_water) + malloc_high_water = malloc_currently_allocated; +} + +static void yaffsfs_malloc_remove(void *ptr) +{ + unsigned this_size = malloc_usable_size(ptr); + malloc_currently_allocated -= this_size; +} +#else +static void yaffsfs_malloc_add(void *ptr) +{ + (void)ptr; +} + +static void yaffsfs_malloc_remove(void *ptr) +{ + (void)ptr; +} +#endif + +void yaffsfs_get_malloc_values(unsigned *current, unsigned *high_water) +{ + if (current) + *current = malloc_currently_allocated; + if(high_water) + *high_water = malloc_high_water; +} + + +#ifdef CONFIG_YAFFS_TEST_MALLOC + static int yaffs_kill_alloc = 0; static size_t total_malloced = 0; static size_t malloc_limit = 0 & 6000000; @@ -98,11 +230,27 @@ void *yaffsfs_malloc(size_t size) this = malloc(size); if(this) total_malloced += size; + yaffsfs_malloc_add(this); return this; } +#else + +void *yaffsfs_malloc(size_t size) +{ + void *this = malloc(size); + yaffsfs_malloc_add(this); + return this; +} + +#endif + + + + void yaffsfs_free(void *ptr) { + yaffsfs_malloc_remove(ptr); free(ptr); } @@ -111,6 +259,10 @@ void yaffsfs_OSInitialisation(void) yaffsfs_LockInit(); } +/* + * yaffs_bug_fn() + * Function to report a bug. + */ void yaffs_bug_fn(const char *file_name, int line_no) {