home *** CD-ROM | disk | FTP | other *** search
- /* sbrk: emulate Unix sbrk call */
- /* by ERS */
- /* jrb: added support for allocation from _heapbase when _stksize == -1
- thanks to Piet van Oostrum & Atze Dijkstra for this idea and
- their diffs. */
-
- /* WARNING: sbrk may not allocate space in continguous memory, i.e.
- two calls to sbrk may not return consecutive memory. This is
- unlike Unix.
- */
-
- #include <osbind.h>
- #ifndef __MINT__
- #include <unixlib.h>
- #else
- #include <unistd.h>
- #include <stddef.h>
- #endif
-
- extern void *_heapbase;
- extern long _stksize;
- static void *top_of_heap = (void *) 0;
-
- /* Number of bytes of writable memory we can expect to be able to get */
- long __lim_data = 0;
- long __malloc_bytes_in_free_list = 0;
- long malloc_sbrk_used = 0;
- long malloc_sbrk_unused = 1024000; /* only dummy initializer */
-
- static void * HeapAlloc( sz )
- size_t sz ;
- {
- char slush [64];
- register void *sp;
- long available;
-
- if(!top_of_heap) top_of_heap = (void *) slush;
- if((_heapbase < (void *) slush) && ((void *) slush < top_of_heap))
- sp = (void *) slush;
- else
- sp = top_of_heap;
- available = (long) (sp - ((size_t)_heapbase + sz));
- if (!__lim_data) __lim_data = available;
- if ( available < 0 )
- {
- return (void *)NULL;
- }
- malloc_sbrk_unused = available + __malloc_bytes_in_free_list;
- __malloc_bytes_in_free_list += sz;
- sp = _heapbase;
- _heapbase = (void *)((size_t)_heapbase + sz);
- _stksize -= (long)sz;
-
- return( sp );
- }
-
- asm(".text; .even; .globl _sbrk; _sbrk:"); /* dept of dirty tricks */
-
- /* provided for compilers with sizeof(int) == 2 */
- void *lsbrk(long n)
- {
- void *rval;
-
- if(_heapbase != NULL)
- {
- return (rval = HeapAlloc(n)) ? rval : (void *) -1L;
- }
- else
- {
- return (rval = (void *) Malloc(n)) ? rval : (void *)-1L;
- }
- }
-
- void brk(void *b)
- {
- if(_heapbase != NULL && b != NULL)
- {
- _heapbase = b;
- }
- }
-
- #if 0
- void *sbrk(x)
- size_t x;
- {
- return (void *) Malloc((long) x);
- }
- #endif
-