-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
MODULE = tlsf | ||
|
||
include $(RIOTBASE)/Makefile.base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
MODULE := tlsf-malloc | ||
|
||
include $(RIOTBASE)/Makefile.base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* 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 */ | ||
|
||
/** | ||
* @} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
/** | ||
* @} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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