Skip to content

Commit

Permalink
Add TLSF (two level segregated fit) PKG
Browse files Browse the repository at this point in the history
This is my second take on #669, because I was asked to separate it from #764.

This change adds a malloc implementation as a PKG, which uses *TLSF* (two
level segregated fit).

The patch file removes the 64bit capatibilities, debug functions, and the
option to have multiple "control blocks" (a control block holds multiple
memory pools). It wraps `malloc()` and friends in `disableIRQ() … restoreIRQ()`.

The implemention does not support 16bit platforms, yet, but probably only some
constants would need fixing. I limited the maximum size of a memory pool to
2**30 bytes = 1GB.

This PKG is not meant to be used by applicitions directly, but by the boards.
The board's initialition code needs to call
`int tlsf_add_pool(void *mem, size_t bytes)` for every free memory region it
has. If the board in using newlib, then this call needs to happen before the
first call to `puts`, `printf`, and friends, because newlib allocates the
control data IO streams (stdin, stdout, stderr) on the heap. Adding a small
(e.g. 1kB) pool before proper board initialization would be a possible solution.

Please read the additional information in the website of the implementator,
http://tlsf.baisoku.org/:

> TLSF (two level segregated fit) is a relatively new memory
allocator designed for embedded systems. It boasts constant
time O(1) malloc/free response time and a 4-byte block
overhead. Though it typically is slightly slower than other
allocators such as dlmalloc, it has no worst-case behavior.

> The original implementation, which comes alongside the white
paper, is distributed under the GNU GPL/LGPL. The code found
here is an original implementation, released into the public
domain, therefore is not subject to any licensing restrictions.

> Features:
 -  O(1) cost for malloc, free, realloc, memalign
 -  Extremely low overhead per allocation (4 bytes)
 -  Low overhead per pool (~3kB)
 -  Low fragmentation
 -  Compiles to only a few kB of code and data

> Caveats:
 -  Currently, assumes architecture can make 4-byte aligned accesses
 -  Not designed to be thread safe; the user must provide this

> Known Issues:
Due to the internal block structure size and the implementation
details of tlsf_memalign, there is worst-case behavior when requesting
small (<16 byte) blocks aligned to 8-byte boundaries. Overuse of memalign
will generally increase fragmentation, but this particular case will leave
lots of unusable "holes" in the heap. The solution would be to internally
align all blocks to 8 bytes, but this will require significantl changes
to the implementation. Contact me if you are interested.
  • Loading branch information
Kijewski committed May 20, 2014
1 parent 05a3570 commit 88e8915
Show file tree
Hide file tree
Showing 4 changed files with 679 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/tlsf/.gitignore
@@ -0,0 +1 @@
/tlsf-*.zip
41 changes: 41 additions & 0 deletions pkg/tlsf/Makefile
@@ -0,0 +1,41 @@
PKG_NAME = tlsf
PKG_VERSION = 3.0
PKG_FILE = tlsf-$(PKG_VERSION).zip
PKG_URL = http://tlsf.baisoku.org/$(PKG_FILE)

ifeq (, $(UNZIP))
ifeq (0, $(shell which unzip 2>&1 > /dev/null ; echo $$?))
UNZIP := $(shell which unzip)
else
ifeq (0, $(shell which 7z 2>&1 > /dev/null ; echo $$?))
UNZIP := $(shell which 7z) x
else
$(error "Neither unzip nor 7z is installed.")
endif
endif
endif

.PHONY: all clean patch reset

all: $(BINDIR)$(PKG_NAME).a

$(BINDIR)$(PKG_NAME).a: $(BINDIR)$(PKG_NAME)-src/Makefile
$(AD)make -C $(<D)

$(BINDIR)$(PKG_NAME)-src/Makefile: $(CURDIR)/$(PKG_FILE) $(CURDIR)/patch.txt
@rm -rf $(@D)
@mkdir -p $(@D)
$(AD)cd $(@D) && $(UNZIP) $(CURDIR)/$(PKG_FILE)
$(AD)cd $(@D) && patch --binary -p0 -N -i $(CURDIR)/patch.txt

$(CURDIR)/$(PKG_FILE):
$(AD)wget -nv -c -O $@ $(PKG_URL)

clean::
rm -rf $(BINDIR)$(PKG_NAME)-src/

distclean:: clean
rm -f $(CURDIR)/$(PKG_FILE)

Makefile.include:
@true
1 change: 1 addition & 0 deletions pkg/tlsf/Makefile.include
@@ -0,0 +1 @@
INCLUDES += -I$(BINDIR)tlsf-src

0 comments on commit 88e8915

Please sign in to comment.