Read about our upcoming Code of Conduct on this issue

Commit e90df3e0 authored by hudson@kremvax.wan's avatar hudson@kremvax.wan
Browse files

Fixed malloc/free and wrote realloc() implementation

parent e7de789f8796
......@@ -47,12 +47,6 @@
#define UNUSED(x) __attribute__((unused)) x
/** We don't have free yet */
static inline void free( const void * x ) { x = 0; }
//extern void * malloc( unsigned len );
#define malloc( len ) debug_malloc( len, __func__ )
/** Compute the number of entries in a static array */
#define COUNT(x) (sizeof(x)/sizeof((x)[0]))
......
......@@ -650,30 +650,20 @@ extern void StopErrBatteryApp( void );
extern void * err_battery_ptr;
extern void * _malloc( size_t len );
static inline void *
debug_malloc( unsigned long len, const char * func )
/** Memory allocation */
struct dryos_meminfo
{
void * rc = _malloc(len);
if(1) DebugMsg( DM_MAGIC, 3, "%s: malloc(%d) = %x",
func,
len,
(unsigned) rc
);
return rc;
}
/** DryOS allocate / Free. Not sure how they differ from malloc */
extern void *
AllocateMemory(
size_t len
);
void * next; // off_0x00;
void * prev; // off_0x04;
uint32_t size; //!< Allocated size or if bit 1 is set already free
};
SIZE_CHECK_STRUCT( dryos_meminfo, 0xC );
extern void * malloc( size_t len );
extern void free( void * buf );
extern void * realloc( void * buf, size_t newlen );
extern void
FreeMemory(
void * buf
);
/** Allocate DMA memory for writing to the CF card */
extern void *
......
......@@ -57,7 +57,7 @@ ISSPACE( char c )
}
//int __errno;
int __errno;
//#define SET_ERRNO(x) __errno = (x)
#define SET_ERRNO(x) /* NOP */
......@@ -210,3 +210,49 @@ streq( const char * a, const char * b )
return *a == *b;
}
/** Exit is tough; we want to kill the current thread, but how? */
#include "bmp.h"
void
exit( int rc )
{
bmp_printf( FONT_SMALL, 0, 50, "Exit %d", rc );
while(1)
;
}
/** realloc is implemented via malloc/free */
void *
realloc(
void * buf,
size_t new_size
)
{
struct dryos_meminfo * mem = buf;
mem--;
// If bit 1, 2 or 3 in mem->size is set then it is not valid
if( mem->size & 3 )
return NULL;
// If new_size is zero, then this is a free
if( new_size == 0 )
{
free(buf);
return NULL;
}
// If the new size is less than the current size do nothing
if( new_size < mem->size )
return buf;
// Allocate a new buffer and copy the old data into it
void * new_buf = malloc( new_size );
memcpy( new_buf, buf, mem->size );
free( buf );
// Return a pointer to the new buffer
return new_buf;
}
......@@ -87,7 +87,10 @@ NSTUB( 0xFF85F1A8, prop_cleanup )
NSTUB( 0xFF86DC0C, create_named_semaphore )
NSTUB( 0xFFA6BD14, gui_task_create )
NSTUB( 0xFF8696BC, LoadCalendarFromRTC )
NSTUB( 0xFF86F150, _malloc )
NSTUB( 0xFF86F150, malloc )
NSTUB( 0xFF86F4E0, free )
NSTUB( 0xFF812E44, cli_save )
NSTUB( 0xFF812E58, sei_restore )
NSTUB( 0xFF85961C, _audio_ic_write )
NSTUB( 0xFF85944C, _audio_ic_read )
NSTUB( 0xFF85B0C8, sounddev_task )
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment