Definition
Prepare before irreversible commit is the rule that every step of an update that can fail, such as reserving memory or building data structures, is completed before the commit: the write, surviving loss of power, after which the system is bound to the new version. Allocation-free fallback is a way back to a working state that never requests memory while it runs, so it cannot fail for lack of memory. Preparation removes failures after the commit; an allocation-free fallback keeps recovery from any remaining failure from failing itself.
Where it fits
Main entry: Staging versus commit versus activation (§8.22).
The Staging versus commit versus activation entry explains the three steps of an update. The failure this rule prevents runs as follows. A candidate, the new version, is committed, and then the request for memory for its run-time object fails. Storage now says that the new version is in force, but the running system cannot use it. If the old version’s object was freed to make room, the system has nothing to fall back to either.
Preparing first gives this order.
Reserve memory for the new object and build it while the old object stays in use.
Check . If step 1 or step 2 fails, release ; is untouched and nothing has been committed.
Commit: write the commit record §8.21, the durable record that names the version in force.
Switch: replace the pointer to with a pointer to in one atomic write, so that every user sees one pointer or the other.
Release only when no reader and no recovery path can still use it.
Until step 5, is the fallback and already exists, so going back needs no allocation. Going back after step 3 also means making storage name the old version again, by the design’s recovery rule.
Preparing first has a price: for a time both objects exist. If needs 50 bytes and needs 60 bytes, the overlap needs bytes, plus the bookkeeping that the memory allocator adds to each block. A 100-byte pool, a region of memory set aside for these objects, cannot hold both. Freeing before building makes room but destroys the fallback, and reordering the source code does not change this arithmetic. The choices are to provide at least 110 bytes plus bookkeeping, for example as two fixed areas reserved when the program is built, or to adopt a different protocol with a stated fallback: free , and if building fails before the commit, restart and rebuild from storage. On a heap, the area from which blocks of any size are allocated, even 110 free bytes may not suffice: heap fragmentation §4.9, free space split into separate pieces, can leave no single piece of 60 bytes.
Allocation-free means that no step on the fallback path asks for memory, directly or indirectly. Checking that one top-level function never calls malloc, the C library function that allocates memory from the heap, is not enough. The whole chain of calls on the fallback path counts: logging functions that format text into newly allocated buffers, error handlers, library routines, callbacks §14.22 (functions handed to other code to be called later), and work deferred to run later on another thread. Each must use memory that already exists. Ownership and object lifetime §4.1, the span from an object’s creation to its release, must also be fixed in advance: one named part of the program owns , meaning that it alone may release , and it releases only after every reader that may still hold a pointer to has finished. Those readers include interrupt handlers §6.12, code that a hardware event starts at any moment, and deferred work.
Limits and common mistakes
Preparation removes the allocation failure after the commit, not every failure. The commit write, the switch, other writes to storage, the release of and effects outside the device, such as a message already sent, can each still fail or be cut off by loss of power, and each needs its own rule.
The prepared object must belong to the committed version. If was built for one candidate and the commit names another, the switch installs the wrong object. Record the candidate’s identity, such as its version number and a cryptographic hash §5.1 of its bytes (a short fingerprint that changes if any byte changes), with , and compare it at the switch.
A fallback held in memory does not survive a restart. After a crash, recovery starts from storage, so the stored records must allow recovery on their own, for example by keeping the last-known-good state §8.25, the last version known to work, until the new version is confirmed.
The memory budget is set by peak memory §11.37, the largest amount in use at any one moment, which here falls during the overlap.
Questions answered
What does allocation-free fallback require?
Allocation-free fallback requires that everything the way back to the old version will use already exists before the irreversible commit, the durable write that binds the system to the new version, so that going back never requests memory. Five things are needed.
The old object stays allocated and usable until no reader and no recovery path can need it, so the new object is built beside it, not in its place.
The memory budget covers that overlap: an old object of 50 bytes and a new one of 60 need bytes plus allocator bookkeeping, so a 100-byte pool cannot provide this fallback.
No function on the fallback path allocates, directly or indirectly, including logging, error handling, library routines, callbacks and deferred work; checking one top-level function for calls to
malloc, the C library’s allocation function, is not enough.One named part of the program owns the old object and releases it only after every holder of a pointer to it has finished.
The prepared object is tied to the exact version that was committed.
This removes only the risk of running out of memory on the way back; writes to storage, the pointer switch and effects outside the device keep their own failure cases.
Related entries: Peak versus retained memory (§11.37), Ownership (§4.2), Recovery fallback (§8.29), Heap fragmentation (§4.9).