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