Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
checks that double free are correctly detected
[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
16 int main(int argc, char**argv)
17 {
18   void *heapA;
19   void *pointers[TESTSIZE];
20   xbt_init(&argc,argv);
21
22   XBT_INFO("Allocating a new heap");
23   heapA = xbt_mheap_new(-1, ((char*)sbrk(0)) + BUFFSIZE);
24   if (heapA == NULL) {
25     perror("attach 1 failed");
26     fprintf(stderr, "bye\n");
27     exit(1);
28   }
29
30   XBT_INFO("HeapA allocated");
31
32   int i, size;
33   for (i = 0; i < TESTSIZE; i++) {
34     size = ((i % 10)+1)* 100;
35     pointers[i] = mmalloc(heapA, size);
36     XBT_INFO("%d bytes allocated with offset %lu", size, ((char*)pointers[i])-((char*)heapA));
37   }
38
39   for (i = 0; i < TESTSIZE; i++) {
40     xbt_ex_t e;
41     int gotit = 1;
42
43     mfree(heapA, pointers[i]);
44     TRY {
45       mfree(heapA, pointers[i]);
46       gotit = 0;
47     } CATCH(e) {
48       xbt_ex_free(e);
49     }
50     if (!gotit)
51       xbt_die("FAIL: A double-free went undetected (for size:%d)",((i%10)+1)*100);
52   }
53
54   XBT_INFO("Done; bye bye");
55   return 0;
56 }