Skip to content

Commit

Permalink
Re #1643:
Browse files Browse the repository at this point in the history
* re-enabled usage of newlib-nano with cmdline option NANO=YES
* added dump of newlib-nano allocator free chunks list
* added newlib-nano allocator fix if cmdline option FIX_NEWLIB_NANO_ALLOCATOR=YES
  • Loading branch information
projectkk2glider committed Sep 2, 2014
1 parent 09d7e9b commit 0f9a02a
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 4 deletions.
26 changes: 24 additions & 2 deletions radio/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,18 @@ USB = JOYSTICK
# NO - normal behaviour, show real channel values
CHANNELS_MONITOR_INV_HIDE = NO

# Use newlib-nano
# Values = NO, YES
NANO = NO


# Modify newlib-nano allocator
# Values = NO, YES
# YES - when all allocated memory is freed and all free
# memory is in one free chunk, remove that chunk
# and restore heap to zero size
FIX_NEWLIB_NANO_ALLOCATOR = NO

#------- END BUILD OPTIONS ---------------------------

# Define programs and commands.
Expand Down Expand Up @@ -657,6 +669,9 @@ ifeq ($(PCB), TARANIS)
else
CPPDEFS = -DREV4
endif
ifeq ($(FIX_NEWLIB_NANO_ALLOCATOR), YES)
CPPDEFS = -DFIX_NEWLIB_NANO_ALLOCATOR
endif
LDSCRIPT = targets/taranis/stm32_flash_bl.ld
TRGT = arm-none-eabi-
MCU = cortex-m3
Expand Down Expand Up @@ -1116,6 +1131,13 @@ MATH_LIB = -lm
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
LDFLAGS += $(MATH_LIB)


ifeq ($(NANO), YES)
#use newlib-nano for linking
NEWLIB_NANO_FLAGS = --specs=nano.specs -u _printf_float
CPPDEFS += -DNANO
endif

# Define Messages
# English
MSG_BEGIN = -------- begin --------
Expand Down Expand Up @@ -1278,7 +1300,7 @@ bitmaps/sticks.lbm: bitmaps/sticks_4x1.xbm bitmaps/splash_9x.xbm bitmaps/splash_

lua_exports.cpp: myeeprom.h ../util/luaexport.py
@echo "Generating a list of Lua exported constants in lua_exports.txt"
@$(CPP) -DPCBTARANIS -DCPUARM -DLUA -DEXPORT myeeprom.h | grep LEXP > lua_exports.txt
@$(CPP) -DPCBTARANIS -DCPUARM -DLUA -DEXPORT myeeprom.h | grep LEXP > lua_exports.txt
@echo "Parsing and generating C Lua exports in lua_exports.cpp"
@python ../util/luaexport.py $(VERSION) lua_exports.txt lua_exports.cpp lua_fields.txt
@rm lua_exports.txt
Expand Down Expand Up @@ -1390,7 +1412,7 @@ OBJS = $(TMP:.s=.o)
@echo
@echo $(MSG_COMPILING) $@
$(CC) $(ARMCPPFLAGS) $< -o allsrc.o
$(CC) $(OBJS) allsrc.o -mcpu=cortex-m3 -mthumb -nostartfiles -lm -T$(LDSCRIPT) -Wl,-Map=$(TARGET).map,--cref,--no-warn-mismatch,--gc-sections -o $@
$(CC) $(OBJS) allsrc.o -mcpu=cortex-m3 -mthumb -nostartfiles -lm -T$(LDSCRIPT) -Wl,-Map=$(TARGET).map,--cref,--no-warn-mismatch,--gc-sections $(NEWLIB_NANO_FLAGS) -o $@
endif

# Target: clean project.
Expand Down
77 changes: 77 additions & 0 deletions radio/src/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,79 @@ static void dispw_256( register uint32_t address, register uint32_t lines )
}
}

#if defined(NANO)
typedef struct malloc_chunk
{
/* ------------------
* chunk->| size (4 bytes) |
* ------------------
* | Padding for |
* | alignment |
* | holding neg |
* | offset to size |
* ------------------
* mem_ptr->| point to next |
* | free when freed|
* | or data load |
* | when allocated |
* ------------------
*/
/* size of the allocated payload area, including size before
CHUNK_OFFSET */
long size;

/* since here, the memory is either the next free block, or data load */
struct malloc_chunk * next;
}chunk;

