home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 5.ddi / CLIBSRC2.ZIP / BRK.CAS < prev    next >
Encoding:
Text File  |  1992-06-10  |  3.7 KB  |  139 lines

  1. /*---------------------------------------------------------------------------
  2.  * filename - brk.cas
  3.  *
  4.  * function(s)
  5.  *        brk    - memory model dependent hook to _brk or __brk
  6.  *        sbrk   - memory model dependent hook to _sbrk or __sbrk
  7.  *        __brk  - changes data-segment space allocation on the near heap
  8.  *        __sbrk - changes data-segment space allocation on the near heap
  9.  *--------------------------------------------------------------------------*/
  10.  
  11. /*
  12.  *      C/C++ Run Time Library - Version 5.0
  13.  *
  14.  *      Copyright (c) 1987, 1992 by Borland International
  15.  *      All Rights Reserved.
  16.  *
  17.  */
  18.  
  19.  
  20. #pragma inline
  21. #include <asmrules.h>
  22. #include <alloc.h>
  23. #include <errno.h>
  24.  
  25.  
  26. #if (LDATA)
  27. #include <_fheap.h>
  28.  
  29. /*--------------------------------------------------------------------------*
  30.    In the large data models brk and sbrk are hooks to _brk and
  31.    _sbrk respectively.  _brk and _sbrk work with the far heap.
  32.    _brk and _sbrk are found in fbrk.c
  33. *---------------------------------------------------------------------------*/
  34.  
  35. int brk(void *addr)
  36. {
  37.         return(_brk((void huge *)addr));
  38. }
  39.  
  40. void    *sbrk(int incr)
  41. {
  42.         return((void *)_sbrk((long)incr));
  43. }
  44.  
  45.  
  46. #else
  47.  
  48. #include <_heap.h>
  49.  
  50.  
  51.  
  52. /*--------------------------------------------------------------------------*
  53.  
  54. Name            __brk - changes data-segment space allocation on the
  55.                         near heap
  56.  
  57. Usage           int __brk(void *endds);
  58.  
  59. Prototype in    alloc.h
  60.  
  61. Description     __brk sets the break value to endds and changes the
  62.                 allocated space accordingly
  63.  
  64. Return value    success : 0
  65.                 failure : -1 and errno set to ENOMEM (Not enough core)
  66.  
  67. *---------------------------------------------------------------------------*/
  68. int near __brk(void *addr)
  69. {
  70.         asm     mov     ax,addr
  71.         asm     mov     dx,sp
  72.         asm     sub     dx,MARGIN
  73.         asm     cmp     ax,dx
  74.         asm     jnb     brkerr
  75.         asm     mov     word ptr __brklvl,ax
  76.         return(0);
  77. brkerr:
  78.         errno = ENOMEM;
  79.         return(-1);
  80. }
  81.  
  82.  
  83. /*--------------------------------------------------------------------------*
  84.  
  85. Name            __sbrk - changes data-segment space allocation on the
  86.                        near heap
  87.  
  88. Usage           void *__sbrk(long incr);
  89.  
  90. Prototype in    alloc.h
  91.  
  92. Description     sbrk adds incr bytes to the break value and changes the
  93.                 allocated space accordingly. incr can be negative, in
  94.                 which case the amount of allocated space is decreased.
  95.  
  96. Return value    success : the old break value
  97.                 failure : -1 and errno set to ENOMEM (Not enough core)
  98.  
  99. *---------------------------------------------------------------------------*/
  100. void * near __sbrk(long incr)
  101. {
  102. asm     mov     ax, W0(incr)
  103. asm     mov     dx, W1(incr)
  104. asm     add     ax, word ptr __brklvl
  105. asm     adc     dx, 0
  106. asm     mov     cx, ax
  107. asm     or      dx, dx
  108. asm     jnz     sbrkErr
  109.  
  110. asm     add     cx, MARGIN
  111. asm     jc      sbrkErr
  112. asm     cmp     cx, sp
  113. asm     jnb     sbrkErr
  114.  
  115. asm     xchg    word ptr __brklvl, ax
  116.         return (void *)_AX;
  117. sbrkErr:
  118.         errno = ENOMEM;
  119.         return((void *)-1);
  120. }
  121.  
  122.  
  123. /*--------------------------------------------------------------------------*
  124.    In the small data models brk and sbrk are hooks to __brk and
  125.    __sbrk respectively.  __brk and __sbrk work with the near heap
  126.    by altering the value of __brklvl, the break level.
  127. *---------------------------------------------------------------------------*/
  128.  
  129. int brk(void *addr)
  130. {
  131.         return(__brk(addr));
  132. }
  133.  
  134. void    *sbrk(int incr)
  135. {
  136.         return(__sbrk((long)incr));
  137. }
  138. #endif
  139.