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