[project @ 2002-07-26 09:35:46 by simonmar]
[ghc-hetmet.git] / ghc / rts / Arena.c
1 /* -----------------------------------------------------------------------------
2    $Id: Arena.c,v 1.3 2002/07/26 09:35:46 simonmar Exp $ 
3    (c) The University of Glasgow 2001
4
5    Arena allocation.  Arenas provide fast memory allocation at the
6    expense of fine-grained recycling of storage: memory may be
7    only be returned to the system by freeing the entire arena, it
8    isn't possible to return individual objects within an arena.
9
10    Do not assume that sequentially allocated objects will be adjacent
11    in memory.
12    
13    Quirks: this allocator makes use of the RTS block allocator.  If
14    the current block doesn't have enough room for the requested
15    object, then a new block is allocated.  This means that allocating
16    large objects will tend to result in wasted space at the end of
17    each block.  In the worst case, half of the allocated space is
18    wasted.  This allocator is therefore best suited to situations in
19    which most allocations are small.
20    -------------------------------------------------------------------------- */
21
22 #include "Rts.h"
23 #include "RtsUtils.h"
24 #include "BlockAlloc.h"
25 #include "Arena.h"
26
27 // Each arena struct is allocated using malloc().
28 struct _Arena {
29     bdescr *current;
30     StgWord *free;              // ptr to next free byte in current block
31     StgWord *lim;               // limit (== last free byte + 1)
32 };
33
34 // We like to keep track of how many blocks we've allocated for 
35 // Storage.c:memInventory().
36 static long arena_blocks = 0;
37
38 // Begin a new arena
39 Arena *
40 newArena( void )
41 {
42     Arena *arena;
43
44     arena = stgMallocBytes(sizeof(Arena), "newArena");
45     arena->current = allocBlock();
46     arena->current->link = NULL;
47     arena->free = arena->current->start;
48     arena->lim  = arena->current->start + BLOCK_SIZE_W;
49     arena_blocks++;
50
51     return arena;
52 }
53
54 // Allocate some memory in an arena
55 void  *
56 arenaAlloc( Arena *arena, size_t size )
57 {
58     void *p;
59     nat size_w;
60     nat req_blocks;
61     bdescr *bd;
62
63 // The minimum alignment of an allocated block.
64 #define MIN_ALIGN 8
65
66     // size of allocated block in words, rounded up to the nearest 
67     // alignment chunk.
68     size_w = ((size + MIN_ALIGN - 1) / MIN_ALIGN) * (MIN_ALIGN/sizeof(W_));
69
70     if ( arena->free + size_w < arena->lim ) {
71         // enough room in the current block...
72         p = arena->free;
73         arena->free += size_w;
74         return p;
75     } else {
76         // allocate a fresh block...
77         req_blocks =  (lnat)BLOCK_ROUND_UP(size_w*sizeof(W_)) / BLOCK_SIZE;
78         bd = allocGroup(req_blocks);
79         arena_blocks += req_blocks;
80
81         bd->gen_no  = 0;
82         bd->step    = NULL;
83         bd->flags   = 0;
84         bd->free    = bd->start;
85         bd->link    = arena->current;
86         arena->current = bd;
87         arena->free = bd->free + size_w;
88         arena->lim = bd->free + bd->blocks * BLOCK_SIZE_W;
89         return bd->start;
90     }
91 }
92
93 // Free an entire arena
94 void
95 arenaFree( Arena *arena )
96 {
97     bdescr *bd, *next;
98
99     for (bd = arena->current; bd != NULL; bd = next) {
100         next = bd->link;
101         arena_blocks -= bd->blocks;
102         ASSERT(arena_blocks >= 0);
103         freeGroup(bd);
104     }
105     free(arena);
106 }
107
108 unsigned long
109 arenaBlocks( void )
110 {
111     return arena_blocks;
112 }
113