#include <mm/vmm.h>
#include <ubixos/types.h>
#include <ubixos/kpanic.h>
#include <ubixos/sched.h>

void *kmalloc(size_t size)
{
	mMap *tmp, *tmp2;
	double pages, p;
	uInt32 retAddr;

	if(size <= 0)
		kpanic("kmalloc: trying to allocate zero sized page\n");

	if(size < 4096)
		pages = 1.0;
	else
		pages = size / 4096.0;
	tmp2 = NULL;

	kprintf("Trying to allocate [%d] bytes\n", size);

	for(p = 0.0 ; p < pages ; p += 1.0)
	{
		if(tmp2 != NULL)
			tmp->Link = tmp2;
		tmp = (mMap *)mmGetFreeVirtualPage(_current);
		if(tmp == NULL)
			kpanic("kmalloc: out of memory\n");
		if(tmp2 == NULL)
			retAddr = tmp->pageAddr;
		tmp2 = tmp;
		kprintf("retAddr = {%x} {%x} {%x}\n", retAddr, tmp->pageAddr, tmp->physicalAddr);
	}
	return (void *)retAddr;
}
