|
CX Framework
Cross-platform C utility framework
|
Macros | |
| #define | pblock |
| #define | pblockUnwind(num) _pblockUnwind((void*)_pblock_unwind_top, (num)) |
| #define | PBLOCK_AFTER |
Advanced Feature: Protected blocks with multi-level stack unwinding capabilities.
Protected blocks provide stack unwinding through setjmp/longjmp, allowing early exit from nested blocks with guaranteed cleanup. They are the foundation for the exception handling system but can be used independently.
Performance Warning:
Protected blocks are heavyweight operations with significant costs:
When to Use:
When NOT to Use:
For most resource management needs, use blkWrap() from Block Wrapping instead.
Example:
| #define pblock |
pblock { }
Advanced Feature: Declares a protected block with stack unwinding capability.
A protected block sets up a setjmp buffer for stack unwinding that can be jumped to using pblockUnwind(). Protected blocks can be nested, and an arbitrary number of layers can be unwound in a single operation.
Cleanup with PBLOCK_AFTER:
A protected block can optionally end with a PBLOCK_AFTER: pseudo-label. Code after this label executes when exiting the block normally OR when unwinding through it, providing guaranteed cleanup similar to a finally clause.
Performance Implications:
return statements are not allowed within protected blocks (compile error in debug builds) Example:
| #define PBLOCK_AFTER |
PBLOCK_AFTER:
Pseudo-label marking the cleanup section of a protected block.
Code after this label is guaranteed to execute before control flow leaves the protected block, similar to a finally clause in exception handling. This occurs both during normal exit and during unwinding.
The cleanup code runs:
Example:
| #define pblockUnwind | ( | num | ) | _pblockUnwind((void*)_pblock_unwind_top, (num)) |
Exits the current protected block early via stack unwinding.
Triggers unwinding of one or more nested protected blocks, jumping to the appropriate PBLOCK_AFTER cleanup section if one exists, then continuing to outer blocks if needed.
| num | Number of nested blocks to exit:
|
Example: