;       SCCSID = @(#)d_reallc.asm	6.1 91/02/15
;	SCCSID = @(#)d_reallc.asm	1.4 90/01/22
; ****************************************************************************
; *									     *
; *			  IBM/Microsoft Confidential			     *
; *									     *
; *		    Copyright (c) IBM Corporation  1987, 1990		     *
; *		    Copyright (c) Microsoft Corp.  1987, 1990		     *
; *			      All Rights Reserved			     *
; *									     *
; ****************************************************************************
;***  d_reallc.asm - API stub routine DosReAllocSeg
;
;	Member of API library
;	Bill Mershon
;	Copyright MicroSoft Corporation, 1986
;	22-Apr-1986
;
;	library routine for API simulator
;
;	Called from dos 2.x,3.x compatible dos 5.x applications
;
;	Special Information
;		none
;
;	Modification History
;
;	14-Aug-86 (billme): change arena trashed to access denied
;
;	20-Aug-86 (billme): change back to arena trashed (since the
;	arena may really be trashed and this is information that
;	should not be lost or disregarded
;
;	06-Jan-87 (billme): Replace initial pushes and final pops
;	with calls to __api_begin and __api_end resp.  This will
;	reduce the size of FAPI and allow support for signals.
;
;
;	04-May-87 (robertc): Install hooks for DOSErrorClass Support.
;	All int 21h errors are passed through api_get_extended to get the
;	extended errorcode and to provide the class, action, and locus for
;	the dynamic error FIFO queue.
;

INCLUDE basemaca.inc
INCLUDE dos2equ.inc
INCLUDE error.inc

	EXTRN	api_get_extended:far	;@@ added support for DOSErrorClass
	EXTRN	__api_begin:near
	EXTRN	__api_end:near

;***	DOSREALLOCSEG - stub to emulate DOSREALLOCSEG function call
;
;	simulates DOS 5.0 call from DOS 2.x,3.x environment
;
;	errorcode = unsigned far pascal
;	DosReAllocSeg(LocSize, Selector)
;
;	ENTRY:
;		LocSize - size in bytes (rounded up to nearest paragraph)
;		Selector - selector of currently allocated block
;
;	RETURN:
;		AX = 0 if no error, else error code (see DOS 5 spec)
;
;	EXIT:
;
;	USES:
;		AX
;
;	Pseudo Code:
;		save registers
;		use BP as pointer to stack parameters
;		round LocSize up to nearest paragraph (16 bytes)
;		execute int 21 with function 4Ah
;		restore registers
;		return
;
;	WARNING:
;		may return error code from Dos 2.x, 3.x (see dos manuals)
;
;	EFFECTS:
;		none
;

	title	DOSREALLOCSEG - stub to emulate DOSREALLOCSEG function call
;
;	EXTRN	DOSREALLOCSEG:FAR
;
;	PUSH	WORD LocSize
;	PUSH	WORD Selector
;	CALL	DOSREALLOCSEG
;


_TEXT	segment byte public 'CODE'

	assume	CS:_TEXT,DS:_TEXT

	public	DOSREALLOCSEG

LocSize		equ	[BP+8]
Selector	equ	[BP+6]

DOSREALLOCSEG	proc	far

	call	__api_begin	; init bp and save bp,ds,bx,cx,dx,si,di,es
				; required to save: bp,es,bx

	mov	ax,LocSize		; AX <- size requested
	test	ax,ax
	jnz	ralloc1			; AX != 0 => less than 65536 desired
	or	ax,0ffffh		; prepare to allocate 65536
ralloc1:
	xor	bx,bx			; BX <- round up 0
	test	ax,0fh			; if lowest nibble is 0
	jz	ralloc2			; no round up needed
	or	bx,1			; BX <- round up 1
ralloc2:
	shr	ax,1
	shr	ax,1			; convert to paragraphs
	shr	ax,1
	shr	ax,1
	add	bx,ax			; BX <- number of paragraphs requested

	mov	ax,Selector		; ES <- selector
	mov	es,ax

	mov	ah,D2_SetBlock
	int	21H			; re-allocate memory
	jc	ErrorExit

	xor	ax,ax
	jmp	NormalExit		;@@ Added support for DOSErrorClass
ErrorExit:				;@@ added support for DOSErrorClass
	call	api_get_extended	;@@ added support for DOSErrorClass
NormalExit:				;@@ added support for DOSErrorClass

	call	__api_end	; check sigs & restore es,di,si,dx,cx,bx,ds,bp

	ret	4	; clear stack of entry parameters (bytes)
DOSREALLOCSEG	endp

_TEXT	ends

	end
