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.
Understanding the heap
Re: Understanding the heap
Hi,
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.
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
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.einsteinjunior wrote: -Where is the heap located for dynamic memory allocation?
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.
If I understand correctly (by the "data area" you mean the .data section), then no, it is elsewhere (as outlined above).einsteinjunior wrote: In my program's data area or is it elsewhere?
In you kernel, just allocate a pointer dynamically. For example, you know your kernel's end point. Just do something like:einsteinjunior wrote: -How do i get a pointer to the heap if this is the case
Code: Select all
heap_start = heap_end = (unsigned long *)mykernelend + 1;
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
- einsteinjunior
- Member
- Posts: 90
- Joined: Tue Sep 11, 2007 6:42 am
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
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