From 60431b8d299408f808fbd9398dbe0ba16db3b8d7 Mon Sep 17 00:00:00 2001 From: Juan Carrano Date: Tue, 24 Apr 2018 10:44:56 +0200 Subject: [PATCH] pkg/tlsf: remove tlsf_g* functions. - The tlsf-malloc functions are named malloc/free/etc and always replace system functions if the module is enabled. - Added GCC specific function attributes. --- pkg/tlsf/contrib/include/tlsf-malloc.h | 33 ++--------- pkg/tlsf/contrib/tlsf-malloc.c | 77 +++++++++++--------------- 2 files changed, 35 insertions(+), 75 deletions(-) diff --git a/pkg/tlsf/contrib/include/tlsf-malloc.h b/pkg/tlsf/contrib/include/tlsf-malloc.h index d0f41125bba6d..48ef8952b81c5 100644 --- a/pkg/tlsf/contrib/include/tlsf-malloc.h +++ b/pkg/tlsf/contrib/include/tlsf-malloc.h @@ -14,14 +14,11 @@ * * This is a malloc/free implementation built on top of the TLSF allocator. * It defines a global tlsf_control block and performs allocations on that - * block. + * block. This implemetation replaces the system malloc * * Additionally, the calls to TLSF are wrapped in irq_disable()/irq_restore(), * to make it thread-safe. * - * By default, this implemetation replaces the system malloc. This behavior can - * be changed by setting the TLSF_MALLOC_NOSYSTEM. - * * If this module is used as the system memory allocator, then the global memory * control block should be initialized as the first thing before the stdlib is * used. Boards should use tlsf_add_global_pool() at startup to add all the memory @@ -52,6 +49,9 @@ extern "C" { * The first time this function is called, it will automatically perform a * tlsf_create() on the global tlsf_control block. * + * @warning If this module is used, then this function MUST be called at least + * once, before any allocations take place. + * * @param mem Pointer to memory area. Should be aligned to 4 bytes. * @param bytes Size in bytes of the memory area. * @@ -66,31 +66,6 @@ int tlsf_add_global_pool(void *mem, size_t bytes); */ tlsf_t *_tlsf_get_global_control(void); -/** - * Allocate a block of size "bytes" - */ -void *tlsf_gmalloc(size_t bytes); - -/** - * Allocate a block of size "bytes*count" - */ -void *tlsf_gcalloc(size_t count, size_t bytes); - -/** - * Allocate an aligned memory block. - */ -void *tlsf_gmemalign(size_t align, size_t bytes); - -/** - * Deallocate and reallocate with a different size. - */ -void *tlsf_grealloc(void *ptr, size_t size); - -/** - * Deallocate a block of data. - */ -void tlsf_gfree(void *ptr); - #ifdef __cplusplus } diff --git a/pkg/tlsf/contrib/tlsf-malloc.c b/pkg/tlsf/contrib/tlsf-malloc.c index 4468fa977aba7..b62f2111bdf19 100644 --- a/pkg/tlsf/contrib/tlsf-malloc.c +++ b/pkg/tlsf/contrib/tlsf-malloc.c @@ -30,54 +30,23 @@ **/ static tlsf_t gheap = NULL; - -/* Replace system memory routines - * - * Doing it this way (instead of changing the function names) has the - * advantage of not breaking programs that use tlsf_g* just because someone - * decides to use them as the default allocation routuines. - * */ -#ifndef TLSF_MALLOC_NOSYSTEM - +/* TODO: Add defines for other compilers */ #ifdef __GNUC__ -#define ALIAS(n, args) __attribute__ ((alias(#n))); -#define VALIAS(n, args) __attribute__ ((alias(#n))); +#define ATTR_MALLOC __attribute__((malloc, alloc_size(1))) +#define ATTR_CALLOC __attribute__((malloc, alloc_size(1,2))) +#define ATTR_MALIGN __attribute__((alloc_align(1), alloc_size(2), malloc)) +#define ATTR_REALLOC __attribute__((alloc_size(2))) #else /* No GNU C -> no alias attribute */ -#define ALIAS(n, args) { return n args; } -#define VALIAS(n, args) { n args; } +#define ATTR_MALLOC +#define ATTR_CALLOC +#define ATTR_MALIGN +#define ATTR_REALLOC #endif /* __GNUC__ */ -/** - * Allocate dynamic memory via TLSF. - */ -void *malloc(size_t size) ALIAS(tlsf_gmalloc, (size)) - -/** - * Allocate and clear memory via TLSF - */ -void *calloc(size_t count, size_t bytes) ALIAS(tlsf_gcalloc, (count, bytes)) - -/** - * Allocate an aligned block via TLSF. - */ -void *memalign(size_t align, size_t bytes) ALIAS(tlsf_gmemalign, (align, bytes)) - -/** - * Reallocate a block of memory via TLSF. - */ -void *realloc(void *ptr, size_t size) ALIAS(tlsf_grealloc, (ptr, size)) - -/** - * Free dynamic memory allocated via TLSF. - */ -void free(void *ptr) VALIAS(tlsf_gfree, (ptr)) - -#endif /* TLSF_MALLOC_NOSYSTEM */ - int tlsf_add_global_pool(void *mem, size_t bytes) { if (gheap == NULL) { @@ -94,7 +63,10 @@ tlsf_t *_tlsf_get_global_control(void) return gheap; } -void *tlsf_gmalloc(size_t bytes) +/** + * Allocate a block of size "bytes" + */ +ATTR_MALLOC void *malloc(size_t bytes) { unsigned old_state = irq_disable(); void *result = tlsf_malloc(gheap, bytes); @@ -103,9 +75,12 @@ void *tlsf_gmalloc(size_t bytes) return result; } -void *tlsf_gcalloc(size_t count, size_t bytes) +/** + * Allocate and clear a block of size "bytes*count" + */ +ATTR_CALLOC void *calloc(size_t count, size_t bytes) { - void *result = tlsf_gmalloc(count * bytes); + void *result = malloc(count * bytes); if (result) { memset(result, 0, count * bytes); @@ -113,7 +88,10 @@ void *tlsf_gcalloc(size_t count, size_t bytes) return result; } -void *tlsf_gmemalign(size_t align, size_t bytes) +/** + * Allocate an aligned memory block. + */ +ATTR_MALIGN void *memalign(size_t align, size_t bytes) { unsigned old_state = irq_disable(); void *result = tlsf_memalign(gheap, align, bytes); @@ -122,7 +100,10 @@ void *tlsf_gmemalign(size_t align, size_t bytes) return result; } -void *tlsf_grealloc(void *ptr, size_t size) +/** + * Deallocate and reallocate with a different size. + */ +ATTR_REALLOC void *realloc(void *ptr, size_t size) { unsigned old_state = irq_disable(); void *result = tlsf_realloc(gheap, ptr, size); @@ -131,7 +112,11 @@ void *tlsf_grealloc(void *ptr, size_t size) return result; } -void tlsf_gfree(void *ptr) + +/** + * Deallocate a block of data. + */ +void free(void *ptr) { unsigned old_state = irq_disable();