home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / editors / mntemacs.zoo / src / lib+ / sbrk.c < prev   
Encoding:
C/C++ Source or Header  |  1992-03-30  |  1.9 KB  |  89 lines

  1. /* sbrk: emulate Unix sbrk call */
  2. /* by ERS */
  3. /* jrb: added support for allocation from _heapbase when _stksize == -1 
  4.     thanks to Piet van Oostrum & Atze Dijkstra for this idea and
  5.         their diffs. */
  6.  
  7. /* WARNING: sbrk may not allocate space in continguous memory, i.e.
  8.    two calls to sbrk may not return consecutive memory. This is
  9.    unlike Unix.
  10. */
  11.  
  12. #include <osbind.h>
  13. #ifndef __MINT__
  14. #include <unixlib.h>
  15. #else
  16. #include <unistd.h>
  17. #include <stddef.h>
  18. #endif 
  19.  
  20. extern void *_heapbase;
  21. extern long _stksize;
  22. static void *top_of_heap = (void *) 0;
  23.  
  24. /* Number of bytes of writable memory we can expect to be able to get */
  25. long    __lim_data = 0;
  26. long    __malloc_bytes_in_free_list = 0;
  27. long    malloc_sbrk_used = 0;
  28. long    malloc_sbrk_unused = 1024000;    /* only dummy initializer */
  29.  
  30. static void * HeapAlloc( sz )
  31. size_t sz ;
  32. {
  33.     char slush [64];
  34.     register void *sp;
  35.     long available;
  36.     
  37.     if(!top_of_heap) top_of_heap = (void *) slush;
  38.     if((_heapbase < (void *) slush) && ((void *) slush < top_of_heap))
  39.         sp = (void *) slush;
  40.     else
  41.         sp = top_of_heap;
  42.     available = (long) (sp - ((size_t)_heapbase + sz));
  43.     if (!__lim_data) __lim_data = available;
  44.     if ( available < 0 )
  45.     {
  46.     return (void *)NULL;
  47.     }
  48.     malloc_sbrk_unused = available + __malloc_bytes_in_free_list;
  49.     __malloc_bytes_in_free_list += sz;
  50.     sp = _heapbase;
  51.     _heapbase = (void *)((size_t)_heapbase + sz);
  52.     _stksize -= (long)sz;
  53.     
  54.     return( sp );
  55. }
  56.  
  57. asm(".text; .even; .globl _sbrk; _sbrk:"); /* dept of dirty tricks */
  58.  
  59. /* provided for compilers with sizeof(int) == 2 */
  60. void *lsbrk(long n)
  61. {
  62.   void *rval;
  63.  
  64.   if(_heapbase != NULL)
  65.   {
  66.       return (rval = HeapAlloc(n)) ? rval : (void *) -1L;
  67.   }
  68.   else
  69.   {
  70.     return (rval = (void *) Malloc(n)) ? rval : (void *)-1L;
  71.   }
  72. }
  73.  
  74. void brk(void *b)
  75. {
  76.   if(_heapbase != NULL && b != NULL)
  77.   {
  78.       _heapbase = b;
  79.   }
  80. }
  81.  
  82. #if 0
  83. void *sbrk(x)
  84. size_t x;
  85. {
  86.     return (void *) Malloc((long) x);
  87. }
  88. #endif
  89.