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