b44f6ddd1d2d632b69a8708136a862a9b79fbcb7
[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
52 extern StgWord8 mblock_map[];
53
54 #if SIZEOF_VOID_P == 4
55 /* On a 32-bit machine a 4KB table is always sufficient */
56 # define MBLOCK_MAP_SIZE        4096
57 # define MBLOCK_MAP_ENTRY(p)    ((StgWord)(p) >> MBLOCK_SHIFT)
58 # define HEAP_ALLOCED(p)        mblock_map[MBLOCK_MAP_ENTRY(p)]
59 # define MARK_HEAP_ALLOCED(p)   (mblock_map[MBLOCK_MAP_ENTRY(p)] = 1)
60
61 #elif defined(ia64_TARGET_ARCH)
62 /* Instead of trying to cover the whole 64-bit address space (which would
63  * require a better data structure), we assume that mmap allocates mappings
64  * from the bottom of region 1, and track some portion of address space from
65  * there upwards (currently 4GB). */
66 # define MBLOCK_MAP_SIZE        4096
67 # define MBLOCK_MAP_ENTRY(p)    (((StgWord)(p) - (1UL << 61)) >> MBLOCK_SHIFT)
68 # define HEAP_ALLOCED(p)        ((MBLOCK_MAP_ENTRY(p) < MBLOCK_MAP_SIZE) \
69                                         && mblock_map[MBLOCK_MAP_ENTRY(p)])
70 # define MARK_HEAP_ALLOCED(p)   ((MBLOCK_MAP_ENTRY(p) < MBLOCK_MAP_SIZE) \
71                                         && (mblock_map[MBLOCK_MAP_ENTRY(p)] = 1))
72
73 #elif SIZEOF_VOID_P == 8
74 /* XXX: This is a HACK, and will not work in general!  We just use the
75  * lower 32 bits of the address, and do the same as for the 32-bit
76  * version.  As long as the OS gives us memory in a roughly linear
77  * fashion, it won't go wrong until we've allocated 4G.  */
78 # define MBLOCK_MAP_SIZE        4096
79 # define MBLOCK_MAP_ENTRY(p)    (((StgWord)(p) & 0xffffffff) >> MBLOCK_SHIFT)
80 # define HEAP_ALLOCED(p)        (mblock_map[MBLOCK_MAP_ENTRY(p)])
81 # define MARK_HEAP_ALLOCED(p)   (mblock_map[MBLOCK_MAP_ENTRY(p)] = 1)
82
83
84 #else
85 # error HEAP_ALLOCED not defined
86 #endif
87
88 #endif // __MBLOCK_H__