14244dcef3c593fc962a1c2146cedc0eafc3fa10
[ghc-hetmet.git] / rts / sm / MBlock.h
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-2008
4  *
5  * MegaBlock Allocator interface.
6  *
7  * ---------------------------------------------------------------------------*/
8
9 #ifndef MBLOCK_H
10 #define MBLOCK_H
11
12 extern lnat RTS_VAR(mblocks_allocated);
13
14 extern void initMBlocks(void);
15 extern void * getMBlock(void);
16 extern void * getMBlocks(nat n);
17 extern void freeAllMBlocks(void);
18
19 #ifdef DEBUG
20 extern void *getFirstMBlock(void);
21 extern void *getNextMBlock(void *mblock);
22 #endif
23
24 /* -----------------------------------------------------------------------------
25    The HEAP_ALLOCED() test.
26
27    HEAP_ALLOCED is called FOR EVERY SINGLE CLOSURE during GC.
28    It needs to be FAST.
29
30    Implementation of HEAP_ALLOCED
31    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32
33    Since heap is allocated in chunks of megablocks (MBLOCK_SIZE), we
34    can just use a table to record which megablocks in the address
35    space belong to the heap.  On a 32-bit machine, with 1Mb
36    megablocks, using 8 bits for each entry in the table, the table
37    requires 4k.  Lookups during GC will be fast, because the table
38    will be quickly cached (indeed, performance measurements showed no
39    measurable difference between doing the table lookup and using a
40    constant comparison).
41
42    On 64-bit machines, we cache one 12-bit block map that describes
43    4096 megablocks or 4GB of memory. If HEAP_ALLOCED is called for
44    an address that is not in the cache, it calls slowIsHeapAlloced
45    (see MBlock.c) which will find the block map for the 4GB block in
46    question.
47    -------------------------------------------------------------------------- */
48
49 #if SIZEOF_VOID_P == 4
50 extern StgWord8 mblock_map[];
51
52 /* On a 32-bit machine a 4KB table is always sufficient */
53 # define MBLOCK_MAP_SIZE        4096
54 # define MBLOCK_MAP_ENTRY(p)    ((StgWord)(p) >> MBLOCK_SHIFT)
55 # define HEAP_ALLOCED(p)        mblock_map[MBLOCK_MAP_ENTRY(p)]
56
57 #elif SIZEOF_VOID_P == 8
58
59 # define MBLOCK_MAP_SIZE        4096
60 # define MBLOCK_MAP_ENTRY(p)    (((StgWord)(p) & 0xffffffff) >> MBLOCK_SHIFT)
61
62 typedef struct {
63     StgWord32   addrHigh32;
64     StgWord8    mblocks[MBLOCK_MAP_SIZE];
65 } MBlockMap;
66
67 extern MBlockMap *mblock_cache;
68
69 StgBool slowIsHeapAlloced(void *p);
70
71 # define HEAP_ALLOCED(p)                                        \
72         ( ((((StgWord)(p)) >> 32) == mblock_cache->addrHigh32)  \
73         ? mblock_cache->mblocks[MBLOCK_MAP_ENTRY(p)]            \
74         : slowIsHeapAlloced(p) )
75
76 #else
77 # error HEAP_ALLOCED not defined
78 #endif
79
80 #endif /* MBLOCK_H */