Exemplo n.º 1
0
// realloc: this is a bit more complex. First we identify the correct memory
// pool and try to realloc there. If this doesn't work, we try to realloc in 
// another pool before giving up.
void* _realloc_r( struct _reent* r, void* ptr, size_t size )
{
  void* temp;
  u32 lstart, lend;
  unsigned i = 0;
  size_t prevsize;
  
  // Realloc with ptr == NULL : malloc
  // Realloc with size == 0 : free
  if( !ptr )
    return size ? _malloc_r( r, size ) : NULL;
  else if( !size )
  {
    _free_r( r, ptr );
    return NULL;
  }

  // At this point we know that this is an actual realloc
  // Identify the memory pool
  while( 1 )
  {
    if( ( lstart = ( u32 )platform_get_first_free_ram( i ) ) == 0 )
      return NULL;
    lstart = ( u32 )tlsf_elua_align_addr( ( void* )lstart );
    lend = ( u32 )platform_get_last_free_ram( i );
    if( ( lstart <= ( u32 )ptr ) && ( ( u32 )ptr <= lend ) )
      break;
    i ++;
  }    
  
  // Easy case: realloc succeeds in the same memory pool
  if( ( temp = realloc_ex( ptr, size, ( void* )lstart ) ) != NULL )
    return temp;
  
  // If realloc returned NULL, look for another pool
  prevsize = tlsf_elua_get_block_size( ptr );
  i = 0;
  while( 1 )
  {
    if( ( temp = platform_get_first_free_ram( i ) ) == NULL )
      break;
    temp = tlsf_elua_align_addr( temp );
    if( ( u32 )temp != lstart )
    {
      if( ( temp = malloc_ex( size, temp ) ) != NULL )
      {
        memcpy( temp, ptr, prevsize < size ? prevsize : size );
        free_ex( ptr, ( void* )lstart );
        break;      
      }
    }
    i ++;
  }
  return temp;
}
Exemplo n.º 2
0
void *_sbrk_r( struct _reent *r, ptrdiff_t incr )
#endif
{
	void *ptr;

	// If increment is negative, return -1
	if( incr < 0 )
		return ( void * )-1;

	// Otherwise ask the platform about our memory space (if needed)
	// We do this for all our memory spaces
	while( 1 ) {
		if( heap_ptr == NULL ) {
			if( ( heap_ptr = ( char * )platform_get_first_free_ram( mem_index ) ) == NULL ) {
				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 try the next memory space
			heap_ptr = NULL;
			mem_index ++;
		} else {
			// Memory found in the current space
			ptr = heap_ptr;
			heap_ptr += incr;
			break;
		}
	}

	return ptr;
}
Exemplo n.º 3
0
// calloc: try to allocate in all the memory pools
void* _calloc_r( struct _reent* r, size_t nelem, size_t elem_size )
{
  unsigned i = 0;
  void* temp = NULL;

  while( 1 )
  {
    if( ( temp = platform_get_first_free_ram( i ) ) == NULL )
      break;
    temp = tlsf_elua_align_addr( temp );
    if( ( temp = calloc_ex( nelem, elem_size, temp ) ) != NULL )
      break;
    i ++;
  }  
  return temp;
}
Exemplo n.º 4
0
// free: find memory pool with the given pointer, then free it
void _free_r( struct _reent* r, void* ptr )
{
  unsigned i = 0;
  u32 lstart, lend;

  while( 1 )
  {
    if( ( lstart = ( u32 )platform_get_first_free_ram( i ) ) == 0 )
      break;
    lstart = ( u32 )tlsf_elua_align_addr( ( void* )lstart );
    lend = ( u32 )platform_get_last_free_ram( i );
    if( ( lstart <= ( u32 )ptr ) && ( ( u32 )ptr <= lend ) )
    {
      free_ex( ptr, ( void* )lstart );
      break;
    }
    i ++;
  }  
}
Exemplo n.º 5
0
void* _sbrk_r( struct _reent* r, ptrdiff_t incr )
{
  char* ptr;
  
  if( incr == 0 )
    ptr = heap_ptr;
  else
  {
    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;
    else
    {
      ptr = heap_ptr;
      heap_ptr += incr; 
    }
  }
  return ptr;
}
Exemplo n.º 6
0
// ****************************************************************************
//  Program entry point
int main (void)
{
  // Initialize platform first
  if( platform_init() != PLATFORM_OK )
  {
    // This should never happen
    while( 1 );
  }
    
  // Initialize device manager
  dm_init();
  
  // And register the ROM filesystem
  dm_register( fs_init() );  
  
  // Initialize XMODEM
  xmodem_init( xmodem_send, xmodem_recv );    
  
  printf( ".text ends at %p, first free RAM is at %p, last free ram is at %p\r\n", etext, platform_get_first_free_ram(), platform_get_last_free_ram() );
  
  // Run the shell
  if( shell_init( XMODEM_MAX_FILE_SIZE ) == 0 )
    printf( "Unable to initialize shell!\n" );
  else
    shell_start();
    
  while( 1 );
}
Exemplo n.º 7
0
int main( void )
{
  // Initialize platform first
  if( platform_init() != PLATFORM_OK )
  {
    // This should never happen
    while( 1 );
  }
    
  // Initialize device manager
  dm_init();
  
  // Register the ROM filesystem
  dm_register( romfs_init() );  
  
  // Initialize XMODEM
  xmodem_init( xmodem_send, xmodem_recv );    
  
  // Initialize terminal
  term_init( TERMINAL_LINES, TERMINAL_COLS, term_out, term_in, term_translate );
  
  printf( ".text ends at %p, first free RAM is at %p, last free ram is at %p\r\n", etext, platform_get_first_free_ram(), platform_get_last_free_ram() );
  
  // Run the shell
  if( shell_init( XMODEM_MAX_FILE_SIZE ) == 0 )
  {
    printf( "Unable to initialize the eLua shell!\n" );
    // Start Lua directly
    char* lua_argv[] = { "lua", NULL };
    lua_main( 1, lua_argv );
  }
  else
    shell_start();

  while( 1 );
}