[project @ 1999-11-02 15:05:38 by simonmar]
[ghc-hetmet.git] / ghc / rts / BlockAlloc.h
1 /* -----------------------------------------------------------------------------
2  * $Id: BlockAlloc.h,v 1.6 1999/11/02 15:05:56 simonmar Exp $
3  *
4  * (c) The GHC Team, 1998-1999
5  *
6  * Block Allocator Interface
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #ifndef BLOCK_ALLOC_H
11 #define BLOCK_ALLOC_H
12
13 /* Initialisation ---------------------------------------------------------- */
14
15 extern void initBlockAllocator(void);
16
17 /* Allocation -------------------------------------------------------------- */
18
19 extern bdescr *allocGroup(nat n);
20 extern bdescr *allocBlock(void);
21
22 /* De-Allocation ----------------------------------------------------------- */
23
24 extern void freeGroup(bdescr *p);
25 extern void freeChain(bdescr *p);
26
27 /* Finding the block descriptor for a given block -------------------------- */
28
29 extern inline bdescr *Bdescr(StgPtr p)
30 {
31   return (bdescr *)
32     ((((W_)p &  MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT)) 
33      | ((W_)p & ~MBLOCK_MASK)
34      );
35 }
36
37 /* Round a value to megablocks --------------------------------------------- */
38
39 #define WORDS_PER_MBLOCK  (BLOCKS_PER_MBLOCK * BLOCK_SIZE_W)
40
41 static inline nat
42 round_to_mblocks(nat words)
43 {
44   if (words > WORDS_PER_MBLOCK) {
45     if ((words % WORDS_PER_MBLOCK) < (WORDS_PER_MBLOCK / 2)) {
46       words = (words / WORDS_PER_MBLOCK) * WORDS_PER_MBLOCK;
47     } else {
48       words = ((words / WORDS_PER_MBLOCK) + 1) * WORDS_PER_MBLOCK;
49     }
50   }
51   return words;
52 }
53
54 /* Debugging  -------------------------------------------------------------- */
55
56 #ifdef DEBUG
57 extern void checkFreeListSanity(void);
58 nat         countFreeList(void);
59 #endif
60
61 #endif BLOCK_ALLOC_H