
* Himpelsoft/Maxon C++ Projekt
*
* Library-Modul "realloc"
*
* Jens Gelhar 28.01.92, 01.09.93, 03.12.94

	xdef	_realloc,realloc__PvUi

	xref	exit__i,_errno,FreeStore__HashTable
	xref	malloc__Ui,free__Pv

_realloc:
realloc__PvUi:	; Stack-Parameter: Zeiger + uint
	move.l	d0,a1
	move.l	8(a7),d1	; neue Größe
	addq.l	#8,d1		; 8 Byte Overhead
	cmp.w	#0,a1
	beq.b	.null1
	cmp.l	-4(a1),d1	; bisherige Größe
	beq.b	.OK		; identisch
.null1	; ganz neuen Speicher allozieren:
	move.l	8(a7),-(a7)	; neue Größe
	jsr	malloc__Ui
	addq.l	#4,a7
	tst.l	d0
	beq.b	.Return
	move.l	d0,a0
	move.l	4(a7),a1
	move.l	8(a7),d1
	move.l	d0,4(a7)	; Ergebnis merken
	move.l	a1,-(a7)	; alter Speicher: schon für "free" pushen
	cmp.w	#0,a1
	beq.b	.copOK
	move.l	-4(a1),d0
	subq.l	#8,d0
	bls.b	.copOK
	cmp.l	d1,d0
	bls.b	.copy
	move.l	d1,d0
.copy	move.w	(a1)+,(a0)+	; wortweises Kopieren ist OK, denn Exec reserviert
	subq.l	#2,d0		; sowieso nur Blöcke gerader Größe (sogar durch 8 teilbar)
	bne.b	.copy
.copOK	tst.l	(a7)
	beq.b	.null2
	jsr	free__Pv
.null2	addq.l	#4,a7
.OK	move.l	4(a7),d0
.Return	rts

	end

