aboutsummaryrefslogtreecommitdiffhomepage
path: root/halloc_util.c
blob: bc575f7eeeb290e259939f2b661865241d27c272 (plain)
1
2
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
63
64
65
66
67
68
69
/** \file halloc.c

    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.

*/

#include "config.h"

#include <stdlib.h>
#include <unistd.h>

#include "util.h"
#include "common.h"
#include "halloc.h"

void *global_context=0;

void halloc_util_init()
{
	global_context = halloc( 0, 0 );
}

void halloc_util_destroy()
{
	halloc_free( global_context );
}

array_list_t *al_halloc( void *context )
{
	array_list_t *res = halloc( context, sizeof( array_list_t ) );
	if( !res )
		die_mem();
	al_init( res );
	halloc_register_function( res, (void (*)(void *)) &al_destroy, res );
	return res;
}

string_buffer_t *sb_halloc( void *context )
{
	string_buffer_t *res = halloc( context, sizeof( string_buffer_t ) );
	if( !res )
		die_mem();
	sb_init( res );
	halloc_register_function( res, (void (*)(void *)) &sb_destroy, res );
	return res;
}

static void halloc_passthrough( void *f )
{
	void (*func)() = (void (*)() )f;
	func();
}

void halloc_register_function_void( void *context, void (*func)() )
{
	halloc_register_function( context, &halloc_passthrough, (void *)func );
}

void *halloc_register( void *context, void *data )
{
	if( !data )
		return 0;
	
	halloc_register_function( context, &free, data );
	return data;
}