Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Give a LD_LIBRARY_PATH to java tests
[simgrid.git] / teshsuite / xbt / mmalloc / mmalloc_test.cpp
1 /* Copyright (c) 2012-2014. 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 <stdio.h>
10 #include <assert.h>
11 #include <fcntl.h>
12 #include <sys/stat.h>
13 #include <stdlib.h>
14 #include <string.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, size;
44   for (i = 0; i < TESTSIZE; i++) {
45     size = size_of_block(i);
46     pointers[i] = mmalloc(heapA, size);
47     XBT_INFO("%d bytes allocated with offset %tx", size, ((char*)pointers[i])-((char*)heapA));
48   }
49   XBT_INFO("All blocks were correctly allocated. Free every second block");
50   for (i = 0; i < TESTSIZE; i+=2) {
51     mfree(heapA, pointers[i]);
52   }
53   XBT_INFO("Memset every second block to zero (yeah, they are not currently allocated :)");
54   for (i = 0; i < TESTSIZE; i+=2) {
55     size = size_of_block(i);
56     memset(pointers[i],0, size);
57   }
58   XBT_INFO("Re-allocate every second block");
59   for (i = 0; i < TESTSIZE; i+=2) {
60     size = size_of_block(i);
61     pointers[i] = mmalloc(heapA, size);
62   }
63
64   XBT_INFO("free all blocks (each one twice, to check that double free are correctly catched)");
65   for (i = 0; i < TESTSIZE; i++) {
66     bool gotit = false;
67     mfree(heapA, pointers[i]);
68     try {
69       mfree(heapA, pointers[i]);
70     } catch(xbt_ex& e) {
71       gotit = true;
72     }
73     if (!gotit)
74       xbt_die("FAIL: A double-free went undetected (for size:%d)",size_of_block(i));
75   }
76
77   XBT_INFO("free again all blocks (to really check that double free are correctly catched)");
78   for (i = 0; i < TESTSIZE; i++) {
79     bool gotit = false;
80     try {
81       mfree(heapA, pointers[i]);
82     } catch(xbt_ex& e) {
83       gotit = true;
84     }
85     if (!gotit)
86       xbt_die("FAIL: A double-free went undetected (for size:%d)",size_of_block(i));
87   }
88
89   XBT_INFO("Damnit, I cannot break mmalloc this time. That's SO disappointing.");
90   return 0;
91 }