Page 1 of 1
malloc and processes' memory
Posted: Wed Jun 25, 2003 11:00 pm
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?
RE:malloc and processes' memory
Posted: Thu Jun 26, 2003 11:00 pm
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
RE:malloc and processes' memory
Posted: Thu Jun 26, 2003 11:00 pm
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
RE:malloc and processes' memory
Posted: Thu Jun 26, 2003 11:00 pm
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.
RE:malloc and processes' memory
Posted: Thu Jun 26, 2003 11:00 pm
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?
RE:malloc and processes' memory
Posted: Fri Jun 27, 2003 11:00 pm
by Anton
exactly