Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove more references to gras.
[simgrid.git] / teshsuite / xbt / mmalloc_test.c
1 #include "xbt/mmalloc.h"
2 #include "xbt.h"
3 #include <stdio.h>
4 #include <assert.h>
5 #include <fcntl.h>
6 #include <sys/stat.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"this test");
12
13 #define BUFFSIZE 204800
14 #define TESTSIZE 100
15 #define size_of_block(i) (((i % 50)+1)* 100)
16
17 int main(int argc, char**argv)
18 {
19   void *heapA;
20   void *pointers[TESTSIZE];
21   xbt_init(&argc,argv);
22
23   XBT_INFO("Allocating a new heap");
24   heapA = xbt_mheap_new(-1, ((char*)sbrk(0)) + BUFFSIZE);
25   if (heapA == NULL) {
26     perror("attach 1 failed");
27     fprintf(stderr, "bye\n");
28     exit(1);
29   }
30
31   XBT_INFO("HeapA allocated");
32
33   int i, size;
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));
38   }
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]);
43   }
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);
48   }
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);
53   }
54
55   XBT_INFO("free all blocks (each one twice, to check that double free are correctly catched)");
56   for (i = 0; i < TESTSIZE; i++) {
57     xbt_ex_t e;
58     int gotit = 1;
59
60     mfree(heapA, pointers[i]);
61     TRY {
62       mfree(heapA, pointers[i]);
63       gotit = 0;
64     } CATCH(e) {
65       xbt_ex_free(e);
66     }
67     if (!gotit)
68       xbt_die("FAIL: A double-free went undetected (for size:%d)",size_of_block(i));
69   }
70
71   XBT_INFO("free again all blocks (to really check that double free are correctly catched)");
72   for (i = 0; i < TESTSIZE; i++) {
73     xbt_ex_t e;
74     int gotit = 1;
75
76     TRY {
77       mfree(heapA, pointers[i]);
78       gotit = 0;
79     } CATCH(e) {
80       xbt_ex_free(e);
81     }
82     if (!gotit)
83       xbt_die("FAIL: A double-free went undetected (for size:%d)",size_of_block(i));
84   }
85
86
87   XBT_INFO("Damnit, I cannot break mmalloc this time. That's SO disappointing.");
88   return 0;
89 }