Next: Misc Support, Previous: I/O Support, Up: Libc [Contents]
To support using any of the memory functions, you need to implement
sbrk(). malloc()
, calloc()
, and realloc()
all call
sbrk()
at there lowest level. caddr_t
is defined elsewhere
as char *
. RAMSIZE
is presently a compile time option. All
this does is move a pointer to heap memory and check for the upper
limit. Example libc support code. sbrk()
returns a
pointer to the previous value before more memory was allocated.
/* _end is set in the linker command file * extern caddr_t _end;/ /* just in case, most boards have at least some memory */ #ifndef RAMSIZE # define RAMSIZE (caddr_t)0x100000 #endif /* * sbrk -- changes heap size size. Get nbytes more * RAM. We just increment a pointer in what's * left of memory on the board. */ caddr_t sbrk(nbytes) int nbytes; { static caddr_t heap_ptr = NULL; caddr_t base; if (heap_ptr == NULL) { heap_ptr = (caddr_t)&_end; } if ((RAMSIZE - heap_ptr) >= 0) { base = heap_ptr; heap_ptr += nbytes; return (base); } else { errno = ENOMEM; return ((caddr_t)-1); } }