-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Is it possible to "lock" a memory block allocated by gc ? #326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
One very easy way to lock a block is to just keep a pointer to that block (blocks won't be freed if a pointer to them exists). If you only need to lock it within the lifetime of a function, then just keep a pointer on the stack (ie a local variable). Otherwise, use a global variable to store the pointer. Then either set that variable to NULL when you're done with the block (and it'll be freed during the next collection), or call gc_free on that pointer (and also set the pointer to NULL if it's a global variable). |
what if say I have an array which has pointers to other objects, I would have to keep those on the stack too, right ? if I understand correctly, the only other way to do this is to add another bit to mark the object as locked ? |
No, those other objects are pointed to (by the array), and so will remain "locked", so to speak. Really you don't need to worry about this. The beauty of a GC is that you don't have to worry about memory management. You just allocate memory, and as long as a pointer to that memory exists somewhere, it'll remain valid memory. |
you know this issue is actually related to the realloc issue, I only ask about this because I was realloc'ing memory to a dynamic array and at some point it returned NULL, which made me think that GC collected the memory.. I will close this one now. Thanks! |
I'm curios about this too. Can GC move allocated and locked blocks (to On Thu, Feb 27, 2014 at 10:20 AM, Damien George notifications@github.comwrote:
Meski "Going to Starbucks for coffee is like going to prison for sex. Sure, |
No. It's a conservative GC, so it cannot do relocations/compacting. It would require some work to make it an exact GC: all pointers that exist in C would have to be treated specially, since at any point they can change. |
is it currently possible to allocate a block with
gc_alloc
and somehow keep it from being collected until it's explicitly free'd withgc_free
? sorry if this doesn't make sense, I don't fully understand how the GC works.The text was updated successfully, but these errors were encountered: