forsyth20121205-174063a80cc13 years agoHistory
963 lines · 15.7 KB · C
RawDownload
#include "u.h"#include "../port/lib.h"#include "mem.h"#include "dat.h"#include "fns.h"#include "interp.h" #define left	u.s.bhl#define right	u.s.bhr#define fwd	u.s.bhf#define prev	u.s.bhv#define parent	u.s.bhp #define RESERVED	256*1024 struct Pool{	char*	name;	int	pnum;	ulong	maxsize;	int	quanta;	int	chunk;	ulong	ressize;	ulong	cursize;	ulong	arenasize;	ulong	hw;	Lock	l;	Bhdr*	root;	Bhdr*	chain;	ulong	nalloc;	ulong	nfree;	int	nbrk;	int	lastfree;	int	warn;	void	(*move)(void*, void*);}; staticstruct{	int	n;	Pool	pool[MAXPOOL];	// Lock	l;} table = {	3,	{		{ "main",  0,	 4*1024*1024, 31,  128*1024, 15*256*1024 },		{ "heap",  1,	16*1024*1024, 31,  128*1024, 15*1024*1024 },		{ "image", 2,	 8*1024*1024, 31, 300*1024, 15*512*1024 },	}}; Pool*	mainmem = &table.pool[0];Pool*	heapmem = &table.pool[1];Pool*	imagmem = &table.pool[2]; static void _auditmemloc(char *, void *);void (*auditmemloc)(char *, void *) = _auditmemloc;static void _poolfault(void *, char *, ulong);void (*poolfault)(void *, char *, ulong) = _poolfault; /*	non tracing *enum {	Npadlong	= 0,	MallocOffset = 0,	ReallocOffset = 0}; * */ /* tracing */enum {	Npadlong	= 2,	MallocOffset = 0,	ReallocOffset = 1}; intmemusehigh(void){	return 	mainmem->cursize > mainmem->ressize ||			heapmem->cursize > heapmem->ressize ||			imagmem->cursize > imagmem->ressize;} voidpoolimmutable(void *v){	Bhdr *b; 	D2B(b, v);	b->magic = MAGIC_I;} voidpoolmutable(void *v){	Bhdr *b; 	D2B(b, v);	b->magic = MAGIC_A;	((Heap*)v)->color = mutator;} char*poolname(Pool *p){	return p->name;} Bhdr*poolchain(Pool *p){	return p->chain;} voidpooldel(Pool *p, Bhdr *t){	Bhdr *s, *f, *rp, *q; 	if(t->parent == nil && p->root != t) {		t->prev->fwd = t->fwd;		t->fwd->prev = t->prev;		return;	} 	if(t->fwd != t) {		f = t->fwd;		s = t->parent;		f->parent = s;		if(s == nil)			p->root = f;		else {			if(s->left == t)				s->left = f;			else				s->right = f;		} 		rp = t->left;		f->left = rp;		if(rp != nil)			rp->parent = f;		rp = t->right;		f->right = rp;		if(rp != nil)			rp->parent = f; 		t->prev->fwd = t->fwd;		t->fwd->prev = t->prev;		return;	} 	if(t->left == nil)		rp = t->right;	else {		if(t->right == nil)			rp = t->left;		else {			f = t;			rp = t->right;			s = rp->left;			while(s != nil) {				f = rp;				rp = s;				s = rp->left;			}			if(f != t) {				s = rp->right;				f->left = s;				if(s != nil)					s->parent = f;				s = t->right;				rp->right = s;				if(s != nil)					s->parent = rp;			}			s = t->left;			rp->left = s;			s->parent = rp;		}	}	q = t->parent;	if(q == nil)		p->root = rp;	else {		if(t == q->left)			q->left = rp;		else			q->right = rp;	}	if(rp != nil)		rp->parent = q;} voidpooladd(Pool *p, Bhdr *q){	int size;	Bhdr *tp, *t; 	q->magic = MAGIC_F; 	q->left = nil;	q->right = nil;	q->parent = nil;	q->fwd = q;	q->prev = q; 	t = p->root;	if(t == nil) {		p->root = q;		return;	} 	size = q->size; 	tp = nil;	while(t != nil) {		if(size == t->size) {			q->prev = t->prev;			q->prev->fwd = q;			q->fwd = t;			t->prev = q;			return;		}		tp = t;		if(size < t->size)			t = t->left;		else			t = t->right;	} 	q->parent = tp;	if(size < tp->size)		tp->left = q;	else		tp->right = q;} voidpoolsummary(void){	int x = 0;	char buf[400]; 	print("\n");	print("    cursize     maxsize       hw         nalloc       nfree      nbrk       max      name\n"); 	x=poolread( buf, sizeof buf - 1, x );	buf[x] = 0;	putstrn(buf, x);	print("\n");} void*poolalloc(Pool *p, ulong asize){	Bhdr *q, *t;	int alloc, ldr, ns, frag;	int osize, size;	Prog *prog; 	if(asize >= 1024*1024*1024)	/* for sanity and to avoid overflow */		return nil;	if(p->cursize > p->ressize && (prog = currun()) != nil && prog->flags&Prestricted)		return nil;	size = asize;	osize = size;	size = (size + BHDRSIZE + p->quanta) & ~(p->quanta); 	ilock(&p->l);	p->nalloc++; 	t = p->root;	q = nil;	while(t) {		if(t->size == size) {			t = t->fwd;			pooldel(p, t);			t->magic = MAGIC_A;			p->cursize += t->size;			if(p->cursize > p->hw)				p->hw = p->cursize;			iunlock(&p->l);			return B2D(t);		}		if(size < t->size) {			q = t;			t = t->left;		}		else			t = t->right;	}	if(q != nil) {		pooldel(p, q);		q->magic = MAGIC_A;		frag = q->size - size;		if(frag < (size>>2) && frag < 0x8000) {			p->cursize += q->size;			if(p->cursize > p->hw)				p->hw = p->cursize;			iunlock(&p->l);			return B2D(q);		}		/* Split */		ns = q->size - size;		q->size = size;		B2T(q)->hdr = q;		t = B2NB(q);		t->size = ns;		B2T(t)->hdr = t;		pooladd(p, t);		p->cursize += q->size;		if(p->cursize > p->hw)			p->hw = p->cursize;		iunlock(&p->l);		return B2D(q);	} 	ns = p->chunk;	if(size > ns)		ns = size;	ldr = p->quanta+1; 	alloc = ns+ldr+ldr;	p->arenasize += alloc;	if(p->arenasize > p->maxsize) {		p->arenasize -= alloc;		ns = p->maxsize-p->arenasize-ldr-ldr;		ns &= ~p->quanta;		if (ns < size) {			if(poolcompact(p)) {				iunlock(&p->l);				return poolalloc(p, osize);			} 			iunlock(&p->l);			if(p->warn)				return nil;			p->warn = 1;			if (p != mainmem || ns > 512)				print("arena too large: %s size %d cursize %lud arenasize %lud maxsize %lud, alloc = %d\n", p->name, osize, p->cursize, p->arenasize, p->maxsize, alloc);			return nil;		}		alloc = ns+ldr+ldr;		p->arenasize += alloc;	} 	p->nbrk++;	t = xalloc(alloc);	if(t == nil) {		p->nbrk--;		iunlock(&p->l);		return nil;	}	/* Double alignment */	t = (Bhdr *)(((ulong)t + 7) & ~7); 	/* TBS xmerge */	if(0 && p->chain != nil && (char*)t-(char*)B2LIMIT(p->chain)-ldr == 0){		/* can merge chains */		if(0)print("merging chains %p and %p in %s\n", p->chain, t, p->name);		q = B2LIMIT(p->chain);		q->magic = MAGIC_A;		q->size = alloc;		B2T(q)->hdr = q;		t = B2NB(q);		t->magic = MAGIC_E;		p->chain->csize += alloc;		p->cursize += alloc;		iunlock(&p->l);		poolfree(p, B2D(q));		/* for backward merge */		return poolalloc(p, osize);	} 	t->magic = MAGIC_E;		/* Make a leader */	t->size = ldr;	t->csize = ns+ldr;	t->clink = p->chain;	p->chain = t;	B2T(t)->hdr = t;	t = B2NB(t); 	t->magic = MAGIC_A;		/* Make the block we are going to return */	t->size = size;	B2T(t)->hdr = t;	q = t; 	ns -= size;			/* Free the rest */	if(ns > 0) {		q = B2NB(t);		q->size = ns;		B2T(q)->hdr = q;		pooladd(p, q);	}	B2NB(q)->magic = MAGIC_E;	/* Mark the end of the chunk */ 	p->cursize += t->size;	if(p->cursize > p->hw)		p->hw = p->cursize;	iunlock(&p->l);	return B2D(t);} voidpoolfree(Pool *p, void *v){	Bhdr *b, *c;	extern Bhdr *ptr; 	D2B(b, v); 	ilock(&p->l);	p->nfree++;	p->cursize -= b->size; 	c = B2NB(b);	if(c->magic == MAGIC_F) {	/* Join forward */		if(c == ptr)			ptr = b;		pooldel(p, c);		c->magic = 0;		b->size += c->size;		B2T(b)->hdr = b;	} 	c = B2PT(b)->hdr;	if(c->magic == MAGIC_F) {	/* Join backward */		if(b == ptr)			ptr = c;		pooldel(p, c);		b->magic = 0;		c->size += b->size;		b = c;		B2T(b)->hdr = b;	} 	pooladd(p, b);	iunlock(&p->l);} void *poolrealloc(Pool *p, void *v, ulong size){	Bhdr *b;	void *nv;	int osize; 	if(size >= 1024*1024*1024)	/* for sanity and to avoid overflow */		return nil;	if(size == 0){		poolfree(p, v);		return nil;	}	SET(osize);	if(v != nil){		ilock(&p->l);		D2B(b, v);		osize = b->size - BHDRSIZE;		iunlock(&p->l);		if(osize >= size)			return v;	}	nv = poolalloc(p, size);	if(nv != nil && v != nil){		memmove(nv, v, osize);		poolfree(p, v);	}	return nv;} ulongpoolmsize(Pool *p, void *v){	Bhdr *b;	ulong size; 	if(v == nil)		return 0;	ilock(&p->l);	D2B(b, v);	size = b->size - BHDRSIZE;	iunlock(&p->l);	return size;} static ulongpoolmax(Pool *p){	Bhdr *t;	ulong size; 	ilock(&p->l);	size = p->maxsize - p->cursize;	t = p->root;	if(t != nil) {		while(t->right != nil)

Lines 1–500 of 963.