home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK7 / SOURCE / STARTUP / DOS / STDALLOC.AS$ / STDALLOC
Encoding:
Text File  |  1991-11-06  |  2.6 KB  |  137 lines

  1.     page    ,132
  2.     title    stdalloc - memory allocation routine for stdargv, stdenvp
  3. ;***
  4. ;stdalloc.asm - memory allocation routine for stdargv, stdenvp
  5. ;
  6. ;    Copyright (c) 1985-1992, Microsoft Corporation.  All rights reserved.
  7. ;
  8. ;Purpose:
  9. ;    Memory allocation for stdargv and stdenvp.
  10. ;
  11. ;*******************************************************************************
  12.  
  13. include version.inc
  14. .xlist
  15. include cmacros.inc
  16. include msdos.inc
  17. include heap.inc
  18. .list
  19.  
  20.  
  21. sBegin    data
  22. assumes ds,data
  23.  
  24. externW     _amblksiz    ; heap seg growth increment
  25.  
  26. sEnd    data
  27.  
  28. externP     malloc        ; get heap memory
  29.  
  30. sBegin    code
  31. assumes ds,data
  32. assumes cs,code
  33.  
  34. externNP _amsg_exit        ; write error and die routine
  35.  
  36. page
  37. ;***
  38. ;_myalloc - argument/environment allocation
  39. ;
  40. ;Purpose:
  41. ;    Used to allocate heap space for both wildcard arguments
  42. ;    and environment strings, ptrs.
  43. ;
  44. ;    Tries to find space in heap, failing this spits out error
  45. ;    message and dies.
  46. ;
  47. ;Entry:
  48. ;    AX    = total number of bytes to allocate table and strings
  49. ;    CX    = error message number in case of death.
  50. ;    DS    = DGROUP
  51. ;
  52. ;Exit:
  53. ;    DX:AX    = address of allocated memory
  54. ;
  55. ;Uses:
  56. ;    CX
  57. ;Preserves:
  58. ;    BX, SI, DI, ES, DS
  59. ;Exceptions:
  60. ;    If can't get enough memory, gives error (code in CX) and dies.
  61. ;
  62. ;*******************************************************************************
  63.  
  64.  
  65. cProc    _myalloc,<NEAR,PUBLIC>
  66.  
  67. ifdef    _WINDOWS
  68. cBegin
  69. else
  70. cBegin    <nogen>
  71. endif
  72.  
  73. assumes ds,data
  74.  
  75.     push    bx        ; save registers
  76.     push    es
  77.     push    cx
  78.  
  79. ;
  80. ; Call malloc() to get the memory
  81. ; Set the grow increment to a small value so that we don't eat up
  82. ; too much memory at runtime.
  83. ;
  84.     mov    cx,_HEAP_GROWSTART ; startup grow increment
  85.     xchg    cx,[_amblksiz]    ; set temp grow increment and save original
  86.  
  87.     push    cx        ; save original grow increment
  88.     push    ax        ; ax = size to allocate
  89.     call    malloc        ; heap request
  90.                 ; return value = <ax> or <dx:ax>
  91.     pop    bx        ; clean off stack
  92.     pop    [_amblksiz]    ; restore original grow increment
  93.  
  94.     pop    cx        ; restore error message code
  95. if sizeD
  96.     mov    bx,dx        ; preserve dx
  97.     or    bx,ax        ; malloc return NULL ??
  98. else
  99.     mov    dx,ds        ; dx:ax = address
  100.     or    ax,ax        ; malloc return NULL ??
  101. endif
  102.     jz    _hpovr        ; yes, return an error
  103.  
  104. ;
  105. ; Success
  106. ; dx:ax = memory address
  107. ;
  108. _hpok:
  109.     pop    es        ; restore registers
  110.     pop    bx
  111.  
  112. ifdef _WINDOWS
  113.     jmp    short done
  114. else
  115.     ret
  116. endif
  117.  
  118. ;
  119. ; Error
  120. ; cx = error message code
  121. ;
  122. _hpovr:
  123.     mov    ax,cx        ; error 2009/2008: no space for enviroment/arguments
  124.     jmp    _amsg_exit    ; give error and die
  125.  
  126. ifdef _WINDOWS
  127. ; windows exit sequence
  128. done:
  129. cEnd
  130. else
  131. cEnd    <nogen>
  132. endif
  133.  
  134. sEnd    code
  135.  
  136.     end
  137.