Show HN: A simple garbage collector for C
1–10 of 62 posts
Re: Show HN: A simple garbage collector for C
#2Re: Show HN: A simple garbage collector for C
#3Is there a simple GC like this one that uses handles instead of C stack scanning to keep track of local references?
Re: Show HN: A simple garbage collector for C
#4Re: Show HN: A simple garbage collector for C
#5I just wish C had something like defer in go, That would cover most cases.
[0] https://web.archive.org/web/20180426195701/http://fdiv.net/2...
Re: Show HN: A simple garbage collector for C
#6Re: Show HN: A simple garbage collector for C
#7I just wish C had something like defer in go, That would cover most cases.
Defer can be emulated with the block extensions of Clang and GCC [0], though I'm not sure how much I'd like to see something like that in a codebase. [0] https://web.archive.org/web/20180426195701/http://fdiv.net/2...
Re: Show HN: A simple garbage collector for C
#8Earlier quoted context omitted.
Defer can be emulated with the block extensions of Clang and GCC [0], though I'm not sure how much I'd like to see something like that in a codebase. [0] https://web.archive.org/web/20180426195701/http://fdiv.net/2...
That link seems to be broken? Nothing opens when I click it.
Re: Show HN: A simple garbage collector for C
#9Earlier quoted context omitted.
Defer can be emulated with the block extensions of Clang and GCC [0], though I'm not sure how much I'd like to see something like that in a codebase. [0] https://web.archive.org/web/20180426195701/http://fdiv.net/2...
That link seems to be broken? Nothing opens when I click it.
The magic is basically:
static inline void defer_cleanup(void (^*b)(void)) { (*b)(); }
#define defer_merge(a,b) a##b
#define defer_varname(a) defer_merge(defer_scopevar_, a)
#define defer __attribute__((cleanup(defer_cleanup))) void (^defer_varname(__COUNTER__))(void) =
Which let's you do: FILE *a = fopen ("a.txt", "r");
if (!a)
return EXIT_FAILURE;
defer
{
fclose (a);
};
It uses cleanup and blocks, both of which are extensions. There may also be some strangeness with the way blocks hold memory. (Block references become const copies).Re: Show HN: A simple garbage collector for C
#10I just wish C had something like defer in go, That would cover most cases.
#define DEFER(EXPR) for(int _tmp=1; _tmp; _tmp=0,(EXPR))
Example: char * data = malloc(32);
DEFER(free(data))
{
// do stuff
}