#define free_list __malloc_free_list
#define sbrk_start __malloc_sbrk_start

extern chunk * free_list;
extern char * sbrk_start;
extern Fifo<512> uart3TxFifo;

void dumpFreeMemory()
{
chunk * pf;
size_t free_size = 0;
size_t total_size;

if (sbrk_start == NULL) total_size = 0;
else total_size = (size_t) ((char*)heap - sbrk_start);

uart3TxFifo.flush();
TRACE("mallinfo:");
char * prev_end = sbrk_start;
for (pf = free_list; pf; pf = pf->next) {
free_size += pf->size;
TRACE("\t%6d %p[%d]", prev_end ? ((char*)pf - prev_end):0, pf, pf->size);
prev_end = (char*)pf + pf->size;
uart3TxFifo.flush();
}

TRACE("\tTotal size: %d", total_size);
TRACE("\tFree size: %d", free_size);
TRACE("\tUsed size: %d", total_size - free_size);
uart3TxFifo.flush();
}

extern int _end;

void freeFreeMemory()
{
//TODO this functio should be atomic
if ((char*)free_list == sbrk_start) {
TRACE("feeing all memory");
free_list = 0;
heap = (unsigned char *)&_end;
}
}

#else // #if defined(NANO)
#define dumpFreeMemory()
#endif // #if defined(NANO)

void debugTask(void* pdata)
{
uint8_t rxchar ;
Expand Down Expand Up @@ -212,6 +285,10 @@ void debugTask(void* pdata)
crlf();
}

if ( rxchar == 'F' )
{
dumpFreeMemory();
}
}
}
#endif
9 changes: 9 additions & 0 deletions radio/src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ extern "C" {
void debugPuts(const char *string, ...);
void dump(unsigned char *data, unsigned int size);

#if defined(NANO)
void dumpFreeMemory();
void freeFreeMemory();
#else
#define dumpFreeMemory()
#define freeFreeMemory()
#endif

#ifdef __cplusplus
}
#endif
Expand All @@ -94,6 +102,7 @@ void dump(unsigned char *data, unsigned int size);

void debugTask(void* pdata);


#else

#define TRACE(...) { }
Expand Down
8 changes: 7 additions & 1 deletion radio/src/fifo.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Fifo
}

bool pop(uint8_t & byte) {
if (ridx == widx) {
if (empty()) {
return false;
}
else {
Expand All @@ -65,6 +65,12 @@ class Fifo
return true;
}
}
bool empty() {
return (ridx == widx);
}
void flush() {
while (!empty()) {};
}

protected:
uint8_t fifo[N];
Expand Down
4 changes: 4 additions & 0 deletions radio/src/lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,10 @@ void luaClose()
lua_close(L);
L = 0;
TRACE("lua_close end");
dumpFreeMemory();
#if defined(FIX_NEWLIB_NANO_ALLOCATOR)
freeFreeMemory();
#endif
}
}

Expand Down
4 changes: 4 additions & 0 deletions radio/src/lua/src/lauxlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,10 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
else {
void * res = realloc(ptr, nsize);
// TRACE("realloc %p[%lu] -> %p[%lu]", ptr, osize, res, nsize);
if (res == 0 ){
TRACE("realloc FAILURE %u", nsize);
dumpFreeMemory();
}
return res;
}
}
Expand Down
3 changes: 2 additions & 1 deletion radio/src/targets/sky9x/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <sys/stat.h>
#include <errno.h>

#include "debug.h"
/*----------------------------------------------------------------------------
* Exported variables
*----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -143,7 +144,7 @@ extern int _write( int file, char *ptr, int len )

extern void _exit( int status )
{
printf( "Exiting with status %d.\n", status ) ;
TRACE( "Exiting with status %d.\n", status ) ;

for ( ; ; ) ;
}
Expand Down

0 comments on commit 0f9a02a

Please sign in to comment.