Skip to content

Commit

Permalink
pkg/tlsf: remove tlsf_g* functions.
Browse files Browse the repository at this point in the history
- The tlsf-malloc functions are named malloc/free/etc and always
replace system functions if the module is enabled.
- Added GCC specific function attributes.
  • Loading branch information
jcarrano committed May 18, 2018
1 parent bec2336 commit 60431b8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 75 deletions.
33 changes: 4 additions & 29 deletions pkg/tlsf/contrib/include/tlsf-malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand All @@ -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
}
Expand Down
77 changes: 31 additions & 46 deletions pkg/tlsf/contrib/tlsf-malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -103,17 +75,23 @@ 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);
}
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);
Expand All @@ -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);
Expand All @@ -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();

Expand Down

0 comments on commit 60431b8

Please sign in to comment.