move the "meat" into the wiki, this file just contains pointers now
[ghc.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 /* Number of usable blocks in a megablock */
134
135 #define BLOCKS_PER_MBLOCK ((MBLOCK_SIZE - FIRST_BLOCK_OFF) / BLOCK_SIZE)
136
137 /* How many blocks in this megablock group */
138
139 #define MBLOCK_GROUP_BLOCKS(n) \
140 (BLOCKS_PER_MBLOCK + (n-1) * (MBLOCK_SIZE / BLOCK_SIZE))
141
142 /* Compute the required size of a megablock group */
143
144 #define BLOCKS_TO_MBLOCKS(n) \
145 (1 + (W_)MBLOCK_ROUND_UP((n-BLOCKS_PER_MBLOCK) * BLOCK_SIZE) / MBLOCK_SIZE)
146
147
148 #ifndef CMINUSMINUS
149 /* to the end... */
150
151 /* Double-linked block lists: --------------------------------------------- */
152
153 INLINE_HEADER void
154 dbl_link_onto(bdescr *bd, bdescr **list)
155 {
156 bd->link = *list;
157 bd->u.back = NULL;
158 if (*list) {
159 (*list)->u.back = bd; /* double-link the list */
160 }
161 *list = bd;
162 }
163
164 /* Initialisation ---------------------------------------------------------- */
165
166 extern void initBlockAllocator(void);
167
168 /* Allocation -------------------------------------------------------------- */
169
170 bdescr *allocGroup(nat n);
171 bdescr *allocBlock(void);
172
173 // versions that take the storage manager lock for you:
174 bdescr *allocGroup_lock(nat n);
175 bdescr *allocBlock_lock(void);
176
177 /* De-Allocation ----------------------------------------------------------- */
178
179 void freeGroup(bdescr *p);
180 void freeChain(bdescr *p);
181
182 // versions that take the storage manager lock for you:
183 void freeGroup_lock(bdescr *p);
184 void freeChain_lock(bdescr *p);
185
186 /* Round a value to megablocks --------------------------------------------- */
187
188 #define WORDS_PER_MBLOCK (BLOCKS_PER_MBLOCK * BLOCK_SIZE_W)
189
190 INLINE_HEADER nat
191 round_to_mblocks(nat words)
192 {
193 if (words > WORDS_PER_MBLOCK) {
194 if ((words % WORDS_PER_MBLOCK) < (WORDS_PER_MBLOCK / 2)) {
195 words = (words / WORDS_PER_MBLOCK) * WORDS_PER_MBLOCK;
196 } else {
197 words = ((words / WORDS_PER_MBLOCK) + 1) * WORDS_PER_MBLOCK;
198 }
199 }
200 return words;
201 }
202
203 #endif /* !CMINUSMINUS */
204 #endif /* BLOCK_H */