summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Pool <mbp@samba.org>2001-12-19 04:50:10 +0000
committerMartin Pool <mbp@samba.org>2001-12-19 04:50:10 +0000
commit25b97a743573b8c5e0ac886e4bfab581c11b3714 (patch)
tree534877752134559b969fa559c71630861c9942f5
parente3390669958f01672f5638ebf975e8bd4fd67665 (diff)
downloadsamba-25b97a743573b8c5e0ac886e4bfab581c11b3714.tar.gz
Start adding some debugging features to talloc based on Samba's
ancient mem_man.c: Each TALLOC_CTX now has a field to store its purpose, to aid in tracking down memory bloat. A new call talloc_init_named() should be used instead of talloc_init() so that this is set. Added talloc_vasprintf to be called by varargs functions.
-rw-r--r--source/include/talloc.h10
-rw-r--r--source/lib/talloc.c45
2 files changed, 48 insertions, 7 deletions
diff --git a/source/include/talloc.h b/source/include/talloc.h
index 89c2f82e056..8e20cc028ad 100644
--- a/source/include/talloc.h
+++ b/source/include/talloc.h
@@ -5,6 +5,7 @@
Version 3.0
Samba temporary memory allocation functions
Copyright (C) Andrew Tridgell 2000
+ Copyright (C) 2001 by Martin Pool <mbp@samba.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -27,9 +28,18 @@ struct talloc_chunk {
void *ptr;
};
+
+/**
+ * talloc allocation pool. All allocated blocks can be freed in one go.
+ **/
typedef struct {
struct talloc_chunk *list;
size_t total_alloc_size;
+
+ /** The name recorded for this pool, if any. Should describe
+ * the purpose for which it was allocated. The string is
+ * allocated within the pool. **/
+ char *name;
} TALLOC_CTX;
#endif
diff --git a/source/lib/talloc.c b/source/lib/talloc.c
index 262219bdaff..13393c1a69d 100644
--- a/source/lib/talloc.c
+++ b/source/lib/talloc.c
@@ -3,6 +3,7 @@
Version 3.0
Samba temporary memory allocation functions
Copyright (C) Andrew Tridgell 2000
+ Copyright (C) 2001 by Martin Pool <mbp@samba.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -35,7 +36,7 @@
#include "includes.h"
-/* initialise talloc context. */
+/** Create a new talloc context. **/
TALLOC_CTX *talloc_init(void)
{
TALLOC_CTX *t;
@@ -49,7 +50,27 @@ TALLOC_CTX *talloc_init(void)
return t;
}
-/* allocate a bit of memory from the specified pool */
+
+
+/**
+ * Create a new talloc context, with a name specifying its purpose.
+ * Please call this in preference to talloc_init().
+ **/
+TALLOC_CTX *talloc_init_named(char const *fmt, ...)
+{
+ TALLOC_CTX *t;
+ va_list ap;
+
+ t = talloc_init();
+ va_start(ap, fmt);
+ t->name = talloc_vasprintf(t, fmt, ap);
+ va_end(ap);
+
+ return t;
+}
+
+
+/** Allocate a bit of memory from the specified pool **/
void *talloc(TALLOC_CTX *t, size_t size)
{
void *p;
@@ -166,24 +187,34 @@ char *talloc_strdup(TALLOC_CTX *t, const char *p)
return talloc_memdup(t, p, strlen(p) + 1);
}
-/* allocate a bit of memory from the specified pool */
+/**
+ * Perform string formatting, and return a pointer to newly allocated
+ * memory holding the result, inside a memory pool.
+ **/
char *talloc_asprintf(TALLOC_CTX *t, const char *fmt, ...)
{
va_list ap;
- int len;
char *ret;
/* work out how long it will be */
va_start(ap, fmt);
- len = vsnprintf(NULL, 0, fmt, ap);
+ ret = talloc_vasprintf(t, fmt, ap);
va_end(ap);
+ return ret;
+}
+
+
+char *talloc_vasprintf(TALLOC_CTX *t, const char *fmt, va_list ap)
+{
+ int len;
+ char *ret;
+ len = vsnprintf(NULL, 0, fmt, ap);
+
ret = talloc(t, len+1);
if (!ret) return NULL;
- va_start(ap, fmt);
vsnprintf(ret, len+1, fmt, ap);
- va_end(ap);
return ret;
}