efa1c42cc240be6e1284d9a7dc1942eedbc449a8
[ghc.git] / testsuite / tests / rts / testblockalloc.c
1 #include "Rts.h"
2
3 #include <stdio.h>
4
5 extern bdescr *allocGroup_lock_lock(uint32_t n);
6 extern void freeGroup_lock(bdescr *p);
7
8 const int ARRSIZE = 256;
9 const int LOOPS = 100;
10 const int MAXALLOC = ((8 * 1024 * 1024) / BLOCK_SIZE - 1);
11 //const int MAXALLOC = ((64 * 1024 * 1024) / BLOCK_SIZE - 1);
12 const int SEED = 0xf00f00;
13
14 extern StgWord mblocks_allocated;
15
16 static void test_random_alloc(void)
17 {
18 bdescr *a[ARRSIZE];
19
20 // repeatedly sweep though the array, allocating new random-sized
21 // objects and deallocating the old ones.
22 for (int i=0; i < LOOPS; i++)
23 {
24 for (int j=0; j < ARRSIZE; j++)
25 {
26 if (i > 0)
27 {
28 IF_DEBUG(block_alloc, debugBelch("A%d: freeing %p, %d blocks @ %p\n", j, a[j], a[j]->blocks, a[j]->start));
29 freeGroup_lock(a[j]);
30 DEBUG_ONLY(checkFreeListSanity());
31 }
32
33 int b = (rand() % MAXALLOC) + 1;
34 a[j] = allocGroup_lock(b);
35 IF_DEBUG(block_alloc, debugBelch("A%d: allocated %p, %d blocks @ %p\n", j, a[j], b, a[j]->start));
36 // allocating zero blocks isn't allowed
37 DEBUG_ONLY(checkFreeListSanity());
38 }
39 }
40
41 for (int j=0; j < ARRSIZE; j++)
42 {
43 freeGroup_lock(a[j]);
44 }
45 }
46
47 static void test_sequential_alloc(void)
48 {
49 bdescr *a[ARRSIZE];
50
51 // this time, sweep forwards allocating new blocks, and then
52 // backwards deallocating them.
53 for (int i=0; i < LOOPS; i++)
54 {
55 for (int j=0; j < ARRSIZE; j++)
56 {
57 int b = (rand() % MAXALLOC) + 1;
58 a[j] = allocGroup_lock(b);
59 IF_DEBUG(block_alloc, debugBelch("B%d,%d: allocated %p, %d blocks @ %p\n", i, j, a[j], b, a[j]->start));
60 DEBUG_ONLY(checkFreeListSanity());
61 }
62 for (int j=ARRSIZE-1; j >= 0; j--)
63 {
64 IF_DEBUG(block_alloc, debugBelch("B%d,%d: freeing %p, %d blocks @ %p\n", i, j, a[j], a[j]->blocks, a[j]->start));
65 freeGroup_lock(a[j]);
66 DEBUG_ONLY(checkFreeListSanity());
67 }
68 }
69 }
70
71 int main (int argc, char *argv[])
72 {
73 int i, j, b;
74
75 bdescr *a[ARRSIZE];
76
77 srand(SEED);
78
79 {
80 RtsConfig conf = defaultRtsConfig;
81 conf.rts_opts_enabled = RtsOptsAll;
82 hs_init_ghc(&argc, &argv, conf);
83 }
84
85 test_random_alloc();
86 test_sequential_alloc();
87
88 DEBUG_ONLY(checkFreeListSanity());
89
90 hs_exit(); // will do a memory leak test
91
92 exit(0);
93 }