Page 1 of 1

Memory manager align on 4K boundaries

Posted: Tue Aug 16, 2016 7:33 am
by michaellangford
Hello, I wrote a simple bitmap memory allocator, using the map of free memory given by GRUB, and linker symbols for where my kernel is. It works fine, and allocates 4KB blocks. I have made it so that it stores the bitmap after the kernel, and only allocates memory after the kernel. I want to use it for page allocation, but I'm not sure how to have it align on 4K boundaries. Any suggestions? Would modifying the memory space that my manager has to work with by increasing the base pointer until it is 4K aligned work? Thanks!

Re: Memory manager align on 4K boundaries

Posted: Tue Aug 16, 2016 8:41 am
by Ch4ozz
Im using this for my malloc function:

Code: Select all

uint32_t mmap_to_address(uint32_t index, uint32_t bit)
{
	if(index >= BITMAP_SIZE || bit >= 32)
		return -1;
	
	return 0x20000 * index + bit * 0x1000;
}
Nothing too advanced though, just change the * 0x1000 to your needs

Hope I understood your question right

Re: Memory manager align on 4K boundaries

Posted: Tue Aug 16, 2016 9:11 am
by Sourcer
I align the base pointer just as you've said. This does the job pretty well.

Code: Select all

#define NEXT_PAGE_BOUNDARY(val) ((val & ~(PAGE_SIZE -1)) + PAGE_SIZE)

Re: Memory manager align on 4K boundaries

Posted: Thu Aug 18, 2016 8:55 am
by michaellangford
Thanks! Your information helped get an idea for 4KB aligning everything. I've got it 4KB aligned now.