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