home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / sbrk.c < prev    next >
C/C++ Source or Header  |  1992-09-17  |  2KB  |  93 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. /* Further WARNING: in a split_mem model the memory addresses will NOT
  13.    be monotonous. sigh!  (i hate these mem models as much as the next
  14.    person. as usual, people at atari are totally oblivious to such
  15.    brain damage, even when pointed out to them. sigh!)
  16. */
  17.  
  18. /*
  19.  * support heat and serve C -- whose author continues to be adamant about
  20.  * size_t -- big sigh!
  21.  */
  22.  
  23. #include <stddef.h>
  24. #include <osbind.h>
  25. #include <unistd.h>
  26. #include <errno.h>
  27.  
  28. extern void *_heapbase;
  29. extern long _stksize;
  30. extern short _split_mem;
  31.  
  32. static void *HeapAlloc __PROTO((unsigned long sz));
  33.  
  34. static void * HeapAlloc( sz )
  35. unsigned long sz ;
  36. {
  37.     char slush [64];
  38.     register void *sp;
  39.     
  40.     sp = (void *)slush;
  41.     sz = (sz + 7) & ~((unsigned long)7L); /* round up request size next octet */
  42.  
  43.     if ( sp < (void *)((char *)_heapbase + sz) )
  44.     {
  45.     return NULL;
  46.     }
  47.     sp = _heapbase;
  48.     _heapbase = (void *)((char *)_heapbase + sz);
  49.     _stksize -= (long)sz;
  50.     
  51.     return( sp );
  52. }
  53.  
  54. #ifdef __GNUC__
  55. asm(".stabs \"_sbrk\",5,0,0,__sbrk"); /* dept of clean tricks */
  56. #endif
  57.  
  58. /* provided for compilers with sizeof(int) == 2 */
  59. void *_sbrk(n)
  60. long n;
  61. {
  62.   void *rval;
  63.  
  64.   if((!_split_mem) && (_heapbase != NULL))
  65.   {
  66.       if(n) rval = HeapAlloc(n);
  67.       else  rval = _heapbase;
  68.   }
  69.   else
  70.   {
  71.       rval = (void *) Malloc(n);
  72.   }
  73.   if (rval == NULL)
  74.   {
  75.       if(_split_mem)
  76.       {  /* now switch over to own heap for further requests, including this one */
  77.       _split_mem = 0;
  78.       return _sbrk(n);
  79.       }
  80.       errno = ENOMEM;
  81.       rval = (void *)(-1L);
  82.   }
  83.   return rval;
  84. }
  85.  
  86. #ifndef __GNUC__
  87. void *sbrk(x)
  88. size_t x;
  89. {
  90.     return _sbrk((long)x);
  91. }
  92. #endif
  93.