Thread: [GD-Linux] Reinitializing the heap
Brought to you by:
vexxed72
From: Josh A. <ja...@se...> - 2001-11-27 21:15:19
|
Hey all, Is it possible to reinitialize the standard libc malloc heap during runtime? I would rather do this than try to make sure every last bit of memory is cleaned up (it's a game after all). I'm looking for anything from an externed function I can call (like __malloc_init) or getting the 'top' of the heap and setting it to 0 or something like that. Any hints would be greatly appreciated, Josh -- Josh Adams [ja...@se...] Senior Programmer, Founder Secret Level [www.secretlevel.com] |
From: J C L. <cl...@ka...> - 2001-11-27 21:33:47
|
On Tue, 27 Nov 2001 13:20:39 -0800 Josh Adams <ja...@se...> wrote: > Hey all, Is it possible to reinitialize the standard libc malloc > heap during runtime? I would rather do this than try to make sure > every last bit of memory is cleaned up (it's a game after all). Not portably/reliably, well, not without just dropping your current context via something like an exec() which does the right VM things. If you can show your context out in a shm block this can work fairly nicely (you get recycled file handles, IPC structures etc as well which can be useful). > I'm looking for anything from an externed function I can call > (like __malloc_init) or getting the 'top' of the heap and setting > it to 0 or something like that. The easiest way to get this behaviour would be to write a local allocator which offered those semantics. I did this at one point with a head implementation that allowed watermarking. Basically you could make watermarks at various times, and then free all still-valid allocations back to the named watermark (yeah, single threaded with linear process flows). -- J C Lawrence ---------(*) Satan, oscillate my metallic sonatas. cl...@ka... He lived as a devil, eh? http://www.kanga.nu/~claw/ Evil is a name of a foeman, as I live. |
From: Ryan C. G. <ic...@cl...> - 2001-11-27 22:31:24
|
> The easiest way to get this behaviour would be to write a local > allocator which offered those semantics. I did this at one point > with a head implementation that allowed watermarking. Basically you > could make watermarks at various times, and then free all > still-valid allocations back to the named watermark (yeah, single > threaded with linear process flows). For the truly brave, along those lines: http://www.gnu.org/manual/glibc-2.2.3/html_chapter/libc_3.html#SEC34 Change the hooks, and you won't even need to change the code that you need to put in a separate memory "boxcar"...Design functions that flag a given memory pointer as contained in a given boxcar, set the hooks appropriately when you want to watch for this, and then, when the questionable code is done, just detach that whole boxcar from the metaphorical memory train and put the original hooks back. This could also be a poor man's garbage collector, if you've got leaky code, and, if coded right, won't add too much overhead to the existing program. That being said, someone needs to speak reasonably and say that all of this is interesting, but bad, bad practice. Shrugging your shoulders and saying "hey, it's a video game after all, so who cares if it's got leaks in the first place and I choose a non portable hack to fix them in the second?" is not only shortsighted, but it says a lot about game coding mentalities that are rampant in the industry. I would recommend that you attack memory problems with the correct tools: dmalloc, ElectricFence, mcheck(), etc. It may save your sanity later on. --ryan. |
From: Steve B. <sjb...@ai...> - 2001-11-28 00:04:29
|
"Josh Adams" wrote: > The easiest way to get this behaviour would be to write a local > allocator which offered those semantics. I did this at one point > with a head implementation that allowed watermarking. Basically you > could make watermarks at various times, and then free all > still-valid allocations back to the named watermark (yeah, single > threaded with linear process flows). That seems dangerous beyond sanity. If you use *any* library functions, you could end up freeing memory it still has pointers to...your graphics library, even (in principal) the C standard library could be using heap memory that you don't know about. Suppose there was something like: void some_glibc_function () { static char *pointer = NULL ; if ( pointer == NULL ) { pointer = malloc ( 100 ) ; precompute_something () ; } ...do something using the precomputed data... } ...that would fail disasterously if you did what you propose. This kind of dirty trick is common in the games console business - but those don't have operating systems (well, not that matter) and you can do this kind of disgusting thing and get away with it. "Ryan C Gordon" replied: >> That being said, someone needs to speak reasonably and say that all of >> this is interesting, but bad, bad practice. I just flat out don't think it'll work. >> Shrugging your shoulders and >> saying "hey, it's a video game after all, so who cares if it's got leaks >> in the first place and I choose a non portable hack to fix them in the >> second?" is not only shortsighted, but it says a lot about game coding >> mentalities that are rampant in the industry. Yep. A memory leak is a **BUG** and you should determinedly seek it out because you don't know what *all* of it's side-effects are until you've found it. What might have the obviously visible side effects of leaking memory might also be corrupting something...who knows? >> I would recommend that you attack memory problems with the correct tools: >> dmalloc, ElectricFence, mcheck(), etc. It may save your sanity later on. Yes. ----------------------------- Steve Baker ------------------------------- Mail : <sjb...@ai...> WorkMail: <sj...@li...> URLs : http://www.sjbaker.org http://plib.sf.net http://tuxaqfh.sf.net http://tuxkart.sf.net http://prettypoly.sf.net http://freeglut.sf.net http://toobular.sf.net http://lodestone.sf.net |