# include "../hdr/macros.h"
xalloc/xfree based on alloc/free in C library at one time.
Also xfreeall() frees all memory allocated (calls brk(II)).
Xfree always coalesces contiguous free blocks.
Xalloc uses a first fit strategy.
Xalloc always allocates words (rounds up).
Xalloc actually allocates one more word than the
amount requested. The extra word (the first word of the
allocated block) contains the size (in bytes) of the entire block.
This size is used by xfree to identify contiguous blocks,
and is used by xalloc to implement the first fit strategy.
Bad things will happen if the size word is changed.
Worse things happen if xfree is called with a
Xalloc returns the address of the allocated area on success,
Xfree and xfreeall don't return anything.
# define SLOP (sizeof(int *))
register struct fb
*np
, *cp
;
usize
=+ 2*sizeof(int *) -1;
usize
=& ~(sizeof(int *) -1);
while((np
= cp
->f_next
) != 0x3FFFFFFF) {
Don't break the block up if it
is not more than SLOP bigger than the
if(usize
+SLOP
>= np
->f_size
)
Break the block into 2 pieces.
cp
= cp
->f_next
= ((int)np
) + usize
;
cp
->f_size
= np
->f_size
- usize
;
zero(&np
->f_next
,usize
-sizeof(int *));
Nothing on the free list is big enough;
get more core from the operating system.
asize
= usize
<1024? 1024: usize
;
asize
= (asize
+511) & (~511);
if((cp
= sbrk(asize
)) == -1) {
return(fatal("out of space (ut9)"));
Lastbrk
= ((int)cp
) + asize
;
Add the new piece to the free list.
register struct fb
*ptr
, *cp
, *np
;
if (aptr
&& aptr
< Lastbrk
) {
ptr
= aptr
-sizeof(int *);
while((np
= cp
->f_next
) < ptr
)
Try to coalesce with the following block.
if(((int)ptr
) + ptr
->f_size
== ((int)np
)) {
ptr
->f_size
=+ np
->f_size
;
ptr
->f_next
= np
->f_next
;
Try to coalesce with the preceding block.
if(((int)cp
) + cp
->f_size
== ((int)ptr
)) {
cp
->f_size
=+ ptr
->f_size
;
cp
->f_next
= ptr
->f_next
;
Freelist
.f_next
= 0x3FFFFFFF;