[e70e45] | 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 | *******************************************************************/ |
---|
[be127e] | 7 | #include "omtTest.h" |
---|
[e70e45] | 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 | |
---|
[36ee6f] | 17 | #ifdef TEST_KEEP |
---|
| 18 | #define OM_CHECK CHECK_LEVEL |
---|
[f3398d] | 19 | #define OM_KEEP 1 |
---|
[36ee6f] | 20 | #define omtTestAlloc omtTestAllocKeep |
---|
| 21 | #define omtTestRealloc omtTestReallocKeep |
---|
| 22 | #define omtTestFree omtTestFreeKeep |
---|
| 23 | #define omtTestDup omtTestDupKeep |
---|
| 24 | #endif |
---|
| 25 | |
---|
[be127e] | 26 | #include "omalloc.h" |
---|
[e70e45] | 27 | |
---|
| 28 | #ifndef OM_ALIGNMENT_NEEDS_WORK |
---|
| 29 | #define omSmallSize2AlignedBin omSmallSize2Bin |
---|
| 30 | #endif |
---|
| 31 | |
---|
[bd8655a] | 32 | #ifdef HAVE_OMALLOC |
---|
[78f1ab] | 33 | void omtTestAlloc(omMemCell cell, unsigned long spec) |
---|
[e70e45] | 34 | { |
---|
[384936] | 35 | size_t size = GET_SIZE(spec); |
---|
[e70e45] | 36 | void* addr; |
---|
| 37 | omBin bin = NULL; |
---|
[f103fb] | 38 | omBin orig_bin = NULL; |
---|
[e70e45] | 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 |
---|
[f103fb] | 47 | bin = omGetSpecBin(size); |
---|
[e70e45] | 48 | } |
---|
| 49 | else |
---|
| 50 | { |
---|
| 51 | if (IS_ALIGNED(spec)) |
---|
| 52 | bin = omSmallSize2AlignedBin(size); |
---|
| 53 | else |
---|
| 54 | bin = omSmallSize2Bin(size); |
---|
| 55 | } |
---|
[13fe1b] | 56 | |
---|
[f103fb] | 57 | if (IS_STICKY_BIN(spec)) |
---|
| 58 | { |
---|
| 59 | orig_bin = bin; |
---|
| 60 | bin = omtGetStickyBin(bin); |
---|
| 61 | } |
---|
[13fe1b] | 62 | |
---|
[e70e45] | 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; |
---|
[f103fb] | 127 | cell->orig_bin = orig_bin; |
---|
[e70e45] | 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; |
---|
[78f1ab] | 138 | unsigned long spec = cell->spec; |
---|
[e70e45] | 139 | omBin bin = cell->bin; |
---|
[f103fb] | 140 | omBin orig_bin = cell->orig_bin; |
---|
[e70e45] | 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); |
---|
[4d59e4] | 154 | else if (IS_FREE_BINADDR(spec) && (bin != NULL) && (size <= OM_MAX_BLOCK_SIZE)) |
---|
[91c4012] | 155 | { |
---|
| 156 | omFreeBinAddr(addr); |
---|
| 157 | } |
---|
[e70e45] | 158 | else |
---|
| 159 | { |
---|
| 160 | if (IS_SLOPPY(spec)) |
---|
| 161 | omfree(addr); |
---|
| 162 | else |
---|
| 163 | omFree(addr); |
---|
| 164 | } |
---|
[13fe1b] | 165 | |
---|
[e70e45] | 166 | if (bin != NULL && IS_SPEC_BIN(spec)) |
---|
[f103fb] | 167 | { |
---|
| 168 | if (orig_bin != NULL) |
---|
| 169 | omUnGetSpecBin(&orig_bin); |
---|
| 170 | else |
---|
| 171 | omUnGetSpecBin(&bin); |
---|
| 172 | } |
---|
[e70e45] | 173 | |
---|
| 174 | cell->addr = NULL; |
---|
| 175 | cell->spec = 0; |
---|
| 176 | cell->bin = NULL; |
---|
[f103fb] | 177 | cell->orig_bin = NULL; |
---|
[e70e45] | 178 | } |
---|
| 179 | |
---|
[78f1ab] | 180 | void omtTestRealloc(omMemCell cell, unsigned long new_spec) |
---|
[e70e45] | 181 | { |
---|
| 182 | void* old_addr = cell->addr; |
---|
[384936] | 183 | unsigned long old_spec = cell->spec; |
---|
[e70e45] | 184 | omBin old_bin = cell->bin; |
---|
[f103fb] | 185 | omBin old_orig_bin = cell->orig_bin; |
---|
[e70e45] | 186 | size_t old_size = GET_SIZE(old_spec); |
---|
| 187 | void* new_addr; |
---|
| 188 | omBin new_bin = NULL; |
---|
[f103fb] | 189 | omBin new_orig_bin = NULL; |
---|
[e70e45] | 190 | size_t new_size = GET_SIZE(new_spec); |
---|
| 191 | size_t real_old_size = omSizeOfAddr(old_addr); |
---|
| 192 | size_t min_size; |
---|
[13fe1b] | 193 | |
---|
[e70e45] | 194 | omtTestDebug(cell); |
---|
| 195 | |
---|
[13fe1b] | 196 | if (old_bin != NULL && IS_FREE_BIN(old_spec) && |
---|
[e70e45] | 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 |
---|
[13fe1b] | 204 | new_bin = omGetSpecBin(new_size); |
---|
[e70e45] | 205 | } |
---|
| 206 | else |
---|
| 207 | { |
---|
| 208 | if (IS_ALIGNED(new_spec)) |
---|
| 209 | new_bin = omSmallSize2AlignedBin(new_size); |
---|
| 210 | else |
---|
| 211 | new_bin = omSmallSize2Bin(new_size); |
---|
[13fe1b] | 212 | } |
---|
[f103fb] | 213 | |
---|
| 214 | if (IS_STICKY_BIN(new_spec)) |
---|
| 215 | { |
---|
| 216 | new_orig_bin = new_bin; |
---|
| 217 | new_bin = omtGetStickyBin(new_bin); |
---|
| 218 | } |
---|
[13fe1b] | 219 | |
---|
[e70e45] | 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 | { |
---|
[13fe1b] | 239 | if (IS_ALIGNED(new_spec)) |
---|
[e70e45] | 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 | { |
---|
[13fe1b] | 251 | if (IS_ALIGNED(new_spec)) |
---|
[e70e45] | 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 | { |
---|
[13fe1b] | 281 | if (IS_ALIGNED(new_spec)) |
---|
[e70e45] | 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 | { |
---|
[13fe1b] | 292 | if (IS_ALIGNED(new_spec)) |
---|
[e70e45] | 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 | |
---|
[13fe1b] | 318 | if (old_bin != NULL && IS_SPEC_BIN(old_spec)) |
---|
[f103fb] | 319 | { |
---|
| 320 | if (old_orig_bin != NULL) |
---|
| 321 | omUnGetSpecBin(&old_orig_bin); |
---|
| 322 | else |
---|
[e70e45] | 323 | omUnGetSpecBin(&old_bin); |
---|
[f103fb] | 324 | } |
---|
[e70e45] | 325 | |
---|
| 326 | new_size = omSizeOfAddr(new_addr); |
---|
| 327 | old_size = real_old_size; |
---|
| 328 | min_size = (new_size < old_size ? new_size : old_size); |
---|
[13fe1b] | 329 | |
---|
[e70e45] | 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) |
---|
[4d59e4] | 336 | TestAddrContent((char *)new_addr + old_size, 0, new_size - old_size); |
---|
[e70e45] | 337 | } |
---|
| 338 | |
---|
| 339 | cell->addr = new_addr; |
---|
| 340 | cell->spec = new_spec; |
---|
| 341 | cell->bin = new_bin; |
---|
[f103fb] | 342 | cell->orig_bin = new_orig_bin; |
---|
[e70e45] | 343 | InitCellAddrContent(cell); |
---|
| 344 | omtTestDebug(cell); |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | #define DO_STRDUP(l) (l & 1) |
---|
[78f1ab] | 348 | void omtTestDup(omMemCell cell, unsigned long spec) |
---|
[e70e45] | 349 | { |
---|
| 350 | omtTestDebug(cell); |
---|
[13fe1b] | 351 | |
---|
[e70e45] | 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 | } |
---|
[bd8655a] | 374 | #endif |
---|