shithub: riscv

ref: 69af2676b0c62a79370158b9a6ee1263d614ac88
dir: /sys/src/libc/9sys/privalloc.c/

View raw version
#include <u.h>
#include <libc.h>

static Lock	privlock;
static int	privinit;
static void	**privs;

extern void	**_privates;
extern int	_nprivates;

void **
privalloc(void)
{
	void **p;
	int i;

	lock(&privlock);
	if(!privinit){
		privinit = 1;
		if(_nprivates){
			_privates[0] = 0;
			for(i = 1; i < _nprivates; i++)
				_privates[i] = &_privates[i - 1];
			privs = &_privates[i - 1];
		}
	}
	p = privs;
	if(p != nil){
		privs = *p;
		*p = nil;
	}
	unlock(&privlock);
	return p;
}

void
privfree(void **p)
{
	lock(&privlock);
	if(p != nil && privinit){
		*p = privs;
		privs = p;
	}
	unlock(&privlock);
}