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