home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / progjour / 1991 / 03 / malloc.asm < prev    next >
Assembly Source File  |  1991-02-20  |  2KB  |  125 lines

  1.     title    memory allocation
  2.     include    asm.inc
  3.  
  4.     public    calloc
  5.     public    free
  6.     public    malloc
  7.     public    realloc
  8.  
  9.     .const
  10. ertx_alloc_range    db    'alloc out of range',0
  11.  
  12.     .code
  13.     extn    ms_dos_strerror,set_strerror
  14.  
  15.  
  16. ;;    calloc
  17. ;
  18. ;    entry    CX    requested byte count (1..FFF0)
  19. ;    exit    AX    actual byte count
  20. ;        ES:DI    storage pointer
  21. ;        Cf    if no storage
  22. ;
  23. calloc    proc
  24.     call    malloc            ; allocate storage
  25.     jc    cal1
  26.  
  27.     pushm    ax,cx,di        ; zero storage
  28.     mov    cx,ax
  29.     movx    ax,0
  30.     shr    cx,1
  31.     rep    stosw
  32.     popm    di,cx,ax
  33. cal1:    ret
  34. calloc    endp
  35.  
  36.  
  37. ;;    free
  38. ;
  39. ;    entry    ES:DI    storage pointer (OK if NULL)
  40. ;    exit    ES:DI    NULL
  41. ;        Cf    if bad pointer
  42. ;    uses    AX
  43. ;
  44. free    proc
  45.     mov    ax,es
  46.     or    ax,di
  47.     jz    fee1            ;\ if NULL pointer - ignore call
  48.  
  49.     mov    ah,49h
  50.     call    ms_dos_strerror
  51.  
  52.     mov    di,NULL_POINTER        ; return NULL (don't modify flags)
  53.     mov    es,di
  54.  
  55. fee1:    ret
  56. free    endp
  57.  
  58.  
  59. ;;    malloc
  60. ;
  61. ;    entry    CX    requested byte count (1..FFF0)
  62. ;    exit    AX    actual byte count
  63. ;        ES:DI    storage pointer (unchanged if error)
  64. ;        Cf    if no storage
  65. ;
  66. malloc    proc
  67.     jcxz    mal2            ; if byte count out of range
  68.     mov    ax,cx
  69.     add    ax,15
  70.     jc    mal2            ; if byte count out of range
  71.     and    al,0F0h
  72.  
  73.     pushm    ax,bx            ; convert byte count to paragraph cnt
  74.     shr    ax,1
  75.     shr    ax,1
  76.     shr    ax,1
  77.     shr    ax,1
  78.     mov    bx,ax
  79.  
  80.     mov    ah,48h            ; request memory from dos
  81.     call    ms_dos_strerror
  82.     jc    mal1            ;  if not enough memory
  83.     mov    es,ax
  84.     movx    di,NULL_POINTER
  85.  
  86. mal1:    popm    bx,ax
  87.     ret
  88.  
  89. mal2:    lea    ax,ertx_alloc_range    ; *alloc out of range*
  90.     jmp    set_strerror
  91. malloc    endp
  92.  
  93.  
  94. ;;    realloc
  95. ;
  96. ;    entry    CX    requested byte count (1..FFF0)
  97. ;        ES:DI    storage pointer
  98. ;    exit    AX    actual byte count
  99. ;        Cf    if not enough memory or cannot increase size
  100. ;
  101. realloc proc
  102.     mov    ax,cx            ; round up to multiple of 16
  103.     add    ax,15
  104.     jc    ral1            ;  if out of range (>FFF0)
  105.     jcxz    ral1            ;  if out of range (<1)
  106.     and    al,0F0h
  107.  
  108.     pushm    ax,bx            ; bytes to paragraphs
  109.     shr    ax,1
  110.     shr    ax,1
  111.     shr    ax,1
  112.     shr    ax,1
  113.     mov    bx,ax
  114.  
  115.     mov    ah,4Ah            ; change storage size via DOS
  116.     call    ms_dos_strerror        ;  (very possible this will NOT work)
  117.     popm    bx,ax
  118.     ret
  119.  
  120. ral1:    lea    ax,ertx_alloc_range    ; *alloc out of range*
  121.     jmp    set_strerror
  122. realloc endp
  123.  
  124.     end
  125.