home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / database / trl14db.zip / TRLSRC.EXE / _TR_ALOC.ASM next >
Assembly Source File  |  1990-10-22  |  5KB  |  147 lines

  1. ; ALLOCMEM.ASM
  2. ;
  3. ; by Ralph Davis
  4. ; Modified by Rick Spence
  5. ;
  6. ; Placed in the public domain by Tom Rettig Associates, 10/22/1990.
  7. ;
  8.  
  9.            PUBLIC   _TR_ALLOCMEM
  10.  
  11. ; TR-LIB low-level memory allocator for C functions
  12. ;
  13. ; Syntax: char *_tr_allocmem(lnbytes);
  14. ;         long lnbytes;
  15. ;
  16. ; Return: pointer to allocated memory in AX:BX
  17.  
  18. ;=============================================
  19. ; Structure for passed parameters
  20.  
  21. IN_PARMS   STRUC
  22.  
  23. OLD_BP     DW      ?             ; caller's BP
  24.            DD      ?             ; caller's return address
  25. LOW_WORD   DW      ?             ; low word of memory requested
  26. HIGH_WRD   DW      ?             ; high word
  27.  
  28. IN_PARMS   ENDS
  29.  
  30. ;============================================
  31.  
  32.  
  33.  
  34. ;*********************************************
  35. CODESEG    SEGMENT BYTE PUBLIC 
  36.            ASSUME  CS:CODESEG
  37. ;---------------------------------------------
  38. ;
  39. ;===========================================
  40. ; DATA AREA
  41. ;
  42. DUMMY_SEG DW      ?     ; Will save the segment of the dummy memory area 
  43.                         ; we allocate
  44. NUM_PARA  DW      ?     ; Will save the number of paragraphs currently
  45.                         ; available
  46. OLD_STRAT DB      0     ; Current allocation strategy
  47. ;
  48. ERR_FLAG  DB      ?     ; Has allocation error occurred?
  49. ;===========================================
  50.  
  51. _TR_ALLOCMEM   PROC    FAR
  52.            PUSH    BP                ; save caller's BP
  53.            MOV     BP,SP             ; set up stack addressability
  54.            PUSH    DS
  55.            PUSH    ES
  56.            AND     CS:ERR_FLAG,0     ; set err_flag to false
  57.            MOV     AH,58H            ; get current allocation
  58.            MOV     AL,0              ; strategy
  59.            INT     21H
  60.            MOV     CS:OLD_STRAT,AL   ; save current strategy
  61.            JNC     AM2
  62.            MOV     CS:OLD_STRAT,0    ; assume current strategy is
  63.                                      ; first fit if error occurred
  64. AM2:       MOV     AH,58H            ; set strategy to high memory
  65.            MOV     AL,1
  66.            MOV     BX,2
  67.            INT     21H
  68.            JNC     AM3               ; successful, go ahead and
  69.                                      ; make allocation request
  70.            OR      ERR_FLAG,0FFH     ; set error flag
  71.            MOV     BX,0FFFFH         ; request impossible amount of memory
  72.                                      ; to find out how much is available
  73.            MOV     AH,48H            ; try to allocate 1 megabyte
  74.            INT     21H               ; BX will tell us how many paragraphs
  75.                                      ; are available
  76.            MOV     DX,BX             ; save it
  77. AM3:
  78.            MOV     AX,[BP].HIGH_WRD  ; pick up passed parameters
  79.            MOV     BX,[BP].LOW_WORD
  80.            OR      AX,AX
  81.            JNZ     AM3A
  82.            OR      BX,BX
  83.            JNZ     AM3A
  84.            JMP     SHORT ALLOC_ERR
  85.  
  86. AM3A:      REPT    4                 ; convert bytes requested
  87.            CLC
  88.            RCR     AX,1              ;   to number of paragraphs
  89.            RCR     BX,1              ;   in BX
  90.            ENDM
  91.            CMP     ERR_FLAG,0FFH     ; did error occur?
  92.            JNE     AM4
  93.            CMP     DX,BX             ; find out right now if memory's there
  94.            JC      ALLOC_ERR 
  95.            PUSH    BX
  96.            SUB     DX,BX             ; we're going to ask for that much 
  97.                                      ;   minus what we want minus 2
  98.            MOV     BX,DX
  99.            SUB     BX,2
  100.            MOV     AH,48H
  101.            INT     21H
  102.            POP     BX
  103.            JC      ALLOC_ERR
  104.            MOV     CS:DUMMY_SEG,AX   ; save segment so we can free it
  105.  
  106.           
  107. AM4:
  108.            INC     BX                ; take care of any modulus-16
  109.            MOV     AH,48H            ; get the memory
  110.            INT     21H               ; from DOS
  111.            PUSHF
  112.            CMP     ERR_FLAG,0FFH     ; was there an error setting strategy?
  113.            JNE     AM5
  114.            MOV     AX,CS:DUMMY_SEG   ; free dummy block allocated
  115.            MOV     ES,AX
  116.            MOV     AH,49H
  117.            INT     21H
  118.            
  119. AM5:
  120.            POPF
  121.            JNC     GOOD_EXIT         ; tests carry from function 48H
  122.                                      ; request
  123. ALLOC_ERR:
  124.            XOR     AX,AX             ; return NULL pointer
  125. GOOD_EXIT: 
  126.            XOR     BX,BX             ; AX:BX now has pointer to 
  127.                                      ; memory block allocated
  128.            PUSH    AX
  129.            PUSH    BX
  130.            MOV     BL,CS:OLD_STRAT   ; reset original allocation strategy
  131.            XOR     BH,BH
  132.            MOV     AH,58H
  133.            MOV     AL,1
  134.            INT     21H               ; if error, too bad, not crucial
  135.            POP     BX                ; retrieve pointer
  136.            POP     AX                ; to return to calling routine
  137.            POP     ES
  138.            POP     DS
  139.            POP     BP
  140.            RET
  141. _TR_ALLOCMEM   ENDP
  142. ;----------------------------------------
  143. CODESEG    ENDS
  144. ;****************************************
  145.            END
  146.  
  147.