[SOLVED] Stuck on passing grub memory map into my allocator

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
RharryR
Posts: 17
Joined: Thu Feb 11, 2016 1:15 am
Location: Milingimbi Island, Australia

[SOLVED] Stuck on passing grub memory map into my allocator

Post by RharryR »

Hey all.
Im writing a basic allocator for my kernel.
I made a awesome allocate function that works great.
I use linked structs that contain infomation about free space for my allocator, here is the struct:

Code: Select all

typedef struct
{
	void* next;
	void* prev;
	uint32_t size;
	void* base;
} free_loc;
That all works fine, but I want to be able to load free memory areas into seperate free_loc's.
I am using grub to get the memory map. Here is my memory allocator init function:

Code: Select all

void pmm_init(void* mmloc, unsigned long mmsize)
{
	mmap_entry_t* cmap=(mmap_entry_t*)mmloc;
	
	int i=0;
	free_loc* last;
	while(cmap < (mmap_entry_t*) mmloc + mmsize)
	{
		if(cmap->type == 1)
		{
			if(i==0)
			{
				first->size = cmap->len;
				first->base = cmap->base;
				first->prev = 0;
				last = first;
			}else{
				free_loc* append;
				append->size = cmap->len;
				append->base = cmap->base;
				append->prev = last;
				last->next = append;
				last = append;
			}
			i++;
		}
		cmap = (mmap_entry_t*) ( (unsigned int)cmap + cmap->size + sizeof(uint32_t));
	}
}
It loops through the memory map, checking if a area is useable.
Heres the issue: I need to somehow leave a reference to the last area for the next area to attach its 'prev' too.
I am trying to use the pointer 'last' to acheive this.
It does what it needs to do fine, but it modifies the last area at this line:

Code: Select all

last = append;
I basicly want to change the 'free_loc' the pointer points too, but it is overwriting the data instead.
Im terrible at explaining so hopefully you can understand.

Thanks, Harry
Last edited by RharryR on Thu Mar 03, 2016 11:46 pm, edited 1 time in total.
RharryR
Posts: 17
Joined: Thu Feb 11, 2016 1:15 am
Location: Milingimbi Island, Australia

Re: Stuck on passing grub memory map into my allocator

Post by RharryR »

OK, I actually solved this 5 seconds after I hit submit, all I had to do was change

Code: Select all

last = append;
to

Code: Select all

last = (void*)append;
. C probaly recignizes that were passing a location rather a value.

Yay! A working allocator :D
Post Reply