ec894da02e9655fddc6cce3dde2c7877e994e87b
[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 to be marked, not copied */
88 #define BF_MARKED    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 /* Block contains only a small amount of live data */
94 #define BF_FRAGMENTED 64
95 /* we know about this block (for finding leaks) */
96 #define BF_KNOWN     128
97
98 /* Finding the block descriptor for a given block -------------------------- */
99
100 #ifdef CMINUSMINUS
101
102 #define Bdescr(p) \
103     ((((p) &  MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT)) \
104      | ((p) & ~MBLOCK_MASK))
105
106 #else
107
108 INLINE_HEADER bdescr *Bdescr(StgPtr p)
109 {
110   return (bdescr *)
111     ((((W_)p &  MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT)) 
112      | ((W_)p & ~MBLOCK_MASK)
113      );
114 }
115
116 #endif
117
118 /* Useful Macros ------------------------------------------------------------ */
119
120 /* Offset of first real data block in a megablock */
121
122 #define FIRST_BLOCK_OFF \
123    ((W_)BLOCK_ROUND_UP(BDESCR_SIZE * (MBLOCK_SIZE / BLOCK_SIZE)))
124
125 /* First data block in a given megablock */
126
127 #define FIRST_BLOCK(m) ((void *)(FIRST_BLOCK_OFF + (W_)(m)))
128    
129 /* Last data block in a given megablock */
130
131 #define LAST_BLOCK(m)  ((void *)(MBLOCK_SIZE-BLOCK_SIZE + (W_)(m)))
132
133 /* First real block descriptor in a megablock */
134
135 #define FIRST_BDESCR(m) \
136    ((bdescr *)((FIRST_BLOCK_OFF>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
137
138 /* Last real block descriptor in a megablock */
139
140 #define LAST_BDESCR(m) \
141   ((bdescr *)(((MBLOCK_SIZE-BLOCK_SIZE)>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
142
143 /* Number of usable blocks in a megablock */
144
145 #define BLOCKS_PER_MBLOCK ((MBLOCK_SIZE - FIRST_BLOCK_OFF) / BLOCK_SIZE)
146
147 /* How many blocks in this megablock group */
148
149 #define MBLOCK_GROUP_BLOCKS(n) \
150    (BLOCKS_PER_MBLOCK + (n-1) * (MBLOCK_SIZE / BLOCK_SIZE))
151
152 /* Compute the required size of a megablock group */
153
154 #define BLOCKS_TO_MBLOCKS(n) \
155    (1 + (W_)MBLOCK_ROUND_UP((n-BLOCKS_PER_MBLOCK) * BLOCK_SIZE) / MBLOCK_SIZE)
156
157
158 #ifndef CMINUSMINUS 
159 /* to the end... */
160
161 /* Double-linked block lists: --------------------------------------------- */
162
163 INLINE_HEADER void
164 dbl_link_onto(bdescr *bd, bdescr **list)
165 {
166   bd->link = *list;
167   bd->u.back = NULL;
168   if (*list) {
169     (*list)->u.back = bd; /* double-link the list */
170   }
171   *list = bd;
172 }
173
174 INLINE_HEADER void
175 dbl_link_remove(bdescr *bd, bdescr **list)
176 {
177     if (bd->u.back) {
178         bd->u.back->link = bd->link;
179     } else {
180         *list = bd->link;
181     }
182     if (bd->link) {
183         bd->link->u.back = bd->u.back;
184     }
185 }
186
187 INLINE_HEADER void
188 dbl_link_insert_after(bdescr *bd, bdescr *after)
189 {
190     bd->link = after->link;
191     bd->u.back = after;
192     if (after->link) {
193         after->link->u.back = bd;
194     }
195     after->link = bd;
196 }
197
198 INLINE_HEADER void
199 dbl_link_replace(bdescr *new, bdescr *old, bdescr **list)
200 {
201     new->link = old->link;
202     new->u.back = old->u.back;
203     if (old->link) {
204         old->link->u.back = new;
205     }
206     if (old->u.back) {
207         old->u.back->link = new;
208     } else {
209         *list = new;
210     }
211 }
212
213 /* Initialisation ---------------------------------------------------------- */
214
215 extern void initBlockAllocator(void);
216
217 /* Allocation -------------------------------------------------------------- */
218
219 bdescr *allocGroup(nat n);
220 bdescr *allocBlock(void);
221
222 // versions that take the storage manager lock for you:
223 bdescr *allocGroup_lock(nat n);
224 bdescr *allocBlock_lock(void);
225
226 /* De-Allocation ----------------------------------------------------------- */
227
228 void freeGroup(bdescr *p);
229 void freeChain(bdescr *p);
230
231 // versions that take the storage manager lock for you:
232 void freeGroup_lock(bdescr *p);
233 void freeChain_lock(bdescr *p);
234
235 bdescr * splitBlockGroup (bdescr *bd, nat blocks);
236
237 /* Round a value to megablocks --------------------------------------------- */
238
239 // We want to allocate an object around a given size, round it up or
240 // down to the nearest size that will fit in an mblock group.
241 INLINE_HEADER StgWord
242 round_to_mblocks(StgWord words)
243 {
244     if (words > BLOCKS_PER_MBLOCK * BLOCK_SIZE_W) {
245         // first, ignore the gap at the beginning of the first mblock by
246         // adding it to the total words.  Then we can pretend we're
247         // dealing in a uniform unit of megablocks.
248         words += FIRST_BLOCK_OFF/sizeof(W_);
249
250         if ((words % MBLOCK_SIZE_W) < (MBLOCK_SIZE_W / 2)) {
251             words = (words / MBLOCK_SIZE_W) * MBLOCK_SIZE_W;
252         } else {
253             words = ((words / MBLOCK_SIZE_W) + 1) * MBLOCK_SIZE_W;
254         }
255
256         words -= FIRST_BLOCK_OFF/sizeof(W_);
257     }
258     return words;
259 }
260
261 INLINE_HEADER StgWord
262 round_up_to_mblocks(StgWord words)
263 {
264     words += FIRST_BLOCK_OFF/sizeof(W_);
265     words = ((words / MBLOCK_SIZE_W) + 1) * MBLOCK_SIZE_W;
266     words -= FIRST_BLOCK_OFF/sizeof(W_);
267     return words;
268 }
269
270 #endif /* !CMINUSMINUS */
271 #endif /* BLOCK_H */