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