PyCyBase 0.1.8.post4__cp313-cp313-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cbase/__infra__.pxd +6 -0
- cbase/__init__.pxd +6 -0
- cbase/__init__.py +28 -0
- cbase/allocator_protocol/__infra__.pxd +58 -0
- cbase/allocator_protocol/__init__.pxd +58 -0
- cbase/allocator_protocol/__init__.py +45 -0
- cbase/allocator_protocol/c_allocator_protocol.c +31818 -0
- cbase/allocator_protocol/c_allocator_protocol.cp313-win_amd64.pyd +0 -0
- cbase/allocator_protocol/c_allocator_protocol.h +352 -0
- cbase/allocator_protocol/c_allocator_protocol.pxd +64 -0
- cbase/allocator_protocol/c_allocator_protocol.pyi +144 -0
- cbase/allocator_protocol/c_allocator_protocol.pyx +209 -0
- cbase/allocator_protocol/c_heap_allocator.c +38144 -0
- cbase/allocator_protocol/c_heap_allocator.cp313-win_amd64.pyd +0 -0
- cbase/allocator_protocol/c_heap_allocator.h +444 -0
- cbase/allocator_protocol/c_heap_allocator.pxd +97 -0
- cbase/allocator_protocol/c_heap_allocator.pyi +161 -0
- cbase/allocator_protocol/c_heap_allocator.pyx +300 -0
- cbase/allocator_protocol/c_nt_shm_allocator.c +33399 -0
- cbase/allocator_protocol/c_nt_shm_allocator.cp313-win_amd64.pyd +0 -0
- cbase/allocator_protocol/c_nt_shm_allocator.h +996 -0
- cbase/allocator_protocol/c_nt_shm_allocator.pxd +102 -0
- cbase/allocator_protocol/c_nt_shm_allocator.pyx +223 -0
- cbase/allocator_protocol/c_shm_allocator.h +1168 -0
- cbase/allocator_protocol/c_shm_allocator.pxd +142 -0
- cbase/allocator_protocol/c_shm_allocator.pyi +299 -0
- cbase/allocator_protocol/c_shm_allocator.pyx +442 -0
- cbase/allocator_protocol/c_shm_comp.c +4643 -0
- cbase/allocator_protocol/c_shm_comp.cp313-win_amd64.pyd +0 -0
- cbase/allocator_protocol/c_shm_comp.h +49 -0
- cbase/allocator_protocol/c_shm_comp.pxd +38 -0
- cbase/allocator_protocol/c_shm_comp.pyx +1 -0
- cbase/backports/__infra__.pxd +17 -0
- cbase/backports/__init__.pxd +17 -0
- cbase/backports/__init__.py +9 -0
- cbase/backports/pydict.h +55 -0
- cbase/backports/pydict.pxd +5 -0
- cbase/backports/pylong.c +9946 -0
- cbase/backports/pylong.cp313-win_amd64.pyd +0 -0
- cbase/backports/pylong.h +79 -0
- cbase/backports/pylong.pxd +23 -0
- cbase/backports/pylong.pyx +61 -0
- cbase/bytemap/__infra__.pxd +42 -0
- cbase/bytemap/__init__.pxd +42 -0
- cbase/bytemap/__init__.py +21 -0
- cbase/bytemap/c_bytemap.c +52076 -0
- cbase/bytemap/c_bytemap.cp313-win_amd64.pyd +0 -0
- cbase/bytemap/c_bytemap.h +867 -0
- cbase/bytemap/c_bytemap.pxd +370 -0
- cbase/bytemap/c_bytemap.pyx +1758 -0
- cbase/bytemap/xxh3.h +55 -0
- cbase/bytemap/xxhash.c +42 -0
- cbase/bytemap/xxhash.h +7487 -0
- cbase/env.c +11109 -0
- cbase/env.cp313-win_amd64.pyd +0 -0
- cbase/env.pxd +8 -0
- cbase/env.pyi +48 -0
- cbase/env.pyx +46 -0
- cbase/includes/cbase/allocator_protocol/c_allocator_protocol.c +31818 -0
- cbase/includes/cbase/allocator_protocol/c_allocator_protocol.h +352 -0
- cbase/includes/cbase/allocator_protocol/c_heap_allocator.c +38144 -0
- cbase/includes/cbase/allocator_protocol/c_heap_allocator.h +444 -0
- cbase/includes/cbase/allocator_protocol/c_nt_shm_allocator.c +33399 -0
- cbase/includes/cbase/allocator_protocol/c_nt_shm_allocator.h +996 -0
- cbase/includes/cbase/allocator_protocol/c_shm_allocator.h +1168 -0
- cbase/includes/cbase/allocator_protocol/c_shm_comp.c +4643 -0
- cbase/includes/cbase/allocator_protocol/c_shm_comp.h +49 -0
- cbase/includes/cbase/backports/pydict.h +55 -0
- cbase/includes/cbase/backports/pylong.c +9946 -0
- cbase/includes/cbase/backports/pylong.h +79 -0
- cbase/includes/cbase/bytemap/c_bytemap.c +52076 -0
- cbase/includes/cbase/bytemap/c_bytemap.h +867 -0
- cbase/includes/cbase/bytemap/xxh3.h +55 -0
- cbase/includes/cbase/bytemap/xxhash.c +42 -0
- cbase/includes/cbase/bytemap/xxhash.h +7487 -0
- cbase/includes/cbase/env.c +11109 -0
- cbase/includes/cbase/int128.h +81 -0
- cbase/includes/cbase/intern_string/c_intern_string.c +28081 -0
- cbase/includes/cbase/intern_string/c_intern_string.h +614 -0
- cbase/includes/cbase/nt/pthread_nt_compat.h +102 -0
- cbase/includes/cbase/nt/regex_nt_compat.h +133 -0
- cbase/includes/cbase/nt/time_sched_nt_compat.h +32 -0
- cbase/int128.h +81 -0
- cbase/int128.pxd +15 -0
- cbase/intern_string/BENCHMARK.md +157 -0
- cbase/intern_string/__infra__.pxd +11 -0
- cbase/intern_string/__init__.pxd +11 -0
- cbase/intern_string/__init__.py +19 -0
- cbase/intern_string/c_intern_string.c +28081 -0
- cbase/intern_string/c_intern_string.cp313-win_amd64.pyd +0 -0
- cbase/intern_string/c_intern_string.h +614 -0
- cbase/intern_string/c_intern_string.pxd +118 -0
- cbase/intern_string/c_intern_string.pyi +376 -0
- cbase/intern_string/c_intern_string.pyx +765 -0
- cbase/nt/pthread_nt_compat.h +102 -0
- cbase/nt/regex_nt_compat.h +133 -0
- cbase/nt/time_sched_nt_compat.h +32 -0
- pycybase-0.1.8.post4.dist-info/METADATA +180 -0
- pycybase-0.1.8.post4.dist-info/RECORD +101 -0
- pycybase-0.1.8.post4.dist-info/WHEEL +5 -0
- pycybase-0.1.8.post4.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
from libc.stdint cimport uintptr_t
|
|
2
|
+
from posix.unistd cimport pid_t
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
cdef extern from "<pthread.h>":
|
|
6
|
+
ctypedef struct pthread_mutex_t:
|
|
7
|
+
pass # Opaque type, details handled by C
|
|
8
|
+
|
|
9
|
+
int pthread_mutex_init(pthread_mutex_t* mutex, void* attr) noexcept nogil
|
|
10
|
+
int pthread_mutex_lock(pthread_mutex_t* mutex) noexcept nogil
|
|
11
|
+
int pthread_mutex_unlock(pthread_mutex_t* mutex) noexcept nogil
|
|
12
|
+
int pthread_mutex_destroy(pthread_mutex_t* mutex) noexcept nogil
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
cdef extern from "cbase/allocator_protocol/c_shm_allocator.h":
|
|
16
|
+
const size_t AP_SHM_AUTOPAGE_CAPACITY
|
|
17
|
+
const size_t AP_SHM_AUTOPAGE_CAPACITY_MAX
|
|
18
|
+
const size_t AP_SHM_AUTOPAGE_ALIGNMENT
|
|
19
|
+
const char* AP_SHM_ALLOCATOR_PREFIX
|
|
20
|
+
const size_t AP_SHM_NAME_LEN
|
|
21
|
+
const size_t AP_SHM_PREFIX_MAX
|
|
22
|
+
const size_t AP_SHM_ALLOCATOR_DEFAULT_REGION_SIZE
|
|
23
|
+
const size_t c_shm_page_overhead
|
|
24
|
+
const size_t c_shm_block_overhead
|
|
25
|
+
|
|
26
|
+
ctypedef struct shm_page:
|
|
27
|
+
size_t capacity
|
|
28
|
+
size_t occupied
|
|
29
|
+
size_t offset
|
|
30
|
+
shm_allocator* allocator;
|
|
31
|
+
shm_memory_block* allocated
|
|
32
|
+
char shm_name[AP_SHM_NAME_LEN]
|
|
33
|
+
char prev_name[AP_SHM_NAME_LEN]
|
|
34
|
+
|
|
35
|
+
ctypedef struct shm_page_ctx:
|
|
36
|
+
shm_page* shm_page
|
|
37
|
+
int shm_fd
|
|
38
|
+
char* buffer
|
|
39
|
+
shm_page_ctx* prev
|
|
40
|
+
|
|
41
|
+
ctypedef struct shm_memory_block:
|
|
42
|
+
size_t capacity
|
|
43
|
+
size_t size
|
|
44
|
+
shm_memory_block* next_free
|
|
45
|
+
shm_memory_block* next_allocated
|
|
46
|
+
shm_page* parent_page
|
|
47
|
+
char buffer[]
|
|
48
|
+
|
|
49
|
+
ctypedef struct shm_allocator:
|
|
50
|
+
char shm_name[AP_SHM_NAME_LEN]
|
|
51
|
+
size_t pid
|
|
52
|
+
pthread_mutex_t lock
|
|
53
|
+
uintptr_t region
|
|
54
|
+
size_t region_size
|
|
55
|
+
size_t mapped_size
|
|
56
|
+
char active_page[AP_SHM_NAME_LEN]
|
|
57
|
+
size_t mapped_pages
|
|
58
|
+
shm_memory_block* free_list
|
|
59
|
+
size_t autopage_capacity
|
|
60
|
+
size_t autopage_capacity_max
|
|
61
|
+
size_t autopage_alignment
|
|
62
|
+
char shm_prefix[AP_SHM_PREFIX_MAX]
|
|
63
|
+
|
|
64
|
+
ctypedef struct shm_allocator_ctx:
|
|
65
|
+
shm_allocator* shm_allocator
|
|
66
|
+
int shm_fd
|
|
67
|
+
shm_page_ctx* active_page
|
|
68
|
+
|
|
69
|
+
size_t c_shm_page_roundup(shm_allocator* allocator, size_t size) noexcept nogil
|
|
70
|
+
size_t c_shm_block_roundup(size_t size) noexcept nogil
|
|
71
|
+
void c_shm_allocator_name(const void* region, const char* shm_prefix, char* out) noexcept nogil
|
|
72
|
+
void c_shm_page_name(shm_allocator* allocator, char* out) noexcept nogil
|
|
73
|
+
int c_shm_scan(const char* prefix, char* out) noexcept nogil
|
|
74
|
+
shm_page_ctx* c_shm_page_new(size_t page_capacity) noexcept nogil
|
|
75
|
+
int c_shm_page_map(shm_allocator* allocator, shm_page_ctx* page_ctx) noexcept nogil
|
|
76
|
+
void c_shm_page_reclaim(shm_allocator* allocator, shm_page_ctx* page_ctx) noexcept nogil
|
|
77
|
+
|
|
78
|
+
shm_page_ctx* c_shm_allocator_extend(shm_allocator_ctx* ctx, size_t capacity, pthread_mutex_t* lock) noexcept nogil
|
|
79
|
+
shm_allocator_ctx* c_shm_allocator_new(size_t region_size, const char* shm_prefix) noexcept nogil
|
|
80
|
+
void c_shm_allocator_free(shm_allocator_ctx* ctx) noexcept nogil
|
|
81
|
+
void* c_shm_calloc(shm_allocator_ctx* ctx, size_t size, pthread_mutex_t* lock) noexcept nogil
|
|
82
|
+
void* c_shm_request(shm_allocator_ctx* ctx, size_t size, int scan_all_pages, pthread_mutex_t* lock) noexcept nogil
|
|
83
|
+
void c_shm_free(void* ptr, pthread_mutex_t* lock) noexcept nogil
|
|
84
|
+
void c_shm_reclaim(shm_allocator_ctx* ctx, pthread_mutex_t* lock) noexcept nogil
|
|
85
|
+
int c_shm_scan_allocator(const char* shm_prefix, char* out) noexcept nogil
|
|
86
|
+
int c_shm_scan_page(const char* shm_prefix, char* out) noexcept nogil
|
|
87
|
+
pid_t c_shm_pid(const char* shm_name) noexcept nogil
|
|
88
|
+
shm_allocator* c_shm_allocator_dangling(const char* shm_prefix, char* shm_name) noexcept nogil
|
|
89
|
+
void c_shm_clear_dangling(const char* shm_prefix) noexcept nogil
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
cdef class SharedMemoryPage:
|
|
93
|
+
cdef shm_page_ctx* ctx
|
|
94
|
+
|
|
95
|
+
@staticmethod
|
|
96
|
+
cdef inline SharedMemoryPage c_from_header(shm_page_ctx* header)
|
|
97
|
+
|
|
98
|
+
cpdef void reclaim(self)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
cdef class SharedMemoryBlock:
|
|
102
|
+
cdef shm_memory_block* block
|
|
103
|
+
cdef readonly bint owner
|
|
104
|
+
|
|
105
|
+
@staticmethod
|
|
106
|
+
cdef inline SharedMemoryBlock c_from_header(shm_memory_block* header, bint owner=?)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
cdef class SharedMemoryAllocator:
|
|
110
|
+
cdef shm_allocator_ctx* ctx
|
|
111
|
+
cdef readonly bint owner
|
|
112
|
+
|
|
113
|
+
@staticmethod
|
|
114
|
+
cdef SharedMemoryAllocator c_from_header(shm_allocator_ctx* header, bint owner=?)
|
|
115
|
+
|
|
116
|
+
cdef inline shm_page_ctx* c_extend(self, size_t capacity=?, pthread_mutex_t* lock=?)
|
|
117
|
+
|
|
118
|
+
cdef inline void* c_calloc(self, size_t size, pthread_mutex_t* lock=?)
|
|
119
|
+
|
|
120
|
+
cdef inline void* c_request(self, size_t size, pthread_mutex_t* lock=?)
|
|
121
|
+
|
|
122
|
+
cdef inline void c_free(self, void* ptr, pthread_mutex_t* lock=NULL)
|
|
123
|
+
|
|
124
|
+
cpdef SharedMemoryPage extend(self, size_t capacity=?, bint with_lock=?)
|
|
125
|
+
|
|
126
|
+
cpdef SharedMemoryBlock calloc(self, size_t size, bint with_lock=?)
|
|
127
|
+
|
|
128
|
+
cpdef SharedMemoryBlock request(self, size_t size, bint scan_all_pages=?, bint with_lock=?)
|
|
129
|
+
|
|
130
|
+
cpdef void free(self, SharedMemoryBlock buffer, bint with_lock=?)
|
|
131
|
+
|
|
132
|
+
cpdef void reclaim(self, bint with_lock=?)
|
|
133
|
+
|
|
134
|
+
cpdef list dangling(self, str shm_prefix=?)
|
|
135
|
+
|
|
136
|
+
cpdef list dangling_pages(self, str shm_prefix=?)
|
|
137
|
+
|
|
138
|
+
cpdef void cleanup_dangling(self, str shm_prefix=?)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
cdef SharedMemoryAllocator ALLOCATOR
|
|
142
|
+
cdef shm_allocator_ctx* C_ALLOCATOR
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
"""POSIX shared-memory backed allocator
|
|
2
|
+
|
|
3
|
+
This module provides a Python wrapper around a C-backed shared memory allocator.
|
|
4
|
+
|
|
5
|
+
The allocator:
|
|
6
|
+
- Is backed by POSIX shared memory (shm). It creates named shm objects
|
|
7
|
+
and maps them into a reserved virtual address region so multiple
|
|
8
|
+
processes can agree on fixed addresses for mapped pages.
|
|
9
|
+
- Requests a fixed virtual address region so pages can be mapped at
|
|
10
|
+
stable addresses across processes (this enables multi-process access
|
|
11
|
+
to the same in-region pointers).
|
|
12
|
+
- By default reserves a ``128 GiB`` virtual address space for page
|
|
13
|
+
mappings (``AP_SHM_ALLOCATOR_DEFAULT_REGION_SIZE``).
|
|
14
|
+
- Is POSIX-only (depends on shm_open/mmap semantics available on POSIX
|
|
15
|
+
systems such as Linux).
|
|
16
|
+
|
|
17
|
+
Compile-time constants
|
|
18
|
+
----------------------
|
|
19
|
+
These values are provided by the C header and may be changed at
|
|
20
|
+
compile time when building the extension. The runtime values come from
|
|
21
|
+
the compiled extension.
|
|
22
|
+
|
|
23
|
+
- ``AP_SHM_AUTOPAGE_CAPACITY``: 64 * 1024 # 64 KiB — first/auto page size
|
|
24
|
+
- ``AP_SHM_AUTOPAGE_CAPACITY_MAX``: 16 * 1024 * 1024 # 16 MiB — maximum page
|
|
25
|
+
- ``AP_SHM_AUTOPAGE_ALIGNMENT``: 4 * 1024 # 4 KiB — page alignment
|
|
26
|
+
- ``AP_SHM_ALLOCATOR_PREFIX``: ``"/c_cbase_shm"`` # default SHM prefix
|
|
27
|
+
- ``AP_SHM_NAME_LEN``: 256 # max SHM name length
|
|
28
|
+
- ``AP_SHM_PREFIX_MAX``: 64 # max custom prefix length
|
|
29
|
+
- ``AP_SHM_ALLOCATOR_DEFAULT_REGION_SIZE``: 128 << 30 # 128 GiB
|
|
30
|
+
|
|
31
|
+
Naming convention
|
|
32
|
+
-----------------
|
|
33
|
+
Allocator SHM objects use the suffix ``_ac`` (e.g. ``/c_cbase_shm_ac_3e8_7f...``).
|
|
34
|
+
Page SHM objects use the suffix ``_pg`` (e.g. ``/c_cbase_shm_pg_3e8_000000``).
|
|
35
|
+
"""
|
|
36
|
+
import ctypes
|
|
37
|
+
from collections.abc import Generator
|
|
38
|
+
from dataclasses import dataclass
|
|
39
|
+
from typing import Annotated
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@dataclass
|
|
43
|
+
class ValueRange:
|
|
44
|
+
lo: int
|
|
45
|
+
hi: int
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
UINTPTR_MAX = ctypes.c_void_p(-1).value
|
|
49
|
+
uintptr_t = Annotated[int, ValueRange(0, UINTPTR_MAX), ctypes.c_void_p]
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class SharedMemoryBlock:
|
|
53
|
+
"""Represents an allocated block inside a shared memory page."""
|
|
54
|
+
|
|
55
|
+
owner: bool
|
|
56
|
+
|
|
57
|
+
def __init__(self, block: uintptr_t = 0, owner: bool = False) -> None: ...
|
|
58
|
+
|
|
59
|
+
def __repr__(self) -> str: ...
|
|
60
|
+
|
|
61
|
+
@property
|
|
62
|
+
def size(self) -> int:
|
|
63
|
+
"""Requested payload size in bytes (-1 if uninitialized, 0 if freed)."""
|
|
64
|
+
|
|
65
|
+
@property
|
|
66
|
+
def capacity(self) -> int:
|
|
67
|
+
"""Aligned capacity of the block (-1 if uninitialized)."""
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def next_free(self) -> SharedMemoryBlock | None:
|
|
71
|
+
"""Next entry in the allocator free list, if any."""
|
|
72
|
+
|
|
73
|
+
@property
|
|
74
|
+
def next_allocated(self) -> SharedMemoryBlock | None:
|
|
75
|
+
"""Next entry in the page's allocation list, if any."""
|
|
76
|
+
|
|
77
|
+
@property
|
|
78
|
+
def buffer(self) -> memoryview | None:
|
|
79
|
+
"""Memoryview of the user buffer (None if uninitialized or zero-sized)."""
|
|
80
|
+
|
|
81
|
+
@property
|
|
82
|
+
def address(self) -> str | None:
|
|
83
|
+
"""Hex string of the buffer address."""
|
|
84
|
+
|
|
85
|
+
@property
|
|
86
|
+
def page_address(self) -> str | None:
|
|
87
|
+
"""Hex string of the parent page's base address."""
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
class SharedMemoryPage:
|
|
91
|
+
"""Represents a mapped shared-memory page."""
|
|
92
|
+
|
|
93
|
+
def __init__(self, page_addr: uintptr_t = 0) -> None: ...
|
|
94
|
+
|
|
95
|
+
def __repr__(self) -> str: ...
|
|
96
|
+
|
|
97
|
+
def __bool__(self) -> bool: ...
|
|
98
|
+
|
|
99
|
+
@classmethod
|
|
100
|
+
def from_buffer(cls, buffer_addr: uintptr_t) -> SharedMemoryPage:
|
|
101
|
+
"""Reconstruct a page wrapper from a buffer address within it."""
|
|
102
|
+
|
|
103
|
+
def allocated(self) -> Generator[SharedMemoryBlock, None, None]:
|
|
104
|
+
"""Iterate allocated blocks newest-first."""
|
|
105
|
+
|
|
106
|
+
def reclaim(self) -> None:
|
|
107
|
+
"""Best-effort reclaim of freed blocks back to unused space."""
|
|
108
|
+
|
|
109
|
+
@property
|
|
110
|
+
def name(self) -> str | None:
|
|
111
|
+
"""The OS shared-memory object name for this page."""
|
|
112
|
+
|
|
113
|
+
@property
|
|
114
|
+
def capacity(self) -> int:
|
|
115
|
+
"""Total capacity of the page in bytes (0 if uninitialized)."""
|
|
116
|
+
|
|
117
|
+
@property
|
|
118
|
+
def occupied(self) -> int:
|
|
119
|
+
"""Currently occupied bytes on the page (0 if uninitialized)."""
|
|
120
|
+
|
|
121
|
+
@property
|
|
122
|
+
def address(self) -> str | None:
|
|
123
|
+
"""Hex string of the page's base address."""
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
class SharedMemoryAllocator:
|
|
127
|
+
"""Top-level allocator managing a large virtual region and multiple pages.
|
|
128
|
+
|
|
129
|
+
Typical usage::
|
|
130
|
+
|
|
131
|
+
>>> alloc = SharedMemoryAllocator()
|
|
132
|
+
>>> page = alloc.extend()
|
|
133
|
+
>>> block = alloc.calloc(1024)
|
|
134
|
+
>>> alloc.free(block)
|
|
135
|
+
>>> alloc.reclaim()
|
|
136
|
+
"""
|
|
137
|
+
|
|
138
|
+
owner: bool
|
|
139
|
+
|
|
140
|
+
def __init__(self, region_size: int = 0, shm_prefix: str | None = None) -> None:
|
|
141
|
+
"""Create and initialize an allocator context.
|
|
142
|
+
|
|
143
|
+
Args:
|
|
144
|
+
region_size: Virtual region size to reserve (0 for default 128 GiB).
|
|
145
|
+
shm_prefix: Custom SHM name prefix (None for ``AP_SHM_ALLOCATOR_PREFIX``).
|
|
146
|
+
|
|
147
|
+
Raises:
|
|
148
|
+
OSError: If the underlying C allocator cannot be created.
|
|
149
|
+
"""
|
|
150
|
+
|
|
151
|
+
def __repr__(self) -> str: ...
|
|
152
|
+
|
|
153
|
+
@classmethod
|
|
154
|
+
def get_pid(cls, shm_name: str) -> int:
|
|
155
|
+
"""Extract the creator PID from an allocator or page SHM name.
|
|
156
|
+
|
|
157
|
+
Args:
|
|
158
|
+
shm_name: SHM object name (with leading '/').
|
|
159
|
+
|
|
160
|
+
Returns:
|
|
161
|
+
PID on success, -1 on parse failure.
|
|
162
|
+
"""
|
|
163
|
+
|
|
164
|
+
def extend(self, capacity: int = 0, with_lock: bool = True) -> SharedMemoryPage:
|
|
165
|
+
"""Add a page (auto-sized when capacity is 0).
|
|
166
|
+
|
|
167
|
+
Raises:
|
|
168
|
+
OSError: On mapping/allocation failure.
|
|
169
|
+
"""
|
|
170
|
+
|
|
171
|
+
def calloc(self, size: int, with_lock: bool = True) -> SharedMemoryBlock:
|
|
172
|
+
"""Allocate zeroed memory from the active page (auto-extending).
|
|
173
|
+
|
|
174
|
+
Raises:
|
|
175
|
+
OSError: On allocation failure.
|
|
176
|
+
"""
|
|
177
|
+
|
|
178
|
+
def request(
|
|
179
|
+
self,
|
|
180
|
+
size: int,
|
|
181
|
+
scan_all_pages: bool = True,
|
|
182
|
+
with_lock: bool = True,
|
|
183
|
+
) -> SharedMemoryBlock:
|
|
184
|
+
"""Request a zeroed block, scanning free list then pages.
|
|
185
|
+
|
|
186
|
+
Raises:
|
|
187
|
+
OSError: On allocation failure.
|
|
188
|
+
"""
|
|
189
|
+
|
|
190
|
+
def free(self, buffer: SharedMemoryBlock, with_lock: bool = True) -> None:
|
|
191
|
+
"""Return a block to the allocator free list."""
|
|
192
|
+
|
|
193
|
+
def reclaim(self, with_lock: bool = True) -> None:
|
|
194
|
+
"""Reclaim freed space across all pages."""
|
|
195
|
+
|
|
196
|
+
def dangling(self, shm_prefix: str | None = None) -> list[str]:
|
|
197
|
+
"""Return allocator SHM names whose creator PID is gone.
|
|
198
|
+
|
|
199
|
+
Args:
|
|
200
|
+
shm_prefix: Prefix to scan for (None for default, appends ``_ac``).
|
|
201
|
+
"""
|
|
202
|
+
|
|
203
|
+
def dangling_pages(self, shm_prefix: str | None = None) -> list[str]:
|
|
204
|
+
"""Return page SHM names whose creator PID is gone.
|
|
205
|
+
|
|
206
|
+
Args:
|
|
207
|
+
shm_prefix: Prefix to scan for (None for default, appends ``_pg``).
|
|
208
|
+
"""
|
|
209
|
+
|
|
210
|
+
def cleanup_dangling(self, shm_prefix: str | None = None) -> None:
|
|
211
|
+
"""Unlink dangling allocator and page SHM objects.
|
|
212
|
+
|
|
213
|
+
Args:
|
|
214
|
+
shm_prefix: Prefix to scan for (None for default).
|
|
215
|
+
"""
|
|
216
|
+
|
|
217
|
+
def pages(self) -> Generator[SharedMemoryPage, None, None]:
|
|
218
|
+
"""Iterate mapped pages newest-first."""
|
|
219
|
+
|
|
220
|
+
def allocated(self) -> Generator[SharedMemoryBlock, None, None]:
|
|
221
|
+
"""Iterate allocated blocks across all pages."""
|
|
222
|
+
|
|
223
|
+
def free_list(self) -> Generator[SharedMemoryBlock, None, None]:
|
|
224
|
+
"""Iterate the allocator-wide free list."""
|
|
225
|
+
|
|
226
|
+
# -- metadata properties --
|
|
227
|
+
|
|
228
|
+
@property
|
|
229
|
+
def name(self) -> str | None:
|
|
230
|
+
"""The allocator SHM object name (with leading '/')."""
|
|
231
|
+
|
|
232
|
+
@property
|
|
233
|
+
def pid(self) -> int:
|
|
234
|
+
"""PID of the process that created the allocator (-1 if unknown)."""
|
|
235
|
+
|
|
236
|
+
@property
|
|
237
|
+
def region(self) -> int:
|
|
238
|
+
"""Base virtual address of the reserved region (-1 if uninitialized)."""
|
|
239
|
+
|
|
240
|
+
@property
|
|
241
|
+
def region_addr(self) -> str | None:
|
|
242
|
+
"""Base virtual address as hex string."""
|
|
243
|
+
|
|
244
|
+
@property
|
|
245
|
+
def region_size(self) -> int:
|
|
246
|
+
"""Virtual region size in bytes (-1 if uninitialized)."""
|
|
247
|
+
|
|
248
|
+
@property
|
|
249
|
+
def mapped_size(self) -> int:
|
|
250
|
+
"""Total mapped bytes within the region (-1 if uninitialized)."""
|
|
251
|
+
|
|
252
|
+
@property
|
|
253
|
+
def mapped_pages(self) -> int:
|
|
254
|
+
"""Number of pages currently mapped (-1 if uninitialized)."""
|
|
255
|
+
|
|
256
|
+
@property
|
|
257
|
+
def active_page(self) -> SharedMemoryPage | None:
|
|
258
|
+
"""Most recently mapped page, or None."""
|
|
259
|
+
|
|
260
|
+
# -- config properties (read/write) --
|
|
261
|
+
|
|
262
|
+
@property
|
|
263
|
+
def autopage_capacity(self) -> int:
|
|
264
|
+
"""Default capacity for auto-sized pages."""
|
|
265
|
+
|
|
266
|
+
@autopage_capacity.setter
|
|
267
|
+
def autopage_capacity(self, value: int) -> None: ...
|
|
268
|
+
|
|
269
|
+
@property
|
|
270
|
+
def autopage_capacity_max(self) -> int:
|
|
271
|
+
"""Maximum capacity a page can grow to."""
|
|
272
|
+
|
|
273
|
+
@autopage_capacity_max.setter
|
|
274
|
+
def autopage_capacity_max(self, value: int) -> None: ...
|
|
275
|
+
|
|
276
|
+
@property
|
|
277
|
+
def autopage_alignment(self) -> int:
|
|
278
|
+
"""Page alignment in bytes."""
|
|
279
|
+
|
|
280
|
+
@autopage_alignment.setter
|
|
281
|
+
def autopage_alignment(self, value: int) -> None: ...
|
|
282
|
+
|
|
283
|
+
@property
|
|
284
|
+
def shm_prefix(self) -> str | None:
|
|
285
|
+
"""The SHM prefix used by this allocator instance."""
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
# Module-level exports
|
|
289
|
+
|
|
290
|
+
ALLOCATOR: SharedMemoryAllocator | None
|
|
291
|
+
"""Global singleton allocator instance."""
|
|
292
|
+
|
|
293
|
+
AP_SHM_AUTOPAGE_CAPACITY: int
|
|
294
|
+
AP_SHM_AUTOPAGE_CAPACITY_MAX: int
|
|
295
|
+
AP_SHM_AUTOPAGE_ALIGNMENT: int
|
|
296
|
+
AP_SHM_ALLOCATOR_PREFIX: str
|
|
297
|
+
AP_SHM_NAME_LEN: int
|
|
298
|
+
AP_SHM_PREFIX_MAX: int
|
|
299
|
+
AP_SHM_ALLOCATOR_DEFAULT_REGION_SIZE: int
|