Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions | Variables
plarena.c File Reference
#include <stdlib.h>
#include <string.h>
#include "plarena.h"
#include "prmem.h"
#include "prbit.h"
#include "prlog.h"
#include "prlock.h"
#include "prinit.h"

Go to the source code of this file.

Defines

#define COUNT(pool, what)   /* nothing */
#define PL_ARENA_DEFAULT_ALIGN   sizeof(double)

Functions

static PRStatus InitializeArenas (void)
static PRStatus LockArena (void)
static void UnlockArena (void)
 PR_IMPLEMENT (void)
 PR_IMPLEMENT (void *)
static void FreeArenaList (PLArenaPool *pool, PLArena *head, PRBool reallyFree)

Variables

static PLArenaarena_freelist
static PRLockarenaLock
static PRCallOnceType once

Define Documentation

#define COUNT (   pool,
  what 
)    /* nothing */

Definition at line 60 of file plarena.c.

#define PL_ARENA_DEFAULT_ALIGN   sizeof(double)

Definition at line 63 of file plarena.c.


Function Documentation

static void FreeArenaList ( PLArenaPool pool,
PLArena head,
PRBool  reallyFree 
) [static]

Definition at line 252 of file plarena.c.

{
    PLArena **ap, *a;

    ap = &head->next;
    a = *ap;
    if (!a)
        return;

#ifdef DEBUG
    do {
        PR_ASSERT(a->base <= a->avail && a->avail <= a->limit);
        a->avail = a->base;
        PL_CLEAR_UNUSED(a);
    } while ((a = a->next) != 0);
    a = *ap;
#endif

    if (reallyFree) {
        do {
            *ap = a->next;
            PL_CLEAR_ARENA(a);
            PL_COUNT_ARENA(pool,--);
            PR_DELETE(a);
        } while ((a = *ap) != 0);
    } else {
        /* Insert the whole arena chain at the front of the freelist. */
        do {
            ap = &(*ap)->next;
        } while (*ap);
        LockArena();
        *ap = arena_freelist;
        arena_freelist = a;
        head->next = 0;
        UnlockArena();
    }

    pool->current = head;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PRStatus InitializeArenas ( void  ) [static]

Definition at line 82 of file plarena.c.

{
    PR_ASSERT( arenaLock == NULL );
    arenaLock = PR_NewLock();
    if ( arenaLock == NULL )
        return PR_FAILURE;
    else
        return PR_SUCCESS;
} /* end ArenaInitialize() */

Here is the call graph for this function:

Here is the caller graph for this function:

static PRStatus LockArena ( void  ) [static]

Definition at line 92 of file plarena.c.

{
    PRStatus rc = PR_CallOnce( &once, InitializeArenas );

    if ( PR_FAILURE != rc )
        PR_Lock( arenaLock );
    return(rc);
} /* end LockArena() */

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 107 of file plarena.c.

{
#if defined(XP_MAC)
#pragma unused (name)
#endif

    if (align == 0)
        align = PL_ARENA_DEFAULT_ALIGN;
    pool->mask = PR_BITMASK(PR_CeilingLog2(align));
    pool->first.next = NULL;
    pool->first.base = pool->first.avail = pool->first.limit =
        (PRUword)PL_ARENA_ALIGN(pool, &pool->first + 1);
    pool->current = &pool->first;
    pool->arenasize = size;                                  
#ifdef PL_ARENAMETER
    memset(&pool->stats, 0, sizeof pool->stats);
    pool->stats.name = strdup(name);
    pool->stats.next = arena_stats_list;
    arena_stats_list = &pool->stats;
#endif
}

Here is the call graph for this function:

PR_IMPLEMENT ( void )

Definition at line 157 of file plarena.c.

{
    PLArena *a;   
    char *rp;     /* returned pointer */

    PR_ASSERT((nb & pool->mask) == 0);
    
    nb = (PRUword)PL_ARENA_ALIGN(pool, nb); /* force alignment */

    /* attempt to allocate from arenas at pool->current */
    {
        a = pool->current;
        do {
            if ( a->avail +nb <= a->limit )  {
                pool->current = a;
                rp = (char *)a->avail;
                a->avail += nb;
                return rp;
            }
        } while( NULL != (a = a->next) );
    }

    /* attempt to allocate from arena_freelist */
    {
        PLArena *p; /* previous pointer, for unlinking from freelist */

        /* lock the arena_freelist. Make access to the freelist MT-Safe */
        if ( PR_FAILURE == LockArena())
            return(0);

        for ( a = arena_freelist, p = NULL; a != NULL ; p = a, a = a->next ) {
            if ( a->base +nb <= a->limit )  {
                if ( p == NULL )
                    arena_freelist = a->next;
                else
                    p->next = a->next;
                UnlockArena();
                a->avail = a->base;
                rp = (char *)a->avail;
                a->avail += nb;
                /* the newly allocated arena is linked after pool->current 
                *  and becomes pool->current */
                a->next = pool->current->next;
                pool->current->next = a;
                pool->current = a;
                if ( NULL == pool->first.next )
                    pool->first.next = a;
                return(rp);
            }
        }
        UnlockArena();
    }

    /* attempt to allocate from the heap */ 
    {  
        PRUint32 sz = PR_MAX(pool->arenasize, nb);
        sz += sizeof *a + pool->mask;  /* header and alignment slop */
        a = (PLArena*)PR_MALLOC(sz);
        if ( NULL != a )  {
            a->limit = (PRUword)a + sz;
            a->base = a->avail = (PRUword)PL_ARENA_ALIGN(pool, a + 1);
            rp = (char *)a->avail;
            a->avail += nb;
            /* the newly allocated arena is linked after pool->current 
            *  and becomes pool->current */
            a->next = pool->current->next;
            pool->current->next = a;
            pool->current = a;
            if ( NULL == pool->first.next )
                pool->first.next = a;
            PL_COUNT_ARENA(pool,++);
            COUNT(pool, nmallocs);
            return(rp);
        }
    }

    /* we got to here, and there's no memory to allocate */
    return(NULL);
} /* --- end PL_ArenaAllocate() --- */

Here is the call graph for this function:

static void UnlockArena ( void  ) [static]

Definition at line 101 of file plarena.c.

{
    PR_Unlock( arenaLock );
    return;
} /* end UnlockArena() */

Here is the caller graph for this function:


Variable Documentation

Definition at line 53 of file plarena.c.

PRLock* arenaLock [static]

Definition at line 65 of file plarena.c.

PRCallOnceType once [static]

Definition at line 66 of file plarena.c.