code: libextents

ref: e96b9e14a06a019e20988fa6a80b8871d0db2c44
dir: /testextents.c/

View raw version
#include <u.h>
#include <libc.h>
#include "extents.h"
#include <bio.h>

/*
	test the extents functionality
	{ echo 2 3 ; echo 100 3; } | ./6.testextents
 */

int chatty9p = 0;

static void
usage(void)
{
	fprint(2, "usage: testextents [-D ]\n");
	exits("usage");
}

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);
}

static void *
emalloc(u32 sz)
{
	void *v;

	if((v = mallocz(sz, 1)) == nil)
		sysfatal("emalloc: %r");

	setmalloctag(v, getcallerpc(&sz));
	return v;
}

void
main(int argc, char *argv[])
{
	Extents es;
	s8 *line, *p;
	Biobuf *bp;
	u64 bno, len;
	char act;

	ARGBEGIN{
	default:	usage();
	case 'D':	chatty9p=8; break;
	}ARGEND

	if(argc != 0)
		usage();

	bp = Bfdopen(0, OREAD);
	Blethal(bp, nil);

	initextents(&es, "testextents", 0, 0, 1, nil, nil, panic, emalloc);
	while((line = Brdstr(bp, '\n', 1)) != nil) {
		act = line[0];
		if(act == '-'){
			len = strtoull(line+1, nil, 10);
			ualloc(&es, len);
		}else{
			bno = strtoull(line, &p, 10);
			p++;	/* for the space */
			len = strtoull(p, nil, 10);
			ufree(&es, bno, len);
		}
		free(line);
	}

	showextentslists(1, "", &es);

	/* why bother? just exits(nil) as cinap suggests */
	Bterm(bp);
	exits(nil);
}