Annotation of os2sdk/startup/os2/stdalloc.asm, revision 1.1

1.1     ! root        1:        TITLE   stdalloc - OS/2 routine to get memory from heap or stack
        !             2: ;***
        !             3: ;5stdallo.asm - OS/2 routine to get memory from heap or stack
        !             4: ;
        !             5: ;      Copyright (c) 1986-1987, Microsoft Corporation.  All rights reserved.
        !             6: ;
        !             7: ;Purpose:
        !             8: ;      This routine returns memory from the heap or stack.
        !             9: ;
        !            10: ;*******************************************************************************
        !            11: 
        !            12: 
        !            13: include        version.inc
        !            14: .xlist
        !            15: include        cmacros.inc
        !            16: include        msdos.inc
        !            17: include        brkctl.inc
        !            18: .list
        !            19: 
        !            20: 
        !            21: sBegin data
        !            22:        assumes ds,data
        !            23: 
        !            24: extrn  _end:word               ; stack bottom
        !            25: 
        !            26: externW        _asizds                 ; limit of data segment
        !            27: externW        _abrktb                 ; heap structure
        !            28: 
        !            29:        extrn   STKHQQ:word     ; limit for stack growth
        !            30: 
        !            31: sEnd   data
        !            32: 
        !            33:        extrn   DOSREALLOCSEG:far ; DOS Function Call
        !            34: 
        !            35: sBegin code
        !            36: assumes        ds,data
        !            37: assumes        cs,code
        !            38: 
        !            39: page
        !            40: ;***
        !            41: ;_stdalloc - try to steal memory from heap or stack
        !            42: ;
        !            43: ;Purpose:
        !            44: ;      This routine returns memory from the heap or stack.
        !            45: ;
        !            46: ;      First the heap is examined to see if it is big enough.
        !            47: ;      If it is not, this routine attempts to lengthen the
        !            48: ;      Global Data Segment in order to enlarge the heap.
        !            49: ;      If this is not possible or is unsuccessful, this
        !            50: ;      code will examine the amount of unused stack space
        !            51: ;      to see if it can accommodate the allocation request.
        !            52: ;
        !            53: ;      If carry is clear on return, then the allocation was
        !            54: ;      successful and DS:AX points to the allocated memory.
        !            55: ;      Carry set on return indicates allocation failure.
        !            56: ;
        !            57: ;      NOTE:   This routine DEPENDS on the fact that DS=SS=DGROUP
        !            58: ;
        !            59: ;      This is always a near routine since it is only called by the C run-time
        !            60: ;
        !            61: ;Entry:
        !            62: ;      AX      = number of bytes requested
        !            63: ;
        !            64: ;Exit:
        !            65: ;      Carry Clear (no error):
        !            66: ;          DS:AX   = address of allocated memory
        !            67: ;      Carry Set:
        !            68: ;          Error - unable to allocate
        !            69: ;
        !            70: ;Uses: BX, CX, DX are destroyed
        !            71: ;      SP will be altered if the memory came from the stack
        !            72: ;
        !            73: ;Exceptions:
        !            74: ;
        !            75: ;*******************************************************************************
        !            76: 
        !            77: labelNP        <PUBLIC,_stdalloc>
        !            78: 
        !            79:        pop     dx              ; get return offset
        !            80: ;
        !            81: ; See if there is space in the heap
        !            82: ; If not, see if the heap can be increased by enlarging the Data Segment
        !            83: ;
        !            84:        mov     bx,[_abrktb].sz ; get current start of heap
        !            85:        add     bx,ax
        !            86:        jc      try_stack       ; not enough room in DS after heap
        !            87:        cmp     bx,[_asizds]
        !            88:        jc      heap_success    ; Yes! Enough Room in Heap
        !            89: 
        !            90:        mov     cx,bx
        !            91:        dec     cx
        !            92:        or      cl,0FH
        !            93:        inc     cx              ; Round up to a multiple of 16 bytes
        !            94: 
        !            95:        push    ax              ; ****** save a copy of AX around call
        !            96: 
        !            97:        push    cx
        !            98:        push    ds
        !            99:        call    DOSREALLOCSEG   ; Try to enlarge the Data Segment
        !           100: 
        !           101:        test    ax,ax
        !           102:        pop     ax              ; ****** restore copy of AX after call
        !           103:        jnz     try_stack       ; Cannot enlarge Data Segment
        !           104: 
        !           105:        dec     cx
        !           106:        mov     [_asizds],cx    ; set size of enlarged Data Segment
        !           107: heap_success:
        !           108:        xchg    bx,[_abrktb].sz ; set new top of heap and
        !           109:        clc                     ; get start of allocated memory
        !           110:        jmp     short return
        !           111: ;
        !           112: ; See if there is enough space on the stack
        !           113: ;
        !           114: try_stack:
        !           115:        mov     bx,sp
        !           116:        sub     bx,ax           ; new position
        !           117:        jb      return          ; error - out of memory
        !           118:        cmp     bx,[STKHQQ]     ; SP - AX : STKHQQ (for heap/stack)
        !           119:        jb      return          ;   error - out of memory
        !           120:                                ; carry is clear!
        !           121:        mov     sp,bx           ; set new stack pointer
        !           122: return:
        !           123: ;
        !           124: ;      Return the address in BX if Carry Clear
        !           125: ;      Return value is garbage if Carry Set
        !           126: ;
        !           127:        mov     ax,bx
        !           128:        jmp     dx              ; return to cx
        !           129: 
        !           130: sEnd   code
        !           131: 
        !           132:        end

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.