1 | /******************************************************************* |
---|
2 | * File: omtTestAlloc.c |
---|
3 | * Purpose: alloc function to be included in omMain.c |
---|
4 | * Author: obachman@mathematik.uni-kl.de (Olaf Bachmann) |
---|
5 | * Created: 11/99 |
---|
6 | * Version: $Id$ |
---|
7 | *******************************************************************/ |
---|
8 | #include <omalloc/omtTest.h> |
---|
9 | |
---|
10 | #ifdef TEST_CHECK |
---|
11 | #define OM_CHECK CHECK_LEVEL |
---|
12 | #define omtTestAlloc omtTestAllocDebug |
---|
13 | #define omtTestRealloc omtTestReallocDebug |
---|
14 | #define omtTestFree omtTestFreeDebug |
---|
15 | #define omtTestDup omtTestDupDebug |
---|
16 | #endif |
---|
17 | |
---|
18 | #ifdef TEST_KEEP |
---|
19 | #define OM_CHECK CHECK_LEVEL |
---|
20 | #define OM_KEEP 1 |
---|
21 | #define omtTestAlloc omtTestAllocKeep |
---|
22 | #define omtTestRealloc omtTestReallocKeep |
---|
23 | #define omtTestFree omtTestFreeKeep |
---|
24 | #define omtTestDup omtTestDupKeep |
---|
25 | #endif |
---|
26 | |
---|
27 | #include <omalloc/omalloc.h> |
---|
28 | |
---|
29 | #ifndef OM_ALIGNMENT_NEEDS_WORK |
---|
30 | #define omSmallSize2AlignedBin omSmallSize2Bin |
---|
31 | #endif |
---|
32 | |
---|
33 | void omtTestAlloc(omMemCell cell, unsigned long spec) |
---|
34 | { |
---|
35 | size_t size = GET_SIZE(spec); |
---|
36 | void* addr; |
---|
37 | omBin bin = NULL; |
---|
38 | omBin orig_bin = NULL; |
---|
39 | |
---|
40 | if (IS_BIN(spec) && (size <= OM_MAX_BLOCK_SIZE || IS_SPEC_BIN(spec))) |
---|
41 | { |
---|
42 | if (IS_SPEC_BIN(spec)) |
---|
43 | { |
---|
44 | if (IS_ALIGNED(spec)) |
---|
45 | bin = omGetAlignedSpecBin(size); |
---|
46 | else |
---|
47 | bin = omGetSpecBin(size); |
---|
48 | } |
---|
49 | else |
---|
50 | { |
---|
51 | if (IS_ALIGNED(spec)) |
---|
52 | bin = omSmallSize2AlignedBin(size); |
---|
53 | else |
---|
54 | bin = omSmallSize2Bin(size); |
---|
55 | } |
---|
56 | |
---|
57 | if (IS_STICKY_BIN(spec)) |
---|
58 | { |
---|
59 | orig_bin = bin; |
---|
60 | bin = omtGetStickyBin(bin); |
---|
61 | } |
---|
62 | |
---|
63 | if (IS_INLINE(spec)) |
---|
64 | { |
---|
65 | if (IS_ZERO(spec)) |
---|
66 | addr = omAlloc0Bin(bin); |
---|
67 | else |
---|
68 | addr = omAllocBin(bin); |
---|
69 | } |
---|
70 | else |
---|
71 | { |
---|
72 | if (IS_ZERO(spec)) |
---|
73 | omTypeAlloc0Bin(void*, addr, bin); |
---|
74 | else |
---|
75 | omTypeAllocBin(void*, addr, bin); |
---|
76 | } |
---|
77 | } |
---|
78 | else |
---|
79 | { |
---|
80 | if (IS_INLINE(spec)) |
---|
81 | { |
---|
82 | if (IS_ZERO(spec)) |
---|
83 | { |
---|
84 | if (IS_ALIGNED(spec)) |
---|
85 | { |
---|
86 | if (IS_SLOPPY(spec)) |
---|
87 | addr = omalloc0(size); |
---|
88 | else |
---|
89 | addr = omAlloc0Aligned(size); |
---|
90 | } |
---|
91 | else |
---|
92 | addr = omAlloc0(size); |
---|
93 | } |
---|
94 | else |
---|
95 | { |
---|
96 | if (IS_ALIGNED(spec)) |
---|
97 | { |
---|
98 | if (IS_SLOPPY(spec)) |
---|
99 | addr = omalloc(size); |
---|
100 | else |
---|
101 | addr = omAllocAligned(size); |
---|
102 | } |
---|
103 | else |
---|
104 | addr = omAlloc(size); |
---|
105 | } |
---|
106 | } |
---|
107 | else |
---|
108 | { |
---|
109 | if (IS_ZERO(spec)) |
---|
110 | { |
---|
111 | if (IS_ALIGNED(spec)) |
---|
112 | omTypeAlloc0Aligned(void*, addr, size); |
---|
113 | else |
---|
114 | omTypeAlloc0(void*, addr, size); |
---|
115 | } |
---|
116 | else |
---|
117 | { |
---|
118 | if (IS_ALIGNED(spec)) |
---|
119 | omTypeAllocAligned(void*, addr, size); |
---|
120 | else |
---|
121 | omTypeAlloc(void*, addr, size); |
---|
122 | } |
---|
123 | } |
---|
124 | } |
---|
125 | cell->addr = addr; |
---|
126 | cell->bin = bin; |
---|
127 | cell->orig_bin = orig_bin; |
---|
128 | cell->spec = spec; |
---|
129 | |
---|
130 | InitCellAddrContent(cell); |
---|
131 | |
---|
132 | omtTestDebug(cell); |
---|
133 | } |
---|
134 | |
---|
135 | void omtTestFree(omMemCell cell) |
---|
136 | { |
---|
137 | void* addr = cell->addr; |
---|
138 | unsigned long spec = cell->spec; |
---|
139 | omBin bin = cell->bin; |
---|
140 | omBin orig_bin = cell->orig_bin; |
---|
141 | size_t size = GET_SIZE(spec); |
---|
142 | |
---|
143 | omtTestDebug(cell); |
---|
144 | |
---|
145 | if (IS_FREE_SIZE(spec)) |
---|
146 | { |
---|
147 | if (IS_SLOPPY(spec)) |
---|
148 | omfreeSize(addr, size); |
---|
149 | else |
---|
150 | omFreeSize(addr, size); |
---|
151 | } |
---|
152 | else if (bin != NULL && IS_FREE_BIN(spec)) |
---|
153 | omFreeBin(addr, bin); |
---|
154 | else if (IS_FREE_BINADDR(spec) && (bin != NULL) && (size <= OM_MAX_BLOCK_SIZE)) |
---|
155 | { |
---|
156 | omFreeBinAddr(addr); |
---|
157 | } |
---|
158 | else |
---|
159 | { |
---|
160 | if (IS_SLOPPY(spec)) |
---|
161 | omfree(addr); |
---|
162 | else |
---|
163 | omFree(addr); |
---|
164 | } |
---|
165 | |
---|
166 | if (bin != NULL && IS_SPEC_BIN(spec)) |
---|
167 | { |
---|
168 | if (orig_bin != NULL) |
---|
169 | omUnGetSpecBin(&orig_bin); |
---|
170 | else |
---|
171 | omUnGetSpecBin(&bin); |
---|
172 | } |
---|
173 | |
---|
174 | cell->addr = NULL; |
---|
175 | cell->spec = 0; |
---|
176 | cell->bin = NULL; |
---|
177 | cell->orig_bin = NULL; |
---|
178 | } |
---|
179 | |
---|
180 | void omtTestRealloc(omMemCell cell, unsigned long new_spec) |
---|
181 | { |
---|
182 | void* old_addr = cell->addr; |
---|
183 | unsigned long old_spec = cell->spec; |
---|
184 | omBin old_bin = cell->bin; |
---|
185 | omBin old_orig_bin = cell->orig_bin; |
---|
186 | size_t old_size = GET_SIZE(old_spec); |
---|
187 | void* new_addr; |
---|
188 | omBin new_bin = NULL; |
---|
189 | omBin new_orig_bin = NULL; |
---|
190 | size_t new_size = GET_SIZE(new_spec); |
---|
191 | size_t real_old_size = omSizeOfAddr(old_addr); |
---|
192 | size_t min_size; |
---|
193 | |
---|
194 | omtTestDebug(cell); |
---|
195 | |
---|
196 | if (old_bin != NULL && IS_FREE_BIN(old_spec) && |
---|
197 | IS_BIN(new_spec) && ((new_size <= OM_MAX_BLOCK_SIZE) || IS_SPEC_BIN(new_spec))) |
---|
198 | { |
---|
199 | if (IS_SPEC_BIN(new_spec)) |
---|
200 | { |
---|
201 | if (IS_ALIGNED(new_spec)) |
---|
202 | new_bin = omGetAlignedSpecBin(new_size); |
---|
203 | else |
---|
204 | new_bin = omGetSpecBin(new_size); |
---|
205 | } |
---|
206 | else |
---|
207 | { |
---|
208 | if (IS_ALIGNED(new_spec)) |
---|
209 | new_bin = omSmallSize2AlignedBin(new_size); |
---|
210 | else |
---|
211 | new_bin = omSmallSize2Bin(new_size); |
---|
212 | } |
---|
213 | |
---|
214 | if (IS_STICKY_BIN(new_spec)) |
---|
215 | { |
---|
216 | new_orig_bin = new_bin; |
---|
217 | new_bin = omtGetStickyBin(new_bin); |
---|
218 | } |
---|
219 | |
---|
220 | if (IS_INLINE(new_spec)) |
---|
221 | { |
---|
222 | if (IS_ZERO(new_spec)) new_addr = omRealloc0Bin(old_addr, old_bin, new_bin); |
---|
223 | else new_addr = omReallocBin(old_addr, old_bin, new_bin); |
---|
224 | } |
---|
225 | else |
---|
226 | { |
---|
227 | if (IS_ZERO(new_spec)) omTypeRealloc0Bin(old_addr, old_bin, void*, new_addr, new_bin); |
---|
228 | else omTypeReallocBin(old_addr, old_bin, void*, new_addr, new_bin); |
---|
229 | } |
---|
230 | } |
---|
231 | else |
---|
232 | { |
---|
233 | if (IS_FREE_SIZE(old_spec)) |
---|
234 | { |
---|
235 | if (IS_INLINE(new_spec)) |
---|
236 | { |
---|
237 | if (IS_ZERO(new_spec)) |
---|
238 | { |
---|
239 | if (IS_ALIGNED(new_spec)) |
---|
240 | { |
---|
241 | if (IS_SLOPPY(new_spec)) |
---|
242 | new_addr = omrealloc0Size(old_addr, old_size, new_size); |
---|
243 | else |
---|
244 | new_addr = omRealloc0AlignedSize(old_addr, old_size, new_size); |
---|
245 | } |
---|
246 | else |
---|
247 | new_addr = omRealloc0Size(old_addr, old_size, new_size); |
---|
248 | } |
---|
249 | else |
---|
250 | { |
---|
251 | if (IS_ALIGNED(new_spec)) |
---|
252 | { |
---|
253 | if (IS_SLOPPY(new_spec)) |
---|
254 | new_addr = omreallocSize(old_addr, old_size, new_size); |
---|
255 | else |
---|
256 | new_addr = omReallocAlignedSize(old_addr, old_size, new_size); |
---|
257 | } |
---|
258 | else new_addr = omReallocSize(old_addr, old_size, new_size); |
---|
259 | } |
---|
260 | } |
---|
261 | else |
---|
262 | { |
---|
263 | if (IS_ZERO(new_spec)) |
---|
264 | { |
---|
265 | if (IS_ALIGNED(new_spec)) omTypeRealloc0AlignedSize(old_addr, old_size, void*, new_addr, new_size); |
---|
266 | else omTypeRealloc0Size(old_addr, old_size, void*, new_addr, new_size); |
---|
267 | } |
---|
268 | else |
---|
269 | { |
---|
270 | if (IS_ALIGNED(new_spec)) omTypeReallocAlignedSize(old_addr, old_size, void*, new_addr, new_size); |
---|
271 | else omTypeReallocSize(old_addr, old_size, void*, new_addr, new_size); |
---|
272 | } |
---|
273 | } |
---|
274 | } |
---|
275 | else |
---|
276 | { |
---|
277 | if (IS_INLINE(new_spec)) |
---|
278 | { |
---|
279 | if (IS_ZERO(new_spec)) |
---|
280 | { |
---|
281 | if (IS_ALIGNED(new_spec)) |
---|
282 | { |
---|
283 | if (IS_SLOPPY(new_spec)) |
---|
284 | new_addr = omrealloc0(old_addr, new_size); |
---|
285 | else |
---|
286 | new_addr = omRealloc0Aligned(old_addr, new_size); |
---|
287 | } |
---|
288 | else new_addr = omRealloc0(old_addr, new_size); |
---|
289 | } |
---|
290 | else |
---|
291 | { |
---|
292 | if (IS_ALIGNED(new_spec)) |
---|
293 | { |
---|
294 | if (IS_SLOPPY(new_spec)) |
---|
295 | new_addr = omrealloc(old_addr, new_size); |
---|
296 | else |
---|
297 | new_addr = omReallocAligned(old_addr, new_size); |
---|
298 | } |
---|
299 | else new_addr = omRealloc(old_addr, new_size); |
---|
300 | } |
---|
301 | } |
---|
302 | else |
---|
303 | { |
---|
304 | if (IS_ZERO(new_spec)) |
---|
305 | { |
---|
306 | if (IS_ALIGNED(new_spec)) omTypeRealloc0Aligned(old_addr, void*, new_addr, new_size); |
---|
307 | else omTypeRealloc0(old_addr, void*, new_addr, new_size); |
---|
308 | } |
---|
309 | else |
---|
310 | { |
---|
311 | if (IS_ALIGNED(new_spec)) omTypeReallocAligned(old_addr, void*, new_addr, new_size); |
---|
312 | else omTypeRealloc(old_addr, void*, new_addr, new_size); |
---|
313 | } |
---|
314 | } |
---|
315 | } |
---|
316 | } |
---|
317 | |
---|
318 | if (old_bin != NULL && IS_SPEC_BIN(old_spec)) |
---|
319 | { |
---|
320 | if (old_orig_bin != NULL) |
---|
321 | omUnGetSpecBin(&old_orig_bin); |
---|
322 | else |
---|
323 | omUnGetSpecBin(&old_bin); |
---|
324 | } |
---|
325 | |
---|
326 | new_size = omSizeOfAddr(new_addr); |
---|
327 | old_size = real_old_size; |
---|
328 | min_size = (new_size < old_size ? new_size : old_size); |
---|
329 | |
---|
330 | if (IS_ZERO(old_spec) && IS_ZERO(new_spec)) |
---|
331 | TestAddrContent(new_addr, 0, new_size); |
---|
332 | else |
---|
333 | { |
---|
334 | TestAddrContent(new_addr, (IS_ZERO(old_spec) ? 0 : old_spec), min_size); |
---|
335 | if (IS_ZERO(new_spec) && old_size < new_size) |
---|
336 | TestAddrContent((char *)new_addr + old_size, 0, new_size - old_size); |
---|
337 | } |
---|
338 | |
---|
339 | cell->addr = new_addr; |
---|
340 | cell->spec = new_spec; |
---|
341 | cell->bin = new_bin; |
---|
342 | cell->orig_bin = new_orig_bin; |
---|
343 | InitCellAddrContent(cell); |
---|
344 | omtTestDebug(cell); |
---|
345 | } |
---|
346 | |
---|
347 | #define DO_STRDUP(l) (l & 1) |
---|
348 | void omtTestDup(omMemCell cell, unsigned long spec) |
---|
349 | { |
---|
350 | omtTestDebug(cell); |
---|
351 | |
---|
352 | if (DO_STRDUP(spec)) |
---|
353 | { |
---|
354 | size_t size = omSizeOfAddr(cell->addr); |
---|
355 | void* new_addr; |
---|
356 | memset(cell->addr, 'a', size - 1); |
---|
357 | ((char*) cell->addr)[size-1] = '\0'; |
---|
358 | new_addr = omStrDup(cell->addr); |
---|
359 | TestAddrContentEqual(new_addr, cell->addr, size); |
---|
360 | omFree(new_addr); |
---|
361 | InitCellAddrContent(cell); |
---|
362 | } |
---|
363 | else |
---|
364 | { |
---|
365 | void* new_addr = omMemDup(cell->addr); |
---|
366 | TestAddrContentEqual(new_addr, cell->addr, omSizeOfAddr(cell->addr)); |
---|
367 | omFree(new_addr); |
---|
368 | new_addr = omMemDupAligned(cell->addr); |
---|
369 | TestAddrContentEqual(new_addr, cell->addr, omSizeOfAddr(cell->addr)); |
---|
370 | omDebugAddrAlignedSize(new_addr, omSizeOfAddr(cell->addr)); |
---|
371 | omFree(new_addr); |
---|
372 | } |
---|
373 | } |
---|