aboutsummaryrefslogtreecommitdiffhomepage
path: root/halloc.c
diff options
context:
space:
mode:
authorGravatar axel <axel@liljencrantz.se>2006-02-07 04:11:01 +1000
committerGravatar axel <axel@liljencrantz.se>2006-02-07 04:11:01 +1000
commit57d53c70e3d1baabbe3207ce0356ca8e38842198 (patch)
tree0408df190f6dadb7a9315e5420f48634ee2272bb /halloc.c
parent4e77ee45164f745992dbf575f2b6c0f3797dabf0 (diff)
Further halloc:ification if fish
darcs-hash:20060206181101-ac50b-db0aac307a81e7f0677acd15a9f38ff8c7ff36d2.gz
Diffstat (limited to 'halloc.c')
-rw-r--r--halloc.c68
1 files changed, 34 insertions, 34 deletions
diff --git a/halloc.c b/halloc.c
index 4d61601c..741f4862 100644
--- a/halloc.c
+++ b/halloc.c
@@ -1,5 +1,9 @@
/** \file halloc.c
- A hierarchical memory allocation system
+
+ 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"
@@ -14,6 +18,7 @@
typedef struct halloc
{
array_list_t children;
+ array_list_t hchildren;
long long data[0];
}
halloc_t;
@@ -26,53 +31,48 @@ static halloc_t *halloc_from_data( void *data )
void *halloc( void *context, size_t size )
{
- halloc_t *res = (halloc_t *)calloc( 1, sizeof(halloc_t) + size );
- if( !res )
- return 0;
- al_init( &res->children );
+ halloc_t *me, *parent;
+ me = (halloc_t *)calloc( 1, sizeof(halloc_t) + size );
+
+ if( !me )
+ return 0;
+
+ al_init( &me->children );
+
if( context )
- {
- halloc_t *parent = halloc_from_data( context );
- al_push( &parent->children, &res->data );
+ {
+ parent = halloc_from_data( context );
+ al_push( &parent->hchildren, &me->data );
}
- return &res->data;
+
+ return &me->data;
}
-void halloc_free( void *context )
+void *halloc_register( void *context, void *data )
{
halloc_t *me;
if( !context )
- return;
+ return data;
me = halloc_from_data( context );
- al_foreach( &me->children, (void (*)(const void *))&halloc_free );
- al_destroy( &me->children );
- free(me);
+ al_push( &me->children, data );
+ return data;
}
-wchar_t *halloc_wcsdup( void *context, const wchar_t *in )
+
+void halloc_free( void *context )
{
- size_t len=wcslen(in);
- wchar_t *out = halloc( context, sizeof( wchar_t)*(len+1));
- if( out == 0 )
- {
- die_mem();
- }
+ halloc_t *me;
+ if( !context )
+ return;
- memcpy( out, in, sizeof( wchar_t)*(len+1));
- return out;
+ me = halloc_from_data( context );
+ al_foreach( &me->hchildren, (void (*)(const void *))&halloc_free );
+ al_foreach( &me->children, (void (*)(const void *))&free );
+ al_destroy( &me->children );
+ al_destroy( &me->hchildren );
+ free(me);
}
-wchar_t *halloc_wcsndup( void *context, const wchar_t *in, int c )
-{
- wchar_t *res = halloc( context, sizeof(wchar_t)*(c+1) );
- if( res == 0 )
- {
- die_mem();
- }
- wcsncpy( res, in, c );
- res[c] = L'\0';
- return res;
-}