Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Typedefs | Functions
xpt_arena.c File Reference
#include "xpt_arena.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

Go to the source code of this file.


struct  BLK_HDR
struct  XPTArena


#define LOG_MALLOC(_a, _req, _used)   ((void)0)
#define LOG_REAL_MALLOC(_a, _size)   ((void)0)
#define LOG_FREE(_a)   ((void)0)
#define LOG_DONE_LOADING(_a)   ((void)0)
#define PRINT_STATS(_a)   ((void)0)
#define XPT_MIN_BLOCK_SIZE   32
#define ALIGN_RND(s, a)   ((a)==1?(s):((((s)+(a)-1)/(a))*(a)))


typedef struct BLK_HDR


 XPT_NewArena (PRUint32 block_size, size_t alignment, const char *name)
 XPT_DestroyArena (XPTArena *arena)
 XPT_DumpStats (XPTArena *arena)
 XPT_ArenaMalloc (XPTArena *arena, size_t size)
 XPT_ArenaStrDup (XPTArena *arena, const char *s)
 XPT_NotifyDoneLoading (XPTArena *arena)
 XPT_ArenaFree (XPTArena *arena, void *block)

Class Documentation

struct BLK_HDR

Definition at line 105 of file xpt_arena.c.

Collaboration diagram for BLK_HDR:
Class Members
BLK_HDR * next
size_t size
struct XPTArena

Definition at line 116 of file xpt_arena.c.

Collaboration diagram for XPTArena:
Class Members
size_t alignment
size_t block_size
BLK_HDR * first
char * name
PRUint8 * next
size_t space

Define Documentation

#define ALIGN_RND (   s,
)    ((a)==1?(s):((((s)+(a)-1)/(a))*(a)))

Definition at line 114 of file xpt_arena.c.

#define LOG_DONE_LOADING (   _a)    ((void)0)

Definition at line 96 of file xpt_arena.c.

#define LOG_FREE (   _a)    ((void)0)

Definition at line 94 of file xpt_arena.c.

#define LOG_MALLOC (   _a,
)    ((void)0)

Definition at line 92 of file xpt_arena.c.

#define LOG_REAL_MALLOC (   _a,
)    ((void)0)

Definition at line 93 of file xpt_arena.c.

#define PRINT_STATS (   _a)    ((void)0)

Definition at line 97 of file xpt_arena.c.

Definition at line 111 of file xpt_arena.c.

Typedef Documentation

typedef struct BLK_HDR

Definition at line 104 of file xpt_arena.c.

Function Documentation

XPT_ArenaFree ( XPTArena arena,
void block 

Definition at line 294 of file xpt_arena.c.

XPT_ArenaMalloc ( XPTArena arena,
size_t  size 

Definition at line 196 of file xpt_arena.c.

    PRUint8 *cur;
    size_t bytes;

    if (!size)
        return NULL;

    if (!arena) {
        return NULL;

    bytes = ALIGN_RND(size, arena->alignment);
    LOG_MALLOC(arena, size, bytes);

    if (bytes > arena->space) {
        BLK_HDR* new_block;
        size_t block_header_size = ALIGN_RND(sizeof(BLK_HDR), arena->alignment);
        size_t new_space = arena->block_size;
        if (bytes > new_space - block_header_size)
            new_space += bytes;

        new_block = (BLK_HDR*) calloc(new_space/arena->alignment, 
        if (!new_block) {
            arena->next = NULL;
            arena->space = 0;
            return NULL;

        LOG_REAL_MALLOC(arena, new_space);

        /* link block into the list of blocks for use when we destroy */
        new_block->next = arena->first;
        arena->first = new_block;

        /* save other block header info */
        new_block->size = new_space;

        /* set info for current block */
        arena->next  = ((PRUint8*)new_block) + block_header_size;
        arena->space = new_space - block_header_size;

#ifdef DEBUG
        /* mark block for corruption check */
        memset(arena->next, 0xcd, arena->space);
#ifdef DEBUG
        /* do corruption check */
        size_t i;
        for (i = 0; i < bytes; ++i) {
            XPT_ASSERT(arena->next[i] == 0xcd);        
        /* we guarantee that the block will be filled with zeros */
        memset(arena->next, 0, bytes);

    cur = arena->next;
    arena->next  += bytes;
    arena->space -= bytes;
    return cur;    

Here is the call graph for this function:

Here is the caller graph for this function:

XPT_ArenaStrDup ( XPTArena arena,
const char *  s 

Definition at line 269 of file xpt_arena.c.

    size_t len;
    char* cur;

    if (!s)
        return NULL;

    len = strlen(s)+1;
    cur = XPT_ArenaMalloc(arena, len);
    memcpy(cur, s, len);
    return cur;

Here is the call graph for this function:

XPT_DestroyArena ( XPTArena arena)

Definition at line 169 of file xpt_arena.c.

    BLK_HDR* cur;
    BLK_HDR* next;
    cur = arena->first;
    while (cur) {
        next = cur->next;
        cur = next;

Here is the caller graph for this function:

XPT_DumpStats ( XPTArena arena)

Definition at line 184 of file xpt_arena.c.


Here is the caller graph for this function:

XPT_NewArena ( PRUint32  block_size,
size_t  alignment,
const char *  name 

Definition at line 137 of file xpt_arena.c.

    XPTArena *arena = calloc(1, sizeof(XPTArena));
    if (arena) {
        if (alignment > sizeof(double))
            alignment = sizeof(double);
        arena->alignment = alignment;

        if (block_size < XPT_MIN_BLOCK_SIZE)
            block_size = XPT_MIN_BLOCK_SIZE;
        arena->block_size = ALIGN_RND(block_size, alignment);

        /* must have room for at least one item! */
        XPT_ASSERT(arena->block_size >= 
                   ALIGN_RND(sizeof(BLK_HDR), alignment) +
                   ALIGN_RND(1, alignment));

        if (name) {
            arena->name = XPT_STRDUP(arena, name);           
            /* fudge the stats since we are using space in the arena */
            arena->LOG_MallocCallCount = 0;
            arena->LOG_MallocTotalBytesRequested = 0;
            arena->LOG_MallocTotalBytesUsed = 0;
#endif /* XPT_ARENA_LOGGING */
    return arena;        

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 284 of file xpt_arena.c.

    if (arena) {