home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 5.ddi / CLIBSRC2.ZIP / FBRK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.2 KB  |  139 lines

  1. /*------------------------------------------------------------------------
  2.  * filename - fbrk.c
  3.  *
  4.  * function(s)
  5.  *        normalize - normalize a pointer
  6.  *        _brk      - changes data-segment space allocation
  7.  *        _sbrk     - changes data-segment space allocation
  8.  *-----------------------------------------------------------------------*/
  9.  
  10. /*
  11.  *      C/C++ Run Time Library - Version 5.0
  12.  *
  13.  *      Copyright (c) 1987, 1992 by Borland International
  14.  *      All Rights Reserved.
  15.  *
  16.  */
  17.  
  18.  
  19. #include <alloc.h>
  20. #include <dos.h>
  21. #include <_fheap.h>
  22.  
  23. #define addressat(segment) ((void far*)((unsigned long)(segment) << 16))
  24.  
  25.  
  26. /*---------------------------------------------------------------------*
  27.  
  28. Name            normalize - normalize a pointer
  29.  
  30. Usage           local to this module
  31.  
  32. Description     normalizes a far pointer
  33.  
  34. Return value    success : 1
  35.                 failure : 0
  36.  
  37. *---------------------------------------------------------------------*/
  38. static  int near pascal normalize(char far *cp)
  39. {
  40.         extern  unsigned _psp;
  41.         static  unsigned xsize = 0;
  42.         unsigned size, result;
  43.  
  44.         size = 1 + FP_SEG(cp);
  45.         size -= _psp;
  46.         size = (size + 63) >> 6;        /* Convert size to 1K units */
  47.         if (size == xsize)              /* Same as current size, do nothing */
  48.         {
  49.                 _brklvl = cp;
  50.                 return(1);
  51.         }
  52.         size <<= 6;
  53.         result = FP_SEG(_heaptop);
  54.         if (size + _psp > result)
  55.                 size = result - _psp;
  56.         result = setblock(_psp, size);
  57.         if (result == 0xffff)
  58.         {
  59.                 xsize = size >> 6;
  60.                 _brklvl = cp;
  61.                 return(1);
  62.         }
  63.         else    {
  64.                 _heaptop = addressat(_psp + result);
  65.                 /* If we fail, pass that back to the outer routines.
  66.                    This only happens if we did an exec of a program that
  67.                    did a keep.  Also, adjust the _heaptop to make future calls
  68.                    faster. */
  69.                 return(0);
  70.         }
  71. }
  72.  
  73.  
  74. /*--------------------------------------------------------------------------*
  75.  
  76.    In the large data models brk and sbrk are hooks to _brk and
  77.    _sbrk respectively.  _brk and _sbrk work with the far heap.
  78.    brk and sbrk are found in BRK.CAS
  79.  
  80. *---------------------------------------------------------------------------*/
  81.  
  82. /*--------------------------------------------------------------------------*
  83.  
  84. Name            _brk - changes data-segment space allocation
  85.  
  86. Usage           int _brk(void huge *addr);
  87.  
  88. Prototype in    alloc.h
  89.  
  90. Description     _brk sets the break value to addr and changes the
  91.                 allocated space accordingly
  92.  
  93. Return value    success : 0
  94.                 failure : -1 and errno set to ENOMEM (Not enough core)
  95.  
  96. *--------------------------------------------------------------------------*/
  97. int cdecl near _brk(void huge *addr)
  98. {
  99.         if ((addr < _heapbase) || (addr > _heaptop) ||
  100.                 !normalize((char far *)addr))
  101.                 return(-1);
  102.         else
  103.                 return(0);
  104. }
  105.  
  106.  
  107. /*---------------------------------------------------------------------*
  108.  
  109. Name            _sbrk - changes data-segment space allocation
  110.  
  111. Usage           void huge *_sbrk(long incr);
  112.  
  113. Prototype in    alloc.h
  114.  
  115. Description     _sbrk adds incr bytes to the break value and changes the
  116.                 allocated space accordingly. incr can be negative, in
  117.                 which case the amount of allocated space is decreased.
  118.  
  119. Return value    success : the old break value
  120.                 failure : -1 and errno set to ENOMEM (Not enough core)
  121.  
  122. *---------------------------------------------------------------------*/
  123. void huge *cdecl near _sbrk(long incr)
  124. {
  125.         char    huge    *cp;
  126.         char    huge    *old;
  127.  
  128.     if ((((long)FP_SEG(_brklvl) << 4) + FP_OFF(_brklvl) + incr) > 0xfffffL)
  129.                 return((void huge *)-1L);
  130.         cp = _brklvl + incr;
  131.         if ((cp < _heapbase) || (cp > _heaptop))
  132.                 return((void huge *)-1L);
  133.         old = _brklvl;
  134.         if (!normalize((char far *)cp))
  135.                 return((void huge *)-1L);
  136.         return(old);
  137. }
  138.  
  139.