#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"

enum
{
	Nhole		= 128,
	Magichole	= 0x484F4C45,			/* HOLE */
};

typedef struct Hole Hole;
typedef struct Xalloc Xalloc;
typedef struct Xhdr Xhdr;

struct Hole
{
	uintptr_t	addr;
	uintptr_t	size;
	uintptr_t	top;
	Hole*	link;
};

struct Xhdr
{
	uint32_t	size;
	uint32_t	magix;
	char	data[];
};

struct Xalloc
{
	Lock;
	Hole	hole[Nhole];
	Hole*	flist;
	Hole*	table;
};

static Xalloc	xlists;

void
xinit(void)
{
	uint32_t maxpages, kpages, n;
	uintptr_t size;
	Confmem *mem;
	Hole *h, *eh;
	int i;

	eh = &xlists.hole[Nhole-1];
	for(h = xlists.hole; h < eh; h++)
		h->link = h+1;

	xlists.flist = xlists.hole;

	kpages = sys->npages - sys->upages;

	for(i=0; i<nelem(sys->mem); i++){
		mem = &sys->mem[i];
		n = mem->npage;
		if(n > kpages)
			n = kpages;
		/* don't try to use non-KADDR-able memory for kernel */
		maxpages = cankaddr(mem->base)/PGSZ;
		if(n > maxpages)
			n = maxpages;
		size = (uintptr_t)n*PGSZ;
		/* first give to kernel */
		if(n > 0){
			mem->kbase = (uintptr_t)KADDR(mem->base);
			mem->klimit = (uintptr_t)mem->kbase+size;
			if(mem->klimit == 0)
				mem->klimit = (uintptr_t)-PGSZ;
			xhole(mem->base, mem->klimit - mem->kbase);
			kpages -= n;
		}
		/* if anything left over, give to user */
		if(n < mem->npage){
			umem_region(mem->base+size, mem->npage - n);
		}
	}
	xsummary();
}

void*
xspanalloc(uint32_t size, int align, uint32_t span)
{
	uintptr_t a, v, t;

	a = (uintptr_t)xalloc(size+align+span);
	if(a == 0)
		panic("xspanalloc: %lud %d %lux", size, align, span);

	if(span > 2) {
		v = (a + span) & ~((uintptr_t)span-1);
		t = v - a;
		if(t > 0)
			xhole(PADDR(a), t);
		t = a + span - v;
		if(t > 0)
			xhole(PADDR(v+size+align), t);
	}
	else
		v = a;

	if(align > 1)
		v = (v + align) & ~((uintptr_t)align-1);

	return (void*)v;
}

void*
xallocz(uint32_t size, int zero)
{
	Xhdr *p;
	Hole *h, **l;

	/* add room for magix & size overhead, round up to nearest vlong */
	size += BY2V + offsetof(Xhdr, data[0]);
	size &= ~(BY2V-1);

	ilock(&xlists);
	l = &xlists.table;
	for(h = *l; h; h = h->link) {
		if(h->size >= size) {
			p = (Xhdr*)KADDR(h->addr);
			h->addr += size;
			h->size -= size;
			if(h->size == 0) {
				*l = h->link;
				h->link = xlists.flist;
				xlists.flist = h;
			}
			iunlock(&xlists);
			if(zero)
				memset(p, 0, size);
			p->magix = Magichole;
			p->size = size;
			return p->data;
		}
		l = &h->link;
	}
	iunlock(&xlists);
	return nil;
}

void*
xalloc(uint32_t size)
{
	return xallocz(size, 1);
}

void
xfree(void *p)
{
	Xhdr *x;

	x = (Xhdr*)((uintptr_t)p - offsetof(Xhdr, data[0]));
	if(x->magix != Magichole) {
		xsummary();
		panic("xfree(%#p) %#ux != %#lux", p, Magichole, x->magix);
	}
	xhole(PADDR((uintptr_t)x), x->size);
}

int
xmerge(void *vp, void *vq)
{
	Xhdr *p, *q;

	p = (Xhdr*)(((uintptr_t)vp - offsetof(Xhdr, data[0])));
	q = (Xhdr*)(((uintptr_t)vq - offsetof(Xhdr, data[0])));
	if(p->magix != Magichole || q->magix != Magichole) {
		int i;
		uint32_t *wd;
		void *badp;

		xsummary();
		badp = (p->magix != Magichole? p: q);
		wd = (uint32_t *)badp - 12;
		for (i = 24; i-- > 0; ) {
			print("%#p: %lux", wd, *wd);
			if (wd == badp)
				print(" <-");
			print("\n");
			wd++;
		}
		panic("xmerge(%#p, %#p) bad magic %#lux, %#lux",
			vp, vq, p->magix, q->magix);
	}
	if((uint8_t*)p+p->size == (uint8_t*)q) {
		p->size += q->size;
		return 1;
	}
	return 0;
}

void
xhole(uintptr_t addr, uintptr_t size)
{
	Hole *h, *c, **l;
	uintptr_t top;

	if(size == 0)
		return;

	top = addr + size;
	ilock(&xlists);
	l = &xlists.table;
	for(h = *l; h; h = h->link) {
		if(h->top == addr) {
			h->size += size;
			h->top = h->addr+h->size;
			c = h->link;
			if(c && h->top == c->addr) {
				h->top += c->size;
				h->size += c->size;
				h->link = c->link;
				c->link = xlists.flist;
				xlists.flist = c;
			}
			iunlock(&xlists);
			return;
		}
		if(h->addr > addr)
			break;
		l = &h->link;
	}
	if(h && top == h->addr) {
		h->addr -= size;
		h->size += size;
		iunlock(&xlists);
		return;
	}

	if(xlists.flist == nil) {
		iunlock(&xlists);
		print("xfree: no free holes, leaked %llud bytes\n", (uint64_t)size);
		return;
	}

	h = xlists.flist;
	xlists.flist = h->link;
	h->addr = addr;
	h->top = top;
	h->size = size;
	h->link = *l;
	*l = h;
	iunlock(&xlists);
}

void
xsummary(void)
{
	int i;
	Hole *h;
	uintptr_t s;

	i = 0;
	for(h = xlists.flist; h; h = h->link)
		i++;
	print("%d holes free\n", i);

	s = 0;
	for(h = xlists.table; h; h = h->link) {
		print("%#8.8p %#8.8p %llud\n", h->addr, h->top, (uint64_t)h->size);
		s += h->size;
	}
	print("%llud bytes free\n", (uint64_t)s);
}
