My Project
Loading...
Searching...
No Matches
omtTestAlloc.c
Go to the documentation of this file.
1/*******************************************************************
2 * File: omtTestAlloc.c
3 * Purpose: alloc function to be included in omMain.c
4 * Author: [email protected] (Olaf Bachmann)
5 * Created: 11/99
6 *******************************************************************/
7#include "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.h"
27
28#ifndef OM_ALIGNMENT_NEEDS_WORK
29#define omSmallSize2AlignedBin omSmallSize2Bin
30#endif
31
32#ifdef HAVE_OMALLOC
33void 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))
46 else
47 bin = omGetSpecBin(size);
48 }
49 else
50 {
51 if (IS_ALIGNED(spec))
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
131
132 omtTestDebug(cell);
133}
134
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
180void 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;
344 omtTestDebug(cell);
345}
346
347#define DO_STRDUP(l) (l & 1)
348void 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);
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}
374#endif
int size(const CanonicalForm &f, const Variable &v)
int size ( const CanonicalForm & f, const Variable & v )
Definition: cf_ops.cc:600
#define omStrDup(s)
Definition: omAllocDecl.h:263
#define omTypeAlloc0Bin(type, addr, bin)
Definition: omAllocDecl.h:204
#define omrealloc(addr, size)
Definition: omAllocDecl.h:233
#define omfree(addr)
Definition: omAllocDecl.h:237
#define omTypeReallocAlignedSize
Definition: omAllocDecl.h:276
#define omTypeRealloc0(o_addr, type, addr, size)
Definition: omAllocDecl.h:224
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
#define omTypeRealloc0Size(o_addr, o_size, type, addr, size)
Definition: omAllocDecl.h:219
#define omAlloc0Aligned
Definition: omAllocDecl.h:274
#define omRealloc0AlignedSize
Definition: omAllocDecl.h:279
#define omrealloc0Size(addr, o_size, size)
Definition: omAllocDecl.h:232
#define omRealloc0Aligned
Definition: omAllocDecl.h:284
#define omAlloc(size)
Definition: omAllocDecl.h:210
#define omreallocSize(addr, o_size, size)
Definition: omAllocDecl.h:231
#define omReallocSize(addr, o_size, size)
Definition: omAllocDecl.h:220
#define omDebugAddrAlignedSize
Definition: omAllocDecl.h:292
#define omTypeAllocBin(type, addr, bin)
Definition: omAllocDecl.h:203
#define omAllocBin(bin)
Definition: omAllocDecl.h:205
#define omReallocAligned
Definition: omAllocDecl.h:283
#define omTypeRealloc0AlignedSize
Definition: omAllocDecl.h:277
#define omrealloc0(addr, size)
Definition: omAllocDecl.h:234
#define omRealloc0Bin(o_addr, o_bin, bin)
Definition: omAllocDecl.h:216
#define omAlloc0Bin(bin)
Definition: omAllocDecl.h:206
#define omTypeRealloc0Bin(o_addr, o_bin, type, addr, bin)
Definition: omAllocDecl.h:214
#define omTypeAlloc0(type, addr, size)
Definition: omAllocDecl.h:209
#define omalloc0(size)
Definition: omAllocDecl.h:229
#define omalloc(size)
Definition: omAllocDecl.h:228
#define omRealloc(addr, size)
Definition: omAllocDecl.h:225
#define omTypeAlloc0Aligned
Definition: omAllocDecl.h:272
#define omReallocAlignedSize
Definition: omAllocDecl.h:278
#define omFree(addr)
Definition: omAllocDecl.h:261
#define omTypeRealloc0Aligned
Definition: omAllocDecl.h:282
#define omAlloc0(size)
Definition: omAllocDecl.h:211
#define omFreeBin(addr, bin)
Definition: omAllocDecl.h:259
#define omFreeBinAddr(addr)
Definition: omAllocDecl.h:258
#define omReallocBin(o_addr, o_bin, bin)
Definition: omAllocDecl.h:215
#define omRealloc0Size(addr, o_size, size)
Definition: omAllocDecl.h:221
#define omMemDup(s)
Definition: omAllocDecl.h:264
#define omTypeAlloc(type, addr, size)
Definition: omAllocDecl.h:208
#define omTypeReallocAligned
Definition: omAllocDecl.h:281
#define omTypeAllocAligned
Definition: omAllocDecl.h:271
#define omRealloc0(addr, size)
Definition: omAllocDecl.h:226
#define omTypeReallocSize(o_addr, o_size, type, addr, size)
Definition: omAllocDecl.h:218
#define omTypeReallocBin(o_addr, o_bin, type, addr, bin)
Definition: omAllocDecl.h:213
#define omMemDupAligned
Definition: omAllocDecl.h:286
#define omTypeRealloc(o_addr, type, addr, size)
Definition: omAllocDecl.h:223
#define omAllocAligned
Definition: omAllocDecl.h:273
#define omfreeSize(addr, size)
Definition: omAllocDecl.h:236
#define omSmallSize2Bin(size)
size_t omSizeOfAddr(const void *addr)
#define omGetSpecBin(size)
Definition: omBin.h:11
#define omGetAlignedSpecBin(size)
Definition: omBin.h:10
#define omUnGetSpecBin(bin_ptr)
Definition: omBin.h:14
#define NULL
Definition: omList.c:12
omBin_t * omBin
Definition: omStructs.h:12
#define OM_MAX_BLOCK_SIZE
Definition: omTables.c:31
void omtTestAlloc(omMemCell cell, unsigned long spec)
Definition: omtTestAlloc.c:33
void omtTestRealloc(omMemCell cell, unsigned long new_spec)
Definition: omtTestAlloc.c:180
#define DO_STRDUP(l)
Definition: omtTestAlloc.c:347
void omtTestDup(omMemCell cell, unsigned long spec)
Definition: omtTestAlloc.c:348
#define omSmallSize2AlignedBin
Definition: omtTestAlloc.c:29
void omtTestFree(omMemCell cell)
Definition: omtTestAlloc.c:135
void TestAddrContent(void *addr, unsigned long value, size_t size)
Definition: omtTest.c:106
omBin omtGetStickyBin(omBin bin)
Definition: omtTest.c:275
void omtTestDebug(omMemCell cell)
Definition: omtTest.c:36
void InitCellAddrContent(omMemCell cell)
Definition: omtTest.c:130
void TestAddrContentEqual(void *s1, void *s2, size_t size)
Definition: omtTest.c:90
#define IS_SPEC_BIN(spec)
Definition: omtTest.h:56
#define IS_FREE_BINADDR(spec)
Definition: omtTest.h:89
#define IS_STICKY_BIN(spec)
Definition: omtTest.h:49
#define IS_ALIGNED(spec)
Definition: omtTest.h:53
#define IS_BIN(spec)
Definition: omtTest.h:55
#define IS_SLOPPY(spec)
Definition: omtTest.h:88
#define IS_ZERO(spec)
Definition: omtTest.h:54
#define IS_INLINE(spec)
Definition: omtTest.h:57
#define IS_FREE_SIZE(spec)
Definition: omtTest.h:86
omMemCell_t * omMemCell
Definition: omtTest.h:33
#define IS_FREE_BIN(spec)
Definition: omtTest.h:87
#define GET_SIZE(spec)
Definition: omtTest.h:51