Skip to content
This repository has been archived by the owner on Jan 7, 2019. It is now read-only.

Commit

Permalink
[examples] Add TLSF allocator example.
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Apr 16, 2016
1 parent 0b457d6 commit ba243c9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/stm32f469_discovery/tlsf-allocator/SConstruct
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# path to the xpcc root directory
xpccpath = '../../..'
# execute the common SConstruct file
execfile(xpccpath + '/scons/SConstruct')
54 changes: 54 additions & 0 deletions examples/stm32f469_discovery/tlsf-allocator/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <xpcc/architecture/platform.hpp>
#include <tlsf.h>

extern tlsf_t tlsf_heap0;
extern uint32_t __heap1_start;

int main()
{
Board::initialize();
Board::Leds::setOutput();

// 200 pointers to allocated memories
void *d[200];
int free_ii = 0;

for (int ii=0; ii < 200; ii++)
{
d[ii] = nullptr;
uint32_t size = rand() % (1024*20);

if ((rand() % 100) >= 75) {
d[ii] = tlsf_malloc(tlsf_heap0, size);
// explicit allocation in CCM
XPCC_LOG_INFO << ".heap0 ";
} else {
d[ii] = malloc(size);
if (d[ii] && d[ii] < &__heap1_start) {
// allocation on SRAM has failed and fallback uses CCM!
XPCC_LOG_INFO << ".heap0FB";
}
else XPCC_LOG_INFO << ".heap123";
}

// print what size we requested and if it succeeded
XPCC_LOG_INFO.printf(" malloc(%2ukB) = ", size/1024);
if (d[ii]) XPCC_LOG_INFO << d[ii];
else XPCC_LOG_INFO << "NO MEMORY ";

if ((rand() % 100) >= 50) {
// only some memory is returned to the system
void* df = d[free_ii++];
XPCC_LOG_INFO << " ...freeing " << df;
free(df);
}
XPCC_LOG_INFO << xpcc::endl;
}

while (1)
{
Board::Leds::toggle();
xpcc::delayMilliseconds(500);
}
return 0;
}
6 changes: 6 additions & 0 deletions examples/stm32f469_discovery/tlsf-allocator/project.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[build]
board = stm32f469_discovery
buildpath = ${xpccpath}/build/stm32f469_discovery/${name}

[parameters]
uart.stm32.3.tx_buffer = 2048

0 comments on commit ba243c9

Please sign in to comment.