1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/pkg/tlsf/contrib/tlsf-malloc.c
Juan Carrano cc907fa54e pkg/tlsf: Fix the way system functions are overriden.
The correct way to overrride the malloc family of functions in newlib-nano is
to provide the *_r (reentrant) variants. Newlib implements the "normal"
functions on top of these (see the newlib source code). Also, internally it calls
the *_r functions when allocating buffers.

If only the "normal" non-reentrant functions are provided this will mean that
some of the code will still use the vanilla newlib allocator. Furthermore, if
one uses the whole heap as a pool for TLSF then the system may in the best case
crash as there is no enough memory for its internall allocations or in the worst
case function eratically (this depends on how the heap reserved, there is an
upcomming series of commits in that direction).

This commit splits the handling between newlib and native. It also prepares the
ground for future work on the pool initialization.

Right now I could only test this in ARM and native and I cannot ensure it will
work on other platforms. Replacing the system's memory allocator is not something
that can be taken lightly and will inevitably require diving into the depths of
the libc. Therefore I would say that using TLSF as a system wide allocator is ATM
supported officially only on those plaftorms.

Testing:

Aside from reading the newlib sources, you can see the issue in a live system
using the debugger.

Compile any example (with or without tlsf-malloc), grab a debugger and place
a breakpoint in sbrk and _sbrk_r. Doing a backtrace will reveal it gets called
by _malloc_r.
2019-09-17 21:18:50 +02:00

62 lines
1.3 KiB
C

/*
* Copyright (C) 2014-2018 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup pkg_tlsf_malloc
* @ingroup pkg
* @ingroup sys
* @{
* @file
*
* @brief TLSF-based global memory allocator.
* @author Juan I Carrano
*
*/
#include <stdio.h>
#include "tlsf.h"
#include "tlsf-malloc.h"
#include "tlsf-malloc-internal.h"
/**
* Global memory heap (really a collection of pools, or areas)
**/
tlsf_t tlsf_malloc_gheap = NULL;
int tlsf_add_global_pool(void *mem, size_t bytes)
{
if (tlsf_malloc_gheap == NULL) {
tlsf_malloc_gheap = tlsf_create_with_pool(mem, bytes);
return tlsf_malloc_gheap == NULL;
}
else {
return tlsf_add_pool(tlsf_malloc_gheap, mem, bytes) == NULL;
}
}
tlsf_t _tlsf_get_global_control(void)
{
return tlsf_malloc_gheap;
}
void tlsf_size_walker(void* ptr, size_t size, int used, void* user)
{
printf("\t%p %s size: %u (%p)\n", ptr, used ? "used" : "free", (unsigned int)size, ptr);
if (used) {
((tlsf_size_container_t *)user)->used += (unsigned int)size;
}
else {
((tlsf_size_container_t *)user)->free += (unsigned int)size;
}
}
/**
* @}
*/