/* align start of data portion by rounding up */
addr = (uintptr)b;
addr = ROUND(addr + sizeof(Block), BLOCKALIGN);
b->base = (uchar*)addr;
/* align end of data portion by rounding down */
b->lim = (uchar*)b + msize(b);
addr = (uintptr)b->lim;
addr &= ~(BLOCKALIGN-1);
b->lim = (uchar*)addr;
/* leave room at beginning for added headers */
b->wp = b->rp = b->lim - size;
return b;
}
Block*
allocb(int size)
{
Block *b;
/*
* Check in a process and wait until successful.
*/
if(up == nil)
panic("allocb without up: %#p", getcallerpc(&size));
while((b = _allocb(size)) == nil){
if(up->nlocks || m->ilockdepth || !islo()){
xsummary();
mallocsummary();
panic("allocb: no memory for %d bytes", size);
}
if(!waserror()){
resrcwait("no memory for allocb");
poperror();
}
}
setmalloctag(b, getcallerpc(&size));
return b;
}
Block*
iallocb(int size)
{
Block *b;
if((b = _allocb(size)) == nil){
static ulong nerr;
if((nerr++%10000)==0){
if(nerr > 10000000){
xsummary();
mallocsummary();
panic("iallocb: out of memory");
}
iprint("iallocb: no memory for %d bytes\n", size);
}
return nil;
}
setmalloctag(b, getcallerpc(&size));
b->flag = BINTR;
return b;
}
void
freeb(Block *b)
{
void *dead = (void*)Bdead;
if(b == nil)
return;
/*
* drivers which perform non cache coherent DMA manage their own buffer
* pool of uncached buffers and provide their own free routine.
*/
if(b->free != nil) {
b->free(b);
return;
}
/* poison the block in case someone is still holding onto it */
b->next = dead;
b->rp = dead;
b->wp = dead;
b->lim = dead;
b->base = dead;