summaryrefslogtreecommitdiff
path: root/testsuite/tests/rts/testmblockalloc.c
blob: df036583873f9442d6829418e79b7b1198ed348a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "Rts.h"

#include <stdio.h>

// 16 * 64 == max 1GB
const int MAXALLOC = 16;
const int ARRSIZE  = 64;

const int LOOPS    = 1000;
const int SEED     = 0xf00f00;

extern lnat mblocks_allocated;

int main (int argc, char *argv[])
{
    int i, j, b;

    void *a[ARRSIZE];
    nat sizes[ARRSIZE];

    srand(SEED);

    {
        RtsConfig conf = defaultRtsConfig;
        conf.rts_opts_enabled = RtsOptsAll;
        hs_init_ghc(&argc, &argv, conf);
    }

   // repeatedly sweep though the array, allocating new random-sized
   // objects and deallocating the old ones.
   for (i=0; i < LOOPS; i++)
   {
       for (j=0; j < ARRSIZE; j++)
       {
           if (i > 0)
           {
               freeMBlocks(a[j], sizes[j]);
           }
           b = (rand() % MAXALLOC) + 1;
           a[j] = getMBlocks(b);
           sizes[j] = b;
       }
   }

   releaseFreeMemory();

   for (j=0; j < ARRSIZE; j++)
   {
       freeMBlocks(a[j], sizes[j]);
   }

   releaseFreeMemory();

    // this time, sweep forwards allocating new blocks, and then
    // backwards deallocating them.
    for (i=0; i < LOOPS; i++)
    {
        for (j=0; j < ARRSIZE; j++)
        {
            b = (rand() % MAXALLOC) + 1;
            a[j] = getMBlocks(b);
            sizes[j] = b;
        }
        for (j=ARRSIZE-1; j >= 0; j--)
        {
            freeMBlocks(a[j], sizes[j]);
        }
    }

    releaseFreeMemory();

    hs_exit(); // will do a memory leak test

    exit(0);
}