00001 #ifndef _TALLOC_H_
00002 #define _TALLOC_H_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <stdlib.h>
00029 #include <stdio.h>
00030 #include <stdarg.h>
00031
00032 #ifdef __cplusplus
00033 extern "C" {
00034 #endif
00035
00045 #define TALLOC_VERSION_MAJOR 2
00046 #define TALLOC_VERSION_MINOR 0
00047
00048 int talloc_version_major(void);
00049 int talloc_version_minor(void);
00050
00074 typedef void TALLOC_CTX;
00075
00076
00077
00078
00079 #ifndef __location__
00080 #define __TALLOC_STRING_LINE1__(s) #s
00081 #define __TALLOC_STRING_LINE2__(s) __TALLOC_STRING_LINE1__(s)
00082 #define __TALLOC_STRING_LINE3__ __TALLOC_STRING_LINE2__(__LINE__)
00083 #define __location__ __FILE__ ":" __TALLOC_STRING_LINE3__
00084 #endif
00085
00086 #ifndef TALLOC_DEPRECATED
00087 #define TALLOC_DEPRECATED 0
00088 #endif
00089
00090 #ifndef PRINTF_ATTRIBUTE
00091 #if (__GNUC__ >= 3)
00092
00096 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
00097 #else
00098 #define PRINTF_ATTRIBUTE(a1, a2)
00099 #endif
00100 #endif
00101
00102 #ifdef DOXYGEN
00103
00136 void *talloc(const void *ctx, #type);
00137 #else
00138 #define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
00139 void *_talloc(const void *context, size_t size);
00140 #endif
00141
00159 void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
00160
00161 #ifdef DOXYGEN
00162
00224 int talloc_free(void *ptr);
00225 #else
00226 #define talloc_free(ctx) _talloc_free(ctx, __location__)
00227 int _talloc_free(void *ptr, const char *location);
00228 #endif
00229
00241 void talloc_free_children(void *ptr);
00242
00243 #ifdef DOXYGEN
00244
00297 void talloc_set_destructor(const void *ptr, int (*destructor)(void *));
00298
00337 void *talloc_steal(const void *new_ctx, const void *ptr);
00338 #else
00339
00340
00341 #if (__GNUC__ >= 3)
00342 #define _TALLOC_TYPEOF(ptr) __typeof__(ptr)
00343 #define talloc_set_destructor(ptr, function) \
00344 do { \
00345 int (*_talloc_destructor_fn)(_TALLOC_TYPEOF(ptr)) = (function); \
00346 _talloc_set_destructor((ptr), (int (*)(void *))_talloc_destructor_fn); \
00347 } while(0)
00348
00349
00350 #define talloc_steal(ctx, ptr) ({ _TALLOC_TYPEOF(ptr) __talloc_steal_ret = (_TALLOC_TYPEOF(ptr))_talloc_steal_loc((ctx),(ptr), __location__); __talloc_steal_ret; })
00351 #else
00352 #define talloc_set_destructor(ptr, function) \
00353 _talloc_set_destructor((ptr), (int (*)(void *))(function))
00354 #define _TALLOC_TYPEOF(ptr) void *
00355 #define talloc_steal(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_steal_loc((ctx),(ptr), __location__)
00356 #endif
00357 void _talloc_set_destructor(const void *ptr, int (*_destructor)(void *));
00358 void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location);
00359 #endif
00360
00391 const char *talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
00392
00393 #ifdef DOXYGEN
00394
00413 void *talloc_move(const void *new_ctx, void **pptr);
00414 #else
00415 #define talloc_move(ctx, pptr) (_TALLOC_TYPEOF(*(pptr)))_talloc_move((ctx),(void *)(pptr))
00416 void *_talloc_move(const void *new_ctx, const void *pptr);
00417 #endif
00418
00435 void talloc_set_name_const(const void *ptr, const char *name);
00436
00460 void *talloc_named(const void *context, size_t size,
00461 const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
00462
00481 void *talloc_named_const(const void *context, size_t size, const char *name);
00482
00483 #ifdef DOXYGEN
00484
00504 void *talloc_size(const void *ctx, size_t size);
00505 #else
00506 #define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)
00507 #endif
00508
00509 #ifdef DOXYGEN
00510
00531 void *talloc_ptrtype(const void *ctx, #type);
00532 #else
00533 #define talloc_ptrtype(ctx, ptr) (_TALLOC_TYPEOF(ptr))talloc_size(ctx, sizeof(*(ptr)))
00534 #endif
00535
00536 #ifdef DOXYGEN
00537
00549 void *talloc_new(const void *ctx);
00550 #else
00551 #define talloc_new(ctx) talloc_named_const(ctx, 0, "talloc_new: " __location__)
00552 #endif
00553
00554 #ifdef DOXYGEN
00555
00583 void *talloc_zero(const void *ctx, #type);
00584
00594 void *talloc_zero_size(const void *ctx, size_t size);
00595 #else
00596 #define talloc_zero(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type)
00597 #define talloc_zero_size(ctx, size) _talloc_zero(ctx, size, __location__)
00598 void *_talloc_zero(const void *ctx, size_t size, const char *name);
00599 #endif
00600
00610 const char *talloc_get_name(const void *ptr);
00611
00624 void *talloc_check_name(const void *ptr, const char *name);
00625
00633 void *talloc_parent(const void *ptr);
00634
00642 const char *talloc_parent_name(const void *ptr);
00643
00658 size_t talloc_total_size(const void *ptr);
00659
00675 size_t talloc_total_blocks(const void *ptr);
00676
00677 #ifdef DOXYGEN
00678
00698 void *talloc_memdup(const void *t, const void *p, size_t size);
00699 #else
00700 #define talloc_memdup(t, p, size) _talloc_memdup(t, p, size, __location__)
00701 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name);
00702 #endif
00703
00704 #ifdef DOXYGEN
00705
00722 void talloc_set_type(const char *ptr, #type);
00723
00741 type *talloc_get_type(const void *ptr, #type);
00742 #else
00743 #define talloc_set_type(ptr, type) talloc_set_name_const(ptr, #type)
00744 #define talloc_get_type(ptr, type) (type *)talloc_check_name(ptr, #type)
00745 #endif
00746
00747 #ifdef DOXYGEN
00748
00762 void *talloc_get_type_abort(const void *ptr, #type);
00763 #else
00764 #ifdef TALLOC_GET_TYPE_ABORT_NOOP
00765 #define talloc_get_type_abort(ptr, type) (type *)(ptr)
00766 #else
00767 #define talloc_get_type_abort(ptr, type) (type *)_talloc_get_type_abort(ptr, #type, __location__)
00768 #endif
00769 void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location);
00770 #endif
00771
00787 void *talloc_find_parent_byname(const void *ctx, const char *name);
00788
00789 #ifdef DOXYGEN
00790
00807 void *talloc_find_parent_bytype(const void *ptr, #type);
00808 #else
00809 #define talloc_find_parent_bytype(ptr, type) (type *)talloc_find_parent_byname(ptr, #type)
00810 #endif
00811
00852 void *talloc_pool(const void *context, size_t size);
00853
00854 #ifdef DOXYGEN
00855
00874 void *talloc_pooled_object(const void *ctx, #type,
00875 unsigned num_subobjects,
00876 size_t total_subobjects_size);
00877 #else
00878 #define talloc_pooled_object(_ctx, _type, \
00879 _num_subobjects, \
00880 _total_subobjects_size) \
00881 (_type *)_talloc_pooled_object((_ctx), sizeof(_type), #_type, \
00882 (_num_subobjects), \
00883 (_total_subobjects_size))
00884 void *_talloc_pooled_object(const void *ctx,
00885 size_t type_size,
00886 const char *type_name,
00887 unsigned num_subobjects,
00888 size_t total_subobjects_size);
00889 #endif
00890
00900 #define TALLOC_FREE(ctx) do { if (ctx != NULL) { talloc_free(ctx); ctx=NULL; } } while(0)
00901
00902
00903
00929 int talloc_increase_ref_count(const void *ptr);
00930
00938 size_t talloc_reference_count(const void *ptr);
00939
00940 #ifdef DOXYGEN
00941
00984 void *talloc_reference(const void *ctx, const void *ptr);
00985 #else
00986 #define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference_loc((ctx),(ptr), __location__)
00987 void *_talloc_reference_loc(const void *context, const void *ptr, const char *location);
00988 #endif
00989
01027 int talloc_unlink(const void *context, void *ptr);
01028
01047 void *talloc_autofree_context(void);
01048
01060 size_t talloc_get_size(const void *ctx);
01061
01069 void talloc_show_parents(const void *context, FILE *file);
01070
01082 int talloc_is_parent(const void *context, const void *ptr);
01083
01102 void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr);
01103
01104
01105
01115 #ifdef DOXYGEN
01116
01147 void *talloc_array(const void *ctx, #type, unsigned count);
01148 #else
01149 #define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
01150 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name);
01151 #endif
01152
01153 #ifdef DOXYGEN
01154
01165 void *talloc_array_size(const void *ctx, size_t size, unsigned count);
01166 #else
01167 #define talloc_array_size(ctx, size, count) _talloc_array(ctx, size, count, __location__)
01168 #endif
01169
01170 #ifdef DOXYGEN
01171
01189 void *talloc_array_ptrtype(const void *ctx, const void *ptr, unsigned count);
01190 #else
01191 #define talloc_array_ptrtype(ctx, ptr, count) (_TALLOC_TYPEOF(ptr))talloc_array_size(ctx, sizeof(*(ptr)), count)
01192 #endif
01193
01194 #ifdef DOXYGEN
01195
01205 size_t talloc_array_length(const void *ctx);
01206 #else
01207 #define talloc_array_length(ctx) (talloc_get_size(ctx)/sizeof(*ctx))
01208 #endif
01209
01210 #ifdef DOXYGEN
01211
01229 void *talloc_zero_array(const void *ctx, #type, unsigned count);
01230 #else
01231 #define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type)
01232 void *_talloc_zero_array(const void *ctx,
01233 size_t el_size,
01234 unsigned count,
01235 const char *name);
01236 #endif
01237
01238 #ifdef DOXYGEN
01239
01269 void *talloc_realloc(const void *ctx, void *ptr, #type, size_t count);
01270 #else
01271 #define talloc_realloc(ctx, p, type, count) (type *)_talloc_realloc_array(ctx, p, sizeof(type), count, #type)
01272 void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name);
01273 #endif
01274
01275 #ifdef DOXYGEN
01276
01290 void *talloc_realloc_size(const void *ctx, void *ptr, size_t size);
01291 #else
01292 #define talloc_realloc_size(ctx, ptr, size) _talloc_realloc(ctx, ptr, size, __location__)
01293 void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name);
01294 #endif
01295
01313 void *talloc_realloc_fn(const void *context, void *ptr, size_t size);
01314
01315
01316
01348 char *talloc_strdup(const void *t, const char *p);
01349
01374 char *talloc_strdup_append(char *s, const char *a);
01375
01410 char *talloc_strdup_append_buffer(char *s, const char *a);
01411
01432 char *talloc_strndup(const void *t, const char *p, size_t n);
01433
01461 char *talloc_strndup_append(char *s, const char *a, size_t n);
01462
01500 char *talloc_strndup_append_buffer(char *s, const char *a, size_t n);
01501
01523 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
01524
01539 char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
01540
01555 char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
01556
01577 char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
01578
01603 char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
01604
01640 char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
01641
01642
01643
01680 void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
01681 void (*callback)(const void *ptr,
01682 int depth, int max_depth,
01683 int is_ref,
01684 void *private_data),
01685 void *private_data);
01686
01701 void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f);
01702
01731 void talloc_report_full(const void *ptr, FILE *f);
01732
01759 void talloc_report(const void *ptr, FILE *f);
01760
01768 void talloc_enable_null_tracking(void);
01769
01777 void talloc_enable_null_tracking_no_autofree(void);
01778
01784 void talloc_disable_null_tracking(void);
01785
01812 void talloc_enable_leak_report(void);
01813
01840 void talloc_enable_leak_report_full(void);
01841
01873 void talloc_set_abort_fn(void (*abort_fn)(const char *reason));
01874
01883 void talloc_set_log_fn(void (*log_fn)(const char *message));
01884
01891 void talloc_set_log_stderr(void);
01892
01910 int talloc_set_memlimit(const void *ctx, size_t max_size);
01911
01912
01913
01914 #if TALLOC_DEPRECATED
01915 #define talloc_zero_p(ctx, type) talloc_zero(ctx, type)
01916 #define talloc_p(ctx, type) talloc(ctx, type)
01917 #define talloc_array_p(ctx, type, count) talloc_array(ctx, type, count)
01918 #define talloc_realloc_p(ctx, p, type, count) talloc_realloc(ctx, p, type, count)
01919 #define talloc_destroy(ctx) talloc_free(ctx)
01920 #define talloc_append_string(c, s, a) (s?talloc_strdup_append(s,a):talloc_strdup(c, a))
01921 #endif
01922
01923 #ifndef TALLOC_MAX_DEPTH
01924 #define TALLOC_MAX_DEPTH 10000
01925 #endif
01926
01927 #ifdef __cplusplus
01928 }
01929 #endif
01930
01931 #endif