summaryrefslogtreecommitdiff
path: root/source4/lib/talloc.c
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2004-09-26 01:41:04 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 12:59:15 -0500
commit38aa97445769c6f1c54d59731b5a0edef91e41b0 (patch)
tree0dfc9a9e23fe9259cc74df4f6493b2867e99494c /source4/lib/talloc.c
parentb295256ea2f71a4fe3b16e0348b907f6ef73167b (diff)
downloadsamba-38aa97445769c6f1c54d59731b5a0edef91e41b0.tar.gz
samba-38aa97445769c6f1c54d59731b5a0edef91e41b0.tar.bz2
samba-38aa97445769c6f1c54d59731b5a0edef91e41b0.zip
r2641: talloc_p() now produces a named talloc pointer, with the name
auto-derived from the type you are allocating. This is done with basically zero overhead by relying on the stringify operator in cpp producing string constants. the result is that --leak-check nicely names all pointers that come from talloc_p() (This used to be commit bd86ebe2972af4d424df20db1e422919aa6203d0)
Diffstat (limited to 'source4/lib/talloc.c')
-rw-r--r--source4/lib/talloc.c34
1 files changed, 31 insertions, 3 deletions
diff --git a/source4/lib/talloc.c b/source4/lib/talloc.c
index 114d0de43b..3e42fc68f8 100644
--- a/source4/lib/talloc.c
+++ b/source4/lib/talloc.c
@@ -39,7 +39,7 @@ struct talloc_chunk {
uint_t magic;
uint_t ref_count;
int (*destructor)(void *);
- char *name;
+ const char *name;
};
/* panic if we get a bad magic value */
@@ -133,7 +133,7 @@ static void talloc_set_name_v(void *ptr, const char *fmt, va_list ap) PRINTF_ATT
static void talloc_set_name_v(void *ptr, const char *fmt, va_list ap)
{
struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
- vasprintf(&tc->name, fmt, ap);
+ tc->name = talloc_vasprintf(ptr, fmt, ap);
}
/*
@@ -148,6 +148,16 @@ void talloc_set_name(void *ptr, const char *fmt, ...) _PRINTF_ATTRIBUTE(2,3)
}
/*
+ more efficient way to add a name to a pointer - the name must point to a
+ true string constant
+*/
+void talloc_set_name_const(void *ptr, const char *name)
+{
+ struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
+ tc->name = name;
+}
+
+/*
create a named talloc pointer. Any talloc pointer can be named, and
talloc_named() operates just like talloc() except that it allows you
to name the pointer.
@@ -171,6 +181,25 @@ void *talloc_named(void *context, size_t size,
}
/*
+ create a named talloc pointer. Any talloc pointer can be named, and
+ talloc_named() operates just like talloc() except that it allows you
+ to name the pointer.
+*/
+void *talloc_named_const(void *context, size_t size, const char *name)
+{
+ void *ptr;
+
+ ptr = talloc(context, size);
+ if (ptr == NULL) {
+ return NULL;
+ }
+
+ talloc_set_name_const(ptr, name);
+
+ return ptr;
+}
+
+/*
return the name of a talloc ptr, or "UNNAMED"
*/
const char *talloc_get_name(void *ptr)
@@ -250,7 +279,6 @@ int talloc_free(void *ptr)
}
tc->magic = TALLOC_MAGIC_FREE;
- if (tc->name) free(tc->name);
free(tc);
return 0;