Reorganisation of the source tree
[ghc-hetmet.git] / rts / 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 * getMBlock(void);
15 extern void * getMBlocks(nat n);
16 extern void freeAllMBlocks(void);
17
18 #if osf3_HOST_OS
19 /* ToDo: Perhaps by adjusting this value we can make linking without
20  * -static work (i.e., not generate a core-dumping executable)? */
21 #if SIZEOF_VOID_P == 8
22 #define HEAP_BASE 0x180000000L
23 #else
24 #error I have no idea where to begin the heap on a non-64-bit osf3 machine.
25 #endif
26
27 #else
28
29 // we're using the generic method
30 #define HEAP_BASE 0
31
32 #endif
33
34 /* -----------------------------------------------------------------------------
35    The HEAP_ALLOCED() test.
36
37    HEAP_ALLOCED is called FOR EVERY SINGLE CLOSURE during GC.
38    It needs to be FAST.
39
40    Implementation of HEAP_ALLOCED
41    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42
43    Since heap is allocated in chunks of megablocks (MBLOCK_SIZE), we
44    can just use a table to record which megablocks in the address
45    space belong to the heap.  On a 32-bit machine, with 1Mb
46    megablocks, using 8 bits for each entry in the table, the table
47    requires 4k.  Lookups during GC will be fast, because the table
48    will be quickly cached (indeed, performance measurements showed no
49    measurable difference between doing the table lookup and using a
50    constant comparison).
51
52    On 64-bit machines, we cache one 12-bit block map that describes
53    4096 megablocks or 4GB of memory. If HEAP_ALLOCED is called for
54    an address that is not in the cache, it calls slowIsHeapAlloced
55    (see MBlock.c) which will find the block map for the 4GB block in
56    question.
57    -------------------------------------------------------------------------- */
58
59 #if SIZEOF_VOID_P == 4
60 extern StgWord8 mblock_map[];
61
62 /* On a 32-bit machine a 4KB table is always sufficient */
63 # define MBLOCK_MAP_SIZE        4096
64 # define MBLOCK_MAP_ENTRY(p)    ((StgWord)(p) >> MBLOCK_SHIFT)
65 # define HEAP_ALLOCED(p)        mblock_map[MBLOCK_MAP_ENTRY(p)]
66
67 #elif SIZEOF_VOID_P == 8
68
69 # define MBLOCK_MAP_SIZE        4096
70 # define MBLOCK_MAP_ENTRY(p)    (((StgWord)(p) & 0xffffffff) >> MBLOCK_SHIFT)
71
72 typedef struct {
73     StgWord32   addrHigh32;
74     StgWord8    mblocks[MBLOCK_MAP_SIZE];
75 } MBlockMap;
76
77 extern MBlockMap *mblock_cache;
78
79 StgBool slowIsHeapAlloced(void *p);
80
81 # define HEAP_ALLOCED(p)                                        \
82         ( ((((StgWord)(p)) >> 32) == mblock_cache->addrHigh32)  \
83         ? mblock_cache->mblocks[MBLOCK_MAP_ENTRY(p)]            \
84         : slowIsHeapAlloced(p) )
85
86 #else
87 # error HEAP_ALLOCED not defined
88 #endif
89
90 #endif /* MBLOCK_H */