shithub: lpa

ref: 8dc7c659e34ad5d244477e01b1ebf4def63be24d
dir: /memory.c/

View raw version
#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>

#include "dat.h"
#include "fns.h"

/* This version of the memory manager is very stupid,
 * but it allows us to get started..
 */
typedef struct Allocation Allocation;
struct Allocation
{
	usize size;
	int tag;
	int root;
	void *buf;
	void *extra;
};

uvlong nallocs;
static Allocation **allocations; /* all allocations */

DataSpec dataspecs[DataMax] = {
	/* DataAux: setup in fs.c */
	[DataSession] = {.size = sizeof(Session) },
	/* DataSessionList: setup in session.c */
	[DataModule] = {.size = sizeof(Module) },
	[DataModuleList] = {.size = sizeof(ModuleList) },
	[DataSymtab] = {.size = sizeof(Symtab) },
	[DataSymbol] = {.size = sizeof(Symbol) },
	[DataEnumeration] = {.size = sizeof(Enumeration) },
	[DataTokenList] = {.size = sizeof(TokenList) },
	[DataAst] = {.size = sizeof(Ast) },
	[DataByteCode] = {.size = sizeof(ByteCode) },
	[DataValueStack] = {.size = sizeof(ValueStack) },
};

void *
alloc(int tag)
{
	usize size = dataspecs[tag].size;
	Allocation *a = emalloc9p(sizeof(Allocation) + size);
	a->size = size;
	a->tag = tag;
	a->root = 0;
	a->buf = ((uchar*)a)+sizeof(Allocation);
	a->extra = nil;
	memset(a->buf, 0, size);

	nallocs++;
	allocations = erealloc9p(allocations, nallocs * sizeof(Allocation*));
	allocations[nallocs-1] = a;

	return a->buf;
}

static Allocation *
allocptr(void *v)
{
	uchar *p = v;
	p -= sizeof(Allocation);
	return (Allocation*)p;
}

int
getalloctag(void *d)
{
	Allocation *a = allocptr(d);
	return a->tag;
}

void
setroot(void *d, int v)
{
	Allocation *a = allocptr(d);
	a->root = v;
}

void *
dataptr(void *d)
{
	Allocation *a = allocptr(d);
	return a->buf;
}

void *
allocextra(void *d, usize size)
{
	Allocation *a = allocptr(d);
	a->extra = erealloc9p(a->extra, size);
	return a->extra;
}