Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / teshsuite / xbt / mmalloc / mmalloc_test.cpp
1 /* Copyright (c) 2012-2014, 2016-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "xbt/mmalloc.h"
8 #include "xbt.h"
9 #include <cassert>
10 #include <cstdio>
11 #include <cstdlib>
12 #include <cstring>
13 #include <fcntl.h>
14 #include <sys/stat.h>
15 #include <unistd.h>
16
17 #include <xbt/ex.hpp>
18
19 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"this test");
20
21 #define BUFFSIZE 204800
22 #define TESTSIZE 100
23 #define size_of_block(i) (((i % 50)+1)* 100)
24
25 int main(int argc, char**argv)
26 {
27   xbt_mheap_t heapA = nullptr;
28   void *pointers[TESTSIZE];
29   xbt_init(&argc,argv);
30
31   XBT_INFO("Allocating a new heap");
32   unsigned long mask = ~((unsigned long)xbt_pagesize - 1);
33   void *addr = (void*)(((unsigned long)sbrk(0) + BUFFSIZE) & mask);
34   heapA = xbt_mheap_new(-1, addr);
35   if (heapA == NULL) {
36     perror("attach 1 failed");
37     fprintf(stderr, "bye\n");
38     exit(1);
39   }
40
41   XBT_INFO("HeapA allocated");
42
43   int i;
44   int size;
45   for (i = 0; i < TESTSIZE; i++) {
46     size = size_of_block(i);
47     pointers[i] = mmalloc(heapA, size);
48     XBT_INFO("%d bytes allocated with offset %zx", size, (size_t)((char*)pointers[i] - (char*)heapA));
49   }
50   XBT_INFO("All blocks were correctly allocated. Free every second block");
51   for (i = 0; i < TESTSIZE; i+=2) {
52     mfree(heapA, pointers[i]);
53   }
54   XBT_INFO("Memset every second block to zero (yeah, they are not currently allocated :)");
55   for (i = 0; i < TESTSIZE; i+=2) {
56     size = size_of_block(i);
57     memset(pointers[i],0, size);
58   }
59   XBT_INFO("Re-allocate every second block");
60   for (i = 0; i < TESTSIZE; i+=2) {
61     size = size_of_block(i);
62     pointers[i] = mmalloc(heapA, size);
63   }
64
65   XBT_INFO("free all blocks (each one twice, to check that double free are correctly catched)");
66   for (i = 0; i < TESTSIZE; i++) {
67     bool gotit = false;
68     mfree(heapA, pointers[i]);
69     try {
70       mfree(heapA, pointers[i]);
71     } catch(xbt_ex& e) {
72       gotit = true;
73     }
74     if (not gotit)
75       xbt_die("FAIL: A double-free went undetected (for size:%d)",size_of_block(i));
76   }
77
78   XBT_INFO("free again all blocks (to really check that double free are correctly catched)");
79   for (i = 0; i < TESTSIZE; i++) {
80     bool gotit = false;
81     try {
82       mfree(heapA, pointers[i]);
83     } catch(xbt_ex& e) {
84       gotit = true;
85     }
86     if (not gotit)
87       xbt_die("FAIL: A double-free went undetected (for size:%d)",size_of_block(i));
88   }
89
90   XBT_INFO("Damnit, I cannot break mmalloc this time. That's SO disappointing.");
91   return 0;
92 }