Skip to content
Snippets Groups Projects
Commit cf686bde authored by Juan Carrano's avatar Juan Carrano
Browse files

pkg/tlsf: Refactor the TLSF package and bring back original TLSF api.

- Cleanup package makefile.
- Download directly from git.
- Remove giant patch.
- Implement malloc function as a contrib package.
- Update ccn example.
- Update ps command.
parent 94541e18
No related branches found
No related tags found
No related merge requests found
...@@ -723,6 +723,10 @@ ifneq (,$(filter rdcli_common,$(USEMODULE))) ...@@ -723,6 +723,10 @@ ifneq (,$(filter rdcli_common,$(USEMODULE)))
USEMODULE += luid USEMODULE += luid
endif endif
ifneq (,$(filter tlsf-malloc,$(USEMODULE)))
USEPKG += tlsf
endif
# always select gpio (until explicit dependencies are sorted out) # always select gpio (until explicit dependencies are sorted out)
FEATURES_OPTIONAL += periph_gpio FEATURES_OPTIONAL += periph_gpio
......
...@@ -37,8 +37,7 @@ USEMODULE += timex ...@@ -37,8 +37,7 @@ USEMODULE += timex
USEMODULE += xtimer USEMODULE += xtimer
USEMODULE += random USEMODULE += random
USEMODULE += prng_xorshift USEMODULE += prng_xorshift
USEMODULE += tlsf-malloc
USEPKG += tlsf
USEPKG += ccn-lite USEPKG += ccn-lite
......
...@@ -37,7 +37,7 @@ static uint32_t _tlsf_heap[TLSF_BUFFER]; ...@@ -37,7 +37,7 @@ static uint32_t _tlsf_heap[TLSF_BUFFER];
int main(void) int main(void)
{ {
tlsf_create_with_pool(_tlsf_heap, sizeof(_tlsf_heap)); tlsf_add_global_pool(_tlsf_heap, sizeof(_tlsf_heap));
msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE); msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE);
puts("Basic CCN-Lite example"); puts("Basic CCN-Lite example");
......
PKG_NAME = tlsf PKG_NAME=tlsf
PKG_VERSION = 3.0 PKG_URL=https://github.com/mattconte/tlsf
PKG_FILE = tlsf-$(PKG_VERSION).zip PKG_VERSION=a1f743ffac0305408b39e791e0ffb45f6d9bc777
PKG_URL = http://download.riot-os.org/$(PKG_FILE) PKG_LICENSE=BSD
PKG_MD5 = 176965d58af525347e582e1630ea9604
PKG_LICENSE = PD
PKG_DIR=$(CURDIR)
PKG_BUILDDIR=$(PKGDIRBASE)/$(PKG_NAME)
PKG_SRCDIR=$(PKG_BUILDDIR)/src
.PHONY: all prepare clean distclean .PHONY: all
all: $(PKG_SRCDIR)/$(PKG_NAME).a all: Makefile.tlsf
@cp Makefile.tlsf $(PKG_BUILDDIR)/Makefile
"$(MAKE)" -C $(PKG_BUILDDIR)
prepare: $(PKG_SRCDIR)/Makefile
$(PKG_SRCDIR)/$(PKG_NAME).a: $(PKG_SRCDIR)/Makefile include $(RIOTBASE)/pkg/pkg.mk
$(Q)"$(MAKE)" -C $(<D)
$(PKG_SRCDIR)/Makefile: $(PKG_BUILDDIR)/$(PKG_FILE) $(CURDIR)/patch.txt
rm -rf $(@D)
mkdir -p $(@D)
$(Q)cd $(@D) && $(UNZIP_HERE) $(PKG_BUILDDIR)/$(PKG_FILE)
$(Q)cd $(@D) && patch --binary -p0 -N -i $(CURDIR)/patch.txt
$(PKG_BUILDDIR)/$(PKG_FILE):
@mkdir -p $(@D)
$(Q)$(DLCACHE) $(PKG_URL) $(PKG_MD5) $@
clean::
rm -rf $(PKG_SRCDIR)/
distclean::
rm -rf $(PKG_BUILDDIR)/
INCLUDES += -I$(PKGDIRBASE)/tlsf/src INCLUDES += -I$(PKGDIRBASE)/tlsf
ifneq (,$(filter tlsf-malloc,$(USEMODULE)))
INCLUDES += -I$(RIOTPKG)/tlsf/contrib/include
DIRS += $(RIOTPKG)/tlsf/contrib
endif
MODULE = tlsf
include $(RIOTBASE)/Makefile.base
MODULE := tlsf-malloc
include $(RIOTBASE)/Makefile.base
/*
* 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.
*/
/**
* @defgroup pkg_tlsf_malloc TLSF-based malloc.
* @ingroup pkg
* @ingroup sys
*
* @brief TLSF-based global memory allocator.
*
* 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. This implemetation replaces the system malloc
*
* Additionally, the calls to TLSF are wrapped in irq_disable()/irq_restore(),
* to make it thread-safe.
*
* 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
* regions they want to make available for dynamic allocation via malloc().
*
* @{
* @file
*
* @brief TLSF-based global memory allocator.
* @author René Kijewski
* @author Juan I Carrano
*
*/
#ifndef TLSF_MALLOC_H
#define TLSF_MALLOC_H
#include <stddef.h>
#include "tlsf.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* Add an area of memory to the global allocator pool.
*
* 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.
*
* @return 0 on success, nonzero on failure.
*/
int tlsf_add_global_pool(void *mem, size_t bytes);
/**
* Get a pointer to the global tlsf_control block.
*
* Use for debugging purposes only.
*/
tlsf_t *_tlsf_get_global_control(void);
#ifdef __cplusplus
}
#endif
#endif /* TLSF_MALLOC_H */
/**
* @}
*/
/*
* 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 René Kijewski
* @author Juan I Carrano
*
*/
#include <stdio.h>
#include <string.h>
#include "irq.h"
#include "tlsf.h"
#include "tlsf-malloc.h"
/**
* Global memory heap (really a collection of pools, or areas)
**/
static tlsf_t gheap = NULL;
/* TODO: Add defines for other compilers */
#ifdef __GNUC__
#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 ATTR_MALLOC
#define ATTR_CALLOC
#define ATTR_MALIGN
#define ATTR_REALLOC
#endif /* __GNUC__ */
int tlsf_add_global_pool(void *mem, size_t bytes)
{
if (gheap == NULL) {
gheap = tlsf_create_with_pool(mem, bytes);
return gheap == NULL;
}
else {
return tlsf_add_pool(gheap, mem, bytes) == NULL;
}
}
tlsf_t *_tlsf_get_global_control(void)
{
return gheap;
}
/**
* Allocate a block of size "bytes"
*/
ATTR_MALLOC void *malloc(size_t bytes)
{
unsigned old_state = irq_disable();
void *result = tlsf_malloc(gheap, bytes);
irq_restore(old_state);
return result;
}
/**
* Allocate and clear a block of size "bytes*count"
*/
ATTR_CALLOC void *calloc(size_t count, size_t bytes)
{
void *result = malloc(count * bytes);
if (result) {
memset(result, 0, count * bytes);
}
return result;
}
/**
* 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);
irq_restore(old_state);
return result;
}
/**
* 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);
irq_restore(old_state);
return result;
}
/**
* Deallocate a block of data.
*/
void free(void *ptr)
{
unsigned old_state = irq_disable();
tlsf_free(gheap, ptr);
irq_restore(old_state);
}
/**
* @}
*/
/**
* @defgroup pkg_tlsf Two-Level Segregated Fit memory allocator
* @ingroup pkg
* @brief TLSF is a general purpose dynamic memory allocator specifically
* designed to meet real-time requirements:
* @see http://www.gii.upv.es/tlsf/
* @see https://github.com/mattconte/tlsf
*
* TLSF provides an implementation of malloc/realloc/free/etc with the following
* characteristics:
*
* - O(1) Performance
* - Works on a user supplied block of memory instead of a global heap.
* - Efficient both in terms of memory overhead and processor time.
* - Low fragmentation.
*
* Additionally, a contrib package @see pkg_tlsf_malloc implements a global heap
* allocator with the standard malloc/free functions.
*
* @todo The tlsf code uses printf to report errors. This is not OK.
*/
This diff is collapsed.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
* *
* @note You should prefer statically allocated memory whenever possible. * @note You should prefer statically allocated memory whenever possible.
* *
* @see pkg_tlsf
*
* @{ * @{
* @file * @file
* *
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#ifdef MODULE_TLSF #ifdef MODULE_TLSF
#include "tlsf.h" #include "tlsf.h"
#include "tlsf-malloc.h"
#endif #endif
/* list of states copied from tcb.h */ /* list of states copied from tcb.h */
...@@ -146,7 +147,7 @@ void ps(void) ...@@ -146,7 +147,7 @@ void ps(void)
overall_stacksz, overall_used); overall_stacksz, overall_used);
# ifdef MODULE_TLSF # ifdef MODULE_TLSF
puts("\nHeap usage:"); puts("\nHeap usage:");
tlsf_walk_pool(NULL); tlsf_walk_pool(tlsf_get_pool(_tlsf_get_global_control()), NULL, NULL);
# endif # endif
#endif #endif
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment