This is the mail archive of the guile@cygnus.com mailing list for the guile project.
Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
---|---|---|
Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |
Chris.Bitmead@misys.com.au writes: > There was some talk a while back about why precise GC was too hard in a C > environment. > I thought I might show people how I did it in a little scheme I wrote a > while back. It's pretty simple > and pretty easy to use, but I'll let people more expert comment on it. You > _don't_ have to use a > special macro to assign objects around. This idea has been pretty much toasted for general c code in guile, as well ;) (something similar might be necessary in code that modifies a chunk of protected memory, but for stack variables, etc, it's not necessary at all, so it will only be visible in parts of the system and smob code). > In a header file I put these definitions... > #define s_beginGcArea() int s_gcStackPos = s_gcContext.cStack.pos > #define s_prepGc(v) pushStackAutoResize(&s_gcContext.cStack, &(v)) > #define s_defGc(type, name, init) type name = (type)(s_prepGc(name), init) > #define s_endGcArea() popStackTo(&s_gcContext.cStack, s_gcStackPos) > > > Now when I want to write a C function called from Scheme I do something > like... > > s_String * s_newString(int len) > { > s_beginGcArea(); > s_defGc(s_String *, rtn, s_unspec_obj); > > rtn = s_allocType(String); > rtn->str = (char *)malloc(len + 1); > rtn->str[len] = EOS; > rtn->length = s_newExactNum(len); > s_endGcArea(); > return rtn; > } What if another thread invokes a gc after the call to s_endGcArea and before you return the value? You could get around this by disabling gc's when you call the c code, but in guile, this means most of the system can't start a gc! Problems would probably arise with continuations in c code, as well (although I can see some workarounds that could fix this). The main issue with using explicit roots on the stack, though, seems to be that it seems to be too easy to forget things when the going gets hairy. Things like using an assignment function, rather than using =, are only a programmer chore in terms of typing, not in terms of the concept involved. With the explicit marking, though, you have to always have the gc in mind when writing c code, which is annoying, and easily forgetable (and really, we want to forget about the gc, for the most part... it isn't doing it's job if we have to constantly worry about it). -- Greg