code: mafs

ref: ce708f49bf5ec6c6b3fe1bb29d401c42b841b649
dir: /misc.c/

View raw version
#include <u.h>
#include <libc.h>
#include "dat.h"
#include "fns.h"

void
chkrunlock(RWLock *q)
{
	if(q->readers <= 0)
		print("runlock(%#p): not locked: pc %#p\n",
			q, getcallerpc(&q));
	else
		runlock(q);
}

u8
chkwunlock(RWLock *q)
{
	if(q->writer <= 0){
		panic("wunlock(%#p): not locked: pc %#p\n",
			q, getcallerpc(&q));
		return 0;
	}
	wunlock(q);
	return 1;
}

void
chkqunlock(QLock *q)
{
	if(q->locked == 0)
		print("qunlock(%#p): not locked: pc %#p\n",
			q, getcallerpc(&q));
	else
		qunlock(q);
}

int
dprintfd(int fd, char *fmt, ...)
{
	static char buf[2048];
	static QLock lk;
	va_list va;
	int n;

	qlock(&lk);
	va_start(va, fmt);
	snprint(buf, 2048, "%d %llud: %s", getpid(), nsec(), fmt);
	n = vfprint(fd, buf, va);
	va_end(va);
	qunlock(&lk);
	return n;
}

int
dprint(char *fmt, ...)
{
	static char buf[2048];
	static QLock lk;
	va_list va;
	int n;

	qlock(&lk);
	va_start(va, fmt);
	snprint(buf, 2048, "%d %llud: %s", getpid(), nsec(), fmt);
	n = vfprint(2, buf, va);
	va_end(va);
	qunlock(&lk);
	return n;
}

void
panic(char *fmt, ...)
{
	char buf[8192], *s;
	va_list arg;


	s = buf;
	s += sprint(s, "%s %d: ", argv0, getpid());
	va_start(arg, fmt);
	s = vseprint(s, buf + sizeof(buf) / sizeof(*buf), fmt, arg);
	va_end(arg);
	*s++ = '\n';
	write(2, buf, s - buf);
	abort();
	exits(buf);
}

void *
emalloc(u32 sz)
{
	return mallocz((ulong)sz, 1);
}