[project @ 2005-03-27 13:41:13 by panne]
[ghc-hetmet.git] / ghc / 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
89 /* Finding the block descriptor for a given block -------------------------- */
90
91 #ifdef CMINUSMINUS
92
93 #define Bdescr(p) \
94     ((((p) &  MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT)) \
95      | ((p) & ~MBLOCK_MASK))
96
97 #else
98
99 INLINE_HEADER bdescr *Bdescr(StgPtr p)
100 {
101   return (bdescr *)
102     ((((W_)p &  MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT)) 
103      | ((W_)p & ~MBLOCK_MASK)
104      );
105 }
106
107 #endif
108
109 /* Useful Macros ------------------------------------------------------------ */
110
111 /* Offset of first real data block in a megablock */
112
113 #define FIRST_BLOCK_OFF \
114    ((W_)BLOCK_ROUND_UP(BDESCR_SIZE * (MBLOCK_SIZE / BLOCK_SIZE)))
115
116 /* First data block in a given megablock */
117
118 #define FIRST_BLOCK(m) ((void *)(FIRST_BLOCK_OFF + (W_)(m)))
119    
120 /* Last data block in a given megablock */
121
122 #define LAST_BLOCK(m)  ((void *)(MBLOCK_SIZE-BLOCK_SIZE + (W_)(m)))
123
124 /* First real block descriptor in a megablock */
125
126 #define FIRST_BDESCR(m) \
127    ((bdescr *)((FIRST_BLOCK_OFF>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
128
129 /* Number of usable blocks in a megablock */
130
131 #define BLOCKS_PER_MBLOCK ((MBLOCK_SIZE - FIRST_BLOCK_OFF) / BLOCK_SIZE)
132
133 /* How many blocks in this megablock group */
134
135 #define MBLOCK_GROUP_BLOCKS(n) \
136    (BLOCKS_PER_MBLOCK + (n-1) * (MBLOCK_SIZE / BLOCK_SIZE))
137
138 /* Compute the required size of a megablock group */
139
140 #define BLOCKS_TO_MBLOCKS(n) \
141    (1 + (W_)MBLOCK_ROUND_UP((n-BLOCKS_PER_MBLOCK) * BLOCK_SIZE) / MBLOCK_SIZE)
142
143
144 #ifndef CMINUSMINUS 
145 /* to the end... */
146
147 /* Double-linked block lists: --------------------------------------------- */
148
149 INLINE_HEADER void
150 dbl_link_onto(bdescr *bd, bdescr **list)
151 {
152   bd->link = *list;
153   bd->u.back = NULL;
154   if (*list) {
155     (*list)->u.back = bd; /* double-link the list */
156   }
157   *list = bd;
158 }
159
160 /* Initialisation ---------------------------------------------------------- */
161
162 extern void initBlockAllocator(void);
163
164 /* Allocation -------------------------------------------------------------- */
165
166 extern bdescr *allocGroup(nat n);
167 extern bdescr *allocBlock(void);
168
169 /* De-Allocation ----------------------------------------------------------- */
170
171 extern void freeGroup(bdescr *p);
172 extern void freeChain(bdescr *p);
173
174 /* Round a value to megablocks --------------------------------------------- */
175
176 #define WORDS_PER_MBLOCK  (BLOCKS_PER_MBLOCK * BLOCK_SIZE_W)
177
178 INLINE_HEADER nat
179 round_to_mblocks(nat words)
180 {
181   if (words > WORDS_PER_MBLOCK) {
182     if ((words % WORDS_PER_MBLOCK) < (WORDS_PER_MBLOCK / 2)) {
183       words = (words / WORDS_PER_MBLOCK) * WORDS_PER_MBLOCK;
184     } else {
185       words = ((words / WORDS_PER_MBLOCK) + 1) * WORDS_PER_MBLOCK;
186     }
187   }
188   return words;
189 }
190
191 #endif /* !CMINUSMINUS */
192 #endif /* BLOCK_H */