code    segment word public
assume	cs:code
include errno.inc
; Memory allocation routines.
; ---------------------------
;
; Memory is allocated by pointer rather than segment: these routines use
; DI as a pointer into a constant segment, thus there is a communal
; maximum limit of slightly less than 64k.
;
; Note: DS should always point to the same constant segment and is assumed to
;       be valid in these routines
;
; Memory control block structure:
;
; offs  type  description
; ----  ----  ----------------------
;  0    byte  signature "R"
;  1    byte  status - "F" free  "A" allocated
;  2    word  size of block in bytes
;  4    word  pointer: previous MCB (0=first block)
;  6    word  pointer: forward MCB (0=last block)
;  8

extrn errno:word

public _msize
_msize:
; return the size of a memory block, or -1 if pointer is invalid
; msize(ptr)
        push    bp
        mov     bp,sp
        push    bx
        mov     bx,[bp+4]
        cmp     word ptr [bx-8],'AR'
        jnz     L2
        mov     ax,word ptr [bx-6]
L1:
        pop     bx
        pop     bp
        ret     2
L2:
        mov     word ptr ds:[errno],ERR_MEM_INVALIDPTR  ; invalid block passed
        mov     ax,-1
        jmp     L1

code ends
end
