From ec0128ef012f4280b2fb607cb9c88c7673894fe6 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 26 Sep 2004 04:59:03 +0000 Subject: [PATCH] r2649: - used some cpp tricks to make users of talloc() and talloc_realloc() to get auto-naming of pointers very cheaply. - fixed a couple of memory leaks found with the new tricks A typical exit report for smbd is now: talloc report on 'null_context' (total 811 bytes in 54 blocks) auth/auth_sam.c:334 contains 20 bytes in 1 blocks struct auth_serversupplied_info contains 498 bytes in 33 blocks UNNAMED contains 8 bytes in 1 blocks lib/data_blob.c:40 contains 16 bytes in 1 blocks iconv(CP850,UTF8) contains 61 bytes in 4 blocks iconv(UTF8,CP850) contains 61 bytes in 4 blocks iconv(UTF8,UTF-16LE) contains 67 bytes in 4 blocks iconv(UTF-16LE,UTF8) contains 67 bytes in 4 blocks UNNAMED contains 13 bytes in 1 blocks which is much better than before (This used to be commit 6e721393d03afd3c2f8ced8422533547a9e33342) --- source4/include/talloc.h | 14 ++++++++++++-- source4/lib/data_blob.c | 5 +++++ source4/lib/talloc.c | 22 +++++++++++++++------- source4/libcli/raw/rawtrans.c | 4 ++-- source4/torture/raw/search.c | 3 +++ 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/source4/include/talloc.h b/source4/include/talloc.h index 5af7fd1da04..9f7281f4277 100644 --- a/source4/include/talloc.h +++ b/source4/include/talloc.h @@ -24,10 +24,20 @@ /* this is only needed for compatibility with the old talloc */ typedef void TALLOC_CTX; +/* + this uses a little trick to allow __LINE__ to be stringified +*/ +#define _STRING_LINE_(s) #s +#define _STRING_LINE2_(s) _STRING_LINE_(s) +#define __LINESTR__ _STRING_LINE2_(__LINE__) +#define __location__ __FILE__ ":" __LINESTR__ + /* useful macros for creating type checked pointers */ +#define talloc(ctx, size) talloc_named_const(ctx, size, __location__) +#define talloc_realloc(ptr, size) _talloc_realloc(ptr, size, __location__) #define talloc_p(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type) -#define talloc_array_p(ctx, type, count) (type *)talloc_array(ctx, sizeof(type), count) -#define talloc_realloc_p(p, type, count) (type *)talloc_realloc_array(p, sizeof(type), count) +#define talloc_array_p(ctx, type, count) (type *)talloc_array(ctx, sizeof(type), count, __location__) +#define talloc_realloc_p(p, type, count) (type *)talloc_realloc_array(p, sizeof(type), count, __location__) #define talloc_destroy(ctx) talloc_free(ctx) diff --git a/source4/lib/data_blob.c b/source4/lib/data_blob.c index e10ebfe6065..c8c57849d27 100644 --- a/source4/lib/data_blob.c +++ b/source4/lib/data_blob.c @@ -56,6 +56,11 @@ DATA_BLOB data_blob_talloc(TALLOC_CTX *mem_ctx, const void *p, size_t length) if (ret.data) { ret.data = talloc_steal(mem_ctx, ret.data); + } else { + /* this ensures the blob has the context attached, so a zero length call + to data_blob_talloc followed by a realloc doesn't cause the memory to come + from the NULL context */ + ret.data = talloc(mem_ctx, 0); } return ret; } diff --git a/source4/lib/talloc.c b/source4/lib/talloc.c index 3e42fc68f8c..c08495aae40 100644 --- a/source4/lib/talloc.c +++ b/source4/lib/talloc.c @@ -26,6 +26,9 @@ #include "includes.h" +#undef talloc +#define talloc(ctx, size) _talloc(ctx, size) + #define MAX_TALLOC_SIZE 0x10000000 #define TALLOC_MAGIC 0xe814ec4f #define TALLOC_MAGIC_FREE 0x7faebef3 @@ -59,7 +62,7 @@ static struct talloc_chunk *talloc_chunk_from_ptr(void *ptr) /* Allocate a bit of memory as a child of an existing pointer */ -void *talloc(void *context, size_t size) +void *_talloc(void *context, size_t size) { struct talloc_chunk *tc; @@ -289,7 +292,7 @@ int talloc_free(void *ptr) /* A talloc version of realloc */ -void *talloc_realloc(void *ptr, size_t size) +void *_talloc_realloc(void *ptr, size_t size, const char *name) { struct talloc_chunk *tc; void *new_ptr; @@ -302,7 +305,7 @@ void *talloc_realloc(void *ptr, size_t size) /* realloc(NULL) is equavalent to malloc() */ if (ptr == NULL) { - return talloc(NULL, size); + return talloc_named_const(NULL, size, name); } tc = talloc_chunk_from_ptr(ptr); @@ -330,6 +333,7 @@ void *talloc_realloc(void *ptr, size_t size) } tc->size = size; + talloc_set_name_const(tc+1, name); return (void *)(tc+1); } @@ -582,26 +586,30 @@ char *talloc_asprintf_append(char *s, /* alloc an array, checking for integer overflow in the array size */ -void *talloc_array(void *ctx, size_t el_size, uint_t count) +void *talloc_array(void *ctx, size_t el_size, uint_t count, const char *name) { if (count == 0 || count >= MAX_TALLOC_SIZE/el_size) { return NULL; } - return talloc(ctx, el_size * count); + return talloc_named_const(ctx, el_size * count, name); } /* realloc an array, checking for integer overflow in the array size */ -void *talloc_realloc_array(void *ptr, size_t el_size, uint_t count) +void *talloc_realloc_array(void *ptr, size_t el_size, uint_t count, const char *name) { if (count == 0 || count >= MAX_TALLOC_SIZE/el_size) { return NULL; } - return talloc_realloc(ptr, el_size * count); + ptr = talloc_realloc(ptr, el_size * count); + if (ptr) { + talloc_set_name_const(ptr, name); + } + return ptr; } /* diff --git a/source4/libcli/raw/rawtrans.c b/source4/libcli/raw/rawtrans.c index 49b43dd9302..e6c928e3edc 100644 --- a/source4/libcli/raw/rawtrans.c +++ b/source4/libcli/raw/rawtrans.c @@ -84,7 +84,7 @@ NTSTATUS smb_raw_trans2_recv(struct smbcli_request *req, /* allocate it */ if (total_data != 0) { - tdata = talloc_realloc(parms->out.data.data,total_data); + tdata = talloc(mem_ctx, total_data); if (!tdata) { DEBUG(0,("smb_raw_receive_trans: failed to enlarge data buffer to %d bytes\n", total_data)); req->status = NT_STATUS_NO_MEMORY; @@ -94,7 +94,7 @@ NTSTATUS smb_raw_trans2_recv(struct smbcli_request *req, } if (total_param != 0) { - tparam = talloc_realloc(parms->out.params.data,total_param); + tparam = talloc(mem_ctx, total_param); if (!tparam) { DEBUG(0,("smb_raw_receive_trans: failed to enlarge param buffer to %d bytes\n", total_param)); req->status = NT_STATUS_NO_MEMORY; diff --git a/source4/torture/raw/search.c b/source4/torture/raw/search.c index 3eb1d2d8e10..d0873e2ef43 100644 --- a/source4/torture/raw/search.c +++ b/source4/torture/raw/search.c @@ -681,6 +681,7 @@ static BOOL test_many_files(struct smbcli_state *cli, TALLOC_CTX *mem_ctx) } free(fname); } + talloc_free(result.list); } done: @@ -898,6 +899,8 @@ static BOOL test_sorted(struct smbcli_state *cli, TALLOC_CTX *mem_ctx) } } + talloc_free(result.list); + done: smb_raw_exit(cli->session); smbcli_deltree(cli->tree, BASEDIR); -- 2.11.4.GIT