#include <mm/vmm.h>
#include <ubixos/vitals.h>
#include <ubixos/spinlock.h>
#include <ubixos/kpanic.h>
#include <ubixos/sched.h>
#include <string.h>

void mmCopyVirtualSpace(kTask_t *task, kTask_t *new)
{
	mMap *tmp, *tmp2 , *prev;

	tmp = task->FirstPage;

	if(tmp != NULL)
	{
		tmp2 = (mMap *)kmalloc(sizeof(mMap));
		new->FirstPage = tmp2;
		prev = NULL;
	}
	else
	{
		new->FirstPage = NULL;
		new->LastPage = NULL;
		return;
	}	
	if(tmp->Next == NULL)
	{
		tmp2->Next = NULL;
		tmp2->Previous = NULL;
		new->LastPage = tmp2;
	}
	else
	{
		for(;;)
		{
			tmp2->Previous = prev;
			tmp2->pageAddr = tmp->pageAddr;
			prev = tmp2;
			tmp2 = (mMap *) kmalloc(sizeof(mMap));
			prev->Next = tmp2;
			tmp = tmp->Next;
			if(tmp == NULL)
			{
				kfree(tmp2);
				prev->Next = NULL;
				new->LastPage = prev;
				break;
			}
		}
	}
	return;
}

