Skip to content

Commit f307314

Browse files
authored
Merge pull request #9006 from jcarrano/tlsf-original-api
pkg/tlsf: revert to original api
2 parents 94541e1 + cf686bd commit f307314

File tree

14 files changed

+284
-613
lines changed

14 files changed

+284
-613
lines changed

Makefile.dep

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,10 @@ ifneq (,$(filter rdcli_common,$(USEMODULE)))
723723
USEMODULE += luid
724724
endif
725725

726+
ifneq (,$(filter tlsf-malloc,$(USEMODULE)))
727+
USEPKG += tlsf
728+
endif
729+
726730
# always select gpio (until explicit dependencies are sorted out)
727731
FEATURES_OPTIONAL += periph_gpio
728732

examples/ccn-lite-relay/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ USEMODULE += timex
3737
USEMODULE += xtimer
3838
USEMODULE += random
3939
USEMODULE += prng_xorshift
40-
41-
USEPKG += tlsf
40+
USEMODULE += tlsf-malloc
4241

4342
USEPKG += ccn-lite
4443

examples/ccn-lite-relay/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static uint32_t _tlsf_heap[TLSF_BUFFER];
3737

3838
int main(void)
3939
{
40-
tlsf_create_with_pool(_tlsf_heap, sizeof(_tlsf_heap));
40+
tlsf_add_global_pool(_tlsf_heap, sizeof(_tlsf_heap));
4141
msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE);
4242

4343
puts("Basic CCN-Lite example");

pkg/tlsf/Makefile

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,13 @@
1-
PKG_NAME = tlsf
2-
PKG_VERSION = 3.0
3-
PKG_FILE = tlsf-$(PKG_VERSION).zip
4-
PKG_URL = http://download.riot-os.org/$(PKG_FILE)
5-
PKG_MD5 = 176965d58af525347e582e1630ea9604
6-
PKG_LICENSE = PD
7-
PKG_DIR=$(CURDIR)
8-
PKG_BUILDDIR=$(PKGDIRBASE)/$(PKG_NAME)
9-
PKG_SRCDIR=$(PKG_BUILDDIR)/src
1+
PKG_NAME=tlsf
2+
PKG_URL=https://github.com/mattconte/tlsf
3+
PKG_VERSION=a1f743ffac0305408b39e791e0ffb45f6d9bc777
4+
PKG_LICENSE=BSD
105

11-
.PHONY: all prepare clean distclean
6+
.PHONY: all
127

13-
all: $(PKG_SRCDIR)/$(PKG_NAME).a
8+
all: Makefile.tlsf
9+
@cp Makefile.tlsf $(PKG_BUILDDIR)/Makefile
10+
"$(MAKE)" -C $(PKG_BUILDDIR)
1411

15-
prepare: $(PKG_SRCDIR)/Makefile
1612

17-
$(PKG_SRCDIR)/$(PKG_NAME).a: $(PKG_SRCDIR)/Makefile
18-
$(Q)"$(MAKE)" -C $(<D)
19-
20-
$(PKG_SRCDIR)/Makefile: $(PKG_BUILDDIR)/$(PKG_FILE) $(CURDIR)/patch.txt
21-
rm -rf $(@D)
22-
mkdir -p $(@D)
23-
$(Q)cd $(@D) && $(UNZIP_HERE) $(PKG_BUILDDIR)/$(PKG_FILE)
24-
$(Q)cd $(@D) && patch --binary -p0 -N -i $(CURDIR)/patch.txt
25-
26-
$(PKG_BUILDDIR)/$(PKG_FILE):
27-
@mkdir -p $(@D)
28-
$(Q)$(DLCACHE) $(PKG_URL) $(PKG_MD5) $@
29-
30-
clean::
31-
rm -rf $(PKG_SRCDIR)/
32-
33-
distclean::
34-
rm -rf $(PKG_BUILDDIR)/
13+
include $(RIOTBASE)/pkg/pkg.mk

