Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/tlsf: revert to original api #9006

Merged
merged 1 commit into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,10 @@ ifneq (,$(filter rdcli_common,$(USEMODULE)))
USEMODULE += luid
endif

ifneq (,$(filter tlsf-malloc,$(USEMODULE)))
USEPKG += tlsf
endif

# always select gpio (until explicit dependencies are sorted out)
FEATURES_OPTIONAL += periph_gpio

Expand Down
3 changes: 1 addition & 2 deletions examples/ccn-lite-relay/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ USEMODULE += timex
USEMODULE += xtimer
USEMODULE += random
USEMODULE += prng_xorshift

USEPKG += tlsf
USEMODULE += tlsf-malloc

USEPKG += ccn-lite

Expand Down
2 changes: 1 addition & 1 deletion examples/ccn-lite-relay/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static uint32_t _tlsf_heap[TLSF_BUFFER];

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);

puts("Basic CCN-Lite example");
Expand Down
39 changes: 9 additions & 30 deletions pkg/tlsf/Makefile
Original file line number Diff line number Diff line change
@@ -1,34 +1,13 @@
PKG_NAME = tlsf
PKG_VERSION = 3.0
PKG_FILE = tlsf-$(PKG_VERSION).zip
PKG_URL = http://download.riot-os.org/$(PKG_FILE)
PKG_MD5 = 176965d58af525347e582e1630ea9604
PKG_LICENSE = PD
PKG_DIR=$(CURDIR)
PKG_BUILDDIR=$(PKGDIRBASE)/$(PKG_NAME)
PKG_SRCDIR=$(PKG_BUILDDIR)/src
PKG_NAME=tlsf
PKG_URL=https://github.com/mattconte/tlsf
PKG_VERSION=a1f743ffac0305408b39e791e0ffb45f6d9bc777
PKG_LICENSE=BSD

.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
$(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)/
include $(RIOTBASE)/pkg/pkg.mk
7 changes: 6 additions & 1 deletion pkg/tlsf/Makefile.include
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
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
3 changes: 3 additions & 0 deletions pkg/tlsf/Makefile.tlsf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MODULE = tlsf

include $(RIOTBASE)/Makefile.base
3 changes: 3 additions & 0 deletions pkg/tlsf/contrib/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MODULE := tlsf-malloc

include $(RIOTBASE)/Makefile.base
78 changes: 78 additions & 0 deletions pkg/tlsf/contrib/include/tlsf-malloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, I think this could be in auto_init modules. For the CCN example, this avoids the need to call tlsf_add_global_pool. If so, this function could be renamed to tlsf_malloc_init. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with auto init. When initializing, tlsf_add_global_pool should be called with an address that depends on the memory map of the chip.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it OK to merge this without auto init support? Maybe it can be the subject of a future PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, sure. Could be done in further PRs

* 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 */

/**
* @}
*/
129 changes: 129 additions & 0 deletions pkg/tlsf/contrib/tlsf-malloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* 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);
}

/**
* @}
*/
21 changes: 21 additions & 0 deletions pkg/tlsf/doc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @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.
*/
Loading