X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftalloc%2Ftalloc.c;h=0c6bcda955165e5cb088ec9dadd9da5863c95676;hp=815f255f7f2ef4c11f905502fdbab0f7d05c445b;hb=bf57898eae29d70a166bf2df5c84601729f039ac;hpb=00ea3f8294da91eeea13f000ebb316f49610def4 diff --git a/ccan/talloc/talloc.c b/ccan/talloc/talloc.c index 815f255f..0c6bcda9 100644 --- a/ccan/talloc/talloc.c +++ b/ccan/talloc/talloc.c @@ -43,7 +43,7 @@ #define ALWAYS_REALLOC 0 -#define MAX_TALLOC_SIZE 0x10000000 +#define MAX_TALLOC_SIZE 0x7FFFFFFF #define TALLOC_MAGIC 0xe814ec70 #define TALLOC_FLAG_FREE 0x01 #define TALLOC_FLAG_LOOP 0x02 @@ -82,10 +82,13 @@ static void *null_context; static pid_t *autofree_context; +static void *(*tc_malloc)(size_t size) = malloc; +static void (*tc_free)(void *ptr) = free; +static void *(*tc_realloc)(void *ptr, size_t size) = realloc; + static void *(*tc_external_realloc)(const void *parent, void *ptr, size_t size); -static void (*tc_lock)(void *); -static void (*tc_unlock)(void *); -static void *tc_lock_data; +static void (*tc_lock)(const void *ctx); +static void (*tc_unlock)(void); struct talloc_reference_handle { struct talloc_reference_handle *next, *prev; @@ -150,16 +153,27 @@ do { \ if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \ } while (0) -static inline void lock(void) +static int locked; +static inline void lock(const void *p) { - if (tc_lock) - tc_lock(tc_lock_data); + if (tc_lock && p) { + struct talloc_chunk *tc = talloc_chunk_from_ptr(p); + + if (tc->flags & TALLOC_FLAG_EXT_ALLOC) { + if (locked) + TALLOC_ABORT("nested locking"); + tc_lock(tc); + locked = 1; + } + } } static inline void unlock(void) { - if (tc_lock) - tc_unlock(tc_lock_data); + if (locked) { + tc_unlock(); + locked = 0; + } } /* @@ -179,14 +193,23 @@ static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr) return tc->parent; } -void *talloc_parent(const void *ptr) +/* This version doesn't do locking, so you must already have it. */ +static void *talloc_parent_nolock(const void *ptr) { struct talloc_chunk *tc; - lock(); tc = talloc_parent_chunk(ptr); + return tc ? TC_PTR_FROM_CHUNK(tc) : NULL; +} + +void *talloc_parent(const void *ptr) +{ + void *parent; + + lock(ptr); + parent = talloc_parent_nolock(ptr); unlock(); - return tc? TC_PTR_FROM_CHUNK(tc) : NULL; + return parent; } /* @@ -196,7 +219,7 @@ const char *talloc_parent_name(const void *ptr) { struct talloc_chunk *tc; - lock(); + lock(ptr); tc = talloc_parent_chunk(ptr); unlock(); @@ -264,7 +287,7 @@ static inline void *__talloc(const void *context, size_t size) } } - tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size); + tc = (struct talloc_chunk *)tc_malloc(TC_HDR_SIZE+size); alloc_done: return init_talloc(parent, tc, size, external); } @@ -346,7 +369,7 @@ void *_talloc_reference(const void *context, const void *ptr) struct talloc_reference_handle *handle; if (unlikely(ptr == NULL)) return NULL; - lock(); + lock(context); tc = talloc_chunk_from_ptr(ptr); handle = (struct talloc_reference_handle *)_talloc_named_const(context, sizeof(struct talloc_reference_handle), @@ -450,7 +473,7 @@ static void *__talloc_steal(const void *new_ctx, const void *ptr) /* internal talloc_free call */ -static inline int _talloc_free(void *ptr) +static inline int _talloc_free(const void *ptr) { struct talloc_chunk *tc; void *oldparent = NULL; @@ -489,7 +512,7 @@ static inline int _talloc_free(void *ptr) return -1; } tc->destructor = (talloc_destructor_t)-1; - if (d(ptr) == -1) { + if (d(discard_const_p(void, ptr)) == -1) { tc->destructor = d; return -1; } @@ -497,7 +520,7 @@ static inline int _talloc_free(void *ptr) } if (unlikely(tc->flags & TALLOC_FLAG_EXT_ALLOC)) - oldparent = talloc_parent(ptr); + oldparent = talloc_parent_nolock(ptr); if (tc->parent) { _TLIST_REMOVE(tc->parent->child, tc); @@ -519,13 +542,28 @@ static inline int _talloc_free(void *ptr) final choice is the null context. */ void *child = TC_PTR_FROM_CHUNK(tc->child); const void *new_parent = null_context; + struct talloc_chunk *old_parent = NULL; if (unlikely(tc->child->refs)) { struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs); if (p) new_parent = TC_PTR_FROM_CHUNK(p); } + /* finding the parent here is potentially quite + expensive, but the alternative, which is to change + talloc to always have a valid tc->parent pointer, + makes realloc more expensive where there are a + large number of children. + + The reason we need the parent pointer here is that + if _talloc_free_internal() fails due to references + or a failing destructor we need to re-parent, but + the free call can invalidate the prev pointer. + */ + if (new_parent == null_context && (tc->child->refs || tc->child->destructor)) { + old_parent = talloc_parent_chunk(ptr); + } if (unlikely(_talloc_free(child) == -1)) { if (new_parent == null_context) { - struct talloc_chunk *p = talloc_parent_chunk(ptr); + struct talloc_chunk *p = old_parent; if (p) new_parent = TC_PTR_FROM_CHUNK(p); } __talloc_steal(new_parent, child); @@ -537,7 +575,7 @@ static inline int _talloc_free(void *ptr) if (unlikely(tc->flags & TALLOC_FLAG_EXT_ALLOC)) tc_external_realloc(oldparent, tc, 0); else - free(tc); + tc_free(tc); return 0; } @@ -546,7 +584,7 @@ void *_talloc_steal(const void *new_ctx, const void *ptr) { void *p; - lock(); + lock(new_ctx); p = __talloc_steal(new_ctx, ptr); unlock(); return p; @@ -598,7 +636,7 @@ int talloc_unlink(const void *context, void *ptr) context = null_context; } - lock(); + lock(context); if (talloc_unreference(context, ptr) == 0) { unlock(); return 0; @@ -645,7 +683,7 @@ int talloc_unlink(const void *context, void *ptr) /* add a name to an existing pointer - va_list version */ -static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0); +static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_FMT(2,0); static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) { @@ -682,7 +720,7 @@ void *talloc_named(const void *context, size_t size, const char *fmt, ...) void *ptr; const char *name; - lock(); + lock(context); ptr = __talloc(context, size); unlock(); if (unlikely(ptr == NULL)) return NULL; @@ -747,9 +785,7 @@ void *talloc_init(const char *fmt, ...) */ talloc_enable_null_tracking(); - lock(); ptr = __talloc(NULL, 0); - unlock(); if (unlikely(ptr == NULL)) return NULL; va_start(ap, fmt); @@ -772,6 +808,39 @@ void *_talloc(const void *context, size_t size) return __talloc(context, size); } +static int talloc_destroy_pointer(void ***pptr) +{ + if ((uintptr_t)**pptr < getpagesize()) + TALLOC_ABORT("Double free or invalid talloc_set?"); + /* Invalidate pointer so it can't be used again. */ + **pptr = (void *)1; + return 0; +} + +void _talloc_set(void *ptr, const void *ctx, size_t size, const char *name) +{ + void ***child; + void *p; + + p = talloc_named_const(ctx, size, name); + if (unlikely(!p)) + goto set_ptr; + + child = talloc(p, void **); + if (unlikely(!child)) { + talloc_free(p); + p = NULL; + goto set_ptr; + } + *child = ptr; + talloc_set_name_const(child, "talloc_set destructor"); + talloc_set_destructor(child, talloc_destroy_pointer); + +set_ptr: + /* memcpy rather than cast avoids aliasing problems. */ + memcpy(ptr, &p, sizeof(p)); +} + /* externally callable talloc_set_name_const() */ @@ -788,7 +857,7 @@ void talloc_set_name_const(const void *ptr, const char *name) void *talloc_named_const(const void *context, size_t size, const char *name) { void *p; - lock(); + lock(context); p = _talloc_named_const(context, size, name); unlock(); return p; @@ -802,11 +871,12 @@ void *talloc_named_const(const void *context, size_t size, const char *name) will not be freed if the ref_count is > 1 or the destructor (if any) returns non-zero */ -int talloc_free(void *ptr) +int talloc_free(const void *ptr) { int saved_errno = errno, ret; - lock(); - ret = _talloc_free(ptr); + + lock(ptr); + ret = _talloc_free(discard_const_p(void, ptr)); unlock(); if (ret == 0) errno = saved_errno; @@ -846,10 +916,10 @@ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *n return NULL; } - lock(); + lock(ptr); if (unlikely(tc->flags & TALLOC_FLAG_EXT_ALLOC)) { /* need to get parent before setting free flag. */ - void *parent = talloc_parent(ptr); + void *parent = talloc_parent_nolock(ptr); tc->flags |= TALLOC_FLAG_FREE; new_ptr = tc_external_realloc(parent, tc, size + TC_HDR_SIZE); } else { @@ -857,13 +927,13 @@ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *n tc->flags |= TALLOC_FLAG_FREE; #if ALWAYS_REALLOC - new_ptr = malloc(size + TC_HDR_SIZE); + new_ptr = tc_malloc(size + TC_HDR_SIZE); if (new_ptr) { memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE); - free(tc); + tc_free(tc); } #else - new_ptr = realloc(tc, size + TC_HDR_SIZE); + new_ptr = tc_realloc(tc, size + TC_HDR_SIZE); #endif } @@ -945,7 +1015,7 @@ size_t talloc_total_size(const void *ptr) return 0; } - lock(); + lock(ptr); total = _talloc_total_size(ptr); unlock(); return total; @@ -979,26 +1049,34 @@ size_t talloc_total_blocks(const void *ptr) { size_t total; - lock(); + lock(ptr); total = _talloc_total_blocks(ptr); unlock(); return total; } -/* - return the number of external references to a pointer -*/ -size_t talloc_reference_count(const void *ptr) +static size_t _talloc_reference_count(const void *ptr) { struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr); struct talloc_reference_handle *h; size_t ret = 0; - lock(); for (h=tc->refs;h;h=h->next) { ret++; } + return ret; +} + +/* + return the number of external references to a pointer +*/ +size_t talloc_reference_count(const void *ptr) +{ + size_t ret; + + lock(talloc_chunk_from_ptr(ptr)); + ret = _talloc_reference_count(ptr); unlock(); return ret; } @@ -1051,7 +1129,7 @@ void talloc_report_depth_cb(const void *ptr, int depth, int max_depth, } if (ptr == NULL) return; - lock(); + lock(ptr); _talloc_report_depth_cb(ptr, depth, max_depth, callback, private_data); unlock(); } @@ -1069,17 +1147,17 @@ static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_ if (depth == 0) { fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n", (max_depth < 0 ? "full " :""), name, - (unsigned long)talloc_total_size(ptr), - (unsigned long)talloc_total_blocks(ptr)); + (unsigned long)_talloc_total_size(ptr), + (unsigned long)_talloc_total_blocks(ptr)); return; } fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n", depth*4, "", name, - (unsigned long)talloc_total_size(ptr), - (unsigned long)talloc_total_blocks(ptr), - (int)talloc_reference_count(ptr), ptr); + (unsigned long)_talloc_total_size(ptr), + (unsigned long)_talloc_total_blocks(ptr), + (int)_talloc_reference_count(ptr), ptr); #if 0 fprintf(f, "content: "); @@ -1149,11 +1227,9 @@ static void talloc_report_null_full(void) */ void talloc_enable_null_tracking(void) { - lock(); if (null_context == NULL) { null_context = _talloc_named_const(NULL, 0, "null_context"); } - unlock(); } /* @@ -1161,10 +1237,8 @@ void talloc_enable_null_tracking(void) */ void talloc_disable_null_tracking(void) { - lock(); _talloc_free(null_context); null_context = NULL; - unlock(); } /* @@ -1192,7 +1266,7 @@ void *_talloc_zero(const void *ctx, size_t size, const char *name) { void *p; - lock(); + lock(ctx); p = _talloc_named_const(ctx, size, name); unlock(); @@ -1210,7 +1284,7 @@ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name { void *newp; - lock(); + lock(t); newp = _talloc_named_const(t, size, name); unlock(); @@ -1273,7 +1347,7 @@ char *talloc_strndup(const void *t, const char *p, size_t n) for (len=0; len= MAX_TALLOC_SIZE/el_size) { return NULL; } - lock(); + lock(ctx); p = _talloc_named_const(ctx, el_size * count, name); unlock(); return p; @@ -1414,9 +1488,7 @@ void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const if (count >= MAX_TALLOC_SIZE/el_size) { return NULL; } - lock(); p = _talloc_zero(ctx, el_size * count, name); - unlock(); return p; } @@ -1494,7 +1566,7 @@ void *talloc_find_parent_byname(const void *context, const char *name) return NULL; } - lock(); + lock(context); tc = talloc_chunk_from_ptr(context); while (tc) { if (tc->name && strcmp(tc->name, name) == 0) { @@ -1522,7 +1594,7 @@ void talloc_show_parents(const void *context, FILE *file) return; } - lock(); + lock(context); tc = talloc_chunk_from_ptr(context); fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context)); while (tc) { @@ -1539,19 +1611,29 @@ void talloc_show_parents(const void *context, FILE *file) int talloc_is_parent(const void *context, const void *ptr) { int ret; - lock(); + lock(context); ret = _talloc_is_parent(context, ptr); unlock(); return ret; } +void talloc_set_allocator(void *(*malloc)(size_t size), + void (*free)(void *ptr), + void *(*realloc)(void *ptr, size_t size)) +{ + tc_malloc = malloc; + tc_free = free; + tc_realloc = realloc; +} + void *talloc_add_external(const void *ctx, - void *(*realloc)(const void *, void *, size_t)) + void *(*realloc)(const void *, void *, size_t), + void (*lock)(const void *p), + void (*unlock)(void)) { struct talloc_chunk *tc, *parent; void *p; - lock(); if (tc_external_realloc && tc_external_realloc != realloc) TALLOC_ABORT("talloc_add_external realloc replaced"); tc_external_realloc = realloc; @@ -1564,14 +1646,8 @@ void *talloc_add_external(const void *ctx, tc = tc_external_realloc(ctx, NULL, TC_HDR_SIZE); p = init_talloc(parent, tc, 0, 1); - unlock(); - - return p; -} - -void _talloc_locksafe(void (*lock)(void *), void (*unlock)(void *), void *data) -{ tc_lock = lock; tc_unlock = unlock; - tc_lock_data = data; + + return p; }