Skip to content

Scope

Description

Open a fresh-allocator scope. Constructs TWO independent AllocType instances on the stack:

  • the user-visible allocator, exposed as name, for the

caller to pass deliberately to helpers and to use through ScopeWith(name) { ... } blocks when they want allocations to land in the user pool.

  • the internal allocator, aliased as MisraScope, used by

all tier-1 library macros (VecInit, StrInitFromCstr, …).

Library scratch allocations and the user’s named-pool allocations therefore never share a backing pool by default. Both instances are destroyed together when the block exits, so anything allocated through either pool is invalid memory after the block.

Usage example (from documentation)

  Scope(lifetimeA, DefaultAllocator) {
      Vec(int) v = VecInit();          // INTERNAL pool (via MisraScope)
      my_helper(&v, lifetimeA);        // helper receives USER pool pointer
  }

CONTROL FLOW: normal fall-through, `break` (or `ExitScope`), and
`continue` at the scope's top level all run the auto-deinit cleanly.
`return` and `goto` out of the scope skip deinit and leak both
allocators - a C-level limitation that has no portable workaround.

Success

Body runs once with name and MisraScope bound to the user-visible and internal allocators respectively; both allocators are deinitialized on block exit.

Failure

Macro cannot fail; the two AllocType##Init calls inherit their own failure behaviour (typically abort-on-OOM for heap-backed allocators).

Usage example (Cross-references)

Usage examples (Cross-references)
    
    int main(int argc, char **argv) {
        Scope(alloc, DefaultAllocator) {
            Zstr hostname = NULL;
    
    static void log_request_summary(Zstr client_addr, Zstr prefix_bytes, size prefix_len) {
        Scope(scope, DefaultAllocator) {
            Str raw = StrInit(scope);
            StrPushBackMany(&raw, prefix_bytes);
        install_signal_handlers();
    
        Scope(alloc, DefaultAllocator) {
            Zstr listen_spec   = NULL;
            Zstr upstream_spec = NULL;
Last updated on