From 1add9f708a2bd648844aeecc02bdfcb3d872e2a6 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Wed, 20 Nov 2013 09:57:58 +0100 Subject: [PATCH 1/7] talloc: inline more static functions We need the code to be as fast as possible. Signed-off-by: Stefan Metzmacher --- lib/talloc/talloc.c | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/lib/talloc/talloc.c b/lib/talloc/talloc.c index 2a5406e..56ad4f7 100644 --- a/lib/talloc/talloc.c +++ b/lib/talloc/talloc.c @@ -235,12 +235,14 @@ struct talloc_memlimit { size_t cur_size; }; -static bool talloc_memlimit_check(struct talloc_memlimit *limit, size_t size); -static void talloc_memlimit_grow(struct talloc_memlimit *limit, +static inline bool talloc_memlimit_check(struct talloc_memlimit *limit, size_t size); +static inline void talloc_memlimit_grow(struct talloc_memlimit *limit, size_t size); -static void talloc_memlimit_shrink(struct talloc_memlimit *limit, +static inline void talloc_memlimit_shrink(struct talloc_memlimit *limit, size_t size); -static void talloc_memlimit_update_on_free(struct talloc_chunk *tc); +static inline void talloc_memlimit_update_on_free(struct talloc_chunk *tc); + +static inline void _talloc_set_name_const(const void *ptr, const char *name); typedef int (*talloc_destructor_t)(void *); @@ -466,41 +468,41 @@ struct talloc_pool_hdr { #define TP_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_pool_hdr)) -static struct talloc_pool_hdr *talloc_pool_from_chunk(struct talloc_chunk *c) +static inline struct talloc_pool_hdr *talloc_pool_from_chunk(struct talloc_chunk *c) { return (struct talloc_pool_hdr *)((char *)c - TP_HDR_SIZE); } -static struct talloc_chunk *talloc_chunk_from_pool(struct talloc_pool_hdr *h) +static inline struct talloc_chunk *talloc_chunk_from_pool(struct talloc_pool_hdr *h) { return (struct talloc_chunk *)((char *)h + TP_HDR_SIZE); } -static void *tc_pool_end(struct talloc_pool_hdr *pool_hdr) +static inline void *tc_pool_end(struct talloc_pool_hdr *pool_hdr) { struct talloc_chunk *tc = talloc_chunk_from_pool(pool_hdr); return (char *)tc + TC_HDR_SIZE + pool_hdr->poolsize; } -static size_t tc_pool_space_left(struct talloc_pool_hdr *pool_hdr) +static inline size_t tc_pool_space_left(struct talloc_pool_hdr *pool_hdr) { return (char *)tc_pool_end(pool_hdr) - (char *)pool_hdr->end; } /* If tc is inside a pool, this gives the next neighbour. */ -static void *tc_next_chunk(struct talloc_chunk *tc) +static inline void *tc_next_chunk(struct talloc_chunk *tc) { return (char *)tc + TC_ALIGN16(TC_HDR_SIZE + tc->size); } -static void *tc_pool_first_chunk(struct talloc_pool_hdr *pool_hdr) +static inline void *tc_pool_first_chunk(struct talloc_pool_hdr *pool_hdr) { struct talloc_chunk *tc = talloc_chunk_from_pool(pool_hdr); return tc_next_chunk(tc); } /* Mark the whole remaining pool as not accessable */ -static void tc_invalidate_pool(struct talloc_pool_hdr *pool_hdr) +static inline void tc_invalidate_pool(struct talloc_pool_hdr *pool_hdr) { size_t flen = tc_pool_space_left(pool_hdr); @@ -517,8 +519,8 @@ static void tc_invalidate_pool(struct talloc_pool_hdr *pool_hdr) Allocate from a pool */ -static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent, - size_t size, size_t prefix_len) +static inline struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent, + size_t size, size_t prefix_len) { struct talloc_pool_hdr *pool_hdr = NULL; size_t space_left; @@ -657,7 +659,7 @@ static inline void *__talloc(const void *context, size_t size) * Create a talloc pool */ -_PUBLIC_ void *talloc_pool(const void *context, size_t size) +static inline void *_talloc_pool(const void *context, size_t size) { struct talloc_chunk *tc; struct talloc_pool_hdr *pool_hdr; @@ -684,6 +686,11 @@ _PUBLIC_ void *talloc_pool(const void *context, size_t size) return result; } +_PUBLIC_ void *talloc_pool(const void *context, size_t size) +{ + return _talloc_pool(context, size); +} + /* * Create a talloc pool correctly sized for a basic size plus * a number of subobjects whose total size is given. Essentially @@ -727,7 +734,7 @@ _PUBLIC_ void *_talloc_pooled_object(const void *ctx, } poolsize = tmp; - ret = talloc_pool(ctx, poolsize); + ret = _talloc_pool(ctx, poolsize); if (ret == NULL) { return NULL; } @@ -743,7 +750,7 @@ _PUBLIC_ void *_talloc_pooled_object(const void *ctx, pool_hdr->end = ((char *)pool_hdr->end + TC_ALIGN16(type_size)); - talloc_set_name_const(ret, type_name); + _talloc_set_name_const(ret, type_name); return ret; overflow: @@ -1858,7 +1865,7 @@ enum talloc_mem_count_type { TOTAL_MEM_LIMIT, }; -static size_t _talloc_total_mem_internal(const void *ptr, +static inline size_t _talloc_total_mem_internal(const void *ptr, enum talloc_mem_count_type type, struct talloc_memlimit *old_limit, struct talloc_memlimit *new_limit) @@ -2666,7 +2673,7 @@ _PUBLIC_ int talloc_is_parent(const void *context, const void *ptr) /* return the total size of memory used by this context and all children */ -static size_t _talloc_total_limit_size(const void *ptr, +static inline size_t _talloc_total_limit_size(const void *ptr, struct talloc_memlimit *old_limit, struct talloc_memlimit *new_limit) { @@ -2674,7 +2681,7 @@ static size_t _talloc_total_limit_size(const void *ptr, old_limit, new_limit); } -static bool talloc_memlimit_check(struct talloc_memlimit *limit, size_t size) +static inline bool talloc_memlimit_check(struct talloc_memlimit *limit, size_t size) { struct talloc_memlimit *l; -- 1.7.9.5 From 5a74ec405dcd87b5316d26756a7df55d761aae3e Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Wed, 4 Dec 2013 23:22:04 +0100 Subject: [PATCH 2/7] talloc: inline talloc_get_name() Signed-off-by: Stefan Metzmacher --- lib/talloc/talloc.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/talloc/talloc.c b/lib/talloc/talloc.c index 56ad4f7..3b8cb81 100644 --- a/lib/talloc/talloc.c +++ b/lib/talloc/talloc.c @@ -1355,7 +1355,7 @@ _PUBLIC_ void *talloc_named(const void *context, size_t size, const char *fmt, . /* return the name of a talloc ptr, or "UNNAMED" */ -_PUBLIC_ const char *talloc_get_name(const void *ptr) +static inline const char *__talloc_get_name(const void *ptr) { struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr); if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) { @@ -1367,6 +1367,10 @@ _PUBLIC_ const char *talloc_get_name(const void *ptr) return "UNNAMED"; } +_PUBLIC_ const char *talloc_get_name(const void *ptr) +{ + return __talloc_get_name(ptr); +} /* check if a pointer has the given name. If it does, return the pointer, @@ -1376,7 +1380,7 @@ _PUBLIC_ void *talloc_check_name(const void *ptr, const char *name) { const char *pname; if (unlikely(ptr == NULL)) return NULL; - pname = talloc_get_name(ptr); + pname = __talloc_get_name(ptr); if (likely(pname == name || strcmp(pname, name) == 0)) { return discard_const_p(void, ptr); } @@ -1410,7 +1414,7 @@ _PUBLIC_ void *_talloc_get_type_abort(const void *ptr, const char *name, const c return NULL; } - pname = talloc_get_name(ptr); + pname = __talloc_get_name(ptr); if (likely(pname == name || strcmp(pname, name) == 0)) { return discard_const_p(void, ptr); } @@ -2028,7 +2032,7 @@ _PUBLIC_ void talloc_report_depth_cb(const void *ptr, int depth, int max_depth, static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f) { - const char *name = talloc_get_name(ptr); + const char *name = __talloc_get_name(ptr); struct talloc_chunk *tc; FILE *f = (FILE *)_f; @@ -2628,9 +2632,9 @@ _PUBLIC_ void talloc_show_parents(const void *context, FILE *file) } tc = talloc_chunk_from_ptr(context); - fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context)); + fprintf(file, "talloc parents of '%s'\n", __talloc_get_name(context)); while (tc) { - fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc))); + fprintf(file, "\t'%s'\n", __talloc_get_name(TC_PTR_FROM_CHUNK(tc))); while (tc && tc->prev) tc = tc->prev; if (tc) { tc = tc->parent; -- 1.7.9.5 From 6d5c68b91b52731eb854a1b5a3ba9cee70d5cd40 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Wed, 4 Dec 2013 15:35:37 +0100 Subject: [PATCH 3/7] talloc: avoid a function call in TALLOC_FREE() if possible. Signed-off-by: Stefan Metzmacher --- lib/talloc/talloc.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/talloc/talloc.h b/lib/talloc/talloc.h index 5d29a8d..0d47d23 100644 --- a/lib/talloc/talloc.h +++ b/lib/talloc/talloc.h @@ -893,7 +893,7 @@ void *_talloc_pooled_object(const void *ctx, * * @param[in] ctx The chunk to be freed. */ -#define TALLOC_FREE(ctx) do { talloc_free(ctx); ctx=NULL; } while(0) +#define TALLOC_FREE(ctx) do { if (ctx != NULL) { talloc_free(ctx); ctx=NULL; } } while(0) /* @} ******************************************************************/ -- 1.7.9.5 From 52a1ec0c1316970763214112be1c407157367ca2 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Thu, 5 Dec 2013 08:36:13 +0100 Subject: [PATCH 4/7] talloc: check for TALLOC_GET_TYPE_ABORT_NOOP Signed-off-by: Stefan Metzmacher --- lib/talloc/talloc.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/talloc/talloc.h b/lib/talloc/talloc.h index 0d47d23..5ece54d 100644 --- a/lib/talloc/talloc.h +++ b/lib/talloc/talloc.h @@ -761,7 +761,11 @@ type *talloc_get_type(const void *ptr, #type); */ void *talloc_get_type_abort(const void *ptr, #type); #else +#ifdef TALLOC_GET_TYPE_ABORT_NOOP +#define talloc_get_type_abort(ptr, type) (type *)(ptr) +#else #define talloc_get_type_abort(ptr, type) (type *)_talloc_get_type_abort(ptr, #type, __location__) +#endif void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location); #endif -- 1.7.9.5 From 2876dccee5e377c8b083b39cc60c8ed647901f18 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Wed, 20 Nov 2013 09:58:09 +0100 Subject: [PATCH 5/7] talloc: fix compiler warning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This avoids the following warning when using: CFLAGS="-O3 -g -fstrict-overflow -Wstrict-overflow=5" ../talloc.c: In Funktion »talloc_is_parent«: ../talloc.c:2658:21: Warnung: assuming signed overflow does not occur when changing X +- C1 cmp C2 to X cmp C1 +- C2 [-Wstrict-overflow] Signed-off-by: Stefan Metzmacher --- lib/talloc/talloc.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/talloc/talloc.c b/lib/talloc/talloc.c index 3b8cb81..fa56ea5 100644 --- a/lib/talloc/talloc.c +++ b/lib/talloc/talloc.c @@ -2655,7 +2655,10 @@ static int _talloc_is_parent(const void *context, const void *ptr, int depth) } tc = talloc_chunk_from_ptr(context); - while (tc && depth > 0) { + while (tc) { + if (depth <= 0) { + return 0; + } if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1; while (tc && tc->prev) tc = tc->prev; if (tc) { -- 1.7.9.5 From 62c876ac13ee2b6f08ed44e0055c57e35dbe8d55 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Thu, 27 Feb 2014 09:28:02 +0100 Subject: [PATCH 6/7] talloc/tests: avoid some unused variable warnings Signed-off-by: Stefan Metzmacher --- lib/talloc/testsuite.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/talloc/testsuite.c b/lib/talloc/testsuite.c index 888d260..a878278 100644 --- a/lib/talloc/testsuite.c +++ b/lib/talloc/testsuite.c @@ -141,6 +141,7 @@ static bool test_ref1(void) CHECK_BLOCKS("ref1", p1, 5); CHECK_BLOCKS("ref1", p2, 1); + CHECK_BLOCKS("ref1", ref, 1); CHECK_BLOCKS("ref1", r1, 2); fprintf(stderr, "Freeing p2\n"); @@ -249,6 +250,7 @@ static bool test_ref3(void) CHECK_BLOCKS("ref3", p1, 2); CHECK_BLOCKS("ref3", p2, 2); CHECK_BLOCKS("ref3", r1, 1); + CHECK_BLOCKS("ref3", ref, 1); fprintf(stderr, "Freeing p1\n"); talloc_free(p1); @@ -291,6 +293,7 @@ static bool test_ref4(void) CHECK_BLOCKS("ref4", p1, 5); CHECK_BLOCKS("ref4", p2, 1); + CHECK_BLOCKS("ref4", ref, 1); CHECK_BLOCKS("ref4", r1, 2); fprintf(stderr, "Freeing r1\n"); @@ -341,6 +344,7 @@ static bool test_unlink1(void) CHECK_BLOCKS("unlink", p1, 7); CHECK_BLOCKS("unlink", p2, 1); + CHECK_BLOCKS("unlink", ref, 1); CHECK_BLOCKS("unlink", r1, 2); fprintf(stderr, "Unreferencing r1\n"); @@ -409,6 +413,8 @@ static bool test_misc(void) name = talloc_set_name(p1, "my name is %s", "foo"); torture_assert_str_equal("misc", talloc_get_name(p1), "my name is foo", "failed: wrong name after talloc_set_name(my name is foo)"); + torture_assert_str_equal("misc", talloc_get_name(p1), name, + "failed: wrong name after talloc_set_name(my name is foo)"); CHECK_BLOCKS("misc", p1, 2); CHECK_BLOCKS("misc", root, 3); @@ -617,6 +623,7 @@ static bool test_realloc_child(void) el2 = talloc(el1->list, struct el2); el2 = talloc(el1->list2, struct el2); el2 = talloc(el1->list3, struct el2); + (void)el2; el1->list = talloc_realloc(el1, el1->list, struct el2 *, 100); el1->list2 = talloc_realloc(el1, el1->list2, struct el2 *, 200); @@ -829,6 +836,8 @@ static bool test_speed(void) p1 = talloc_size(ctx, loop % 100); p2 = talloc_strdup(p1, "foo bar"); p3 = talloc_size(p1, 300); + (void)p2; + (void)p3; talloc_free(p1); } count += 3 * loop; @@ -848,6 +857,8 @@ static bool test_speed(void) p1 = talloc_size(ctx, loop % 100); p2 = talloc_strdup(p1, "foo bar"); p3 = talloc_size(p1, 300); + (void)p2; + (void)p3; talloc_free(p1); } count += 3 * loop; @@ -1380,6 +1391,7 @@ static bool test_free_children(void) root = talloc_new(NULL); p1 = talloc_strdup(root, "foo1"); p2 = talloc_strdup(p1, "foo2"); + (void)p2; talloc_set_name(p1, "%s", "testname"); talloc_free_children(p1); @@ -1404,6 +1416,7 @@ static bool test_free_children(void) name2 = talloc_get_name(p1); /* but this does */ talloc_free_children(p1); + (void)name2; torture_assert("namecheck", strcmp(talloc_get_name(p1), "testname2") == 0, "wrong name"); CHECK_BLOCKS("name1", p1, 1); -- 1.7.9.5 From c67bc10477709b9a95315d74fe3838a8046cbc5b Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Thu, 15 May 2014 14:53:49 +0200 Subject: [PATCH 7/7] talloc: version 2.1.1 Changes: - documentation updates - a fix for pytalloc-util.pc - performance improvements here and there - fixed compiler warnings Signed-off-by: Stefan Metzmacher --- lib/talloc/wscript | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/talloc/wscript b/lib/talloc/wscript index 1ca41f6..9df64d9 100644 --- a/lib/talloc/wscript +++ b/lib/talloc/wscript @@ -1,7 +1,7 @@ #!/usr/bin/env python APPNAME = 'talloc' -VERSION = '2.1.0' +VERSION = '2.1.1' blddir = 'bin' -- 1.7.9.5