malloc and processes' memory

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
HOS

malloc and processes' memory

Post by HOS »

lets say i have 256mb of memory and the system/kernel is using from 0-2mb, after 2mb it is free for graphics/apps. would it be a good idea to use malloc() to allocate video memory of whatever size needed (say 4mb for my video configuration)? or should the video memory be in a fixed place? also, is it only the kernel that would make calls to malloc() or would processes also? i was thinking of having the kernel allocate memory for a new process and then just map the process to that memory, and the process would not have to allocate anything on its own. is this a good ide?
Anton

RE:malloc and processes' memory

Post by Anton »

"i was thinking of having the kernel allocate memory for a new process and then just map the process to that memory, and the process would not have to allocate anything on its own", thats how it is done in real OSs, except that processes still need to call malloc some times for dynamic data, such as a big matrix to solve.

"also, is it only the kernel that would make calls to malloc() or would processes also?"Kernel never call's malloc, it has it's own ways. malloc is only used by processes.
" would it be a good idea to use malloc() to allocate video memory of whatever size needed (say 4mb for my video configuration)? or should the video memory be in a fixed place?"You can't do it like that, since you have certain restrictions on where you can map the video memory of the video card(like a 1mb alignment). Also, malloc usualy works in virtual addresses not physical.
Anton
HOS

RE:malloc and processes' memory

Post by HOS »

you say that the kernel allocates memory for a new process, but the kernel does not ever call malloc() but the process calls malloc() for dynamic storage. ok, then is the memory that malloc() is in charge of separate from where processes run from? and how does the kernel allocate memory and decide where to put the process without using malloc()?

thanks for your feedback, i am just getting into memory management
Anton

RE:malloc and processes' memory

Post by Anton »

There are at least two levels of memory allocation:system(one allocated by the kernel driver-module Physical Mem Manager), and dynamic(malloc).
When malloc is called by the user program it tries to allocate memory, if it can't then it calls a system function to allocate a "big" chunk of memory, and then it preeceds with it's allocation.
Basicly malloc allocates any sized chunk of memory, where as the system function should work in big blocks(4k). The allocation algorithm is also different, since malloc saves the size of the block in the block it self, but the system function can't do that.
HOS

RE:malloc and processes' memory

Post by HOS »

ok, so then the system function controls pretty much all of ram (except for fixed reserved portions) and it allocates memory in 4k pages for processes, etc, and also for malloc() to reassign in variable amounts to whatever process requests it?
Anton

RE:malloc and processes' memory

Post by Anton »

exactly
Post Reply