[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / includes / Block.h
1 /* -----------------------------------------------------------------------------
2  * $Id: Block.h,v 1.2 1998/12/02 13:20:53 simonm Exp $
3  *
4  * Block structure for the storage manager
5  *
6  * ---------------------------------------------------------------------------*/
7
8 #ifndef BLOCK_H
9 #define BLOCK_H
10
11 /* The actual block and megablock-size constants are defined in
12  * includes/Constants.h, all constants here are derived from these.
13  */
14
15 /* Block related constants (4k blocks) */
16
17 #define BLOCK_SIZE_W (BLOCK_SIZE/sizeof(W_))
18 #define BLOCK_MASK   (BLOCK_SIZE-1)
19
20 #define BLOCK_ROUND_UP(p)   ((void *) (((W_)(p)+BLOCK_SIZE-1) & ~BLOCK_MASK))
21 #define BLOCK_ROUND_DOWN(p) ((void *) ((W_)(p) & ~BLOCK_MASK))
22
23 /* Megablock related constants (1M megablocks) */
24
25 #define MBLOCK_SIZE_W  (MBLOCK_SIZE/sizeof(W_))
26 #define MBLOCK_MASK    (MBLOCK_SIZE-1)
27
28 #define MBLOCK_ROUND_UP(p)   ((void *)(((W_)(p)+MBLOCK_SIZE-1) & ~MBLOCK_MASK))
29 #define MBLOCK_ROUND_DOWN(p) ((void *)((W_)(p) & ~MBLOCK_MASK ))
30
31
32 /* -----------------------------------------------------------------------------
33  * Block descriptor.  This structure *must* be the right length, so we
34  * can do pointer arithmetic on pointers to it.
35  */
36
37 /* The block descriptor is 64 bytes on a 64-bit machine, and 32-bytes
38  * on a 32-bit machine.
39  */
40
41 typedef struct _bdescr {
42   StgPtr start;                 /* start addr of memory */
43   StgPtr free;                  /* first free byte of memory */
44   struct _bdescr *link;         /* used for chaining blocks together */
45   struct _bdescr *back;         /* used (occasionally) for doubly-linked lists*/
46   StgNat32 gen;                 /* generation */
47   StgNat32 step;                /* step */
48   StgNat32 blocks;              /* no. of blocks (if grp head, 0 otherwise) */
49 #if SIZEOF_VOID_P == 8
50   StgNat32 _padding[5];
51 #else
52   StgNat32 _padding[1];
53 #endif
54 } bdescr;
55
56 #if SIZEOF_VOID_P == 8
57 #define BDESCR_SIZE  0x40
58 #define BDESCR_MASK  0x3f
59 #define BDESCR_SHIFT 6
60 #else
61 #define BDESCR_SIZE  0x20
62 #define BDESCR_MASK  0x1f
63 #define BDESCR_SHIFT 5
64 #endif
65
66 /* Useful Macros ------------------------------------------------------------ */
67
68 /* Offset of first real data block in a megablock */
69
70 #define FIRST_BLOCK_OFF \
71    ((W_)BLOCK_ROUND_UP(MBLOCK_SIZE / BLOCK_SIZE * BDESCR_SIZE))
72
73 /* First data block in a given megablock */
74
75 #define FIRST_BLOCK(m) ((void *)(FIRST_BLOCK_OFF + (W_)(m)))
76    
77 /* Last data block in a given megablock */
78
79 #define LAST_BLOCK(m)  ((void *)(MBLOCK_SIZE-BLOCK_SIZE + (W_)(m)))
80
81 /* First real block descriptor in a megablock */
82
83 #define FIRST_BDESCR(m) \
84    ((bdescr *)((FIRST_BLOCK_OFF>>(BLOCK_SHIFT-BDESCR_SHIFT)) + (W_)(m)))
85
86 /* Number of usable blocks in a megablock */
87
88 #define BLOCKS_PER_MBLOCK ((MBLOCK_SIZE - FIRST_BLOCK_OFF) / BLOCK_SIZE)
89
90 /* How many blocks in this megablock group */
91
92 #define MBLOCK_GROUP_BLOCKS(n) \
93    (BLOCKS_PER_MBLOCK + (n-1) * (MBLOCK_SIZE / BLOCK_SIZE))
94
95 /* Compute the required size of a megablock group */
96
97 #define BLOCKS_TO_MBLOCKS(n) \
98    (1 + (W_)MBLOCK_ROUND_UP((n-BLOCKS_PER_MBLOCK) * BLOCK_SIZE) / MBLOCK_SIZE)
99
100 #endif BLOCK_H