home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / Fortran.51 / DISK5 / DOS / STDALLOC.AS$ / STDALLOC.bin
Text File  |  1989-09-27  |  2KB  |  123 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-1990, 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. cBegin    <nogen>
  68.  
  69. assumes ds,data
  70.  
  71.     push    bx        ; save registers
  72.     push    es
  73.     push    cx
  74.  
  75. ;
  76. ; Call malloc() to get the memory
  77. ; Set the grow increment to a small value so that we don't eat up
  78. ; too much memory at runtime.
  79. ;
  80.     mov    cx,_HEAP_GROWSTART ; startup grow increment
  81.     xchg    cx,[_amblksiz]    ; set temp grow increment and save original
  82.  
  83.     push    cx        ; save original grow increment
  84.     push    ax        ; ax = size to allocate
  85.     call    malloc        ; heap request
  86.                 ; return value = <ax> or <dx:ax>
  87.     pop    bx        ; clean off stack
  88.     pop    [_amblksiz]    ; restore original grow increment
  89.  
  90.     pop    cx        ; restore error message code
  91. if sizeD
  92.     mov    bx,dx        ; preserve dx
  93.     or    bx,ax        ; malloc return NULL ??
  94. else
  95.     mov    dx,ds        ; dx:ax = address
  96.     or    ax,ax        ; malloc return NULL ??
  97. endif
  98.     jz    _hpovr        ; yes, return an error
  99.  
  100. ;
  101. ; Success
  102. ; dx:ax = memory address
  103. ;
  104. _hpok:
  105.     pop    es        ; restore registers
  106.     pop    bx
  107.  
  108.     ret
  109.  
  110. ;
  111. ; Error
  112. ; cx = error message code
  113. ;
  114. _hpovr:
  115.     mov    ax,cx        ; error 2009/2008: no space for enviroment/arguments
  116.     jmp    _amsg_exit    ; give error and die
  117.  
  118. cEnd    <nogen>
  119.  
  120. sEnd    code
  121.  
  122.     end
  123.