Path: utzoo!mnetor!uunet!husc6!cmcl2!brl-adm!umd5!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c++ Subject: Re: Proposal for Exceptions for C++ Message-ID: <10998@mimsy.UUCP> Date: 9 Apr 88 22:22:40 GMT References: <8180006@eecs.nwu.edu> <6590029@hplsla.HP.COM> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 54 In article <6590029@hplsla.HP.COM> jima@hplsla.HP.COM (Jim Adcock) writes: >Well now, there seems to be several people who claim to have solutions >to the C++ exception problem, but I have yet to see anyone explain how >to call destructors for the objects going out of scope. What is needed is a generic `unwind-protect' (not necessarily in the language itself, but certainly for the compiler). The code generated will then look something like this (pseudo C++ syntax): /* object o; protection p: */ if (unwind_protect(&p)) { o.~o(); unwind_resume(&p); } o.o(); ... o.~o(); remove_protection(&p); It is important that the `unwind_protect' and `remove_protection' operations be fast, as these could occur quite often. In stack architectures this can usually be done with `protection objects' and address arithmetic. Something like the following can be made to work: struct protection { struct protection *outer; /* outer protection(s) */ jmp_buf label; /* context */ } InnermostProt; /* current innermost active protection */ #define unwind_protect(p) \ ((p)->outer = InnermostProt, \ InnermostProt = (p), \ setjmp((p)->label)) #define remove_protection(p) (InnermostProt = (p)->outer) /* * unwind compares each stack frame with InnermostProt * to see if there is a protection frame there, and if so * jumps to it. unwind_resume can be optimised if necessary * by noting when protections belong to the same frame. */ There remains the problem that the object may be in any of three `improper' states (not constructed, only partially constructed, or partially destroyed) at the time the exception causes the stack to be unwound past the protection markers. There appear to be only two cures: either defer the unwinding (and hence the entire exception process) until the object is stable, or ensure that the con- and de-struction processes use only atomic operations. The former approach fails in the general case (exceptions during -struction either oscillate or fail utterly); the latter is just plain hard, even with help from the programmer. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris