allow build settings to be overriden by adding mk/validate.mk
[ghc-hetmet.git] / includes / Block.h
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-1999
4  *
5  * Block structure for the storage manager
6  *
7  * ---------------------------------------------------------------------------*/
8
9 #ifndef BLOCK_H
10 #define BLOCK_H
11
12 /* The actual block and megablock-size constants are defined in
13  * includes/Constants.h, all constants here are derived from these.
14  */
15
16 /* Block related constants (BLOCK_SHIFT is defined in Constants.h) */
17
18 #define BLOCK_SIZE   (1<<BLOCK_SHIFT)
19 #define BLOCK_SIZE_W (BLOCK_SIZE/sizeof(W_))
20 #define BLOCK_MASK   (BLOCK_SIZE-1)
21
22 #define BLOCK_ROUND_UP(p)   ((void *) (((W_)(p)+BLOCK_SIZE-1) & ~BLOCK_MASK))
23 #define BLOCK_ROUND_DOWN(p) ((void *) ((W_)(p) & ~BLOCK_MASK))
24
25 /* Megablock related constants (MBLOCK_SHIFT is defined in Constants.h) */
26
27 #define MBLOCK_SIZE    (1<<MBLOCK_SHIFT)
28 #define MBLOCK_SIZE_W  (MBLOCK_SIZE/sizeof(W_))
29 #define MBLOCK_MASK    (MBLOCK_SIZE-1)
30
31 #define MBLOCK_ROUND_UP(p)   ((void *)(((W_)(p)+MBLOCK_SIZE-1) & ~MBLOCK_MASK))
32 #define MBLOCK_ROUND_DOWN(p) ((void *)((W_)(p) & ~MBLOCK_MASK ))
33
34 /* The largest size an object can be before we give it a block of its
35  * own and treat it as an immovable object during GC, expressed as a
36  * fraction of BLOCK_SIZE.
37  */
38 #define LARGE_OBJECT_THRESHOLD ((nat)(BLOCK_SIZE * 8 / 10))
39
40 /* -----------------------------------------------------------------------------
41  * Block descriptor.  This structure *must* be the right length, so we
42  * can do pointer arithmetic on pointers to it.
43  */
44
45 /* The block descriptor is 64 bytes on a 64-bit machine, and 32-bytes
46  * on a 32-bit machine.
47  */
48
49 #ifndef CMINUSMINUS
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 #endif
69
70 #if SIZEOF_VOID_P == 8
71 #define BDESCR_SIZE  0x40
72 #define BDESCR_MASK  0x3f
73 #define BDESCR_SHIFT 6
74 #else
75 #define BDESCR_SIZE  0x20
76 #define BDESCR_MASK  0x1f
77 #define BDESCR_SHIFT 5
78 #endif
79
80 /* Block contains objects evacuated during this GC */
81 #define BF_EVACUATED 1
82 /* Block is a large object */
83 #define BF_LARGE     2
84 /* Block is pinned */
85 #define BF_PINNED    4
86 /* Block is part of a compacted generation */
87 #define BF_COMPACTED 8
88 /* Block is free, and on the free list  (TODO: is this used?) */
89 #define BF_FREE      16
90 /* Block is executable */
91 #define BF_EXEC      32
92
93 /* Finding the block descriptor for a given block -------------------------- */
94
95 #ifdef CMINUSMINUS
96
97 #define Bdescr(p) \
98     ((((p) &  MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT)) \
99      | ((p) & ~MBLOCK_MASK))
100
101 #else
102
103 INLINE_HEADER bdescr *Bdescr(StgPtr p)
104 {
105   return (bdescr *)
106     ((((W_)p &  MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT)) 
107      | ((W_)p & ~MBLOCK_MASK)
108      );
109 }
110
111 #endif
112
113 /* Useful Macros ------------------------------------------------------------ */
114
115 /* Offset of first real data block in a megablock */
116
117 #define FIRST_BLOCK_OFF \
118    ((W_)BLOCK_ROUND_UP(BDESCR_SIZE * (MBLOCK_SIZE / BLOCK_SIZE)))
119
120 /* First data block in a given megablock */
121
122 #define FIRST_BLOCK(m) ((void *)(FIRST_BLOCK_OFF + (W_)(m)))
123    
124 /* Last data block in a given megablock */
125
126 #define LAST_BLOCK(m)  ((void *)(MBLOCK_SIZE-BLOCK_SIZE + (W_)(m)))
127
128 /* First real block descriptor in a megablock */
129
130 #define FIRST_BDESCR(m) \
131    ((bdescr *)((FIRST_BLOCK_OFF>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
132
133 /* Last real block descriptor in a megablock */
134
135 #define LAST_BDESCR(m) \
136   ((bdescr *)(((MBLOCK_SIZE-BLOCK_SIZE)>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
137
138 /* Number of usable blocks in a megablock */
139
140 #define BLOCKS_PER_MBLOCK ((MBLOCK_SIZE - FIRST_BLOCK_OFF) / BLOCK_SIZE)
141
142 /* How many blocks in this megablock group */
143
144 #define MBLOCK_GROUP_BLOCKS(n) \
145    (BLOCKS_PER_MBLOCK + (n-1) * (MBLOCK_SIZE / BLOCK_SIZE))
146
147 /* Compute the required size of a megablock group */
148
149 #define BLOCKS_TO_MBLOCKS(n) \
150    (1 + (W_)MBLOCK_ROUND_UP((n-BLOCKS_PER_MBLOCK) * BLOCK_SIZE) / MBLOCK_SIZE)
151
152
153 #ifndef CMINUSMINUS 
154 /* to the end... */
155
156 /* Double-linked block lists: --------------------------------------------- */
157
158 INLINE_HEADER void
159 dbl_link_onto(bdescr *bd, bdescr **list)
160 {
161   bd->link = *list;
162   bd->u.back = NULL;
163   if (*list) {
164     (*list)->u.back = bd; /* double-link the list */
165   }
166   *list = bd;
167 }
168
169 INLINE_HEADER void
170 dbl_link_remove(bdescr *bd, bdescr **list)
171 {
172     if (bd->u.back) {
173         bd->u.back->link = bd->link;
174     } else {
175         *list = bd->link;
176     }
177     if (bd->link) {
178         bd->link->u.back = bd->u.back;
179     }
180 }
181
182 INLINE_HEADER void
183 dbl_link_insert_after(bdescr *bd, bdescr *after)
184 {
185     bd->link = after->link;
186     bd->u.back = after;
187     if (after->link) {
188         after->link->u.back = bd;
189     }
190     after->link = bd;
191 }
192
193 INLINE_HEADER void
194 dbl_link_replace(bdescr *new, bdescr *old, bdescr **list)
195 {
196     new->link = old->link;
197     new->u.back = old->u.back;
198     if (old->link) {
199         old->link->u.back = new;
200     }
201     if (old->u.back) {
202         old->u.back->link = new;
203     } else {
204         *list = new;
205     }
206 }
207
208 /* Initialisation ---------------------------------------------------------- */
209
210 extern void initBlockAllocator(void);
211
212 /* Allocation -------------------------------------------------------------- */
213
214 bdescr *allocGroup(nat n);
215 bdescr *allocBlock(void);
216
217 // versions that take the storage manager lock for you:
218 bdescr *allocGroup_lock(nat n);
219 bdescr *allocBlock_lock(void);
220
221 /* De-Allocation ----------------------------------------------------------- */
222
223 void freeGroup(bdescr *p);
224 void freeChain(bdescr *p);
225
226 // versions that take the storage manager lock for you:
227 void freeGroup_lock(bdescr *p);
228 void freeChain_lock(bdescr *p);
229
230 /* Round a value to megablocks --------------------------------------------- */
231
232 #define WORDS_PER_MBLOCK  (BLOCKS_PER_MBLOCK * BLOCK_SIZE_W)
233
234 INLINE_HEADER nat
235 round_to_mblocks(nat words)
236 {
237   if (words > WORDS_PER_MBLOCK) {
238     if ((words % WORDS_PER_MBLOCK) < (WORDS_PER_MBLOCK / 2)) {
239       words = (words / WORDS_PER_MBLOCK) * WORDS_PER_MBLOCK;
240     } else {
241       words = ((words / WORDS_PER_MBLOCK) + 1) * WORDS_PER_MBLOCK;
242     }
243   }
244   return words;
245 }
246
247 #endif /* !CMINUSMINUS */
248 #endif /* BLOCK_H */