• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Similar core function to LGPL licensed talloc from Samba
3  */
4 
5 #ifndef _HIERALLOC_H_
6 #define _HIERALLOC_H_
7 
8 #include <stdio.h>
9 #include <stdarg.h>
10 
11 // allow __LINE__ to be stringified
12 #ifndef __location__
13 #define __HIERALLOC_STRING_0__(s)   #s
14 #define __HIERALLOC_STRING_1__(s)   __HIERALLOC_STRING_0__(s)
15 #define __HIERALLOC_STRING_2__      __HIERALLOC_STRING_1__(__LINE__)
16 #define __location__                __FILE__ ":" __HIERALLOC_STRING_2__
17 #endif
18 
19 #define hieralloc(ctx, type) (type *)hieralloc_allocate(ctx, sizeof(type), #type)
20 #define hieralloc_size(ctx, size) hieralloc_allocate(ctx, size, "sz:"__location__)
21 #define hieralloc_new(ctx) hieralloc_allocate(ctx, 0, "nw:" __location__)
22 #define hieralloc_zero(ctx, type) (type *)_hieralloc_zero(ctx, sizeof(type), "zr:"#type)
23 #define hieralloc_zero_size(ctx, size) _hieralloc_zero(ctx, size, "zrsz:"__location__)
24 #define hieralloc_array(ctx, type, count) (type *)hieralloc_allocate(ctx, sizeof(type) * count, "ar:"#type)
25 #define hieralloc_realloc(ctx, p, type, count) (type *)hieralloc_reallocate(ctx, p, sizeof(type) * count, "re:"#type)
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 // allocate memory and attach to parent context and siblings
31 void * hieralloc_allocate(const void * context, unsigned size, const char * name);
32 
33 // (re)allocate memory and attach to parent context and siblings
34 void * hieralloc_reallocate(const void * context, void * ptr, unsigned size, const char * name);
35 
36 // calls destructor if set, and frees children.
37 // if destructor returns -1, then do nothing and return -1.
38 int hieralloc_free(void * ptr);
39 
40 // creates 0 allocation to be used as parent context
41 void * hieralloc_init(const char * name);
42 
43 // returns global context
44 void * hieralloc_autofree_context();
45 
46 // sets destructor to be called before freeing; dctor return -1 aborts free
47 void hieralloc_set_destructor(const void * ptr, int (* destructor)(void *));
48 
49 // gets parent context of allocated memory
50 void * hieralloc_parent(const void * ptr);
51 
52 // moves allocation to new parent context; maintain children but update siblings
53 // returns ptr on success
54 void * hieralloc_steal(const void * new_ctx, const void * ptr);
55 
56 // not implemented from talloc_reference
57 void * hieralloc_reference(const void * ref_ctx, const void * ptr);
58 
59 // not implemented from talloc_unlink
60 int hieralloc_unlink(const void * ctx, void * ptr);
61 
62 // allocate and zero memory
63 void * _hieralloc_zero(const void * ctx, unsigned size, const char * name);
64 
65 // allocate and copy
66 char * hieralloc_strdup(const void * ctx, const char * str);
67 
68 // allocate and copy
69 char * hieralloc_strndup(const void * ctx, const char * str, unsigned len);
70 
71 // reallocate and append
72 char * hieralloc_strdup_append(char * str, const char * append);
73 
74 // reallocate and append
75 char * hieralloc_strndup_append(char * str, const char * append, unsigned len);
76 
77 // allocate and vsprintf
78 char * hieralloc_vasprintf(const void * ctx, const char * fmt, va_list va);
79 
80 // allocate and sprintf
81 char * hieralloc_asprintf(const void * ctx, const char * fmt, ...);
82 
83 // reallocate and append vsprintf
84 char * hieralloc_vasprintf_append(char * str, const char * fmt, va_list va);
85 
86 // reallocate and append sprintf
87 char * hieralloc_asprintf_append(char * str, const char * fmt, ...);
88 
89 // report self and child allocations
90 void hieralloc_report(const void * ptr, FILE * file);
91 
92 void hieralloc_report_brief(const void * ptr, FILE * file);
93 
94 void hieralloc_report_lineage(const void * ptr, FILE * file, int tab);
95 
96 int hieralloc_find(const void * top, const void * ptr, FILE * file, int tab);
97 
98 #ifdef __cplusplus
99 }
100 #endif
101 
102 #endif
103