[project @ 2004-02-12 02:04:59 by mthomas]
[ghc-hetmet.git] / ghc / includes / Block.h
1 /* -----------------------------------------------------------------------------
2  * $Id: Block.h,v 1.16 2003/11/26 12:14:26 simonmar Exp $
3  *
4  * (c) The GHC Team, 1998-1999
5  *
6  * Block structure for the storage manager
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #ifndef BLOCK_H
11 #define BLOCK_H
12
13 /* The actual block and megablock-size constants are defined in
14  * includes/Constants.h, all constants here are derived from these.
15  */
16
17 /* Block related constants (BLOCK_SHIFT is defined in Constants.h) */
18
19 #define BLOCK_SIZE   (1<<BLOCK_SHIFT)
20 #define BLOCK_SIZE_W (BLOCK_SIZE/sizeof(W_))
21 #define BLOCK_MASK   (BLOCK_SIZE-1)
22
23 #define BLOCK_ROUND_UP(p)   ((void *) (((W_)(p)+BLOCK_SIZE-1) & ~BLOCK_MASK))
24 #define BLOCK_ROUND_DOWN(p) ((void *) ((W_)(p) & ~BLOCK_MASK))
25
26 /* Megablock related constants (MBLOCK_SHIFT is defined in Constants.h) */
27
28 #define MBLOCK_SIZE    (1<<MBLOCK_SHIFT)
29 #define MBLOCK_SIZE_W  (MBLOCK_SIZE/sizeof(W_))
30 #define MBLOCK_MASK    (MBLOCK_SIZE-1)
31
32 #define MBLOCK_ROUND_UP(p)   ((void *)(((W_)(p)+MBLOCK_SIZE-1) & ~MBLOCK_MASK))
33 #define MBLOCK_ROUND_DOWN(p) ((void *)((W_)(p) & ~MBLOCK_MASK ))
34
35 /* The largest size an object can be before we give it a block of its
36  * own and treat it as an immovable object during GC, expressed as a
37  * fraction of BLOCK_SIZE.
38  */
39 #define LARGE_OBJECT_THRESHOLD ((nat)(BLOCK_SIZE * 8 / 10))
40
41 /* -----------------------------------------------------------------------------
42  * Block descriptor.  This structure *must* be the right length, so we
43  * can do pointer arithmetic on pointers to it.
44  */
45
46 /* The block descriptor is 64 bytes on a 64-bit machine, and 32-bytes
47  * on a 32-bit machine.
48  */
49
50 typedef struct _bdescr {
51   StgPtr start;                 /* start addr of memory */
52   StgPtr free;                  /* first free byte of memory */
53   struct _bdescr *link;         /* used for chaining blocks together */
54   union { 
55       struct _bdescr *back;     /* used (occasionally) for doubly-linked lists*/
56       StgWord *bitmap;
57   } u;
58   unsigned int gen_no;          /* generation */
59   struct _step *step;           /* step */
60   StgWord32 blocks;             /* no. of blocks (if grp head, 0 otherwise) */
61   StgWord32 flags;              /* block is in to-space */
62 #if SIZEOF_VOID_P == 8
63   StgWord32 _padding[2];
64 #else
65   StgWord32 _padding[0];
66 #endif
67 } bdescr;
68
69 #if SIZEOF_VOID_P == 8
70 #define BDESCR_SIZE  0x40
71 #define BDESCR_MASK  0x3f
72 #define BDESCR_SHIFT 6
73 #else
74 #define BDESCR_SIZE  0x20
75 #define BDESCR_MASK  0x1f
76 #define BDESCR_SHIFT 5
77 #endif
78
79 // Block contains objects evacuated during this GC
80 #define BF_EVACUATED 1
81 // Block is a large object
82 #define BF_LARGE     2
83 // Block is pinned
84 #define BF_PINNED    4
85 // Block is part of a compacted generation
86 #define BF_COMPACTED 8
87
88 /* Finding the block descriptor for a given block -------------------------- */
89
90 INLINE_HEADER bdescr *Bdescr(StgPtr p)
91 {
92   return (bdescr *)
93     ((((W_)p &  MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT)) 
94      | ((W_)p & ~MBLOCK_MASK)
95      );
96 }
97
98 /* Useful Macros ------------------------------------------------------------ */
99
100 /* Offset of first real data block in a megablock */
101
102 #define FIRST_BLOCK_OFF \
103    ((W_)BLOCK_ROUND_UP(BDESCR_SIZE * (MBLOCK_SIZE / BLOCK_SIZE)))
104
105 /* First data block in a given megablock */
106
107 #define FIRST_BLOCK(m) ((void *)(FIRST_BLOCK_OFF + (W_)(m)))
108    
109 /* Last data block in a given megablock */
110
111 #define LAST_BLOCK(m)  ((void *)(MBLOCK_SIZE-BLOCK_SIZE + (W_)(m)))
112
113 /* First real block descriptor in a megablock */
114
115 #define FIRST_BDESCR(m) \
116    ((bdescr *)((FIRST_BLOCK_OFF>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
117
118 /* Number of usable blocks in a megablock */
119
120 #define BLOCKS_PER_MBLOCK ((MBLOCK_SIZE - FIRST_BLOCK_OFF) / BLOCK_SIZE)
121
122 /* How many blocks in this megablock group */
123
124 #define MBLOCK_GROUP_BLOCKS(n) \
125    (BLOCKS_PER_MBLOCK + (n-1) * (MBLOCK_SIZE / BLOCK_SIZE))
126
127 /* Compute the required size of a megablock group */
128
129 #define BLOCKS_TO_MBLOCKS(n) \
130    (1 + (W_)MBLOCK_ROUND_UP((n-BLOCKS_PER_MBLOCK) * BLOCK_SIZE) / MBLOCK_SIZE)
131
132 #endif /* BLOCK_H */