pkg/tlsf/Makefile.include

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
INCLUDES += -I$(PKGDIRBASE)/tlsf/src
1+
INCLUDES += -I$(PKGDIRBASE)/tlsf
2+
3+
ifneq (,$(filter tlsf-malloc,$(USEMODULE)))
4+
INCLUDES += -I$(RIOTPKG)/tlsf/contrib/include
5+
DIRS += $(RIOTPKG)/tlsf/contrib
6+
endif

pkg/tlsf/Makefile.tlsf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
MODULE = tlsf
2+
3+
include $(RIOTBASE)/Makefile.base

pkg/tlsf/contrib/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
MODULE := tlsf-malloc
2+
3+
include $(RIOTBASE)/Makefile.base
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (C) 2014-2018 Freie Universität Berlin
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser
5+
* General Public License v2.1. See the file LICENSE in the top level
6+
* directory for more details.
7+
*/
8+
/**
9+
* @defgroup pkg_tlsf_malloc TLSF-based malloc.
10+
* @ingroup pkg
11+
* @ingroup sys
12+
*
13+
* @brief TLSF-based global memory allocator.
14+
*
15+
* This is a malloc/free implementation built on top of the TLSF allocator.
16+
* It defines a global tlsf_control block and performs allocations on that
17+
* block. This implemetation replaces the system malloc
18+
*
19+
* Additionally, the calls to TLSF are wrapped in irq_disable()/irq_restore(),
20+
* to make it thread-safe.
21+
*
22+
* If this module is used as the system memory allocator, then the global memory
23+
* control block should be initialized as the first thing before the stdlib is
24+
* used. Boards should use tlsf_add_global_pool() at startup to add all the memory
25+
* regions they want to make available for dynamic allocation via malloc().
26+
*
27+
* @{
28+
* @file
29+
*
30+
* @brief TLSF-based global memory allocator.
31+
* @author René Kijewski
32+
* @author Juan I Carrano
33+
*
34+
*/
35+
36+
#ifndef TLSF_MALLOC_H
37+
#define TLSF_MALLOC_H
38+
39+
#include <stddef.h>
40+
#include "tlsf.h"
41+
42+
#ifdef __cplusplus
43+
extern "C" {
44+
#endif
45+
46+
/**
47+
* Add an area of memory to the global allocator pool.
48+
*
49+
* The first time this function is called, it will automatically perform a
50+
* tlsf_create() on the global tlsf_control block.
51+
*
52+
* @warning If this module is used, then this function MUST be called at least
53+
* once, before any allocations take place.
54+
*
55+
* @param mem Pointer to memory area. Should be aligned to 4 bytes.
56+
* @param bytes Size in bytes of the memory area.
57+
*
58+
* @return 0 on success, nonzero on failure.
59+
*/
60+
int tlsf_add_global_pool(void *mem, size_t bytes);
61+
62+
/**
63+
* Get a pointer to the global tlsf_control block.
64+
*
65+
* Use for debugging purposes only.
66+
*/
67+
tlsf_t *_tlsf_get_global_control(void);
68+
69+
70+
#ifdef __cplusplus
71+
}
72+
#endif
73+
74+
#endif /* TLSF_MALLOC_H */
75+
76+
/**
77+
* @}
78+
*/

