Mercurial > hg > emotion-detection-top-level
comparison Code/Descriptors/yin/private/src/mwrap.h @ 0:ea0c737c6323
first commit
author | Dawn Black <dawn.black@eecs.qmul.ac.uk> |
---|---|
date | Thu, 26 Jul 2012 14:46:25 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:ea0c737c6323 |
---|---|
1 /* | |
2 19 Dec 94 - Sept 95 | |
3 Alain de Cheveigne, LLF, CNRS/Universite Paris 7. | |
4 Modified: Sept 2000 | |
5 | |
6 file mwrap.h | |
7 | |
8 To use the malloc wrappers: | |
9 - include this file in all source files that call malloc routines, | |
10 - link with mwrap.o, | |
11 - compile all files with -DMWRAP to turn on wrapping and checking. | |
12 | |
13 */ | |
14 | |
15 #ifndef MWRAP_H | |
16 #define MWRAP_H | |
17 | |
18 #include <stdio.h> | |
19 #include <stdlib.h> | |
20 | |
21 /* NOTE: (char *) is used for pointer arithmetic. | |
22 Pointer arguments of all macros are cast to (char *). */ | |
23 | |
24 /* node structure for binary splay tree */ | |
25 typedef struct mwrap_node_struct *mwrap_node ; | |
26 struct mwrap_node_struct { | |
27 char *lo; /* base of block */ | |
28 char *hi; /* one beyond last address in block */ | |
29 mwrap_node up; | |
30 mwrap_node left; | |
31 mwrap_node right; | |
32 }; | |
33 | |
34 | |
35 /* Abbreviations for macros. Change in case of conflict. | |
36 | |
37 Assignment macros: | |
38 GET(x) equivalent to 'x' on a rhs, but memory address is checked before access | |
39 SET(x) equivalent to 'x' on a lhs, but memory address is checked before assignment | |
40 CPY(p,q,n) copy n values starting from p to n values starting from q, after checking both | |
41 | |
42 Test macros: | |
43 POK(p) pointer p is within an allocated block. | |
44 BOK(b) pointer b is the base of an allocated block. | |
45 PQOK(p, q) pointers p and q are in same allocated block. | |
46 PBOK(p, b) pointer p is in block of base b. | |
47 | |
48 MWRAPOK() check internal tree for sanity (slow) | |
49 | |
50 Query macros: | |
51 PBASE(p) base of block containing p, NULL if there is none. | |
52 PBUMP(p) one beyond last byte of block. | |
53 PSIZE(p) size of block containing p, -1 if none. | |
54 | |
55 Explicit block registering macros. | |
56 CHECKIN(lo, hi) register block of bounds lo, hi | |
57 CHECKOUT(lo) unregister block of base lo | |
58 | |
59 */ | |
60 | |
61 #define GET(x) MWRAP_GET(x) | |
62 #define SET(x) MWRAP_SET(x) | |
63 #define CPY(p,q,n) MWRAP_CPY(p,q,n) | |
64 #define POK(p) MWRAP_POK(p) | |
65 #define BOK(b) MWRAP_BOK(b) | |
66 #define PQOK(p,q) MWRAP_PQOK((p),(q)) | |
67 #define PBOK(p,b) MWRAP_PBOK((p),(b)) | |
68 #define MWRAPOK() MWRAP_MWRAPOK() | |
69 #define PBASE(p) MWRAP_PBASE(p) | |
70 #define PSIZE(p) MWRAP_PSIZE(p) | |
71 #define CHECKIN(lo, hi) MWRAP_CHECKIN((lo), (hi)) | |
72 #define CHECKOUT(lo) MWRAP_CHECKOUT(lo) | |
73 | |
74 | |
75 /* Definitions of macros. | |
76 All are conditional on MWRAP being defined. | |
77 For speed, macros check for a "lucky" hit (block already | |
78 at root) before handing over to functions. */ | |
79 | |
80 #ifdef MWRAP | |
81 | |
82 #define MWRAP_GET(x) \ | |
83 ( (MWRAP_POK(&x)) ? (x) : (x) ) | |
84 | |
85 #define MWRAP_SET(x) \ | |
86 MWRAP_POK(&x) ; (x) | |
87 | |
88 #define MWRAP_CPY(p, q, n) \ | |
89 do {int i; \ | |
90 MWRAP_PQOK((p),(p)+(n-1)); \ | |
91 MWRAP_PQOK((q),(q)+(n-1)); \ | |
92 for(i=0;i<n;i++) (q)[i]=(p)[i]; \ | |
93 } while(0) | |
94 | |
95 /* #define MWRAP_POK(p) \ | |
96 do { if (mwrap_tree \ | |
97 && (char *) (p) >= mwrap_tree->lo \ | |
98 && (char *) (p) < mwrap_tree->hi) break; \ | |
99 mwrap_pok((char *) (p), __FILE__, __LINE__); } while (0) */ | |
100 | |
101 #define MWRAP_POK(p) \ | |
102 ((mwrap_tree && (char *) (p) >= mwrap_tree->lo && (char *) (p) <mwrap_tree->hi) ? \ | |
103 1 : mwrap_pok((char *) (p), __FILE__, __LINE__)) | |
104 | |
105 | |
106 /* #define MWRAP_BOK(b) \ | |
107 do { if (mwrap_tree \ | |
108 && (char *) (b) == mwrap_tree->lo) break; \ | |
109 mwrap_bok((char *) (b), __FILE__, __LINE__); } while (0) */ | |
110 | |
111 #define MWRAP_BOK(b) \ | |
112 ((mwrap_tree && (char *) (b) == mwrap_tree->lo) ? 1 : mwrap_bok((char *) (b), __FILE__, __LINE__)) | |
113 | |
114 | |
115 /* #define MWRAP_PQOK(p, q) \ | |
116 do { if (mwrap_tree \ | |
117 && (char *) (p) >= mwrap_tree->lo \ | |
118 && (char *) (p) < mwrap_tree->hi \ | |
119 && (char *) (q) >= mwrap_tree->lo \ | |
120 && (char *) (q) < mwrap_tree->lo) break; \ | |
121 mwrap_pqok((char *)(p), (char *)(q), __FILE__, __LINE__); } while (0) */ | |
122 | |
123 #define MWRAP_PQOK(p,q) \ | |
124 ((mwrap_tree \ | |
125 && (char *) (p) >= mwrap_tree->lo \ | |
126 && (char *) (p) < mwrap_tree->hi \ | |
127 && (char *) (q) >= mwrap_tree->lo \ | |
128 && (char *) (q) < mwrap_tree->lo ) ? \ | |
129 1 : mwrap_pqok((char *)(p), (char *)(q), __FILE__, __LINE__)) | |
130 | |
131 /* #define MWRAP_PBOK(p, b) \ | |
132 do { if (mwrap_tree \ | |
133 && (char *) (b) == mwrap_tree->lo \ | |
134 && (char *) (p) >= mwrap_tree->lo \ | |
135 && (char *) (p) < mwrap_tree->lo) break; \ | |
136 mwrap_pbok((char *)(p), (char *)(b), __FILE__, __LINE__); } while (0) */ | |
137 | |
138 #define MWRAP_PBOK(p, b) \ | |
139 (( mwrap_tree \ | |
140 && (char *) (b) == mwrap_tree->lo \ | |
141 && (char *) (p) >= mwrap_tree->lo \ | |
142 && (char *) (p) < mwrap_tree->lo) ? \ | |
143 1 : mwrap_pbok((char *)(p), (char *)(b), __FILE__, __LINE__)) | |
144 | |
145 #define MWRAP_MWRAPOK() mwrap_checktree(__FILE__, __LINE__) | |
146 | |
147 #define MWRAP_PBASE(p) mwrap_pbase((char *) p) | |
148 #define MWRAP_PBUMP(p) mwrap_pbump((char *) p) | |
149 #define MWRAP_PSIZE(p) mwrap_psize((char *) p) | |
150 #define MWRAP_CHECKIN(lo, hi) \ | |
151 mwrap_checkin((char *) (lo), (char *) (hi), __FILE__, __LINE__) | |
152 #define MWRAP_CHECKOUT(lo) \ | |
153 mwrap_checkout( (char *) (lo), __FILE__, __LINE__) | |
154 | |
155 #else | |
156 | |
157 #define MWRAP_GET(x) (x) | |
158 #define MWRAP_SET(x) (x) | |
159 #define MWRAP_CPY(p,q,n) | |
160 #define MWRAP_POK(p) | |
161 #define MWRAP_BOK(p) | |
162 #define MWRAP_PBOK(p,b) | |
163 #define MWRAP_PQOK(p,q) | |
164 #define MWRAP_MWRAPOK() | |
165 #define MWRAP_PBASE(p) (void *) mwrap_squeal(__FILE__, __LINE__) | |
166 #define MWRAP_PBUMP(p) (void *) mwrap_squeal(__FILE__, __LINE__) | |
167 #define MWRAP_PSIZE(p) (long) mwrap_squeal(__FILE__, __LINE__) | |
168 #define MWRAP_CHECKIN(lo, hi) | |
169 #define MWRAP_CHECKOUT(lo) | |
170 #endif | |
171 | |
172 | |
173 /* Wrapping macros. | |
174 | |
175 If MWRAP is defined, all malloc routines are wrapped and the | |
176 original routines are available as "nowrap_malloc()", etc.. | |
177 | |
178 If MWRAP is not defined, malloc routines are not wrapped | |
179 and "nowrap_malloc()", etc. are #defined to "malloc()", etc. | |
180 */ | |
181 | |
182 #ifdef MWRAP | |
183 | |
184 #ifndef MWRAP_C | |
185 #define malloc(size) mwrap_malloc((size), __FILE__, __LINE__) | |
186 #define free(p) mwrap_free((p), __FILE__, __LINE__) | |
187 #define realloc(p, size) mwrap_realloc((p), (size), __FILE__, __LINE__) | |
188 #define calloc(n, size) mwrap_calloc((n), (size), __FILE__, __LINE__) | |
189 #define cfree(p) mwrap_cfree((p), __FILE__, __LINE__) | |
190 #define valloc(size) mwrap_valloc((size), __FILE__, __LINE__) | |
191 #define vfree(p) mwrap_vfree((p), __FILE__, __LINE__) | |
192 #define memalign(a, p) mwrap_memalign((a), (p), __FILE__, __LINE__) | |
193 #endif /* MWRAP_C */ | |
194 | |
195 #else | |
196 | |
197 #ifndef MWRAP_C | |
198 #define nowrap_malloc(size) malloc(size) | |
199 #define nowrap_free(p) free(p) | |
200 #define nowrap_realloc(p, size) realloc(p) | |
201 #define nowrap_calloc(n, size) calloc((n), (size)) | |
202 #define nowrap_cfree(p) cfree(p) | |
203 #define nowrap_valloc(size) valloc(size) | |
204 #define nowrap_vfree(p) vfree(p) | |
205 #define nowrap_memalign(a, p) memalign((a), (p)) | |
206 #endif /* MWRAP_C */ | |
207 | |
208 #endif /* MWRAP */ | |
209 | |
210 | |
211 /* three global variables */ | |
212 | |
213 #ifndef MWRAP_C | |
214 extern mwrap_node mwrap_tree; /* the tree */ | |
215 extern char *mwrap_file; /* caller source file */ | |
216 extern long mwrap_line; /* line in caller source file */ | |
217 #endif | |
218 | |
219 | |
220 /* Data types used by malloc library. Modify if necessary */ | |
221 typedef void DATATYPE; /* returned by malloc(), etc. */ | |
222 typedef size_t SIZETYPE; /* fed to malloc(), etc. */ | |
223 typedef void VOIDTYPE; /* returned by free(), etc. */ | |
224 | |
225 | |
226 /* prototypes of routines made public */ | |
227 | |
228 /* malloc wrappers */ | |
229 DATATYPE *mwrap_malloc(SIZETYPE size, char *file, long line); | |
230 VOIDTYPE mwrap_free(DATATYPE *p, char *file, long line); | |
231 DATATYPE *mwrap_realloc(DATATYPE *p, SIZETYPE size, char *file, long line); | |
232 DATATYPE *mwrap_calloc(SIZETYPE n, SIZETYPE size, char *file, long line); | |
233 VOIDTYPE mwrap_cfree(DATATYPE *p, char *file, long line); | |
234 DATATYPE *mwrap_valloc(SIZETYPE size, char *file, long line); | |
235 VOIDTYPE mwrap_vfree(DATATYPE *p, char *file, long line); | |
236 DATATYPE *mwrap_memalign(SIZETYPE alignment, SIZETYPE size, char *file, long line); | |
237 | |
238 /* unwrapped versions */ | |
239 #ifdef MWRAP | |
240 DATATYPE *nowrap_malloc(SIZETYPE size); | |
241 VOIDTYPE nowrap_free(DATATYPE *p); | |
242 DATATYPE *nowrap_realloc(DATATYPE *p, SIZETYPE size); | |
243 DATATYPE *nowrap_calloc(SIZETYPE n, SIZETYPE size); | |
244 VOIDTYPE nowrap_cfree(DATATYPE *p); | |
245 DATATYPE *nowrap_valloc(SIZETYPE size); | |
246 VOIDTYPE nowrap_vfree(DATATYPE *p); | |
247 DATATYPE *nowrap_memalign(SIZETYPE alignment, SIZETYPE size); | |
248 #endif | |
249 | |
250 /* explicit block registering */ | |
251 void mwrap_checkin(char *lo, char *hi, char *file, long line); | |
252 void mwrap_checkout(char *lo, char *file, long line); | |
253 | |
254 /* tests & queries */ | |
255 int mwrap_pok(char *p, char *file, long line); | |
256 int mwrap_bok(char *b, char *file, long line); | |
257 int mwrap_pqok(char *p, char *q, char *file, long line); | |
258 int mwrap_pbok(char *p, char *b, char *file, long line); | |
259 char *mwrap_pbase(char *p); | |
260 char *mwrap_pbump(char *p); | |
261 long mwrap_psize(char *p); | |
262 long mwrap_squeal(char *file, long line); | |
263 | |
264 /* change default functions */ | |
265 void mwrap_set_errorfunction(void (*f)(int level)); | |
266 void mwrap_set_messagefunction(void (*f)(int level, char * message)); | |
267 | |
268 #endif |