Plan 9 from Bell Labs’s /usr/web/sources/extra/9hist/port/allocb.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


## diffname port/allocb.c 1999/0710
## diff -e /dev/null /n/emeliedump/1999/0710/sys/src/brazil/port/allocb.c
0a
#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"error.h"
#include	"pool.h"

enum
{
	Hdrspc		= 64,		/* leave room for high-level headers */
	Bdead		= 0x51494F42,	/* "QIOB" */
};

struct
{
	Lock;
	ulong	bytes;
} ialloc;

/*
 *  allocate blocks, round the data base upt to a multiple of BLOCKALIGN.
 */
Block*
allocb(int size)
{
	Block *b;
	ulong addr;
	int n;

	n = sizeof(Block) + size;
	b = malloc(n+Hdrspc);
	if(b == 0)
		exhausted("Blocks");
	memset(b, 0, sizeof(Block));

	/* align start of data portion by rounding up */
	addr = (ulong)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 = (ulong)(b->lim);
	addr = addr & ~(BLOCKALIGN-1);
	b->lim = (uchar*)addr;

	/* leave sluff at beginning for added headers */
	b->rp = b->lim - ROUND(size, BLOCKALIGN);
	if(b->rp < b->base)
		panic("allocb");
	b->wp = b->rp;
	tagwithpc(b, getcallerpc(&size));

	return b;
}

/*
 *  interrupt time allocation
 */
Block*
iallocb(int size)
{
	Block *b;
	ulong addr;
	int n;

	if(ialloc.bytes > conf.ialloc){
		print("iallocb: limited %lud/%lud\n",
			ialloc.bytes, conf.ialloc);
		return 0;
	}

	n = sizeof(Block) + size;
	b = malloc(n+Hdrspc);
	if(b == 0){
		print("iallocb: no memory %lud/%lud\n",
			ialloc.bytes, conf.ialloc);
		return nil;
	}
	memset(b, 0, sizeof(Block));

	/* align start of data portion by rounding up */
	addr = (ulong)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 = (ulong)(b->lim);
	addr = addr & ~(BLOCKALIGN-1);
	b->lim = (uchar*)addr;

	/* leave sluff at beginning for added headers */
	b->rp = b->lim - ROUND(size, BLOCKALIGN);
	if(b->rp < b->base)
		panic("allocb");
	b->wp = b->rp;

	b->flag = BINTR;

	ilock(&ialloc);
	ialloc.bytes += b->lim - b->base;
	iunlock(&ialloc);
	tagwithpc(b, getcallerpc(&size));

	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) {
		b->free(b);
		return;
	}
	if(b->flag & BINTR) {
		ilock(&ialloc);
		ialloc.bytes -= b->lim - b->base;
		iunlock(&ialloc);
	}

	/* 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;

	free(b);
}

void
checkb(Block *b, char *msg)
{
	void *dead = (void*)Bdead;

	if(b == dead)
		panic("checkb b %s %lux", msg, b);
	if(b->base == dead || b->lim == dead || b->next == dead
	  || b->rp == dead || b->wp == dead){
		print("checkb: base 0x%8.8luX lim 0x%8.8luX next 0x%8.8luX\n",
			b->base, b->lim, b->next);
		print("checkb: rp 0x%8.8luX wp 0x%8.8luX\n", b->rp, b->wp);
		panic("checkb dead: %s\n", msg);
	}

	if(b->base > b->lim)
		panic("checkb 0 %s %lux %lux", msg, b->base, b->lim);
	if(b->rp < b->base)
		panic("checkb 1 %s %lux %lux", msg, b->base, b->rp);
	if(b->wp < b->base)
		panic("checkb 2 %s %lux %lux", msg, b->base, b->wp);
	if(b->rp > b->lim)
		panic("checkb 3 %s %lux %lux", msg, b->rp, b->lim);
	if(b->wp > b->lim)
		panic("checkb 4 %s %lux %lux", msg, b->wp, b->lim);

}

void
iallocsummary(void)
{
	print("ialloc %lud/%lud\n", ialloc.bytes, conf.ialloc);
}
.
## diffname port/allocb.c 1999/0712
## diff -e /n/emeliedump/1999/0710/sys/src/brazil/port/allocb.c /n/emeliedump/1999/0712/sys/src/brazil/port/allocb.c
75c
	b = mallocz(n+Hdrspc, 0);
.
32c
	b = mallocz(n+Hdrspc, 0);
.
22c
 *  allocate blocks, round the data base up to a multiple of BLOCKALIGN.
.
## diffname port/allocb.c 1999/0713
## diff -e /n/emeliedump/1999/0712/sys/src/brazil/port/allocb.c /n/emeliedump/1999/0713/sys/src/brazil/port/allocb.c
75c
	b = malloc(n+Hdrspc);
.
32c
	b = malloc(n+Hdrspc);
.
22c
 *  allocate blocks, round the data base upt to a multiple of BLOCKALIGN.
.
## diffname port/allocb.c 1999/0714
## diff -e /n/emeliedump/1999/0713/sys/src/brazil/port/allocb.c /n/emeliedump/1999/0714/sys/src/brazil/port/allocb.c
105c
	setmalloctag(b, getcallerpc(&size));
.
53c
	setmalloctag(b, getcallerpc(&size));
.
## diffname port/allocb.c 1999/1110
## diff -e /n/emeliedump/1999/0714/sys/src/brazil/port/allocb.c /n/emeliedump/1999/1110/sys/src/9/port/allocb.c
22c
 *  allocate blocks, round the data base up to a multiple of BLOCKALIGN.
.
## diffname port/allocb.c 2001/0504
## diff -e /n/emeliedump/1999/1110/sys/src/9/port/allocb.c /n/emeliedump/2001/0504/sys/src/9/port/allocb.c
105d
81,99c
	setmalloctag(b, getcallerpc(&size));
.
74,76c
	if((b = _allocb(size)) == nil){
.
65,66d
58,60d
52a

	return b;
}

Block*
allocb(int size)
{
	Block *b;

	/*
	 * Check in a process and wait until successful.
	 * Can still error out of here, though.
	 */
	if(up == nil)
		panic("allocb without up: %uX\n", getcallerpc(&size));
	if((b = _allocb(size)) == nil)
		panic("allocb: no memory\n");
