Memory Handling
Memory Notifier
It is difficult and error-prone for programmers to always check the result of every API call that can possibly fail due to memory allocation errors. Calls such as strdup and asprintf are often assumed to succeed, but they can, and do fail when memory is depleted.
A better approach is to globally detect and handle memory allocation errors in one place. GoAhead handles memory allocation errors globally. When a memory allocation fails, a memory notifier is invoked. The application can then determine what action to take. Typically, GoAhead will restart in such circumstances. The websSetMemNotifier call is used to define your own custom memory notifier.
Alternate Allocator
GoAhead provides an alternate application-specific memory allocator to use instead of malloc. This allocator is useful when the native operating system malloc suffers from excessive fragmentation. The GoAhead allocator is deterministic and allocates and frees memory in constant time O(1).
The allocator is optimized for frequent allocations of small blocks (< 4K). It uses a scheme of buddy free queues for fast allocation. Memory allocations are aligned on 16 byte boundaries on 64-bit systems and otherwise on 8 byte boundaries. It will not return chunks of unused memory back to the O/S and does not otherwise coalesce memory.
Enabling the Allocator
The allocator is not enabled by default as most operating systems now have better malloc implementations. To enable, rebuild with:
ME_GOAHEAD_REPLACE_MALLOC=1 make