2006-02-06 22:25:02 +08:00
|
|
|
/** \file halloc.c
|
2006-02-07 02:11:01 +08:00
|
|
|
|
|
|
|
A hierarchical memory allocation system. Works just like talloc
|
|
|
|
used in Samba, except that an arbitrary block allocated with
|
|
|
|
malloc() can be registered to be freed by halloc_free.
|
|
|
|
|
2006-02-06 22:25:02 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2006-02-28 21:17:16 +08:00
|
|
|
|
2006-02-06 22:25:02 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2006-02-28 21:17:16 +08:00
|
|
|
#include "fallback.h"
|
2006-02-06 22:25:02 +08:00
|
|
|
#include "util.h"
|
2006-02-28 21:17:16 +08:00
|
|
|
|
2006-02-06 22:25:02 +08:00
|
|
|
#include "common.h"
|
|
|
|
#include "halloc.h"
|
|
|
|
|
2006-02-12 21:18:46 +08:00
|
|
|
#define HALLOC_BLOCK_SIZE 128
|
2006-02-11 08:13:17 +08:00
|
|
|
#define HALLOC_SCRAP_SIZE 16
|
|
|
|
|
2006-02-12 21:18:46 +08:00
|
|
|
#ifdef HALLOC_DEBUG
|
2006-02-11 08:13:17 +08:00
|
|
|
static int child_count=0;
|
|
|
|
static int child_size=0;
|
|
|
|
static int alloc_count =0;
|
|
|
|
static int alloc_spill = 0;
|
|
|
|
static pid_t pid=0;
|
|
|
|
static int parent_count=0;
|
2006-02-12 21:18:46 +08:00
|
|
|
#endif
|
2006-02-11 08:13:17 +08:00
|
|
|
|
2006-02-06 22:25:02 +08:00
|
|
|
typedef struct halloc
|
|
|
|
{
|
|
|
|
array_list_t children;
|
2006-02-11 08:13:17 +08:00
|
|
|
void *scratch;
|
|
|
|
size_t scratch_free;
|
2006-02-06 22:25:02 +08:00
|
|
|
long long data[0];
|
|
|
|
}
|
|
|
|
halloc_t;
|
|
|
|
|
|
|
|
static halloc_t *halloc_from_data( void *data )
|
|
|
|
{
|
2006-03-29 08:14:50 +08:00
|
|
|
return (halloc_t *)(((char *)data) - sizeof( halloc_t ) );
|
2006-02-06 22:25:02 +08:00
|
|
|
}
|
|
|
|
|
2006-02-11 08:13:17 +08:00
|
|
|
static void late_free( void *data)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-02-12 21:18:46 +08:00
|
|
|
#ifdef HALLOC_DEBUG
|
2006-02-11 08:13:17 +08:00
|
|
|
static void woot()
|
|
|
|
{
|
|
|
|
if( getpid() == pid )
|
|
|
|
{
|
|
|
|
debug( 1, L"%d parents, %d children with average child size of %.2f bytes caused %d allocs, average spill of %.2f bytes",
|
|
|
|
parent_count, child_count, (double)child_size/child_count,
|
|
|
|
parent_count+alloc_count, (double)alloc_spill/(parent_count+alloc_count) );
|
|
|
|
}
|
|
|
|
}
|
2006-02-12 21:18:46 +08:00
|
|
|
#endif
|
2006-02-06 22:25:02 +08:00
|
|
|
|
|
|
|
void *halloc( void *context, size_t size )
|
2006-02-09 23:50:20 +08:00
|
|
|
{
|
2006-02-07 02:11:01 +08:00
|
|
|
halloc_t *me, *parent;
|
2006-02-06 22:25:02 +08:00
|
|
|
if( context )
|
2006-02-11 08:13:17 +08:00
|
|
|
{
|
|
|
|
void *res;
|
|
|
|
|
2006-02-12 21:18:46 +08:00
|
|
|
#ifdef HALLOC_DEBUG
|
2006-02-11 08:13:17 +08:00
|
|
|
|
|
|
|
if( !child_count )
|
|
|
|
{
|
|
|
|
pid = getpid();
|
|
|
|
atexit( woot );
|
|
|
|
}
|
|
|
|
|
|
|
|
child_count++;
|
|
|
|
child_size += size;
|
2006-02-12 21:18:46 +08:00
|
|
|
#endif
|
2006-02-07 02:11:01 +08:00
|
|
|
parent = halloc_from_data( context );
|
2006-02-11 08:13:17 +08:00
|
|
|
if( size <= parent->scratch_free )
|
|
|
|
{
|
|
|
|
res = parent->scratch;
|
|
|
|
parent->scratch_free -= size;
|
2006-03-29 08:14:50 +08:00
|
|
|
parent->scratch = ((char *)parent->scratch)+size;
|
2006-02-11 08:13:17 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-02-12 21:18:46 +08:00
|
|
|
|
|
|
|
#ifdef HALLOC_DEBUG
|
2006-02-11 08:13:17 +08:00
|
|
|
alloc_count++;
|
2006-02-12 21:18:46 +08:00
|
|
|
#endif
|
|
|
|
|
2006-02-11 08:13:17 +08:00
|
|
|
if( parent->scratch_free < HALLOC_SCRAP_SIZE )
|
|
|
|
{
|
2006-02-12 21:18:46 +08:00
|
|
|
#ifdef HALLOC_DEBUG
|
2006-02-11 08:13:17 +08:00
|
|
|
alloc_spill += parent->scratch_free;
|
2006-02-12 21:18:46 +08:00
|
|
|
#endif
|
2006-02-11 08:13:17 +08:00
|
|
|
res = calloc( 1, size + HALLOC_BLOCK_SIZE );
|
2006-03-29 08:14:50 +08:00
|
|
|
parent->scratch = (char *)res + size;
|
2006-02-11 08:13:17 +08:00
|
|
|
parent->scratch_free = HALLOC_BLOCK_SIZE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res = calloc( 1, size );
|
|
|
|
}
|
|
|
|
al_push( &parent->children, &late_free );
|
|
|
|
al_push( &parent->children, res );
|
|
|
|
|
|
|
|
}
|
|
|
|
return res;
|
2006-02-09 23:50:20 +08:00
|
|
|
|
2006-02-11 08:13:17 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
me = (halloc_t *)calloc( 1, sizeof(halloc_t) + size + HALLOC_BLOCK_SIZE );
|
|
|
|
|
|
|
|
if( !me )
|
|
|
|
return 0;
|
2006-02-12 21:18:46 +08:00
|
|
|
#ifdef HALLOC_DEBUG
|
2006-02-11 08:13:17 +08:00
|
|
|
parent_count++;
|
2006-02-12 21:18:46 +08:00
|
|
|
#endif
|
2006-03-29 08:14:50 +08:00
|
|
|
me->scratch = ((char *)me) + sizeof(halloc_t) + size;
|
2006-02-11 08:13:17 +08:00
|
|
|
me->scratch_free = HALLOC_BLOCK_SIZE;
|
|
|
|
|
|
|
|
al_init( &me->children );
|
|
|
|
return &me->data;
|
|
|
|
}
|
2006-02-06 22:25:02 +08:00
|
|
|
}
|
|
|
|
|
2006-02-09 23:50:20 +08:00
|
|
|
void halloc_register_function( void *context, void (*func)(void *), void *data )
|
2006-02-06 22:25:02 +08:00
|
|
|
{
|
|
|
|
halloc_t *me;
|
|
|
|
if( !context )
|
2006-02-09 23:50:20 +08:00
|
|
|
return;
|
2006-02-06 22:25:02 +08:00
|
|
|
|
|
|
|
me = halloc_from_data( context );
|
2006-02-09 23:50:20 +08:00
|
|
|
al_push( &me->children, func );
|
2006-02-07 02:11:01 +08:00
|
|
|
al_push( &me->children, data );
|
2006-02-06 22:25:02 +08:00
|
|
|
}
|
|
|
|
|
2006-02-07 02:11:01 +08:00
|
|
|
void halloc_free( void *context )
|
2006-02-06 22:25:02 +08:00
|
|
|
{
|
2006-02-07 02:11:01 +08:00
|
|
|
halloc_t *me;
|
2006-02-09 23:50:20 +08:00
|
|
|
int i;
|
|
|
|
|
2006-02-07 02:11:01 +08:00
|
|
|
if( !context )
|
|
|
|
return;
|
2006-02-11 08:13:17 +08:00
|
|
|
|
2006-02-09 23:50:20 +08:00
|
|
|
|
2006-02-07 02:11:01 +08:00
|
|
|
me = halloc_from_data( context );
|
2006-02-11 08:13:17 +08:00
|
|
|
|
2006-02-12 21:18:46 +08:00
|
|
|
#ifdef HALLOC_DEBUG
|
2006-02-11 08:13:17 +08:00
|
|
|
alloc_spill += me->scratch_free;
|
2006-02-12 21:18:46 +08:00
|
|
|
#endif
|
2006-02-11 08:13:17 +08:00
|
|
|
for( i=0; i<al_get_count(&me->children); i+=2 )
|
|
|
|
{
|
|
|
|
void (*func)(void *) = (void (*)(void *))al_get( &me->children, i );
|
|
|
|
void * data = (void *)al_get( &me->children, i+1 );
|
|
|
|
if( func != &late_free )
|
|
|
|
func( data );
|
|
|
|
}
|
2006-02-09 23:50:20 +08:00
|
|
|
for( i=0; i<al_get_count(&me->children); i+=2 )
|
|
|
|
{
|
|
|
|
void (*func)(void *) = (void (*)(void *))al_get( &me->children, i );
|
|
|
|
void * data = (void *)al_get( &me->children, i+1 );
|
2006-02-11 08:13:17 +08:00
|
|
|
if( func == &late_free )
|
|
|
|
free( data );
|
2006-02-09 23:50:20 +08:00
|
|
|
}
|
2006-02-07 02:11:01 +08:00
|
|
|
al_destroy( &me->children );
|
|
|
|
free(me);
|
2006-02-06 22:25:02 +08:00
|
|
|
}
|