memory-allocators-arenas-c
Design, implement, and verify high-performance custom memory allocators in C and C++. Build Arena / Bump allocators, Pool / Fixed-size block allocators, and Free-List allocators. Ensure strict hardware memory alignment (alignof, power-of-two boundaries), zero-fragmentation lifecycles, and cache line locality. Trigger when building game engines, high-frequency trading (HFT) engines, or embedded runtime systems.
- License 许可
- MIT
- 最近更新
- 2026-10-17
- Stars
- ★ 8
- Forks
- 1
01 前言 Front Matter
- name
- memory-allocators-arenas-c
- metadata
- { "category": "Low-Level Systems Drivers and Kernel" }
- description
- Design, implement, and verify high-performance custom memory allocators in C and C++. Build Arena / Bump allocators, Pool / Fixed-size block allocators, and Free-List allocators. Ensure strict hardware memory alignment (alignof, power-of-two boundaries), zero-fragmentation lifecycles, and cache line locality. Trigger when building game engines, high-frequency trading (HFT) engines, or embedded runtime systems.
- compatibility
- C99 / C11, C++17, POSIX / Windows memory primitives
03 SKILL.md 正文
Custom Memory Allocators & Arenas Skill Guide
This skill specifies algorithms, pointer arithmetic patterns, and hardware alignment requirements for implementing zero-overhead custom memory allocators in C and C++.
1. Allocator Topologies & Trade-offs
+-----------------------+-----------------------+-----------------------+
| Allocator Type | Allocation Time | Deallocation Strategy |
+-----------------------+-----------------------+-----------------------+
| Arena (Bump) | O(1) pointer addition | O(1) Reset all at once|
| Pool (Fixed-Size) | O(1) pop free-list | O(1) push free-list |
| Free-List (Segregated)| O(N) or O(log N) | Coalesce adjacent free|
+-----------------------+-----------------------+-----------------------+
2. Production C Implementation: Arena & Pool Allocators
A. High-Performance Hardware-Aligned Arena Allocator
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#define DEFAULT_ALIGNMENT (sizeof(void*))
typedef struct {
uint8_t *buffer;
size_t capacity;
size_t offset;
size_t prev_offset;
} MemoryArena;
static inline uintptr_t align_forward(uintptr_t ptr, size_t alignment) {
assert((alignment & (alignment - 1)) == 0 && "Alignment must be power of two");
uintptr_t a = (uintptr_t)alignment;
return (ptr + a - 1) & ~(a - 1);
}
void arena_init(MemoryArena *arena, void *backing_buffer, size_t capacity) {
arena->buffer = (uint8_t*)backing_buffer;
arena->capacity = capacity;
arena->offset = 0;
arena->prev_offset = 0;
}
void *arena_alloc_aligned(MemoryArena *arena, size_t size, size_t alignment) {
uintptr_t current_ptr = (uintptr_t)arena->buffer + (uintptr_t)arena->offset;
uintptr_t aligned_ptr = align_forward(current_ptr, alignment);
size_t padding = aligned_ptr - current_ptr;
if (arena->offset + padding + size > arena->capacity) {
return NULL; // Out of memory
}
arena->prev_offset = arena->offset;
arena->offset += padding + size;
return (void*)aligned_ptr;
}
void *arena_alloc(MemoryArena *arena, size_t size) {
return arena_alloc_aligned(arena, size, DEFAULT_ALIGNMENT);
}
void arena_reset(MemoryArena *arena) {
// Instant O(1) deallocation of all allocated memory
arena->offset = 0;
arena->prev_offset = 0;
}
B. Fixed-Size Block Pool Allocator
typedef struct PoolFreeNode {
struct PoolFreeNode *next;
} PoolFreeNode;
typedef struct {
uint8_t *buffer;
size_t block_size;
size_t capacity;
PoolFreeNode *free_list;
} MemoryPool;
void pool_init(MemoryPool *pool, void *backing_buffer, size_t total_size, size_t block_size) {
assert(block_size >= sizeof(PoolFreeNode) && "Block size must fit free node pointer");
pool->buffer = (uint8_t*)backing_buffer;
pool->block_size = block_size;
pool->capacity = total_size;
pool->free_list = NULL;
// Chain all blocks into the free list
size_t num_blocks = total_size / block_size;
for (size_t i = 0; i < num_blocks; ++i) {
PoolFreeNode *node = (PoolFreeNode*)(pool->buffer + (i * block_size));
node->next = pool->free_list;
pool->free_list = node;
}
}
void *pool_alloc(MemoryPool *pool) {
if (!pool->free_list) {
return NULL; // Pool exhausted
}
PoolFreeNode *node = pool->free_list;
pool->free_list = node->next;
return (void*)node;
}
void pool_free(MemoryPool *pool, void *ptr) {
if (!ptr) return;
PoolFreeNode *node = (PoolFreeNode*)ptr;
node->next = pool->free_list;
pool->free_list = node;
}
3. Best Practices & Hardware Guidelines
- Power-of-Two Alignment: Always enforce that alignments are powers of two (
(align & (align - 1)) == 0). - Cache Line Locality: For high-throughput structs, align to 64 bytes (
alignas(64)) to avoid false sharing across CPU cores. - Double Free Elimination: Arena allocators completely eliminate individual double-free and memory fragmentation bugs because memory is only reclaimed en masse.
04 同仓库更多 Skills
security-audit
Comprehensive security guidance, source code audit, vulnerability assessment, and penetration test review for codebases, APIs, microservices, CLI tools, libraries, and LLM-backed applications. Triggers on requests to audit a codebase, conduct security reviews, find vulnerabilities or flaws, pen-test source code, or when the user asks in Arabic (فحص أمني، تدقيق الكود أمنياً، مراجعة الثغرات، فحص أمان المشروع). Supports Guidance mode (focused review) and Full Audit mode (deterministic 6-phase harness).
malware-analysis-reversing
Production malware analysis workflows, reverse engineering methodologies, isolated sandbox triage, static/dynamic binary analysis, memory forensics, and YARA detection rule authoring.
application-security-pentesting
Production Application Security (AppSec) standards, OWASP Top 10 vulnerability remediation, SAST/DAST pipeline integration, secure code review, threat modeling, and defensive security posture.
oauth2-oidc-security-flows
Implement enterprise OAuth 2.1 and OpenID Connect (OIDC) authorization code flow with Proof Key for Code Exchange (PKCE), state and nonce verification, refresh token rotation, token revocation, and centralized IdP federation (Keycloak, Auth0, Okta). Trigger when building secure authentication, single sign-on (SSO), or third-party delegated authorization.
jwt-session-management-hardening
Harden JWT-based authentication and session lifecycles using asymmetric signing (RS256/EdDSA), JWKS rotation, strict claims validation, Redis-backed sliding window sessions, revocation lists, and secure HttpOnly cookie storage. Trigger when implementing token validation, session revocation, or hardening API authentication.
soc2-compliance-automation
Best practices for implementing SOC 2 Type II compliance controls, continuous evidence collection, infrastructure policy-as-code, and automated security monitoring. Use when automating Trust Services Criteria (Security, Availability, Confidentiality, Processing Integrity, Privacy), audit logging, IAM least privilege, or CI/CD security gates.