include	clib.inc
include	iost.inc
include	malloc.inc
include	errno.inc
include	string.inc

_TEXT	SEGMENT

oinitst	proc _CType public io:dword, bsize:word
	mov bx,word ptr io
	mov dx,[bx].S_IOST.ios_file
	invoke memzero,io,SIZE S_IOST
	dec ax
	mov word ptr [bx].S_IOST.ios_bb,ax	; CRC to FFFFFFFFh
	mov word ptr [bx].S_IOST.ios_bb+2,ax
	mov ax,bsize
	mov [bx].S_IOST.ios_size,ax
	mov [bx].S_IOST.ios_file,dx
	invoke malloc,ax
	mov bx,word ptr io
	.if ax && bsize == -1
	    xor ax,ax
	    inc dx
	.endif
	mov word ptr [bx].S_IOST.ios_bp,ax
	mov word ptr [bx].S_IOST.ios_bp+2,dx
	test dx,dx
	ret
oinitst	endp

ofreest	proc _CType public io:dword
	mov bx,word ptr io
	xor ax,ax
	mov dx,word ptr [bx].ios_bp+2
	.if ax != dx
	    .if ax == word ptr [bx].ios_bp
		dec dx
	    .endif
	    push dx
	    push ax
	    mov word ptr [bx].ios_bp,ax
	    mov word ptr [bx].ios_bp+2,ax
	    call free
	    mov ax,ER_MEM
	.endif
	ret
ofreest	endp

_TEXT	ENDS

	END
