home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 5.ddi / CLIBSRC2.ZIP / STKAVAIL.ASM < prev    next >
Encoding:
Assembly Source File  |  1992-06-10  |  2.5 KB  |  88 lines

  1. ;[]-----------------------------------------------------------------[]
  2. ;|      STKAVAIL.ASM -- calculate free stack space                   |
  3. ;[]-----------------------------------------------------------------[]
  4.  
  5. ;
  6. ;       C/C++ Run Time Library - Version 5.0
  7. ;       Copyright (c) 1991, 1992 by Borland International
  8. ;       All Rights Reserved.
  9.  
  10.         INCLUDE RULES.ASI
  11.  
  12. ;       Segments Definitions
  13.  
  14. Header@
  15.  
  16. ;----------------------------------------------------------------------
  17. ; In large data models, the stack is in its own segment.  The stack
  18. ; starts at SS:__stklen and grows down to SS:0.
  19. ;
  20. ; In small data models, the stack is in the DGROUP, and grows down
  21. ; to meet the heap.  The end of the heap is marked by ___brklvl.
  22. ;----------------------------------------------------------------------
  23.  
  24. ifdef _WINDOWS
  25. pStackTop equ   0Ah             ; SS:[0A] contains addr of top of stack
  26. else
  27.   if LDATA eq false
  28. ExtSym@         __brklvl, WORD, __CDECL__
  29.   endif
  30. endif
  31.  
  32. ; REGSTACK contains the number of bytes taken up in the stack
  33. ; by registers saved in the normal function prologue.  This includes
  34. ; space for SI and DI, plus DS in huge model.
  35.  
  36. ifdef __HUGE__
  37. REGSTACK   equ     6
  38. else
  39. REGSTACK   equ     4
  40. endif
  41.  
  42. ;-----------------------------------------------------------------------
  43. ;
  44. ;Name           stackavail - get available stack size
  45. ;
  46. ;Usage          #include <malloc.h>
  47. ;               size_t stackavail(void);
  48. ;
  49. ;Prototype in   malloc.h
  50. ;
  51. ;Description    This function returns the approximate number of bytes
  52. ;               available in the stack for allocation by alloca().
  53. ;
  54. ;Return value   The number of available stack bytes.
  55. ;
  56. ;------------------------------------------------------------------------
  57.  
  58. Code_seg@
  59.  
  60. PubProc@ stackavail, __CDECL__
  61.  
  62.         mov     ax,sp           ; get current stack pointer
  63. ifdef _WINDOWS
  64.         mov     cx,pStackTop    ; get base of stack
  65.         add     cx,REGSTACK     ; leave space for pushing registers
  66. else
  67.     if LDATA eq false
  68.         mov     cx,__brklvl@    ; get base of stack
  69.         add     cx,REGSTACK     ; leave space for pushing registers
  70.     else
  71.         mov     cx,REGSTACK     ; leave space for pushing registers
  72.     endif
  73. endif
  74.         sub     ax,cx           ; AX = (current SP) - (stack base)
  75.         jb      noroom          ; if current SP is below stack base, no room
  76.         ret
  77. noroom:
  78.         xor     ax,ax           ; return zero
  79.         ret
  80.  
  81. EndProc@ stackavail, __CDECL__
  82.  
  83. Code_EndS@
  84.  
  85.         end
  86.