;       SCCSID = @(#)d_realhg.asm	6.1 91/02/15
;      SCCSID = @(#)d_realhg.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_realhg.asm - API stub routine DosReAllocHuge
;
;	Member of API library
;	David Silverstein
;	Copyright MicroSoft Corporation, 1986
;	28-Apr-1986
;
;	library routine for API simulator
;
;	Called from dos 2.x,3.x compatible dos 5.x applications
;
;	Special Information
;		none
;
;	Modification History
;
;	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

;***	DOSREALLOCHUGE
;
;	simulates DOS 5.0 call from DOS 2.x,3.x environment
;
;	errorcode = unsigned far pascal
;	DosReAllocHuge(NumSeg, LocSize, Selector)
;
;	ENTRY:
;		NumSeg - Number of 65536-byte segments
;		LocSize - Number of bytes in last segment (rounded up to the
;		       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
;		calculate number of paragraphs
;		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	DOSREALLOCHUGE
;
;	EXTRN	DOSREALLOCHUGE:FAR
;
;	PUSH	WORD NumSeg
;	PUSH	WORD LocSize
;	PUSH	WORD Selector
;	CALL	DOSREALLOCHUGE
;


_TEXT	segment byte public 'CODE'

	assume	CS:_TEXT, DS:nothing

	public	DOSREALLOCHUGE

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

DOSREALLOCHUGE	proc	far

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

	mov	bx,[NumSeg]		; get number of 65536-byte segments
	test	bx,0fff0h		; is too much being requested?
	jnz	ErrorExit		; if so, set DOS error

	clc				; make sure the first rotate loads zero
					; in the most significant bit
	ror	bx,1			; Convert NumSeg to paragraphs
	ror	bx,1			;    same as shift left 12 bits
	ror	bx,1
	ror	bx,1

	mov	ax,[LocSize]		; AX <- size requested
	test	ax,0fh
	jz	realloc1		; no round up needed
	inc	bx			; BX <- round up 1
	jc	ErrorExit		; error if we wrapped
realloc1:
	shr	ax,1			; convert Size to paragraphs
	shr	ax,1
	shr	ax,1
	shr	ax,1
	add	bx,ax			; BX <- number of paragraphs requested
	jc	ErrorExit		; if no overflow, go allocate
	mov	ax,[Selector]		; ES <- selector
	mov	es,ax

	mov	ah,D2_SetBlock
	int	21H			; re-allocate memory
	jc	ExtErrorExit		; exit with current error code
	xor	ax,ax
	jmp	realloc2

ErrorExit:
	mov	ax,error_not_enough_memory
realloc2:
	jmp	NormalExit		;@@ Added support for DOSErrorClass
ExtErrorExit:				;@@ 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	6	; clear stack of entry parameters (bytes)
DOSREALLOCHUGE	endp

_TEXT	ends

	end
