yaffsfs: Add commenting for OS glue interface
[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
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         if(!addr)
56                 return -1;
57         return 0;
58 }
59
60 /*
61  * yaffsfs_Lock()
62  * yaffsfs_Unlock()
63  * A single mechanism to lock and unlock yaffs. Hook up to a mutex or whatever.
64  * Here are two examples, one using POSIX pthreads, the other doing nothing.
65  */
66
67 #ifdef CONFIG_YAFFS_USE_PTHREADS
68 #include <pthread.h>
69 static pthread_mutex_t mutex1;
70
71
72 void yaffsfs_Lock(void)
73 {
74         pthread_mutex_lock( &mutex1 );
75 }
76
77 void yaffsfs_Unlock(void)
78 {
79         pthread_mutex_unlock( &mutex1 );
80 }
81
82 void yaffsfs_LockInit(void)
83 {
84         pthread_mutex_init( &mutex1, NULL);
85 }
86
87 #else
88
89 void yaffsfs_Lock(void)
90 {
91 }
92
93 void yaffsfs_Unlock(void)
94 {
95 }
96
97 void yaffsfs_LockInit(void)
98 {
99 }
100 #endif
101
102 /*
103  * yaffsfs_CurrentTime() retrns a 32-bit timestamp.
104  * 
105  * Can return 0 if your system does not care about time.
106  */
107  
108 u32 yaffsfs_CurrentTime(void)
109 {
110         return time(NULL);
111 }
112
113
114 /*
115  * yaffsfs_malloc()
116  * yaffsfs_free()
117  *
118  * Functions to allocate and free memory.
119  */
120  
121 #ifdef CONFIG_YAFFS_TEST_MALLOC
122
123 static int yaffs_kill_alloc = 0;
124 static size_t total_malloced = 0;
125 static size_t malloc_limit = 0 & 6000000;
126
127 void *yaffsfs_malloc(size_t size)
128 {
129         void * this;
130         if(yaffs_kill_alloc)
131                 return NULL;
132         if(malloc_limit && malloc_limit <(total_malloced + size) )
133                 return NULL;
134
135         this = malloc(size);
136         if(this)
137                 total_malloced += size;
138         return this;
139 }
140
141 #else
142
143 void *yaffsfs_malloc(size_t size)
144 {
145         return malloc(size);
146 }
147
148 #endif
149
150 void yaffsfs_free(void *ptr)
151 {
152         free(ptr);
153 }
154
155 void yaffsfs_OSInitialisation(void)
156 {
157         yaffsfs_LockInit();
158 }
159
160 /*
161  * yaffs_bug_fn()
162  * Function to report a bug.
163  */
164  
165 void yaffs_bug_fn(const char *file_name, int line_no)
166 {
167         printf("yaffs bug detected %s:%d\n",
168                 file_name, line_no);
169         assert(0);
170 }