; *********************************************************************** ; * * ; * A Simple Implementation of malloc() and free() * ; * * ; *********************************************************************** ; Author: John Zaitseff ; Date: 5th April, 2003 ; Version: 1.1 ; This file provides the an extremely simple implementation of the functions ; "malloc" and "free". This allows the GNU Debugger to interactively call ; assembly language programs that manipulate strings. In other words, this ; allows the GNU Debugger to call a function within the assembly language ; program when using the debugger "print" command with string arguments. ; ----------------------------------------------------------------------- ; Assembly language preamble and constant values .set NULL, 0 ; A NULL pointer in C .text .align ; ----------------------------------------------------------------------- ; Function: void *malloc (size_t size) ; This Standard C function is meant to return a pointer to space for an ; object of size "size", or NULL if the request cannot be satisfied. The ; space is uninitialised. ; This particular implementation of malloc() simply returns a pointer to ; a block of memory. This means that malloc() can only be used ONCE in ; the program! Although this might seem like a major limitation, it is ; only meant to be used by the GNU Debugger (via the "print" command) for ; calling functions with a SINGLE "char *" parameter. .global malloc .type malloc, function malloc: ldr r1, =malloc_block_size ; Check if client wants to malloc cmp r0, r1 ; more memory than is available movhs r0, #NULL ; Yes: return NULL ldrlo r0, =malloc_block ; No: return a pointer to the block mov pc, lr ; Return to the caller .size malloc, .-malloc ; ----------------------------------------------------------------------- ; Function: void free (void *p) ; This Standard C function is meant to deallocate the memory pointed to by ; the pointer "p". ; This particular implementation of free() does nothing. .global free .type free, function free: mov pc, lr ; Return to the caller .size free, .-free ; ----------------------------------------------------------------------- ; Allocate uninitialised space for malloc() .bss malloc_block: .skip 8192 ; Allow 8KB for malloc() malloc_block_end: .set malloc_block_size, (malloc_block_end - malloc_block) .end