I have a memory manager in my OS. Should I allocate any memory using the malloc() function just after my OS boots?
Thanks,
Stephen
Memory Manager
Re:Memory Manager
That's a very vague question. It looks like your asking "I have written a malloc, should I use it?". You need to be more specific.
Re:Memory Manager
Your question suggests that your concept of memory management is... vague.
Where does that malloc() you want to use come from? Did you write it yourself?
Traditionally, malloc() is a user-space function that handles the higher levels of memory management (i.e., first fit / best fit / ... ) using a memory pool given to the application by the kernel at startup. When that pool is exhausted, malloc() calls sbrk(), morecore() or some other kernel function that allocates additional memory to that application's pool.
One of the goals of a good malloc() is to keep the unused pool as small as possible (not to waste memory) while calling the kernel as seldom as possible (not to waste clock cycles).
That means that using malloc() in kernel space is... strange. Usually, you have some specific kernel-space funktion (e.g. kmalloc()) that does the allocation work for the kernel, but that function can call sbrk() / morecore() directly without having to go through a system call, since you are in kernel space already, right?
If you answered my first question, "did you write that malloc() yourself", with yes, you most likely have written a kmalloc(). No, you should not allocate all available memory. You should allocate what your kernel needs, and leave the rest for the memory manager to pass out to incoming sbrk() / morecore() calls.
If you didn't write it yourself, are you sure you wrote the necessary kernel functions (sbrk() most likely) to support the malloc() you copied?
Where does that malloc() you want to use come from? Did you write it yourself?
Traditionally, malloc() is a user-space function that handles the higher levels of memory management (i.e., first fit / best fit / ... ) using a memory pool given to the application by the kernel at startup. When that pool is exhausted, malloc() calls sbrk(), morecore() or some other kernel function that allocates additional memory to that application's pool.
One of the goals of a good malloc() is to keep the unused pool as small as possible (not to waste memory) while calling the kernel as seldom as possible (not to waste clock cycles).
That means that using malloc() in kernel space is... strange. Usually, you have some specific kernel-space funktion (e.g. kmalloc()) that does the allocation work for the kernel, but that function can call sbrk() / morecore() directly without having to go through a system call, since you are in kernel space already, right?
If you answered my first question, "did you write that malloc() yourself", with yes, you most likely have written a kmalloc(). No, you should not allocate all available memory. You should allocate what your kernel needs, and leave the rest for the memory manager to pass out to incoming sbrk() / morecore() calls.
If you didn't write it yourself, are you sure you wrote the necessary kernel functions (sbrk() most likely) to support the malloc() you copied?
Every good solution is obvious once you've found it.