Understanding the heap

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
User avatar
einsteinjunior
Member
Member
Posts: 90
Joined: Tue Sep 11, 2007 6:42 am

Understanding the heap

Post by einsteinjunior »

Hi everyone out there,

I am currently designing an operating system ,but i am planning to add some more and that will need from what i see ,dynamic memory allocation within the kernel itself.I am coding in standard C.
What i wish to do now is to code a malloc ,free and realloc function.
I am having problems understanding what actually is going on behind the scenes of these functions.My questions are:

-Where is the heap located for dynamic memory allocation?In my program's data area or is it elsewhere?

-How do i get a pointer to the heap if this is the case so i can allocate memory and if not the case,how do i then implement dynamic memory allocation?

Thanks to all out there.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Understanding the heap

Post by AJ »

Hi,
einsteinjunior wrote: -Where is the heap located for dynamic memory allocation?
Basically, wherever you want it to be. For example, my kernel is located at the 3GiB mark (0xC0000000). My kernel's heap starts just above this and can expand up to around 0xEFFFFFFF. I use the space above 0xF0000000 for other stuff.

My user programs tend to be loaded at the 1MiB mark (0x100000). Say a program is 0x1000 bytes long, the heap will therefore start at 0x101000. As the program's stack is just below the kernel, the heap can expand upwards as long as it does not get to within 1 page of the stack.
einsteinjunior wrote: In my program's data area or is it elsewhere?
If I understand correctly (by the "data area" you mean the .data section), then no, it is elsewhere (as outlined above).

einsteinjunior wrote: -How do i get a pointer to the heap if this is the case
In you kernel, just allocate a pointer dynamically. For example, you know your kernel's end point. Just do something like:

Code: Select all

heap_start = heap_end = (unsigned long *)mykernelend + 1;
Whenever you call your kernel version of malloc(), it needs to check the heap size, and call something like sbrk(x) to extend the heap upwards (after doing some basic safety checking). Conventionally, you could have a linked list of free heap space that malloc() allocates from within this heap.

In user space, sbrk() needs to point to a system call.

There is a lot more to malloc() and free(). For more information, I suggest reading http://www.osdever.net/tutorials/pdf/memory1.pdf and http://www.osdever.net/tutorials/pdf/memory2.pdf .

I hope some of this helps. Cheers,
Adam
User avatar
einsteinjunior
Member
Member
Posts: 90
Joined: Tue Sep 11, 2007 6:42 am

Post by einsteinjunior »

Thanks for the reply.
I will be using the PE file format for my applications.
How cani write the user app so that it can know where the heap starts effectively since here we will be dealing with a segmented memory model as far as the PE file format is concerned.

Thanks Ak once more :D
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Hi,

I'm afraid I am pretty unfamiliar with segmentation. If you are just referring to PE segments, you can still relocate these in a flat memory model.

What you need to do is write a crt0 function. This function gets called as the entry point of your user space program. It will call the system call for sbrk() before calling your program's main() function. So, sbrk(0) will give you the heap base and sbrk(desiredstartingheapsize) will give you the heap end.

You can then set up the first 'free space item' in your linked list which is traversed by malloc(). If you are using paging, you do not need to page in all the space allocated on the heap until it is actually used by the user app (you will need a page fault exception handler which can deal with this).

Cheers,
Adam
Post Reply