1
0
mirror of https://github.com/elua/elua.git synced 2025-01-08 20:56:17 +08:00

Finally added support for multiple memory spaces (preliminary). It works on my

LPC2888 board. The allocator used is dlmalloc, just as in Newlib, but it's a 
newer version than can handle non-contiguous memory spaces (2.8.3, as opposed
to 2.6.4 in Newlib 1.16.0, I really have no idea why they're using such an
ancient version of dlmalloc). To use it add "allocator=multiple" to your scons
command line (default for LPC2888).
This commit is contained in:
Bogdan Marinescu 2008-08-16 22:27:02 +00:00
parent 660418c23d
commit 7f7315adfd
4 changed files with 5161 additions and 10 deletions

View File

@ -1,6 +1,7 @@
import os, sys
target = ARGUMENTS.get( 'target', 'lua' ).lower()
cputype = ARGUMENTS.get( 'cpu', 'at91sam7x256' ).lower()
allocator = ARGUMENTS.get( 'allocator', '' ).lower()
# List of platform/CPU combinations
cpu_list = { 'at91sam7x' : [ 'at91sam7x256', 'at91sam7x512' ],
@ -25,9 +26,23 @@ else:
print cpu,
print
sys.exit( -1 )
# CPU/allocator mapping (if allocator not specified)
if allocator == '':
if cputype in [ 'lpc2888' ]:
allocator = 'multiple'
else:
allocator = 'newlib'
elif allocator not in [ 'newlib', 'multiple' ]:
print "Unknown allocator", allocator
print "Allocator can be either 'newlib' or 'multiple'"
sys.exit( -1 )
output = 'elua_' + target + '_' + cputype
cdefs = '-D%s' % cputype
if allocator == 'multiple':
cdefs = cdefs + " -DUSE_MULTIPLE_ALLOCATOR"
# Lua source files and include path
lua_files = """lapi.c lcode.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c lmem.c lobject.c lopcodes.c
@ -49,7 +64,7 @@ local_include = local_include + " -Isrc/modules -Isrc/platform/%s" % platform
local_libs = ''
# Application files
app_files = " src/romfs.c src/main.c src/xmodem.c src/shell.c src/term.c"
app_files = " src/romfs.c src/main.c src/xmodem.c src/shell.c src/term.c src/malloc.c"
# Newlib related files
newlib_files = " src/newlib/devman.c src/newlib/stubs.c src/newlib/genstd.c"

1149
inc/malloc.h Normal file

File diff suppressed because it is too large Load Diff

3926
src/malloc.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -9,6 +9,7 @@
#include "devman.h"
#include "ioctl.h"
#include "platform.h"
#include "malloc.h"
// Utility function: look in the device manager table and find the index
// for the given name. Returns an index into the device structure, -1 if error.
@ -176,29 +177,55 @@ _ssize_t _write_r( struct _reent *r, int file, const void *ptr, size_t len )
}
// *****************************************************************************
// _sbrk_r
// _sbrk_r (newlib) / elua_sbrk (multiple)
static char *heap_ptr;
static int mem_index;
#ifdef USE_MULTIPLE_ALLOCATOR
void* elua_sbrk( ptrdiff_t incr )
#else
void* _sbrk_r( struct _reent* r, ptrdiff_t incr )
#endif
{
char* ptr;
void* ptr;
// If increment is 0 return the current break
if( incr == 0 )
ptr = heap_ptr;
else
return heap_ptr;
// Otherwise ask the platform about our memory space (if needed)
// We do this for all our memory spaces
while( 1 )
{
if( heap_ptr == NULL )
heap_ptr = ( char* )platform_get_first_free_ram( mem_index );
// If no more memory spaces are available, return with error
if( heap_ptr == NULL )
heap_ptr = ( char* )platform_get_first_free_ram( 0 );
if( heap_ptr + incr > ( char* )platform_get_last_free_ram( 0 ) )
{
ptr = ( void* )-1;
break;
}
// Do we have space in the current memory space?
if( heap_ptr + incr > ( char* )platform_get_last_free_ram( mem_index ) )
{
// We don't, so increment our memory space and call sbrk recursively
// to handle this
heap_ptr = NULL;
mem_index ++;
}
else
{
// Memory found in the current space
ptr = heap_ptr;
heap_ptr += incr;
heap_ptr += incr;
break;
}
}
return ptr;
}
}
// *****************************************************************************
// _lseek_r
@ -266,11 +293,15 @@ int _gettimeofday_r( struct _reent *r, struct timeval *tv, void *tz )
}
#include <stdlib.h>
void exit( int status )
void _exit( int status )
{
while( 1 );
}
int _kill( int pid, int sig )
{
return -1;
}
#endif
// If LUA_INTONLY is defined, "redirect" printf/scanf calls to their integer counterparts
@ -286,3 +317,33 @@ int __svfscanf_r( struct _reent *r, FILE *stream, const char *format, va_list ap
return __svfiscanf_r( r, stream, format, ap );
}
#endif // #ifdef LUA_INTONLY
#ifdef USE_MULTIPLE_ALLOCATOR
// Redirect all allocator calls to our dlmalloc
void* _malloc_r( struct _reent* r, size_t size )
{
return dlmalloc( size );
}
void* _calloc_r( struct _reent* r, size_t nelem, size_t elem_size )
{
return dlcalloc( nelem, elem_size );
}
void _free_r( struct _reent* r, void* ptr )
{
dlfree( ptr );
}
void* _realloc_r( struct _reent* r, void* ptr, size_t size )
{
return dlrealloc( ptr, size );
}
void* _memalign_r( struct _reent* r, size_t align, size_t nbytes )
{
return dlmemalign( align, nbytes );
}
#endif // #ifdef USE_MULTIPLE_ALLOCATOR