How would you implement refcounting in the presence of longjmp? I don't see a graceful way to do it.
- a 'recovery' list of (object pointer, old refcount) pairs
- whenever you allocate something, add (object pointer, 0) to the list
- when you update a reference count, check whether the object is in the list.
If it is not, add it to the list, thus remembering the original refcount
- whenever the code decreases reference count to zero, add the object to a
'to be deleted' list (this could possibly be a linked list chained through
the 'refcount' fields; the old refcount will be in the recovery list)
- when the parser reaches toplevel without longjmp, clear the first list,
and delete any objects in the 'to be deleted' list.
- when a longjmp occurs, walk the list and reset reference counts. For
zero 'old refcounts', delete the object
Elegant? Not really, but with proper macros/functions, it should not be much less elegant than reference counting on its own.Bikeshedding like this isn't useful to anyone.
As for the alternatives, well return values never killed anyone, did they? You can use gotos to simplify the control flow within the functions. Other than that, there really isn't much to explain.
I don't like longjmp because I never expect them in C. I never think "hey, the control flow might jump to some point 20 stack frames above at any moment when I enter this library". And then I use mutexes. Cue the drama. You can't even protect yourself from the stack unwinding with handler-case or try ... catch/except.
If you criticize my work, you're doing me a favor.