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