Arena.c 3.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/* -----------------------------------------------------------------------------
   (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.
11

12 13 14 15 16 17 18 19 20
   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.
   -------------------------------------------------------------------------- */

21
#include "PosixSource.h"
22
#include "Rts.h"
23

24 25 26 27 28 29
#include "RtsUtils.h"
#include "Arena.h"

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

34
// We like to keep track of how many blocks we've allocated for
35 36 37 38 39 40 41 42 43 44
// Storage.c:memInventory().
static long arena_blocks = 0;

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

    arena = stgMallocBytes(sizeof(Arena), "newArena");
45
    arena->current = allocBlock_lock();
46 47 48 49 50 51 52 53
    arena->current->link = NULL;
    arena->free = arena->current->start;
    arena->lim  = arena->current->start + BLOCK_SIZE_W;
    arena_blocks++;

    return arena;
}

54 55 56 57 58 59 60
// The minimum alignment of an allocated block.
#define MIN_ALIGN 8

/* 'n' is assumed to be a power of 2 */
#define ROUNDUP(x,n)  (((x)+((n)-1))&(~((n)-1)))
#define B_TO_W(x)     ((x) / sizeof(W_))

61 62 63 64 65
// Allocate some memory in an arena
void  *
arenaAlloc( Arena *arena, size_t size )
{
    void *p;
66 67
    uint32_t size_w;
    uint32_t req_blocks;
68 69
    bdescr *bd;

70 71
    // round up to nearest alignment chunk.
    size = ROUNDUP(size,MIN_ALIGN);
72

73 74
    // size of allocated block in words.
    size_w = B_TO_W(size);
75 76

    if ( arena->free + size_w < arena->lim ) {
77 78 79 80
        // enough room in the current block...
        p = arena->free;
        arena->free += size_w;
        return p;
81
    } else {
82 83 84 85
        // allocate a fresh block...
        req_blocks =  (W_)BLOCK_ROUND_UP(size) / BLOCK_SIZE;
        bd = allocGroup_lock(req_blocks);
        arena_blocks += req_blocks;
86

87 88
        bd->gen_no  = 0;
        bd->gen     = NULL;
Simon Marlow's avatar
Simon Marlow committed
89
        bd->dest_no = 0;
90 91 92 93 94 95 96
        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;
97 98 99 100 101 102 103 104 105 106
    }
}

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

    for (bd = arena->current; bd != NULL; bd = next) {
107 108 109 110
        next = bd->link;
        arena_blocks -= bd->blocks;
        ASSERT(arena_blocks >= 0);
        freeGroup_lock(bd);
111
    }
112
    stgFree(arena);
113 114 115 116 117 118 119
}

unsigned long
arenaBlocks( void )
{
    return arena_blocks;
}