[project @ 2001-07-23 17:23:19 by simonmar]
[ghc-hetmet.git] / ghc / includes / Block.h
1 /* -----------------------------------------------------------------------------
2  * $Id: Block.h,v 1.9 2001/07/23 17:23:19 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 (4k blocks) */
18
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 (1M megablocks) */
26
27 #define MBLOCK_SIZE_W  (MBLOCK_SIZE/sizeof(W_))
28 #define MBLOCK_MASK    (MBLOCK_SIZE-1)
29
30 #define MBLOCK_ROUND_UP(p)   ((void *)(((W_)(p)+MBLOCK_SIZE-1) & ~MBLOCK_MASK))
31 #define MBLOCK_ROUND_DOWN(p) ((void *)((W_)(p) & ~MBLOCK_MASK ))
32
33
34 /* -----------------------------------------------------------------------------
35  * Block descriptor.  This structure *must* be the right length, so we
36  * can do pointer arithmetic on pointers to it.
37  */
38
39 /* The block descriptor is 64 bytes on a 64-bit machine, and 32-bytes
40  * on a 32-bit machine.
41  */
42
43 typedef struct _bdescr {
44   StgPtr start;                 /* start addr of memory */
45   StgPtr free;                  /* first free byte of memory */
46   struct _bdescr *link;         /* used for chaining blocks together */
47   union { 
48       struct _bdescr *back;     /* used (occasionally) for doubly-linked lists*/
49       StgWord *bitmap;
50   } u;
51   unsigned int gen_no;          /* generation */
52   struct _step *step;           /* step */
53   StgWord32 blocks;             /* no. of blocks (if grp head, 0 otherwise) */
54   StgWord32 flags;              /* block is in to-space */
55 #if SIZEOF_VOID_P == 8
56   StgWord32 _padding[2];
57 #else
58   StgWord32 _padding[0];
59 #endif
60 } bdescr;
61
62 #if SIZEOF_VOID_P == 8
63 #define BDESCR_SIZE  0x40
64 #define BDESCR_MASK  0x3f
65 #define BDESCR_SHIFT 6
66 #else
67 #define BDESCR_SIZE  0x20
68 #define BDESCR_MASK  0x1f
69 #define BDESCR_SHIFT 5
70 #endif
71
72 #define BF_EVACUATED 1
73 #define BF_LARGE     2
74
75 /* Finding the block descriptor for a given block -------------------------- */
76
77 static inline bdescr *Bdescr(StgPtr p)
78 {
79   return (bdescr *)
80     ((((W_)p &  MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT)) 
81      | ((W_)p & ~MBLOCK_MASK)
82      );
83 }
84
85 /* Useful Macros ------------------------------------------------------------ */
86
87 /* Offset of first real data block in a megablock */
88
89 #define FIRST_BLOCK_OFF \
90    ((W_)BLOCK_ROUND_UP(MBLOCK_SIZE / BLOCK_SIZE * BDESCR_SIZE))
91
92 /* First data block in a given megablock */
93
94 #define FIRST_BLOCK(m) ((void *)(FIRST_BLOCK_OFF + (W_)(m)))
95    
96 /* Last data block in a given megablock */
97
98 #define LAST_BLOCK(m)  ((void *)(MBLOCK_SIZE-BLOCK_SIZE + (W_)(m)))
99
100 /* First real block descriptor in a megablock */
101
102 #define FIRST_BDESCR(m) \
103    ((bdescr *)((FIRST_BLOCK_OFF>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
104
105 /* Number of usable blocks in a megablock */
106
107 #define BLOCKS_PER_MBLOCK ((MBLOCK_SIZE - FIRST_BLOCK_OFF) / BLOCK_SIZE)
108
109 /* How many blocks in this megablock group */
110
111 #define MBLOCK_GROUP_BLOCKS(n) \
112    (BLOCKS_PER_MBLOCK + (n-1) * (MBLOCK_SIZE / BLOCK_SIZE))
113
114 /* Compute the required size of a megablock group */
115
116 #define BLOCKS_TO_MBLOCKS(n) \
117    (1 + (W_)MBLOCK_ROUND_UP((n-BLOCKS_PER_MBLOCK) * BLOCK_SIZE) / MBLOCK_SIZE)
118
119 #endif /* BLOCK_H */