aboutsummaryrefslogtreecommitdiffhomepage
path: root/halloc.c
diff options
context:
space:
mode:
authorGravatar axel <axel@liljencrantz.se>2006-02-07 00:25:02 +1000
committerGravatar axel <axel@liljencrantz.se>2006-02-07 00:25:02 +1000
commit585191310bb476deb7bdd4214adcc8b1076c9fd4 (patch)
treed3d9391e592b04fc1cb7a4e7ecbac4c66f9b4a92 /halloc.c
parent530bbfc9ac3234149a939c65da3d448279bf7de9 (diff)
First checkin of transition to using a new hierarchical memory allocator, some of the memory associated with a job is covered
darcs-hash:20060206142502-ac50b-ba1c9a4f64ea0f44f65303a125f9ddae5bd31e2f.gz
Diffstat (limited to 'halloc.c')
-rw-r--r--halloc.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/halloc.c b/halloc.c
new file mode 100644
index 00000000..4d61601c
--- /dev/null
+++ b/halloc.c
@@ -0,0 +1,78 @@
+/** \file halloc.c
+ A hierarchical memory allocation system
+*/
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "util.h"
+#include "common.h"
+#include "halloc.h"
+
+typedef struct halloc
+{
+ array_list_t children;
+ long long data[0];
+}
+ halloc_t;
+
+static halloc_t *halloc_from_data( void *data )
+{
+ return (halloc_t *)(data - sizeof( halloc_t ) );
+}
+
+
+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 );
+
+ if( context )
+ {
+ halloc_t *parent = halloc_from_data( context );
+ al_push( &parent->children, &res->data );
+ }
+ return &res->data;
+}
+
+void halloc_free( void *context )
+{
+ halloc_t *me;
+ if( !context )
+ return;
+
+ me = halloc_from_data( context );
+ al_foreach( &me->children, (void (*)(const void *))&halloc_free );
+ al_destroy( &me->children );
+ free(me);
+}
+
+wchar_t *halloc_wcsdup( void *context, const wchar_t *in )
+{
+ size_t len=wcslen(in);
+ wchar_t *out = halloc( context, sizeof( wchar_t)*(len+1));
+ if( out == 0 )
+ {
+ die_mem();
+ }
+
+ memcpy( out, in, sizeof( wchar_t)*(len+1));
+ return out;
+}
+
+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;
+}