heap management in JamesM tutorial

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
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

heap management in JamesM tutorial

Post by vjain20 »

Hi,

I am not understanding the calculation of physical address in the kmalloc function in JamesM tutorial.
As far as I understand , every address is identity-mapped. So the virtual address of the allocated block (addr) should
equal its physical address. Why is it added to address of the frame ?

Code: Select all

u32int kmalloc_int(u32int sz, int align, u32int *phys)
{
    if (kheap != 0)
    {
        void *addr = alloc(sz, (u8int)align, kheap);
        if (phys != 0)
        {
            pte_t *page = get_pte((u32int)addr, 0, kernel_directory);
            *phys = page->frame*0x1000 + (u32int)addr&0xFFF;
        }
        return (u32int)addr;
    }else {.... }
- Thanks
Vaibhav jain
User avatar
serviper
Member
Member
Posts: 31
Joined: Sat Jul 16, 2011 6:05 am
Location: China
Contact:

Re: heap management in JamesM tutorial

Post by serviper »

vjain20 wrote:Hi,

I am not understanding the calculation of physical address in the kmalloc function in JamesM tutorial.
As far as I understand , every address is identity-mapped. So the virtual address of the allocated block (addr) should
equal its physical address. Why is it added to address of the frame ?

Code: Select all

u32int kmalloc_int(u32int sz, int align, u32int *phys)
{
    if (kheap != 0)
    {
        void *addr = alloc(sz, (u8int)align, kheap);
        if (phys != 0)
        {
            pte_t *page = get_pte((u32int)addr, 0, kernel_directory);
            *phys = page->frame*0x1000 + (u32int)addr&0xFFF;
        }
        return (u32int)addr;
    }else {.... }
The kernel heap starts at linear address 0xC0000000 (3GB) - not identity mapped.
Look for KHEAP_START in kheap.h.
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: heap management in JamesM tutorial

Post by vjain20 »

Sorry I did not think much. Now I got it.
addr is the virtual address of the allocated block so the last 12 bits will be the offset.
Adding these to the physical address of the frame will give the physical address of the block.
Thanks!
- Thanks
Vaibhav jain
Post Reply