Samba temporary memory allocation functions
Copyright (C) Andrew Tridgell 2004-2005
+ Copyright (C) Stefan Metzmacher 2006
** NOTE! The following LGPL license applies to the talloc
** library. This does NOT imply that all of Samba is released
#define TALLOC_DEPRECATED 0
#endif
+#ifndef PRINTF_ATTRIBUTE
+#if (__GNUC__ >= 3)
+/** Use gcc attribute to check printf fns. a1 is the 1-based index of
+ * the parameter containing the format, and a2 the index of the first
+ * argument. Note that some gcc 2.x versions don't handle this
+ * properly **/
+#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
+#else
+#define PRINTF_ATTRIBUTE(a1, a2)
+#endif
+#endif
+
+/* try to make talloc_set_destructor() and talloc_steal() type safe,
+ if we have a recent gcc */
+#if (__GNUC__ >= 3)
+#define _TALLOC_TYPEOF(ptr) __typeof__(ptr)
+#define talloc_set_destructor(ptr, function) \
+ do { \
+ int (*_talloc_destructor_fn)(_TALLOC_TYPEOF(ptr)) = (function); \
+ _talloc_set_destructor((ptr), (int (*)(void *))_talloc_destructor_fn); \
+ } while(0)
+/* this extremely strange macro is to avoid some braindamaged warning
+ stupidity in gcc 4.1.x */
+#define talloc_steal(ctx, ptr) ({ _TALLOC_TYPEOF(ptr) __talloc_steal_ret = (_TALLOC_TYPEOF(ptr))_talloc_steal((ctx),(ptr)); __talloc_steal_ret; })
+#define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference((ctx),(ptr))
+#else
+#define talloc_set_destructor(ptr, function) \
+ _talloc_set_destructor((ptr), (int (*)(void *))(function))
+#define _TALLOC_TYPEOF(ptr) void *
+#define talloc_steal(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_steal((ctx),(ptr))
+#define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference((ctx),(ptr))
+#endif
+
/* useful macros for creating type checked pointers */
#define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
#define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)
+#define talloc_ptrtype(ctx, ptr) (_TALLOC_TYPEOF(ptr))talloc_size(ctx, sizeof(*(ptr)))
#define talloc_new(ctx) talloc_named_const(ctx, 0, "talloc_new: " __location__)
#define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type)
#define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
#define talloc_array_size(ctx, size, count) _talloc_array(ctx, size, count, __location__)
+#define talloc_array_ptrtype(ctx, ptr, count) (_TALLOC_TYPEOF(ptr))talloc_array_size(ctx, sizeof(*(ptr)), count)
#define talloc_realloc(ctx, p, type, count) (type *)_talloc_realloc_array(ctx, p, sizeof(type), count, #type)
#define talloc_realloc_size(ctx, ptr, size) _talloc_realloc(ctx, ptr, size, __location__)
#define talloc_memdup(t, p, size) _talloc_memdup(t, p, size, __location__)
-#define malloc_p(type) (type *)malloc(sizeof(type))
-#define malloc_array_p(type, count) (type *)realloc_array(NULL, sizeof(type), count)
-#define realloc_p(p, type, count) (type *)realloc_array(p, sizeof(type), count)
-
-#if 0
-/* Not correct for Samba3. */
-#define data_blob(ptr, size) data_blob_named(ptr, size, "DATA_BLOB: "__location__)
-#define data_blob_talloc(ctx, ptr, size) data_blob_talloc_named(ctx, ptr, size, "DATA_BLOB: "__location__)
-#define data_blob_dup_talloc(ctx, blob) data_blob_talloc_named(ctx, (blob)->data, (blob)->length, "DATA_BLOB: "__location__)
-#endif
-
#define talloc_set_type(ptr, type) talloc_set_name_const(ptr, #type)
#define talloc_get_type(ptr, type) (type *)talloc_check_name(ptr, #type)
#define talloc_find_parent_bytype(ptr, type) (type *)talloc_find_parent_byname(ptr, #type)
-
#if TALLOC_DEPRECATED
#define talloc_zero_p(ctx, type) talloc_zero(ctx, type)
#define talloc_p(ctx, type) talloc(ctx, type)
#define talloc_destroy(ctx) talloc_free(ctx)
#endif
-#ifndef PRINTF_ATTRIBUTE
-#if (__GNUC__ >= 3)
-/** Use gcc attribute to check printf fns. a1 is the 1-based index of
- * the parameter containing the format, and a2 the index of the first
- * argument. Note that some gcc 2.x versions don't handle this
- * properly **/
-#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
-#else
-#define PRINTF_ATTRIBUTE(a1, a2)
-#endif
-#endif
-
-
/* The following definitions come from talloc.c */
void *_talloc(const void *context, size_t size);
-void talloc_set_destructor(const void *ptr, int (*destructor)(void *));
-void talloc_increase_ref_count(const void *ptr);
-void *talloc_reference(const void *context, const void *ptr);
+void _talloc_set_destructor(const void *ptr, int (*destructor)(void *));
+int talloc_increase_ref_count(const void *ptr);
+size_t talloc_reference_count(const void *ptr);
+void *_talloc_reference(const void *context, const void *ptr);
int talloc_unlink(const void *context, void *ptr);
-void talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
+const char *talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
void talloc_set_name_const(const void *ptr, const char *name);
void *talloc_named(const void *context, size_t size,
const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
void *talloc_named_const(const void *context, size_t size, const char *name);
const char *talloc_get_name(const void *ptr);
void *talloc_check_name(const void *ptr, const char *name);
-void talloc_report_depth(const void *ptr, FILE *f, int depth);
void *talloc_parent(const void *ptr);
void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
int talloc_free(void *ptr);
+void talloc_free_children(void *ptr);
void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name);
-/* void *talloc_steal(const void *new_ctx, const void *ptr); */
-off_t talloc_total_size(const void *ptr);
-off_t talloc_total_blocks(const void *ptr);
+void *_talloc_steal(const void *new_ctx, const void *ptr);
+size_t talloc_total_size(const void *ptr);
+size_t talloc_total_blocks(const void *ptr);
+void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
+ void (*callback)(const void *ptr,
+ int depth, int max_depth,
+ int is_ref,
+ void *private_data),
+ void *private_data);
+void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f);
void talloc_report_full(const void *ptr, FILE *f);
void talloc_report(const void *ptr, FILE *f);
void talloc_enable_null_tracking(void);
+void talloc_disable_null_tracking(void);
void talloc_enable_leak_report(void);
void talloc_enable_leak_report_full(void);
void *_talloc_zero(const void *ctx, size_t size, const char *name);
char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
-char *talloc_asprintf_append(char *s,
- const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
+char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name);
void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name);
void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name);
size_t talloc_get_size(const void *ctx);
void *talloc_find_parent_byname(const void *ctx, const char *name);
void talloc_show_parents(const void *context, FILE *file);
-int talloc_is_parent(const void *context, const char *ptr);
+int talloc_is_parent(const void *context, const void *ptr);
#endif
-/*
+/*
Samba Unix SMB/CIFS implementation.
Samba trivial allocation library - new interface
NOTE: Please read talloc_guide.txt for full documentation
Copyright (C) Andrew Tridgell 2004
-
+ Copyright (C) Stefan Metzmacher 2006
+
** NOTE! The following LGPL license applies to the talloc
** library. This does NOT imply that all of Samba is released
** under the LGPL
-
+
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
*/
#ifdef _SAMBA_BUILD_
+#include "version.h"
+#if (SAMBA_VERSION_MAJOR<4)
#include "includes.h"
-#if ((SAMBA_VERSION_MAJOR==3)&&(SAMBA_VERSION_MINOR<9))
/* This is to circumvent SAMBA3's paranoid malloc checker. Here in this file
* we trust ourselves... */
#ifdef malloc
#ifdef realloc
#undef realloc
#endif
-#endif
-#else
+#define _TALLOC_SAMBA3
+#endif /* (SAMBA_VERSION_MAJOR<4) */
+#endif /* _SAMBA_BUILD_ */
+
+#ifndef _TALLOC_SAMBA3
+#include "config.h"
+
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <stdarg.h>
+#include <errno.h>
+#ifdef HAVE_STDINT_H
#include <stdint.h>
-#include "talloc.h"
-/* assume a modern system */
-#define HAVE_VA_COPY
#endif
+#if defined(HAVE_STDARG_H)
+#include <stdarg.h>
+#elif defined (HAVE_VARARGS_H)
+#include <varargs.h>
+#else
+#error "no var arg header"
+#endif
+
+#include "talloc.h"
+#endif /* not _TALLOC_SAMBA3 */
+
/* use this to force every realloc to change the pointer, to stress test
code that might not cope */
#define ALWAYS_REALLOC 0
talloc_enable_leak_report_full() is called, otherwise it remains
NULL
*/
-static const void *null_context;
+static void *null_context;
static void *cleanup_context;
-static void *talloc_steal(const void *new_ctx, const void *ptr);
-
struct talloc_reference_handle {
struct talloc_reference_handle *next, *prev;
void *ptr;
if the destructor fails then the free is failed, and the memory can
be continued to be used
*/
-void talloc_set_destructor(const void *ptr, int (*destructor)(void *))
+void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
{
struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
tc->destructor = destructor;
/*
increase the reference count on a piece of memory.
*/
-void talloc_increase_ref_count(const void *ptr)
+int talloc_increase_ref_count(const void *ptr)
{
- talloc_reference(null_context, ptr);
+ if (!talloc_reference(null_context, ptr)) {
+ return -1;
+ }
+ return 0;
}
/*
helper for talloc_reference()
*/
-static int talloc_reference_destructor(void *ptr)
+static int talloc_reference_destructor(struct talloc_reference_handle *handle)
{
- struct talloc_reference_handle *handle =
- (struct talloc_reference_handle *)ptr;
- struct talloc_chunk *tc1 = talloc_chunk_from_ptr(ptr);
- struct talloc_chunk *tc2 = talloc_chunk_from_ptr(handle->ptr);
- if (tc1->destructor != (talloc_destructor_t)-1) {
- tc1->destructor = NULL;
- }
- _TLIST_REMOVE(tc2->refs, handle);
- talloc_free(handle);
+ struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
+ _TLIST_REMOVE(ptr_tc->refs, handle);
return 0;
}
same underlying data, and you want to be able to free the two instances separately,
and in either order
*/
-void *talloc_reference(const void *context, const void *ptr)
+void *_talloc_reference(const void *context, const void *ptr)
{
struct talloc_chunk *tc;
struct talloc_reference_handle *handle;
if (ptr == NULL) return NULL;
tc = talloc_chunk_from_ptr(ptr);
- handle = (struct talloc_reference_handle *)talloc_named_const(
- context, sizeof(*handle), TALLOC_MAGIC_REFERENCE);
-
+ handle = (struct talloc_reference_handle *)talloc_named_const(context,
+ sizeof(struct talloc_reference_handle),
+ TALLOC_MAGIC_REFERENCE);
if (handle == NULL) return NULL;
/* note that we hang the destructor off the handle, not the
return -1;
}
- talloc_set_destructor(h, NULL);
- _TLIST_REMOVE(tc->refs, h);
- talloc_free(h);
- return 0;
+ return talloc_free(h);
}
/*
/*
add a name to an existing pointer - va_list version
*/
-static void talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
+static const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
-static void talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
+static const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
{
struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
tc->name = talloc_vasprintf(ptr, fmt, ap);
if (tc->name) {
talloc_set_name_const(tc->name, ".name");
}
+ return tc->name;
}
/*
add a name to an existing pointer
*/
-void talloc_set_name(const void *ptr, const char *fmt, ...)
+const char *talloc_set_name(const void *ptr, const char *fmt, ...)
{
+ const char *name;
va_list ap;
va_start(ap, fmt);
- talloc_set_name_v(ptr, fmt, ap);
+ name = talloc_set_name_v(ptr, fmt, ap);
va_end(ap);
+ return name;
}
/*
{
va_list ap;
void *ptr;
+ const char *name;
ptr = _talloc(context, size);
if (ptr == NULL) return NULL;
va_start(ap, fmt);
- talloc_set_name_v(ptr, fmt, ap);
+ name = talloc_set_name_v(ptr, fmt, ap);
va_end(ap);
+ if (name == NULL) {
+ talloc_free(ptr);
+ return NULL;
+ }
+
return ptr;
}
{
va_list ap;
void *ptr;
-
- talloc_enable_null_tracking();
+ const char *name;
ptr = _talloc(NULL, 0);
if (ptr == NULL) return NULL;
va_start(ap, fmt);
- talloc_set_name_v(ptr, fmt, ap);
+ name = talloc_set_name_v(ptr, fmt, ap);
va_end(ap);
+ if (name == NULL) {
+ talloc_free(ptr);
+ return NULL;
+ }
+
return ptr;
}
int talloc_free(void *ptr)
{
struct talloc_chunk *tc;
+ int old_errno;
if (ptr == NULL) {
return -1;
if (tc->refs) {
int is_child;
- struct talloc_reference_handle *handle = tc->refs;
- is_child = talloc_is_parent(handle, (const char *)handle->ptr);
- talloc_reference_destructor(tc->refs);
+ /* check this is a reference from a child or grantchild
+ * back to it's parent or grantparent
+ *
+ * in that case we need to remove the reference and
+ * call another instance of talloc_free() on the current
+ * pointer.
+ */
+ is_child = talloc_is_parent(tc->refs, ptr);
+ talloc_free(tc->refs);
if (is_child) {
return talloc_free(ptr);
}
tc->destructor = NULL;
}
- tc->flags |= TALLOC_FLAG_LOOP;
-
- talloc_free_children(ptr);
-
if (tc->parent) {
_TLIST_REMOVE(tc->parent->child, tc);
if (tc->parent->child) {
if (tc->next) tc->next->prev = tc->prev;
}
- tc->flags |= TALLOC_FLAG_FREE;
+ tc->flags |= TALLOC_FLAG_LOOP;
+ talloc_free_children(ptr);
+ tc->flags |= TALLOC_FLAG_FREE;
+ old_errno = errno;
free(tc);
+ errno = old_errno;
return 0;
}
tc = (struct talloc_chunk *)new_ptr;
tc->flags &= ~TALLOC_FLAG_FREE;
if (tc->parent) {
- tc->parent->child = (struct talloc_chunk *)new_ptr;
+ tc->parent->child = tc;
}
if (tc->child) {
- tc->child->parent = (struct talloc_chunk *)new_ptr;
+ tc->child->parent = tc;
}
if (tc->prev) {
ptr on success, or NULL if it could not be transferred.
passing NULL as ptr will always return NULL with no side effects.
*/
-static void *talloc_steal(const void *new_ctx, const void *ptr)
+void *_talloc_steal(const void *new_ctx, const void *ptr)
{
struct talloc_chunk *tc, *new_tc;
/*
return the total size of a talloc pool (subtree)
*/
-off_t talloc_total_size(const void *ptr)
+size_t talloc_total_size(const void *ptr)
{
- off_t total = 0;
+ size_t total = 0;
struct talloc_chunk *c, *tc;
-
+
if (ptr == NULL) {
ptr = null_context;
}
/*
return the total number of blocks in a talloc pool (subtree)
*/
-off_t talloc_total_blocks(const void *ptr)
+size_t talloc_total_blocks(const void *ptr)
{
- off_t total = 0;
+ size_t total = 0;
struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
if (tc->flags & TALLOC_FLAG_LOOP) {
/*
return the number of external references to a pointer
*/
-static int talloc_reference_count(const void *ptr)
+size_t talloc_reference_count(const void *ptr)
{
struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
struct talloc_reference_handle *h;
- int ret = 0;
+ size_t ret = 0;
for (h=tc->refs;h;h=h->next) {
ret++;
/*
report on memory usage by all children of a pointer, giving a full tree view
*/
-void talloc_report_depth(const void *ptr, FILE *f, int depth)
+void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
+ void (*callback)(const void *ptr,
+ int depth, int max_depth,
+ int is_ref,
+ void *private_data),
+ void *private_data)
{
- struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
+ struct talloc_chunk *c, *tc;
+
+ if (ptr == NULL) {
+ ptr = null_context;
+ }
+ if (ptr == NULL) return;
+
+ tc = talloc_chunk_from_ptr(ptr);
if (tc->flags & TALLOC_FLAG_LOOP) {
return;
}
- tc->flags |= TALLOC_FLAG_LOOP;
+ callback(ptr, depth, max_depth, 0, private_data);
+
+ if (max_depth >= 0 && depth >= max_depth) {
+ return;
+ }
+ tc->flags |= TALLOC_FLAG_LOOP;
for (c=tc->child;c;c=c->next) {
if (c->name == TALLOC_MAGIC_REFERENCE) {
- struct talloc_reference_handle *handle =
- (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
- const char *name2 = talloc_get_name(handle->ptr);
- fprintf(f, "%*sreference to: %s\n", depth*4, "", name2);
+ struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
+ callback(h->ptr, depth + 1, max_depth, 1, private_data);
} else {
- const char *name = talloc_get_name(TC_PTR_FROM_CHUNK(c));
- fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d)\n",
- depth*4, "",
- name,
- (unsigned long)talloc_total_size(TC_PTR_FROM_CHUNK(c)),
- (unsigned long)talloc_total_blocks(TC_PTR_FROM_CHUNK(c)),
- talloc_reference_count(TC_PTR_FROM_CHUNK(c)));
- talloc_report_depth(TC_PTR_FROM_CHUNK(c), f, depth+1);
+ talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
}
}
tc->flags &= ~TALLOC_FLAG_LOOP;
}
-/*
- report on memory usage by all children of a pointer, giving a full tree view
-*/
-void talloc_report_full(const void *ptr, FILE *f)
+static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
{
- if (ptr == NULL) {
- ptr = null_context;
+ const char *name = talloc_get_name(ptr);
+ FILE *f = (FILE *)_f;
+
+ if (is_ref) {
+ fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
+ return;
+ }
+
+ if (depth == 0) {
+ fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
+ (max_depth < 0 ? "full " :""), name,
+ (unsigned long)talloc_total_size(ptr),
+ (unsigned long)talloc_total_blocks(ptr));
+ return;
}
- if (ptr == NULL) return;
- fprintf(f,"full talloc report on '%s' (total %lu bytes in %lu blocks)\n",
- talloc_get_name(ptr),
+ fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d)\n",
+ depth*4, "",
+ name,
(unsigned long)talloc_total_size(ptr),
- (unsigned long)talloc_total_blocks(ptr));
+ (unsigned long)talloc_total_blocks(ptr),
+ talloc_reference_count(ptr));
+}
- talloc_report_depth(ptr, f, 1);
+/*
+ report on memory usage by all children of a pointer, giving a full tree view
+*/
+void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
+{
+ talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
fflush(f);
}
+/*
+ report on memory usage by all children of a pointer, giving a full tree view
+*/
+void talloc_report_full(const void *ptr, FILE *f)
+{
+ talloc_report_depth_file(ptr, 0, -1, f);
+}
+
/*
report on memory usage by all children of a pointer
*/
void talloc_report(const void *ptr, FILE *f)
{
- struct talloc_chunk *c, *tc;
-
- if (ptr == NULL) {
- ptr = null_context;
- }
- if (ptr == NULL) return;
-
- fprintf(f,"talloc report on '%s' (total %lu bytes in %lu blocks)\n",
- talloc_get_name(ptr),
- (unsigned long)talloc_total_size(ptr),
- (unsigned long)talloc_total_blocks(ptr));
-
- tc = talloc_chunk_from_ptr(ptr);
-
- for (c=tc->child;c;c=c->next) {
- fprintf(f, "\t%-30s contains %6lu bytes in %3lu blocks\n",
- talloc_get_name(TC_PTR_FROM_CHUNK(c)),
- (unsigned long)talloc_total_size(TC_PTR_FROM_CHUNK(c)),
- (unsigned long)talloc_total_blocks(TC_PTR_FROM_CHUNK(c)));
- }
- fflush(f);
+ talloc_report_depth_file(ptr, 0, 1, f);
}
/*
}
}
-/*
- free allocated global memory
-*/
-
-void talloc_nc_free(void)
-{
- if ( null_context )
- talloc_free( (void*)null_context );
-}
-
/*
enable tracking of the NULL context
*/
}
}
-#ifdef _SAMBA_BUILD_
-/* Ugly calls to Samba-specific sprintf_append... JRA. */
-
/*
- report on memory usage by all children of a pointer, giving a full tree view
+ disable tracking of the NULL context
*/
-static void talloc_report_depth_str(const void *ptr, char **pps, ssize_t *plen, size_t *pbuflen, int depth)
+void talloc_disable_null_tracking(void)
{
- struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
-
- if (tc->flags & TALLOC_FLAG_LOOP) {
- return;
- }
-
- tc->flags |= TALLOC_FLAG_LOOP;
-
- for (c=tc->child;c;c=c->next) {
- if (c->name == TALLOC_MAGIC_REFERENCE) {
- struct talloc_reference_handle *handle =
- (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
- const char *name2 = talloc_get_name(handle->ptr);
-
- sprintf_append(NULL, pps, plen, pbuflen,
- "%*sreference to: %s\n", depth*4, "", name2);
-
- } else {
- const char *name = talloc_get_name(TC_PTR_FROM_CHUNK(c));
-
- sprintf_append(NULL, pps, plen, pbuflen,
- "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d)\n",
- depth*4, "",
- name,
- (unsigned long)talloc_total_size(TC_PTR_FROM_CHUNK(c)),
- (unsigned long)talloc_total_blocks(TC_PTR_FROM_CHUNK(c)),
- talloc_reference_count(TC_PTR_FROM_CHUNK(c)));
-
- talloc_report_depth_str(TC_PTR_FROM_CHUNK(c), pps, plen, pbuflen, depth+1);
- }
- }
- tc->flags &= ~TALLOC_FLAG_LOOP;
+ talloc_free(null_context);
+ null_context = NULL;
}
-/*
- report on memory usage by all children of a pointer
-*/
-char *talloc_describe_all(void)
-{
- ssize_t len = 0;
- size_t buflen = 512;
- char *s = NULL;
-
- if (null_context == NULL) {
- return NULL;
- }
-
- sprintf_append(NULL, &s, &len, &buflen,
- "full talloc report on '%s' (total %lu bytes in %lu blocks)\n",
- talloc_get_name(null_context),
- (unsigned long)talloc_total_size(null_context),
- (unsigned long)talloc_total_blocks(null_context));
-
- if (!s) {
- return NULL;
- }
- talloc_report_depth_str(null_context, &s, &len, &buflen, 1);
- return s;
-}
-#endif
-
/*
enable leak reporting on exit
*/
return ret;
}
-#ifndef VA_COPY
-#ifdef HAVE_VA_COPY
-#define VA_COPY(dest, src) va_copy(dest, src)
-#elif defined(HAVE___VA_COPY)
-#define VA_COPY(dest, src) __va_copy(dest, src)
+#ifndef HAVE_VA_COPY
+#ifdef HAVE___VA_COPY
+#define va_copy(dest, src) __va_copy(dest, src)
#else
-#define VA_COPY(dest, src) (dest) = (src)
+#define va_copy(dest, src) (dest) = (src)
#endif
#endif
va_list ap2;
char c;
- VA_COPY(ap2, ap);
+ va_copy(ap2, ap);
/* this call looks strange, but it makes it work on older solaris boxes */
if ((len = vsnprintf(&c, 1, fmt, ap2)) < 0) {
ret = (char *)_talloc(t, len+1);
if (ret) {
- VA_COPY(ap2, ap);
+ va_copy(ap2, ap);
vsnprintf(ret, len+1, fmt, ap2);
talloc_set_name_const(ret, ret);
}
* and return @p s, which may have moved. Good for gradually
* accumulating output into a string buffer.
**/
-
char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
{
struct talloc_chunk *tc;
int len, s_len;
va_list ap2;
+ char c;
if (s == NULL) {
return talloc_vasprintf(NULL, fmt, ap);
tc = talloc_chunk_from_ptr(s);
- VA_COPY(ap2, ap);
+ va_copy(ap2, ap);
s_len = tc->size - 1;
- if ((len = vsnprintf(NULL, 0, fmt, ap2)) <= 0) {
+ if ((len = vsnprintf(&c, 1, fmt, ap2)) <= 0) {
/* Either the vsnprintf failed or the format resulted in
* no characters being formatted. In the former case, we
* ought to return NULL, in the latter we ought to return
s = talloc_realloc(NULL, s, char, s_len + len+1);
if (!s) return NULL;
- VA_COPY(ap2, ap);
+ va_copy(ap2, ap);
vsnprintf(s+s_len, len+1, fmt, ap2);
talloc_set_name_const(s, s);
if (tc) {
tc = tc->parent;
}
-
}
return NULL;
}
/*
return 1 if ptr is a parent of context
*/
-int talloc_is_parent(const void *context, const char *ptr)
+int talloc_is_parent(const void *context, const void *ptr)
{
struct talloc_chunk *tc;