.
51c
		panic("_allocb");
.
36a
	b->next = nil;
	b->list = nil;
	b->free = 0;
	b->flag = 0;

.
31,35c
	if((b = mallocz(sizeof(Block)+size+Hdrspc, 0)) == nil)
		return nil;
.
29d
21,25c
static Block*
_allocb(int size)
.
## diffname port/allocb.c 2001/0527
## diff -e /n/emeliedump/2001/0504/sys/src/9/port/allocb.c /n/emeliedump/2001/0527/sys/src/9/port/allocb.c
7d
## diffname port/allocb.c 2001/0804
## diff -e /n/emeliedump/2001/0527/sys/src/9/port/allocb.c /n/emeliedump/2001/0804/sys/src/9/port/allocb.c
66c
{
xsummary();
mallocsummary();
		panic("allocb: no memory for %d bytes\n", size);
}
.
## diffname port/allocb.c 2001/0822
## diff -e /n/emeliedump/2001/0804/sys/src/9/port/allocb.c /n/emeliedump/2001/0822/sys/src/9/port/allocb.c
70c
	}
.
65,68c
	if((b = _allocb(size)) == nil){
		xsummary();
		mallocsummary();
.
## diffname port/allocb.c 2002/0114
## diff -e /n/emeliedump/2001/0822/sys/src/9/port/allocb.c /n/emeliedump/2002/0114/sys/src/9/port/allocb.c
64c
		panic("allocb without up: %luX\n", getcallerpc(&size));
.
## diffname port/allocb.c 2002/0711
## diff -e /n/emeliedump/2002/0114/sys/src/9/port/allocb.c /n/emeliedump/2002/0711/sys/src/9/port/allocb.c
87,88c
		if((m2++%10000)==0)
			print("iallocb: no memory %lud/%lud\n",
				ialloc.bytes, conf.ialloc);
.
81,82c
		if((m1++%10000)==0)
			print("iallocb: limited %lud/%lud\n",
				ialloc.bytes, conf.ialloc);
.
78a
	static int m1, m2;
.

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.