mirror of
https://github.com/samba-team/samba.git
synced 2025-03-11 16:58:40 +03:00
Make TALLOC_CTX and talloc_chunk private to talloc.c.
Add a global singly-linked list of all active talloc pools, so that we can eventually show how much memory is used for different purposes. This also gives a check that pools are not being doubly freed. talloc_init_named now handle a NULL name properly (ie does nothing) Add accessor talloc_pool_name().
This commit is contained in:
parent
7da982e926
commit
4c6c03c8c7
@ -28,25 +28,10 @@
|
|||||||
* @sa talloc.c
|
* @sa talloc.c
|
||||||
*/
|
*/
|
||||||
|
|
||||||
struct talloc_chunk {
|
|
||||||
struct talloc_chunk *next;
|
|
||||||
size_t size;
|
|
||||||
void *ptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* talloc allocation pool. All allocated blocks can be freed in one go.
|
* talloc allocation pool. All allocated blocks can be freed in one go.
|
||||||
**/
|
**/
|
||||||
typedef struct {
|
typedef struct talloc_ctx TALLOC_CTX;
|
||||||
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;
|
|
||||||
|
|
||||||
TALLOC_CTX *talloc_init_named(char const *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
|
TALLOC_CTX *talloc_init_named(char const *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
|
||||||
|
|
||||||
|
@ -49,6 +49,65 @@
|
|||||||
|
|
||||||
#include "includes.h"
|
#include "includes.h"
|
||||||
|
|
||||||
|
struct talloc_chunk {
|
||||||
|
struct talloc_chunk *next;
|
||||||
|
size_t size;
|
||||||
|
void *ptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct talloc_ctx {
|
||||||
|
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;
|
||||||
|
|
||||||
|
/** Pointer to the next allocate talloc pool, so that we can
|
||||||
|
* summarize all talloc memory usage. **/
|
||||||
|
struct talloc_ctx *next_ctx;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start of linked list of all talloc pools.
|
||||||
|
**/
|
||||||
|
TALLOC_CTX *list_head = NULL;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add to the global list
|
||||||
|
**/
|
||||||
|
static void talloc_enroll(TALLOC_CTX *t)
|
||||||
|
{
|
||||||
|
t->next_ctx = list_head;
|
||||||
|
list_head = t;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void talloc_disenroll(TALLOC_CTX *t)
|
||||||
|
{
|
||||||
|
TALLOC_CTX **ttmp;
|
||||||
|
|
||||||
|
/* Use a double-* so that no special case is required for the
|
||||||
|
* list head. */
|
||||||
|
for (ttmp = &list_head; *ttmp; ttmp = &((*ttmp)->next_ctx))
|
||||||
|
if (*ttmp == t) {
|
||||||
|
/* ttmp is the link that points to t, either
|
||||||
|
* list_head or the next_ctx link in its
|
||||||
|
* predecessor */
|
||||||
|
*ttmp = t->next_ctx;
|
||||||
|
t->next_ctx = NULL; /* clobber */
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
abort(); /* oops, this talloc was already
|
||||||
|
* clobbered or something else went
|
||||||
|
* wrong. */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Create a new talloc context. **/
|
/** Create a new talloc context. **/
|
||||||
TALLOC_CTX *talloc_init(void)
|
TALLOC_CTX *talloc_init(void)
|
||||||
{
|
{
|
||||||
@ -59,6 +118,8 @@ TALLOC_CTX *talloc_init(void)
|
|||||||
|
|
||||||
t->list = NULL;
|
t->list = NULL;
|
||||||
t->total_alloc_size = 0;
|
t->total_alloc_size = 0;
|
||||||
|
t->name = NULL;
|
||||||
|
talloc_enroll(t);
|
||||||
|
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
@ -75,9 +136,11 @@ TALLOC_CTX *talloc_init(void)
|
|||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
||||||
t = talloc_init();
|
t = talloc_init();
|
||||||
va_start(ap, fmt);
|
if (fmt) {
|
||||||
t->name = talloc_vasprintf(t, fmt, ap);
|
va_start(ap, fmt);
|
||||||
va_end(ap);
|
t->name = talloc_vasprintf(t, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
}
|
||||||
|
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
@ -161,6 +224,7 @@ void talloc_destroy(TALLOC_CTX *t)
|
|||||||
if (!t)
|
if (!t)
|
||||||
return;
|
return;
|
||||||
talloc_destroy_pool(t);
|
talloc_destroy_pool(t);
|
||||||
|
talloc_disenroll(t);
|
||||||
memset(t, 0, sizeof(*t));
|
memset(t, 0, sizeof(*t));
|
||||||
SAFE_FREE(t);
|
SAFE_FREE(t);
|
||||||
}
|
}
|
||||||
@ -171,6 +235,12 @@ size_t talloc_pool_size(TALLOC_CTX *t)
|
|||||||
return t->total_alloc_size;
|
return t->total_alloc_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char * talloc_pool_name(TALLOC_CTX const *t)
|
||||||
|
{
|
||||||
|
return t->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/** talloc and zero memory. */
|
/** talloc and zero memory. */
|
||||||
void *talloc_zero(TALLOC_CTX *t, size_t size)
|
void *talloc_zero(TALLOC_CTX *t, size_t size)
|
||||||
{
|
{
|
||||||
|
@ -41,16 +41,21 @@ int main(void)
|
|||||||
p = talloc(ctx[i], size);
|
p = talloc(ctx[i], size);
|
||||||
if (!p) {
|
if (!p) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"failed to talloc %0.f bytes\n", size);
|
"failed to talloc %.0f bytes\n",
|
||||||
|
(double) size);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < NCTX; i++) {
|
for (i = 0; i < NCTX; i++) {
|
||||||
printf("talloc@%p %-40s %db\n", ctx[i], ctx[i]->name,
|
printf("talloc@%p %-40s %db\n", ctx[i],
|
||||||
ctx[i]->total_alloc_size);
|
talloc_pool_name(ctx[i]),
|
||||||
|
talloc_pool_size(ctx[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (i = NCTX - 1; i >= 0; i--)
|
||||||
|
talloc_destroy(ctx[i]);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user