Arena.c 2.99 KB
Newer Older
1
/* -----------------------------------------------------------------------------
2
   $Id: Arena.c,v 1.3 2002/07/26 09:35:46 simonmar Exp $ 
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
   (c) The University of Glasgow 2001

   Arena allocation.  Arenas provide fast memory allocation at the
   expense of fine-grained recycling of storage: memory may be
   only be returned to the system by freeing the entire arena, it
   isn't possible to return individual objects within an arena.

   Do not assume that sequentially allocated objects will be adjacent
   in memory.
   
   Quirks: this allocator makes use of the RTS block allocator.  If
   the current block doesn't have enough room for the requested
   object, then a new block is allocated.  This means that allocating
   large objects will tend to result in wasted space at the end of
   each block.  In the worst case, half of the allocated space is
   wasted.  This allocator is therefore best suited to situations in
   which most allocations are small.
   -------------------------------------------------------------------------- */

#include "Rts.h"
#include "RtsUtils.h"
#include "BlockAlloc.h"
#include "Arena.h"

// Each arena struct is allocated using malloc().
struct _Arena {
    bdescr *current;
    StgWord *free;		// ptr to next free byte in current block
    StgWord *lim;		// limit (== last free byte + 1)
};

// We like to keep track of how many blocks we've allocated for 
// Storage.c:memInventory().
static long arena_blocks = 0;

// Begin a new arena
Arena *
newArena( void )
{
    Arena *arena;

    arena = stgMallocBytes(sizeof(Arena), "newArena");
    arena->current = allocBlock();
    arena->current->link = NULL;
    arena->free = arena->current->start;
    arena->lim  = arena->current->start + BLOCK_SIZE_W;
    arena_blocks++;

    return arena;
}

// Allocate some memory in an arena
void  *
arenaAlloc( Arena *arena, size_t size )
{
    void *p;
    nat size_w;
    nat req_blocks;
    bdescr *bd;

63
64
65
66
67
68
// The minimum alignment of an allocated block.
#define MIN_ALIGN 8

    // size of allocated block in words, rounded up to the nearest 
    // alignment chunk.
    size_w = ((size + MIN_ALIGN - 1) / MIN_ALIGN) * (MIN_ALIGN/sizeof(W_));
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

    if ( arena->free + size_w < arena->lim ) {
	// enough room in the current block...
	p = arena->free;
	arena->free += size_w;
	return p;
    } else {
	// allocate a fresh block...
	req_blocks =  (lnat)BLOCK_ROUND_UP(size_w*sizeof(W_)) / BLOCK_SIZE;
	bd = allocGroup(req_blocks);
	arena_blocks += req_blocks;

	bd->gen_no  = 0;
	bd->step    = NULL;
	bd->flags   = 0;
	bd->free    = bd->start;
	bd->link    = arena->current;
	arena->current = bd;
	arena->free = bd->free + size_w;
	arena->lim = bd->free + bd->blocks * BLOCK_SIZE_W;
	return bd->start;
    }
}

// Free an entire arena
void
arenaFree( Arena *arena )
{
    bdescr *bd, *next;

    for (bd = arena->current; bd != NULL; bd = next) {
	next = bd->link;
	arena_blocks -= bd->blocks;
	ASSERT(arena_blocks >= 0);
	freeGroup(bd);
    }
    free(arena);
}

unsigned long
arenaBlocks( void )
{
    return arena_blocks;
}