Skip to content
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

Customizable object marking #17

Open
p7g opened this issue Dec 13, 2020 · 0 comments
Open

Customizable object marking #17

p7g opened this issue Dec 13, 2020 · 0 comments

Comments

@p7g
Copy link
Owner

p7g commented Dec 13, 2020

Currently, when allocating an object that should be managed by the garbage collector, you can specify a function that will run to clean up before the object is freed. It is not, however, possible to customize how the object is marked.

To do this, you have to add a new branch to cb_value_mark, which won't work when extensions are available.

A new field could be added to cb_gc_header which is a function pointer to a function that will mark the object. The default (if the field is NULL) would be a function that does nothing. The downside is that every object will one pointer larger (which adds up). Unfortunately, it's not really feasible to put this function anywhere else. In Python, it could live in the type of the object, but since we don't have extensible types here, that wouldn't help much.

One alternative would be to define a struct like this:

struct cb_gc_hooks {
	void (*deinit)(void *obj);
	void (*mark)(void *obj);
};

And then every object would still have just one pointer, to an instance of this struct. In most cases I think this should be statically allocated. If any new fields are needed in the future they could be added to the struct type.

The downside here would be an extra level of indirection when collecting and marking the object, but that is probably more acceptable than every object be 4 or 8 bytes larger.

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

1 participant