P99
|
◆ P99_UNWIND_PROTECT
Unwind execution from several levels of nesting inside a function. This macro allows several levels of block statements to be unwound safely. // do something
while (cond0) {
for (;cond1;) {
if (cond2) P99_UNWIND(-1);
}
}
// do some cleanup here
// if everything went well ::p99_unwind_code has value 0 otherwise it
// receives a value from P99_UNWIND
}
Here P99_UNWIND terminates the execution of the inner blocks and resumes execution at the special "label" P99_PROTECT, if present. The argument to P99_UNWIND controls how many levels are unwound. If it is positive, at most the specified number of levels is unwound, but never more levels than there are on the call stack of the enclosing function. If it is negative, all levels on the stack of the enclosing function are unwound and during unwinding, this negative argument (that was passed to P99_UNWIND) is accessible through p99_unwind_code.
Let us see this at an example. The natural way with P99_UNWIND_PROTECT to check for a valid return of unsigned char*volatile buffer = 0;
buffer = malloc(bignumber);
if (!buffer || IamSick) P99_UNWIND 1;
// do something complicated with buffer
free(buffer);
}
If we would not declare
Definition at line 612 of file p99_block.h. |