1 #include "xbt/mmalloc.h"
11 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"this test");
13 #define BUFFSIZE 204800
15 #define size_of_block(i) (((i % 50)+1)* 100)
17 int main(int argc, char**argv)
20 void *pointers[TESTSIZE];
23 XBT_INFO("Allocating a new heap");
24 heapA = xbt_mheap_new(-1, ((char*)sbrk(0)) + BUFFSIZE);
26 perror("attach 1 failed");
27 fprintf(stderr, "bye\n");
31 XBT_INFO("HeapA allocated");
34 for (i = 0; i < TESTSIZE; i++) {
35 size = size_of_block(i);
36 pointers[i] = mmalloc(heapA, size);
37 XBT_INFO("%d bytes allocated with offset %tx", size, ((char*)pointers[i])-((char*)heapA));
39 XBT_INFO("All blocks were correctly allocated. Free every second block");
40 for (i = 0; i < TESTSIZE; i+=2) {
41 size = size_of_block(i);
42 mfree(heapA,pointers[i]);
44 XBT_INFO("Memset every second block to zero (yeah, they are not currently allocated :)");
45 for (i = 0; i < TESTSIZE; i+=2) {
46 size = size_of_block(i);
47 memset(pointers[i],0, size);
49 XBT_INFO("Re-allocate every second block");
50 for (i = 0; i < TESTSIZE; i+=2) {
51 size = size_of_block(i);
52 pointers[i] = mmalloc(heapA, size);
55 XBT_INFO("free all blocks (each one twice, to check that double free are correctly catched)");
56 for (i = 0; i < TESTSIZE; i++) {
60 mfree(heapA, pointers[i]);
62 mfree(heapA, pointers[i]);
68 xbt_die("FAIL: A double-free went undetected (for size:%d)",size_of_block(i));
71 XBT_INFO("free again all blocks (to really check that double free are correctly catched)");
72 for (i = 0; i < TESTSIZE; i++) {
77 mfree(heapA, pointers[i]);
83 xbt_die("FAIL: A double-free went undetected (for size:%d)",size_of_block(i));
87 XBT_INFO("Damnit, I cannot break mmalloc this time. That's SO disappointing.");