pkg/tlsf/contrib/tlsf-malloc.c

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright (C) 2014-2018 Freie Universität Berlin
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser
5+
* General Public License v2.1. See the file LICENSE in the top level
6+
* directory for more details.
7+
*/
8+
/**
9+
* @ingroup pkg_tlsf_malloc
10+
* @ingroup pkg
11+
* @ingroup sys
12+
* @{
13+
* @file
14+
*
15+
* @brief TLSF-based global memory allocator.
16+
* @author René Kijewski
17+
* @author Juan I Carrano
18+
*
19+
*/
20+
21+
#include <stdio.h>
22+
#include <string.h>
23+
24+
#include "irq.h"
25+
#include "tlsf.h"
26+
#include "tlsf-malloc.h"
27+
28+
/**
29+
* Global memory heap (really a collection of pools, or areas)
30+
**/
31+
static tlsf_t gheap = NULL;
32+
33+
/* TODO: Add defines for other compilers */
34+
#ifdef __GNUC__
35+
36+
#define ATTR_MALLOC __attribute__((malloc, alloc_size(1)))
37+
#define ATTR_CALLOC __attribute__((malloc, alloc_size(1,2)))
38+
#define ATTR_MALIGN __attribute__((alloc_align(1), alloc_size(2), malloc))
39+
#define ATTR_REALLOC __attribute__((alloc_size(2)))
40+
41+
#else /* No GNU C -> no alias attribute */
42+
43+
#define ATTR_MALLOC
44+
#define ATTR_CALLOC
45+
#define ATTR_MALIGN
46+
#define ATTR_REALLOC
47+
48+
#endif /* __GNUC__ */
49+
50+
int tlsf_add_global_pool(void *mem, size_t bytes)
51+
{
52+
if (gheap == NULL) {
53+
gheap = tlsf_create_with_pool(mem, bytes);
54+
return gheap == NULL;
55+
}
56+
else {
57+
return tlsf_add_pool(gheap, mem, bytes) == NULL;
58+
}
59+
}
60+
61+
tlsf_t *_tlsf_get_global_control(void)
62+
{
63+
return gheap;
64+
}
65+
66+
/**
67+
* Allocate a block of size "bytes"
68+
*/
69+
ATTR_MALLOC void *malloc(size_t bytes)
70+
{
71+
unsigned old_state = irq_disable();
72+
void *result = tlsf_malloc(gheap, bytes);
73+
74+
irq_restore(old_state);
75+
return result;
76+
}
77+
78+
/**
79+
* Allocate and clear a block of size "bytes*count"
80+
*/
81+
ATTR_CALLOC void *calloc(size_t count, size_t bytes)
82+
{
83+
void *result = malloc(count * bytes);
84+
85+
if (result) {
86+
memset(result, 0, count * bytes);
87+
}
88+
return result;
89+
}
90+
91+
/**
92+
* Allocate an aligned memory block.
93+
*/
94+
ATTR_MALIGN void *memalign(size_t align, size_t bytes)
95+
{
96+
unsigned old_state = irq_disable();
97+
void *result = tlsf_memalign(gheap, align, bytes);
98+
99+
irq_restore(old_state);
100+
return result;
101+
}
102+
103+
/**
104+
* Deallocate and reallocate with a different size.
105+
*/
106+
ATTR_REALLOC void *realloc(void *ptr, size_t size)
107+
{
108+
unsigned old_state = irq_disable();
109+
void *result = tlsf_realloc(gheap, ptr, size);
110+
111+
irq_restore(old_state);
112+
return result;
113+
}
114+
115+
116+
/**
117+
* Deallocate a block of data.
118+
*/
119+
void free(void *ptr)
120+
{
121+
unsigned old_state = irq_disable();
122+
123+
tlsf_free(gheap, ptr);
124+
irq_restore(old_state);
125+
}
126+
127+
/**
128+
* @}
129+
*/

pkg/tlsf/doc.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @defgroup pkg_tlsf Two-Level Segregated Fit memory allocator
3+
* @ingroup pkg
4+
* @brief TLSF is a general purpose dynamic memory allocator specifically
5+
* designed to meet real-time requirements:
6+
* @see http://www.gii.upv.es/tlsf/
7+
* @see https://github.com/mattconte/tlsf
8+
*
9+
* TLSF provides an implementation of malloc/realloc/free/etc with the following
10+
* characteristics:
11+
*
12+
* - O(1) Performance
13+
* - Works on a user supplied block of memory instead of a global heap.
14+
* - Efficient both in terms of memory overhead and processor time.
15+
* - Low fragmentation.
16+
*
17+
* Additionally, a contrib package @see pkg_tlsf_malloc implements a global heap
18+
* allocator with the standard malloc/free functions.
19+
*
20+
* @todo The tlsf code uses printf to report errors. This is not OK.
21+
*/

0 commit comments

Comments
 (0)