Allocator Protocol¶
The allocator protocol is the central abstraction of PyCyBase — a
pluggable memory allocation layer that dispatches to heap, shared-memory
(SHM), or raw malloc backends based on global environment settings.
Module: cbase.allocator_protocol
- class cbase.allocator_protocol.MemoryBlock¶
Bases:
objectCommon base for heap/SHM block handles.
Tracks ownership of the underlying C buffer and holds a strong reference to the owning allocator (
__allocator__), so an allocator can never be torn down while its blocks are still alive (e.g. interpreter-exit teardown order). Subclasses override_free_blockto release the C buffer.
- class cbase.allocator_protocol.HeapAllocator¶
Bases:
object- __init__(*args, **kwargs)¶
- active_page¶
- Type:
- allocated(self)¶
- Return type:
- calloc(self, size_t size, bool with_lock=True) HeapMemoryBlock¶
- Parameters:
- Return type:
- extend(self, size_t capacity=0, bool with_lock=True) HeapMemoryPage¶
- Parameters:
- Return type:
- free(self, HeapMemoryBlock buffer, bool with_lock=True) void¶
- Parameters:
buffer (
HeapMemoryBlock)with_lock (
bool)
- Return type:
- free_list(self)¶
- Return type:
- pages(self)¶
- Return type:
- request(self, size_t size, bool with_lock=True, bool scan_all_pages=True) HeapMemoryBlock¶
- Parameters:
- Return type:
- class cbase.allocator_protocol.HeapMemoryBlock¶
Bases:
MemoryBlock- buffer¶
- Type:
- next_allocated¶
- Type:
- next_free¶
- Type:
- parent_page¶
- Type:
- class cbase.allocator_protocol.HeapMemoryPage¶
Bases:
object- allocated(self)¶
- Return type:
- allocator¶
- Type:
- classmethod from_buffer(buffer_addr)¶
- Return type:
Bases:
object- Type:
- Return type:
- Type:
- Type:
- Type:
- Parameters:
- Return type:
- Parameters:
- Return type:
- Parameters:
buffer (
SharedMemoryBlock)with_lock (
bool)
- Return type:
- Return type:
- Type:
- Type:
str
- Type:
- Return type:
- Type:
- Type:
- Type:
- Parameters:
- Return type:
str
Bases:
MemoryBlock- Type:
- Type:
- Type:
- Type:
- Type:
Bases:
object- Return type:
size_t
- Return type:
str
- Type:
size_t
- Return type:
- cbase.allocator_protocol.shm_cleanup()¶
cleanup()
- class cbase.allocator_protocol.AllocatorConfigContext(dict overrides=None, **kwargs)¶
Bases:
EnvConfigContext- Parameters:
kwargs (
Any)
Configuration Contexts¶
The AllocatorConfigContext extends EnvConfigContext to
dispatch configuration changes to the underlying heap and shared-memory
allocators. Accepted keyword arguments:
Key |
Type |
Description |
|---|---|---|
|
|
Enable/disable mutex locking for thread safety |
|
|
Enable/disable shared memory allocation |
|
|
Enable/disable free-list reuse |
|
|
Propagated to both heap and SHM allocators |
|
|
Propagated to both heap and SHM allocators |
|
|
Propagated to both heap and SHM allocators |
Sentinel Contexts¶
The module exports pre-configured context instances for common use:
from cbase.allocator_protocol import AP_SHARED, AP_LOCKED, AP_LOCKFREE, AP_FREELIST
AP_SHARED— enable shared-memory allocationAP_LOCKED— enable thread-safety lockingAP_LOCKFREE— disable thread-safety lockingAP_FREELIST— enable free-list reuse (no effect in SHM mode)
Usage Examples¶
Basic allocation with the default allocator (SHM-backed):
from cbase.allocator_protocol import AllocatorProtocol, AP_SHARED
with AP_SHARED:
alloc = AllocatorProtocol(1024)
alloc.buf[0] = b'x'
print(f"size={alloc.size}, with_shm={alloc.with_shm}")
Composing contexts with |:
from cbase.allocator_protocol import AP_SHARED, AP_LOCKED, AP_FREELIST
# SHM + thread-safe + freelist
ctx = AP_SHARED | AP_LOCKED | AP_FREELIST
with ctx:
alloc = AllocatorProtocol(4096)
# ... use alloc ...
Inverting a context with ~ to disable a flag:
unlocked = ~AP_LOCKED # disables locking
Using as a decorator:
@AP_SHARED
def allocate_shared():
return AllocatorProtocol(2048)
Under the Hood¶
The module maintains three global allocator schematics (C structs) that define the default, SHM-only, and heap-only configurations:
AP_DEFAULT_ALLOCATOR— SHM-backed, locked, freelist-enabledAP_SHM_ALLOCATOR— SHM-backed, locked, freelist-enabled, no heapAP_HEAP_ALLOCATOR— heap-backed, locked, freelist-enabled, no SHM
AllocatorProtocol(size) checks AP_DEFAULT_ALLOCATOR.with_shm at
construction time and selects the appropriate backend.
See also
Heap Allocator — In-process heap allocator details
Shared-Memory Allocator — Shared-memory allocator details
Environment Config Context — Base
EnvConfigContextclass