[project @ 2005-02-10 13:01:52 by simonmar]
[ghc-hetmet.git] / ghc / includes / Block.h
1 /* -----------------------------------------------------------------------------
2  * $Id: Block.h,v 1.18 2005/02/10 13:02:00 simonmar Exp $
3  *
4  * (c) The GHC Team, 1998-1999
5  *
6  * Block structure for the storage manager
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #ifndef BLOCK_H
11 #define BLOCK_H
12
13 /* The actual block and megablock-size constants are defined in
14  * includes/Constants.h, all constants here are derived from these.
15  */
16
17 /* Block related constants (BLOCK_SHIFT is defined in Constants.h) */
18
19 #define BLOCK_SIZE   (1<<BLOCK_SHIFT)
20 #define BLOCK_SIZE_W (BLOCK_SIZE/sizeof(W_))
21 #define BLOCK_MASK   (BLOCK_SIZE-1)
22
23 #define BLOCK_ROUND_UP(p)   ((void *) (((W_)(p)+BLOCK_SIZE-1) & ~BLOCK_MASK))
24 #define BLOCK_ROUND_DOWN(p) ((void *) ((W_)(p) & ~BLOCK_MASK))
25
26 /* Megablock related constants (MBLOCK_SHIFT is defined in Constants.h) */
27
28 #define MBLOCK_SIZE    (1<<MBLOCK_SHIFT)
29 #define MBLOCK_SIZE_W  (MBLOCK_SIZE/sizeof(W_))
30 #define MBLOCK_MASK    (MBLOCK_SIZE-1)
31
32 #define MBLOCK_ROUND_UP(p)   ((void *)(((W_)(p)+MBLOCK_SIZE-1) & ~MBLOCK_MASK))
33 #define MBLOCK_ROUND_DOWN(p) ((void *)((W_)(p) & ~MBLOCK_MASK ))
34
35 /* The largest size an object can be before we give it a block of its
36  * own and treat it as an immovable object during GC, expressed as a
37  * fraction of BLOCK_SIZE.
38  */
39 #define LARGE_OBJECT_THRESHOLD ((nat)(BLOCK_SIZE * 8 / 10))
40
41 /* -----------------------------------------------------------------------------
42  * Block descriptor.  This structure *must* be the right length, so we
43  * can do pointer arithmetic on pointers to it.
44  */
45
46 /* The block descriptor is 64 bytes on a 64-bit machine, and 32-bytes
47  * on a 32-bit machine.
48  */
49
50 #ifndef CMINUSMINUS
51 typedef struct bdescr_ {
52   StgPtr start;                 /* start addr of memory */
53   StgPtr free;                  /* first free byte of memory */
54   struct bdescr_ *link;         /* used for chaining blocks together */
55   union { 
56       struct bdescr_ *back;     /* used (occasionally) for doubly-linked lists*/
57       StgWord *bitmap;
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
90 /* Finding the block descriptor for a given block -------------------------- */
91
92 #ifdef CMINUSMINUS
93
94 #define Bdescr(p) \
95     ((((p) &  MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT)) \
96      | ((p) & ~MBLOCK_MASK))
97
98 #else
99
100 INLINE_HEADER bdescr *Bdescr(StgPtr p)
101 {
102   return (bdescr *)
103     ((((W_)p &  MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT)) 
104      | ((W_)p & ~MBLOCK_MASK)
105      );
106 }
107
108 #endif
109
110 /* Useful Macros ------------------------------------------------------------ */
111
112 /* Offset of first real data block in a megablock */
113
114 #define FIRST_BLOCK_OFF \
115    ((W_)BLOCK_ROUND_UP(BDESCR_SIZE * (MBLOCK_SIZE / BLOCK_SIZE)))
116
117 /* First data block in a given megablock */
118
119 #define FIRST_BLOCK(m) ((void *)(FIRST_BLOCK_OFF + (W_)(m)))
120    
121 /* Last data block in a given megablock */
122
123 #define LAST_BLOCK(m)  ((void *)(MBLOCK_SIZE-BLOCK_SIZE + (W_)(m)))
124
125 /* First real block descriptor in a megablock */
126
127 #define FIRST_BDESCR(m) \
128    ((bdescr *)((FIRST_BLOCK_OFF>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
129
130 /* Number of usable blocks in a megablock */
131
132 #define BLOCKS_PER_MBLOCK ((MBLOCK_SIZE - FIRST_BLOCK_OFF) / BLOCK_SIZE)
133
134 /* How many blocks in this megablock group */
135
136 #define MBLOCK_GROUP_BLOCKS(n) \
137    (BLOCKS_PER_MBLOCK + (n-1) * (MBLOCK_SIZE / BLOCK_SIZE))
138
139 /* Compute the required size of a megablock group */
140
141 #define BLOCKS_TO_MBLOCKS(n) \
142    (1 + (W_)MBLOCK_ROUND_UP((n-BLOCKS_PER_MBLOCK) * BLOCK_SIZE) / MBLOCK_SIZE)
143
144
145 #ifndef CMINUSMINUS 
146 // to the end...
147
148 /* Double-linked block lists: --------------------------------------------- */
149
150 INLINE_HEADER void
151 dbl_link_onto(bdescr *bd, bdescr **list)
152 {
153   bd->link = *list;
154   bd->u.back = NULL;
155   if (*list) {
156     (*list)->u.back = bd; /* double-link the list */
157   }
158   *list = bd;
159 }
160
161 /* Initialisation ---------------------------------------------------------- */
162
163 extern void initBlockAllocator(void);
164
165 /* Allocation -------------------------------------------------------------- */
166
167 extern bdescr *allocGroup(nat n);
168 extern bdescr *allocBlock(void);
169
170 /* De-Allocation ----------------------------------------------------------- */
171
172 extern void freeGroup(bdescr *p);
173 extern void freeChain(bdescr *p);
174
175 /* Round a value to megablocks --------------------------------------------- */
176
177 #define WORDS_PER_MBLOCK  (BLOCKS_PER_MBLOCK * BLOCK_SIZE_W)
178
179 INLINE_HEADER nat
180 round_to_mblocks(nat words)
181 {
182   if (words > WORDS_PER_MBLOCK) {
183     if ((words % WORDS_PER_MBLOCK) < (WORDS_PER_MBLOCK / 2)) {
184       words = (words / WORDS_PER_MBLOCK) * WORDS_PER_MBLOCK;
185     } else {
186       words = ((words / WORDS_PER_MBLOCK) + 1) * WORDS_PER_MBLOCK;
187     }
188   }
189   return words;
190 }
191
192 #endif /* !CMINUSMINUS */
193 #endif /* BLOCK_H */