Overview
Proone implements custom memory allocation functions (prefixed withprne_) to ensure consistent behavior across platforms and facilitate future debugging capabilities. All memory allocated using framework functions must be freed with prne_free().
Zero-Length Allocation Policy
POSIX-compliant memory functions have inconsistent behavior for zero-length allocations - they may return either a valid pointer or NULL. Proone does not tolerate this inconsistency as it must handle memory allocation failures gracefully.The Problem
Consider this problematic code:calloc() returns NULL or a valid pointer for zero-length allocation.
The Solution
Proone framework allocation functions always return NULL for zero-length allocation. This forces implementations to properly check both the parameter and return value:Core Allocation Functions
prne_malloc()
Safemalloc() with overflow checking and consistent zero-length behavior.
se- Size of each elementcnt- Number of elements
NULLif calculated size is zero (errno untouched)NULLon allocation failure or integer overflow (errno = ENOMEM)- Pointer to allocated memory on success
prne_realloc()
Reallocate memory with consistent zero-length behavior.ptr- Pointer to previously allocated memoryse- Size of each elementcnt- Number of elements
NULLif calculated size is zero (frees ptr, errno untouched)NULLon allocation failure or integer overflow (errno = ENOMEM)- Pointer to reallocated memory on success
realloc(), calling with zero size has the same effect as free().
Implementation: (util_rt.c:105-124)
prne_calloc()
Allocate zeroed memory with consistent behavior.se- Size of each elementcnt- Number of elements
NULLif either parameter is zero (errno untouched)NULLon allocation failure (errno = ENOMEM)- Pointer to zeroed memory on success
prne_free()
Free memory allocated by framework functions.- Provides a hook for future resource debugging (similar to MSVC macros)
- Maintains consistency with framework allocation functions
- Currently wraps standard
free()but allows for future enhancements
String Allocation Functions
Convenience functions for string memory management:prne_alloc_str()
len + 1 bytes for a string (includes null terminator).
prne_realloc_str()
prne_realloc(), zero length does not free memory (allocates 1 byte for null terminator).
prne_dup_str() / prne_redup_str()
prne_sfree_str()
Design Rationale
The framework allocation functions serve three purposes:- Consistent zero-length behavior - Always return NULL for zero-length allocations across all platforms
- Overflow protection - Built-in integer overflow checks (especially important for 16-bit machines)
- Future debugging support - Provides hooks for implementing memory allocation event systems when Valgrind becomes too cumbersome
Resource Allocation Hook
Bothprne_free() and prne_close() are designed to facilitate framework-level resource debugging in the future. This may be useful when Valgrind becomes too cumbersome. Additionally, these hooks can maintain a registry of file descriptors for use in prefork() and atfork() equivalents.
Best Practices
- Always use framework functions - Use
prne_malloc(),prne_calloc(),prne_realloc()instead of standard C functions - Check both conditions - When checking allocation success:
if (n > 0 && ret == NULL) - Match allocation/deallocation - Memory allocated with
prne_*()must be freed withprne_free() - Use string helpers - Prefer
prne_alloc_str()over manualmalloc(strlen(str)+1) - Secure sensitive data - Use
prne_sfree_str()for strings containing credentials or keys
See Also
- Resource Allocation - Transparent structures, opaque types, and ownership patterns
- Developer Notes - Additional implementation notes and debugging tips
