diff options
Diffstat (limited to 'ta')
-rw-r--r-- | ta/README | 2 | ||||
-rw-r--r-- | ta/ta.c | 32 | ||||
-rw-r--r-- | ta/ta.h | 23 | ||||
-rw-r--r-- | ta/ta_utils.c | 21 |
4 files changed, 36 insertions, 42 deletions
@@ -31,7 +31,7 @@ will be replaced with TA calls, and the talloc wrapper will be removed. Documentation for the talloc API is here: - http://talloc.samba.org/talloc/doc/html/modules.html + https://talloc.samba.org/talloc/doc/html/group__talloc.html There are some minor differences with mpv's talloc bridge. mpv calls abort() on allocation failures, and the talloc_set_destructor() signature is slightly @@ -13,18 +13,15 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include <assert.h> +#include <stddef.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> -#include <stdio.h> -#include <assert.h> #define TA_NO_WRAPPERS #include "ta.h" -// Note: the actual minimum alignment is dictated by malloc(). It doesn't -// make sense to set this value higher than malloc's alignment. -#define MIN_ALIGN 16 - #if !defined(TA_MEMORY_DEBUGGING) #if !defined(NDEBUG) #define TA_MEMORY_DEBUGGING 1 @@ -52,6 +49,7 @@ struct ta_header { #define CANARY 0xD3ADB3EF +#define MIN_ALIGN _Alignof(max_align_t) union aligned_header { struct ta_header ta; // Make sure to satisfy typical alignment requirements @@ -267,9 +265,9 @@ void ta_set_destructor(void *ptr, void (*destructor)(void *)) #if TA_MEMORY_DEBUGGING -#include <pthread.h> +#include "osdep/threads.h" -static pthread_mutex_t ta_dbg_mutex = PTHREAD_MUTEX_INITIALIZER; +static mp_static_mutex ta_dbg_mutex = MP_STATIC_MUTEX_INITIALIZER; static bool enable_leak_check; // pretty much constant static struct ta_header leak_node; static char allocation_is_string; @@ -278,12 +276,12 @@ static void ta_dbg_add(struct ta_header *h) { h->canary = CANARY; if (enable_leak_check) { - pthread_mutex_lock(&ta_dbg_mutex); + mp_mutex_lock(&ta_dbg_mutex); h->leak_next = &leak_node; h->leak_prev = leak_node.leak_prev; leak_node.leak_prev->leak_next = h; leak_node.leak_prev = h; - pthread_mutex_unlock(&ta_dbg_mutex); + mp_mutex_unlock(&ta_dbg_mutex); } } @@ -302,10 +300,10 @@ static void ta_dbg_remove(struct ta_header *h) { ta_dbg_check_header(h); if (h->leak_next) { // assume checking for !=NULL invariant ok without lock - pthread_mutex_lock(&ta_dbg_mutex); + mp_mutex_lock(&ta_dbg_mutex); h->leak_next->leak_prev = h->leak_prev; h->leak_prev->leak_next = h->leak_next; - pthread_mutex_unlock(&ta_dbg_mutex); + mp_mutex_unlock(&ta_dbg_mutex); h->leak_next = h->leak_prev = NULL; } h->canary = 0; @@ -321,7 +319,7 @@ static size_t get_children_size(struct ta_header *h) static void print_leak_report(void) { - pthread_mutex_lock(&ta_dbg_mutex); + mp_mutex_lock(&ta_dbg_mutex); if (leak_node.leak_next && leak_node.leak_next != &leak_node) { size_t size = 0; size_t num_blocks = 0; @@ -333,7 +331,7 @@ static void print_leak_report(void) // Don't list those with parent; logically, only parents are listed if (!cur->next) { size_t c_size = get_children_size(cur); - char name[30] = {0}; + char name[50] = {0}; if (cur->name) snprintf(name, sizeof(name), "%s", cur->name); if (cur->name == &allocation_is_string) { @@ -356,19 +354,19 @@ static void print_leak_report(void) } fprintf(stderr, "%zu bytes in %zu blocks.\n", size, num_blocks); } - pthread_mutex_unlock(&ta_dbg_mutex); + mp_mutex_unlock(&ta_dbg_mutex); } void ta_enable_leak_report(void) { - pthread_mutex_lock(&ta_dbg_mutex); + mp_mutex_lock(&ta_dbg_mutex); enable_leak_check = true; if (!leak_node.leak_prev && !leak_node.leak_next) { leak_node.leak_prev = &leak_node; leak_node.leak_next = &leak_node; atexit(print_leak_report); } - pthread_mutex_unlock(&ta_dbg_mutex); + mp_mutex_unlock(&ta_dbg_mutex); } /* Set a (static) string that will be printed if the memory allocation in ptr @@ -144,9 +144,26 @@ void *ta_xrealloc_size(void *ta_parent, void *ptr, size_t size); #define ta_xrealloc_size(...) ta_dbg_set_loc(ta_xrealloc_size(__VA_ARGS__), TA_LOC) #endif -void ta_oom_b(bool b); -char *ta_oom_s(char *s); -void *ta_oom_p(void *p); +static inline void *ta_oom_p(void *p) +{ + if (!p) + abort(); + return p; +} + +static inline void ta_oom_b(bool b) +{ + if (!b) + abort(); +} + +static inline char *ta_oom_s(char *s) +{ + if (!s) + abort(); + return s; +} + // Generic pointer #define ta_oom_g(ptr) (TA_TYPEOF(ptr))ta_oom_p(ptr) diff --git a/ta/ta_utils.c b/ta/ta_utils.c index 62469686db..294ad8d7f2 100644 --- a/ta/ta_utils.c +++ b/ta/ta_utils.c @@ -265,27 +265,6 @@ bool ta_vasprintf_append_buffer(char **str, const char *fmt, va_list ap) return ta_vasprintf_append_at(str, size, fmt, ap); } - -void *ta_oom_p(void *p) -{ - if (!p) - abort(); - return p; -} - -void ta_oom_b(bool b) -{ - if (!b) - abort(); -} - -char *ta_oom_s(char *s) -{ - if (!s) - abort(); - return s; -} - void *ta_xmemdup(void *ta_parent, void *ptr, size_t size) { void *new = ta_memdup(ta_parent, ptr, size); |