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