added test alloc with working purecap

This commit is contained in:
2025-04-07 17:10:55 +01:00
parent 4894364ddf
commit 8cf048b6b2
5 changed files with 465 additions and 10 deletions

View File

@@ -49,7 +49,7 @@
// perror("mmap");
// return;
// }
// #else
// #else
// mem = calloc(pool, 1);
// if (!mem) {
// fprintf(stderr, "alloc_init(): could not allocate heap\n");
@@ -58,7 +58,7 @@
// #endif
// // Each bitmap entry can represent 8 blocks and each block is 16 bytes
// // So space representable in one uint8_t is 16 * 8 = 128 bytes
// uint64_t sz = pool / 128;
// uint64_t sz = pool / 128;
// if (sz == 0)
// sz = 1; // allocate at least one to keep track of small pools
@@ -89,19 +89,19 @@
// int rem = sz % 16;
// sz += (16 - rem);
// }
// // Allocate two extra blocks
// // Allocate two extra blocks
// // First will be allocated at just behind the first user accessible block
// // This block will have the number of blocks allocated and a randomly generated magic number each 8 bytes long
// // The last block has the "magic number" present in the first block
// // If this magic number gets modified then when free() tries to free the memory
// // If this magic number gets modified then when free() tries to free the memory
// // Buffer overruns will be caught and this allocator gets poisoned i.e it can no longer allocate memory
// // This is because all blocks are laid out sequentially and if the user overruns the blocks allocated
// // This is because all blocks are laid out sequentially and if the user overruns the blocks allocated
// // Then the user may have overwritten the contents of other blocks and it is not possible to estimate the damage caused
// // and data corrupted. All pointers to blocks allocated immediately become invalid and free() posions the allocator
// // This helps catch buffer overflows early on
// // This helps catch buffer overflows early on
// uint64_t blk = (sz / 16) + 2;
// // if we are posioned, all allocation requests will fail
// // if we are posioned, all allocation requests will fail
// if (poison)
// return 0;
// // Loop through the entire bitmap. If a free block is found, check if there are at least blk free blocks after it.
@@ -134,7 +134,7 @@
// // Return the user a pointer which points to the region just above our metadata block
// return mem + ((i + 1) * 16);
// }
// next:
// next:
// }
// fprintf(stderr, "Pool has been exhausted...Cannot allocate more memory");
// return 0;
@@ -165,7 +165,7 @@
// offset -= 16;
// offset /= 16;
// // Clear all bits representing this block so next call to alloc() can use this
// // Clear all bits representing this block so next call to alloc() can use this
// for (uint64_t j = offset; j < offset + blk + 1; j++) {
// MARK(j);
// }
@@ -174,4 +174,4 @@
// // Do not call this unless you are absolutely sure about the cause of poisoning
// void clear_posion() {
// poison = 0;
// }
// }