e2e691ab0e17db80981f5fc923b3b5a47eab9756
[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       StgPtr  scan;             /* scan pointer for copying GC */
58   } u;
59   unsigned int gen_no;          /* generation */
60   struct step_ *step;           /* step */
61   StgWord32 blocks;             /* no. of blocks (if grp head, 0 otherwise) */
62   StgWord32 flags;              /* block is in to-space */
63 #if SIZEOF_VOID_P == 8
64   StgWord32 _padding[2];
65 #else
66   StgWord32 _padding[0];
67 #endif
68 } bdescr;
69 #endif
70
71 #if SIZEOF_VOID_P == 8
72 #define BDESCR_SIZE  0x40
73 #define BDESCR_MASK  0x3f
74 #define BDESCR_SHIFT 6
75 #else
76 #define BDESCR_SIZE  0x20
77 #define BDESCR_MASK  0x1f
78 #define BDESCR_SHIFT 5
79 #endif
80
81 /* Block contains objects evacuated during this GC */
82 #define BF_EVACUATED 1
83 /* Block is a large object */
84 #define BF_LARGE     2
85 /* Block is pinned */
86 #define BF_PINNED    4
87 /* Block is part of a compacted generation */
88 #define BF_COMPACTED 8
89 /* Block is free, and on the free list  (TODO: is this used?) */
90 #define BF_FREE      16
91 /* Block is executable */
92 #define BF_EXEC      32
93
94 /* Finding the block descriptor for a given block -------------------------- */
95
96 #ifdef CMINUSMINUS
97
98 #define Bdescr(p) \
99     ((((p) &  MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT)) \
100      | ((p) & ~MBLOCK_MASK))
101
102 #else
103
104 INLINE_HEADER bdescr *Bdescr(StgPtr p)
105 {
106   return (bdescr *)
107     ((((W_)p &  MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT)) 
108      | ((W_)p & ~MBLOCK_MASK)
109      );
110 }
111
112 #endif
113
114 /* Useful Macros ------------------------------------------------------------ */
115
116 /* Offset of first real data block in a megablock */
117
118 #define FIRST_BLOCK_OFF \
119    ((W_)BLOCK_ROUND_UP(BDESCR_SIZE * (MBLOCK_SIZE / BLOCK_SIZE)))
120
121 /* First data block in a given megablock */
122
123 #define FIRST_BLOCK(m) ((void *)(FIRST_BLOCK_OFF + (W_)(m)))
124    
125 /* Last data block in a given megablock */
126
127 #define LAST_BLOCK(m)  ((void *)(MBLOCK_SIZE-BLOCK_SIZE + (W_)(m)))
128
129 /* First real block descriptor in a megablock */
130
131 #define FIRST_BDESCR(m) \
132    ((bdescr *)((FIRST_BLOCK_OFF>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
133
134 /* Last real block descriptor in a megablock */
135
136 #define LAST_BDESCR(m) \
137   ((bdescr *)(((MBLOCK_SIZE-BLOCK_SIZE)>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
138
139 /* Number of usable blocks in a megablock */
140
141 #define BLOCKS_PER_MBLOCK ((MBLOCK_SIZE - FIRST_BLOCK_OFF) / BLOCK_SIZE)
142
143 /* How many blocks in this megablock group */
144
145 #define MBLOCK_GROUP_BLOCKS(n) \
146    (BLOCKS_PER_MBLOCK + (n-1) * (MBLOCK_SIZE / BLOCK_SIZE))
147
148 /* Compute the required size of a megablock group */
149
150 #define BLOCKS_TO_MBLOCKS(n) \
151    (1 + (W_)MBLOCK_ROUND_UP((n-BLOCKS_PER_MBLOCK) * BLOCK_SIZE) / MBLOCK_SIZE)
152
153
154 #ifndef CMINUSMINUS 
155 /* to the end... */
156
157 /* Double-linked block lists: --------------------------------------------- */
158
159 INLINE_HEADER void
160 dbl_link_onto(bdescr *bd, bdescr **list)
161 {
162   bd->link = *list;
163   bd->u.back = NULL;
164   if (*list) {
165     (*list)->u.back = bd; /* double-link the list */
166   }
167   *list = bd;
168 }
169
170 INLINE_HEADER void
171 dbl_link_remove(bdescr *bd, bdescr **list)
172 {
173     if (bd->u.back) {
174         bd->u.back->link = bd->link;
175     } else {
176         *list = bd->link;
177     }
178     if (bd->link) {
179         bd->link->u.back = bd->u.back;
180     }
181 }
182
183 INLINE_HEADER void
184 dbl_link_insert_after(bdescr *bd, bdescr *after)
185 {
186     bd->link = after->link;
187     bd->u.back = after;
188     if (after->link) {
189         after->link->u.back = bd;
190     }
191     after->link = bd;
192 }
193
194 INLINE_HEADER void
195 dbl_link_replace(bdescr *new, bdescr *old, bdescr **list)
196 {
197     new->link = old->link;
198     new->u.back = old->u.back;
199     if (old->link) {
200         old->link->u.back = new;
201     }
202     if (old->u.back) {
203         old->u.back->link = new;
204     } else {
205         *list = new;
206     }
207 }
208
209 /* Initialisation ---------------------------------------------------------- */
210
211 extern void initBlockAllocator(void);
212
213 /* Allocation -------------------------------------------------------------- */
214
215 bdescr *allocGroup(nat n);
216 bdescr *allocBlock(void);
217
218 // versions that take the storage manager lock for you:
219 bdescr *allocGroup_lock(nat n);
220 bdescr *allocBlock_lock(void);
221
222 /* De-Allocation ----------------------------------------------------------- */
223
224 void freeGroup(bdescr *p);
225 void freeChain(bdescr *p);
226
227 // versions that take the storage manager lock for you:
228 void freeGroup_lock(bdescr *p);
229 void freeChain_lock(bdescr *p);
230
231 bdescr * splitBlockGroup (bdescr *bd, nat blocks);
232
233 /* Round a value to megablocks --------------------------------------------- */
234
235 // We want to allocate an object around a given size, round it up or
236 // down to the nearest size that will fit in an mblock group.
237 INLINE_HEADER StgWord
238 round_to_mblocks(StgWord words)
239 {
240     if (words > BLOCKS_PER_MBLOCK * BLOCK_SIZE_W) {
241         // first, ignore the gap at the beginning of the first mblock by
242         // adding it to the total words.  Then we can pretend we're
243         // dealing in a uniform unit of megablocks.
244         words += FIRST_BLOCK_OFF/sizeof(W_);
245
246         if ((words % MBLOCK_SIZE_W) < (MBLOCK_SIZE_W / 2)) {
247             words = (words / MBLOCK_SIZE_W) * MBLOCK_SIZE_W;
248         } else {
249             words = ((words / MBLOCK_SIZE_W) + 1) * MBLOCK_SIZE_W;
250         }
251
252         words -= FIRST_BLOCK_OFF/sizeof(W_);
253     }
254     return words;
255 }
256
257 #endif /* !CMINUSMINUS */
258 #endif /* BLOCK_H */