Skip to content

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

Closed
iabdalkader opened this issue Feb 26, 2014 · 7 comments
Closed

Is it possible to "lock" a memory block allocated by gc ? #326

iabdalkader opened this issue Feb 26, 2014 · 7 comments

Comments

@iabdalkader
Copy link
Contributor

is it currently possible to allocate a block with gc_alloc and somehow keep it from being collected until it's explicitly free'd with gc_free ? sorry if this doesn't make sense, I don't fully understand how the GC works.

@dpgeorge
Copy link
Member

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).

@iabdalkader
Copy link
Contributor Author

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 ?

@dpgeorge
Copy link
Member

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.

@iabdalkader
Copy link
Contributor Author

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!

@meski
Copy link

meski commented Feb 26, 2014

I'm curios about this too. Can GC move allocated and locked blocks (to
defragment the heap) and keep the pointers valid? That'd require a virtual
/ physical manager, I'd guess.

On Thu, Feb 27, 2014 at 10:20 AM, Damien George notifications@github.comwrote:

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.


Reply to this email directly or view it on GitHubhttps://github.com//issues/326#issuecomment-36190672
.

Meski

http://courteous.ly/aAOZcv

"Going to Starbucks for coffee is like going to prison for sex. Sure,
you'll get it, but it's going to be rough" - Adam Hills

@iabdalkader
Copy link
Contributor Author

@meski issue #322 is more related I think...

@dpgeorge
Copy link
Member

Can GC move allocated and locked blocks (to defragment the heap) and keep the pointers valid?

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants