1 /* Copyright (c) 2012-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "xbt/mmalloc.h"
17 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"this test");
19 #define BUFFSIZE 204800
21 #define size_of_block(i) (((i % 50)+1)* 100)
23 int main(int argc, char**argv)
26 void *pointers[TESTSIZE];
29 XBT_INFO("Allocating a new heap");
30 unsigned long mask = ~((unsigned long)xbt_pagesize - 1);
31 void *addr = (void*)(((unsigned long)sbrk(0) + BUFFSIZE) & mask);
32 heapA = xbt_mheap_new(-1, addr);
34 perror("attach 1 failed");
35 fprintf(stderr, "bye\n");
39 XBT_INFO("HeapA allocated");
42 for (i = 0; i < TESTSIZE; i++) {
43 size = size_of_block(i);
44 pointers[i] = mmalloc(heapA, size);
45 XBT_INFO("%d bytes allocated with offset %tx", size, ((char*)pointers[i])-((char*)heapA));
47 XBT_INFO("All blocks were correctly allocated. Free every second block");
48 for (i = 0; i < TESTSIZE; i+=2) {
49 mfree(heapA,pointers[i]);
51 XBT_INFO("Memset every second block to zero (yeah, they are not currently allocated :)");
52 for (i = 0; i < TESTSIZE; i+=2) {
53 size = size_of_block(i);
54 memset(pointers[i],0, size);
56 XBT_INFO("Re-allocate every second block");
57 for (i = 0; i < TESTSIZE; i+=2) {
58 size = size_of_block(i);
59 pointers[i] = mmalloc(heapA, size);
62 XBT_INFO("free all blocks (each one twice, to check that double free are correctly catched)");
63 for (i = 0; i < TESTSIZE; i++) {
67 mfree(heapA, pointers[i]);
69 mfree(heapA, pointers[i]);
75 xbt_die("FAIL: A double-free went undetected (for size:%d)",size_of_block(i));
78 XBT_INFO("free again all blocks (to really check that double free are correctly catched)");
79 for (i = 0; i < TESTSIZE; i++) {
84 mfree(heapA, pointers[i]);
90 xbt_die("FAIL: A double-free went undetected (for size:%d)",size_of_block(i));
94 XBT_INFO("Damnit, I cannot break mmalloc this time. That's SO disappointing.");