
		include	pool.i

; -> d0 - size
;    a0 - pool
; <- d0 - mem

		XDEF	_AsmAllocPooled

_AsmAllocPooled	cmpi.w	#39,LIB_VERSION(a6)
		bcs.s	8$
		jmp	_LVOAllocPooled(a6)

8$		movem.l	d2/d3/a2-a4,-(sp)
		move.l	d0,d2
		movea.l	a0,a4
		beq	1$			; !size
		move.l	a0,d0
		beq	1$			; !pool
		cmp.l	ThreshSize(a0),d2
		bcc.s	2$			; separate block
		TSTNODE	a4,a3
		beq.s	5$			; list empty!
7$		tst.l	LN_TYPE(a3)
		beq.s	6$			; not a memheader!
		movea.l	a3,a0
		move.l	d2,d0
		RECALL	Allocate
		tst.l	d0
		bne.s	4$			; got some memory
6$		TSTNODE	a3,a3
		bne.s	7$			; more chunks to check
5$		moveq	#MH_SIZE+8,d3
		add.l	PuddleSize(a4),d3
		move.l	d3,d0
		move.l	MemoryFlags(a4),d1
		RECALL	AllocMem
		movea.l	d0,a2
		tst.l	d0			; new chunk?
		beq.s	1$
		move.l	d3,(a2)+		; save size
		move.l	PuddleSize(a4),d0
		lea	MH_SIZE+4(a2),a0	; init MemChunk
		clr.l	MC_NEXT(a0)
		move.l	d0,MC_BYTES(a0)
		lea	LN_TYPE(a2),a1		; init MemoryHeader
		move.l	a6,0(a1)
		addq.w	#8,a1
		move.l	a0,(a1)+		; MH_FIRST
		move.l	a0,(a1)+		; MH_LOWER
		adda.l	d0,a0
		move.l	a0,(a1)+		; MH_UPPER
		move.l	d0,0(a1)		; MH_FREE
		movea.l	a4,a0
		movea.l	a2,a1
		RECALL	AddHead			; insert into list
		movea.l	a2,a0
		move.l	d2,d0
		RECALL	Allocate		; always successful!
4$		btst	#0,MemoryFlags+1(a4)
		beq.s	1$			; not MEMF_CLEAR
		movea.l	d0,a0
		addq.l	#7,d2			; round and
		lsr.l	#3,d2			; divide by 8
3$		clr.l	(a0)+
		clr.l	(a0)+
		subq.l	#1,d2
		bne.s	3$			; clear chunk
		bra.s	1$

2$		moveq	#LH_SIZE+2,d0		; allocate a separate block
		add.l	d2,d0
		move.l	d0,d2
		move.l	MemoryFlags(a4),d1
		RECALL	AllocMem
		movea.l	d0,a2
		tst.l	d0
		beq.s	1$			; no memory...
		move.l	d2,(a2)+
		move.l	a4,a0
		movea.l	a2,a1
		RECALL	AddTail			; insert into list
		addq.w	#LN_TYPE,a2
		clr.l	(a2)+
		move.l	a2,d0			; block start
1$		movem.l	(sp)+,d2/d3/a2-a4
		rts

		end
