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,442 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
from cpython.unicode cimport PyUnicode_FromString, PyUnicode_AsUTF8
|
|
4
|
+
from libc.errno cimport errno
|
|
5
|
+
|
|
6
|
+
from .c_shm_comp cimport C_SHM_COMP
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
cdef class SharedMemoryPage:
|
|
10
|
+
def __cinit__(self, uintptr_t page_addr=0):
|
|
11
|
+
self.ctx = <shm_page_ctx*> page_addr if page_addr else NULL
|
|
12
|
+
|
|
13
|
+
@staticmethod
|
|
14
|
+
cdef inline SharedMemoryPage c_from_header(shm_page_ctx* header):
|
|
15
|
+
cdef SharedMemoryPage instance = SharedMemoryPage.__new__(SharedMemoryPage, 0)
|
|
16
|
+
instance.ctx = header
|
|
17
|
+
return instance
|
|
18
|
+
|
|
19
|
+
def __bool__(self):
|
|
20
|
+
return self.ctx is not NULL
|
|
21
|
+
|
|
22
|
+
def __repr__(self):
|
|
23
|
+
if self.ctx and self.ctx.shm_page:
|
|
24
|
+
return f"<{self.__class__.__name__}>(name={self.name}, capacity={self.capacity:,}, occupied={self.occupied:,})>"
|
|
25
|
+
return f"<{self.__class__.__name__}>(uninitialized)"
|
|
26
|
+
|
|
27
|
+
@classmethod
|
|
28
|
+
def from_buffer(cls, uintptr_t buffer_addr) -> SharedMemoryPage:
|
|
29
|
+
cdef SharedMemoryPage instance = cls.__new__(cls, 0)
|
|
30
|
+
instance.ctx = <shm_page_ctx*> (<char*> buffer_addr - sizeof(shm_page_ctx))
|
|
31
|
+
return instance
|
|
32
|
+
|
|
33
|
+
def allocated(self):
|
|
34
|
+
if not self.ctx or not self.ctx.shm_page:
|
|
35
|
+
return
|
|
36
|
+
|
|
37
|
+
cdef shm_memory_block* block = self.ctx.shm_page.allocated
|
|
38
|
+
while block:
|
|
39
|
+
yield SharedMemoryBlock.c_from_header(block, False)
|
|
40
|
+
block = block.next_allocated
|
|
41
|
+
|
|
42
|
+
cpdef void reclaim(self):
|
|
43
|
+
if not self.ctx:
|
|
44
|
+
raise RuntimeError(f"Uninitialized <{self.__class__.__name__}>")
|
|
45
|
+
c_shm_page_reclaim(self.ctx.shm_page.allocator, self.ctx)
|
|
46
|
+
|
|
47
|
+
property name:
|
|
48
|
+
def __get__(self) -> str:
|
|
49
|
+
if not self.ctx or not self.ctx.shm_page:
|
|
50
|
+
return None
|
|
51
|
+
return PyUnicode_FromString(self.ctx.shm_page.shm_name)
|
|
52
|
+
|
|
53
|
+
property capacity:
|
|
54
|
+
def __get__(self) -> size_t:
|
|
55
|
+
if self.ctx and self.ctx.shm_page:
|
|
56
|
+
return <size_t> self.ctx.shm_page.capacity
|
|
57
|
+
return 0
|
|
58
|
+
|
|
59
|
+
property occupied:
|
|
60
|
+
def __get__(self) -> size_t:
|
|
61
|
+
if self.ctx and self.ctx.shm_page:
|
|
62
|
+
return <size_t> self.ctx.shm_page.occupied
|
|
63
|
+
return 0
|
|
64
|
+
|
|
65
|
+
property address:
|
|
66
|
+
def __get__(self):
|
|
67
|
+
if self.ctx:
|
|
68
|
+
return f'{<uintptr_t> self.ctx:#0x}'
|
|
69
|
+
return None
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
cdef class SharedMemoryBlock:
|
|
73
|
+
def __cinit__(self, uintptr_t block=0, bint owner=False):
|
|
74
|
+
self.block = <shm_memory_block*> block if block else NULL
|
|
75
|
+
self.owner = owner
|
|
76
|
+
|
|
77
|
+
def __dealloc__(self):
|
|
78
|
+
if not self.owner:
|
|
79
|
+
return
|
|
80
|
+
|
|
81
|
+
if self.block:
|
|
82
|
+
c_shm_free(self.block.buffer, &self.block.parent_page.allocator.lock)
|
|
83
|
+
|
|
84
|
+
@staticmethod
|
|
85
|
+
cdef inline SharedMemoryBlock c_from_header(shm_memory_block* header, bint owner=False):
|
|
86
|
+
cdef SharedMemoryBlock instance = SharedMemoryBlock.__new__(SharedMemoryBlock, 0, owner)
|
|
87
|
+
instance.block = header
|
|
88
|
+
return instance
|
|
89
|
+
|
|
90
|
+
def __repr__(self):
|
|
91
|
+
if self.block:
|
|
92
|
+
return f"<{self.__class__.__name__}>(size={self.block.size}, capacity={self.block.capacity})"
|
|
93
|
+
return f"<{self.__class__.__name__}>(uninitialized)"
|
|
94
|
+
|
|
95
|
+
property size:
|
|
96
|
+
def __get__(self):
|
|
97
|
+
if not self.block:
|
|
98
|
+
return -1
|
|
99
|
+
return <size_t> self.block.size
|
|
100
|
+
|
|
101
|
+
property capacity:
|
|
102
|
+
def __get__(self):
|
|
103
|
+
if not self.block:
|
|
104
|
+
return -1
|
|
105
|
+
return <size_t> self.block.capacity
|
|
106
|
+
|
|
107
|
+
property next_free:
|
|
108
|
+
def __get__(self):
|
|
109
|
+
if self.block and self.block.next_free:
|
|
110
|
+
return SharedMemoryBlock.c_from_header(self.block.next_free, False)
|
|
111
|
+
return None
|
|
112
|
+
|
|
113
|
+
property next_allocated:
|
|
114
|
+
def __get__(self):
|
|
115
|
+
if self.block and self.block.next_allocated:
|
|
116
|
+
return SharedMemoryBlock.c_from_header(self.block.next_allocated, False)
|
|
117
|
+
return None
|
|
118
|
+
|
|
119
|
+
property buffer:
|
|
120
|
+
def __get__(self):
|
|
121
|
+
if not self.block:
|
|
122
|
+
return None
|
|
123
|
+
cdef size_t size = self.block.size
|
|
124
|
+
if size:
|
|
125
|
+
return <char[:size]> self.block.buffer
|
|
126
|
+
return None
|
|
127
|
+
|
|
128
|
+
property address:
|
|
129
|
+
def __get__(self):
|
|
130
|
+
if self.block:
|
|
131
|
+
return f'{<uintptr_t> self.block.buffer:#0x}'
|
|
132
|
+
return None
|
|
133
|
+
|
|
134
|
+
property page_address:
|
|
135
|
+
def __get__(self):
|
|
136
|
+
if self.block and self.block.parent_page:
|
|
137
|
+
return f'{<uintptr_t> self.block.parent_page:#0x}'
|
|
138
|
+
return None
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
cdef class SharedMemoryAllocator:
|
|
142
|
+
def __init__(
|
|
143
|
+
self,
|
|
144
|
+
size_t region_size=AP_SHM_ALLOCATOR_DEFAULT_REGION_SIZE,
|
|
145
|
+
str shm_prefix=PyUnicode_FromString(AP_SHM_ALLOCATOR_PREFIX)
|
|
146
|
+
):
|
|
147
|
+
if not region_size:
|
|
148
|
+
return
|
|
149
|
+
self.ctx = c_shm_allocator_new(region_size, PyUnicode_AsUTF8(shm_prefix) if shm_prefix else NULL)
|
|
150
|
+
if not self.ctx:
|
|
151
|
+
raise OSError(errno, "Initialize SHM allocator failed")
|
|
152
|
+
self.owner = True
|
|
153
|
+
|
|
154
|
+
def __dealloc__(self):
|
|
155
|
+
if not self.owner:
|
|
156
|
+
return
|
|
157
|
+
|
|
158
|
+
if self.ctx:
|
|
159
|
+
c_shm_allocator_free(self.ctx)
|
|
160
|
+
|
|
161
|
+
@staticmethod
|
|
162
|
+
cdef SharedMemoryAllocator c_from_header(shm_allocator_ctx* header, bint owner=False):
|
|
163
|
+
cdef SharedMemoryAllocator instance = SharedMemoryAllocator.__new__(SharedMemoryAllocator)
|
|
164
|
+
instance.ctx = header
|
|
165
|
+
instance.owner = owner
|
|
166
|
+
return instance
|
|
167
|
+
|
|
168
|
+
cdef inline shm_page_ctx* c_extend(self, size_t capacity=0, pthread_mutex_t* lock=NULL):
|
|
169
|
+
if not self.ctx:
|
|
170
|
+
raise RuntimeError(f'Uninitialized <{self.__class__.__name__}>')
|
|
171
|
+
return c_shm_allocator_extend(self.ctx, capacity, lock)
|
|
172
|
+
|
|
173
|
+
cdef inline void* c_calloc(self, size_t size, pthread_mutex_t* lock=NULL):
|
|
174
|
+
if not self.ctx:
|
|
175
|
+
raise RuntimeError(f'Uninitialized <{self.__class__.__name__}>')
|
|
176
|
+
return c_shm_calloc(self.ctx, size, lock)
|
|
177
|
+
|
|
178
|
+
cdef inline void* c_request(self, size_t size, pthread_mutex_t* lock=NULL):
|
|
179
|
+
if not self.ctx:
|
|
180
|
+
raise RuntimeError(f'Uninitialized <{self.__class__.__name__}>')
|
|
181
|
+
return c_shm_request(self.ctx, size, 1, lock)
|
|
182
|
+
|
|
183
|
+
cdef inline void c_free(self, void* ptr, pthread_mutex_t* lock=NULL):
|
|
184
|
+
if not self.ctx:
|
|
185
|
+
raise RuntimeError(f'Uninitialized <{self.__class__.__name__}>')
|
|
186
|
+
c_shm_free(ptr, lock)
|
|
187
|
+
|
|
188
|
+
def __repr__(self):
|
|
189
|
+
if self.ctx and self.ctx.shm_allocator:
|
|
190
|
+
return f"<{self.__class__.__name__}>(name={self.name}, pid={self.pid}, mapped_addr={<uintptr_t> self.ctx.shm_allocator.region}>"
|
|
191
|
+
return f"<{self.__class__.__name__}>(uninitialized)"
|
|
192
|
+
|
|
193
|
+
@classmethod
|
|
194
|
+
def get_pid(cls, str shm_name):
|
|
195
|
+
return c_shm_pid(PyUnicode_AsUTF8(shm_name))
|
|
196
|
+
|
|
197
|
+
cpdef SharedMemoryPage extend(self, size_t capacity=0, bint with_lock=True):
|
|
198
|
+
if not self.ctx:
|
|
199
|
+
raise RuntimeError(f'Uninitialized <{self.__class__.__name__}>')
|
|
200
|
+
cdef pthread_mutex_t* lock = &self.ctx.shm_allocator.lock if with_lock else NULL
|
|
201
|
+
cdef shm_page_ctx* page = c_shm_allocator_extend(self.ctx, capacity, lock)
|
|
202
|
+
if not page:
|
|
203
|
+
raise OSError(errno, f'<{self.__class__.__name__}> failed to extend new page')
|
|
204
|
+
return SharedMemoryPage.c_from_header(page)
|
|
205
|
+
|
|
206
|
+
cpdef SharedMemoryBlock calloc(self, size_t size, bint with_lock=True):
|
|
207
|
+
if not self.ctx:
|
|
208
|
+
raise RuntimeError(f'Uninitialized <{self.__class__.__name__}>')
|
|
209
|
+
cdef pthread_mutex_t* lock = &self.ctx.shm_allocator.lock if with_lock else NULL
|
|
210
|
+
cdef void* p = c_shm_calloc(self.ctx, size, lock)
|
|
211
|
+
if not p:
|
|
212
|
+
raise OSError(errno, f'<{self.__class__.__name__}> failed to calloc new buffer')
|
|
213
|
+
|
|
214
|
+
cdef shm_memory_block* block = <shm_memory_block*> (<char*> p - sizeof(shm_memory_block))
|
|
215
|
+
return SharedMemoryBlock.c_from_header(block, True)
|
|
216
|
+
|
|
217
|
+
cpdef SharedMemoryBlock request(self, size_t size, bint scan_all_pages=True, bint with_lock=True):
|
|
218
|
+
if not self.ctx:
|
|
219
|
+
raise RuntimeError(f'Uninitialized <{self.__class__.__name__}>')
|
|
220
|
+
cdef pthread_mutex_t* lock = &self.ctx.shm_allocator.lock if with_lock else NULL
|
|
221
|
+
cdef void* p = c_shm_request(self.ctx, size, scan_all_pages, lock)
|
|
222
|
+
if not p:
|
|
223
|
+
raise OSError(errno, f'<{self.__class__.__name__}> failed to request new buffer')
|
|
224
|
+
|
|
225
|
+
cdef shm_memory_block* block = <shm_memory_block*> (<char*> p - sizeof(shm_memory_block))
|
|
226
|
+
return SharedMemoryBlock.c_from_header(block, True)
|
|
227
|
+
|
|
228
|
+
cpdef void free(self, SharedMemoryBlock buffer, bint with_lock=True):
|
|
229
|
+
if not self.ctx:
|
|
230
|
+
raise RuntimeError(f'Uninitialized <{self.__class__.__name__}>')
|
|
231
|
+
if not buffer or not buffer.block:
|
|
232
|
+
return
|
|
233
|
+
cdef pthread_mutex_t* lock = &self.ctx.shm_allocator.lock if with_lock else NULL
|
|
234
|
+
c_shm_free(<void*> buffer.block.buffer, lock)
|
|
235
|
+
buffer.owner = False
|
|
236
|
+
buffer.block = NULL
|
|
237
|
+
|
|
238
|
+
cpdef void reclaim(self, bint with_lock=True):
|
|
239
|
+
if not self.ctx:
|
|
240
|
+
raise RuntimeError(f'Uninitialized <{self.__class__.__name__}>')
|
|
241
|
+
cdef pthread_mutex_t* lock = &self.ctx.shm_allocator.lock if with_lock else NULL
|
|
242
|
+
c_shm_reclaim(self.ctx, lock)
|
|
243
|
+
|
|
244
|
+
cpdef list dangling(self, str shm_prefix=None):
|
|
245
|
+
cdef list out = []
|
|
246
|
+
cdef list entries = os.listdir('/dev/shm')
|
|
247
|
+
cdef str prefix = shm_prefix if shm_prefix else PyUnicode_FromString(AP_SHM_ALLOCATOR_PREFIX)
|
|
248
|
+
prefix = prefix + '_ac'
|
|
249
|
+
|
|
250
|
+
if prefix.startswith('/'):
|
|
251
|
+
prefix = prefix[1:]
|
|
252
|
+
|
|
253
|
+
cdef str shm_name, candidate
|
|
254
|
+
cdef pid_t p
|
|
255
|
+
for shm_name in entries:
|
|
256
|
+
if not shm_name.startswith(prefix):
|
|
257
|
+
continue
|
|
258
|
+
candidate = '/' + shm_name
|
|
259
|
+
p = c_shm_pid(PyUnicode_AsUTF8(candidate))
|
|
260
|
+
if p <= 0:
|
|
261
|
+
continue
|
|
262
|
+
try:
|
|
263
|
+
os.kill(p, 0)
|
|
264
|
+
except ProcessLookupError:
|
|
265
|
+
out.append(candidate)
|
|
266
|
+
except PermissionError:
|
|
267
|
+
continue
|
|
268
|
+
except OSError:
|
|
269
|
+
continue
|
|
270
|
+
return out
|
|
271
|
+
|
|
272
|
+
cpdef list dangling_pages(self, str shm_prefix=None):
|
|
273
|
+
cdef list out = []
|
|
274
|
+
cdef list entries = os.listdir('/dev/shm')
|
|
275
|
+
cdef str prefix = shm_prefix if shm_prefix else PyUnicode_FromString(AP_SHM_ALLOCATOR_PREFIX)
|
|
276
|
+
prefix = prefix + '_pg'
|
|
277
|
+
|
|
278
|
+
if prefix.startswith('/'):
|
|
279
|
+
prefix = prefix[1:]
|
|
280
|
+
|
|
281
|
+
cdef str shm_name, candidate
|
|
282
|
+
cdef pid_t p
|
|
283
|
+
for shm_name in entries:
|
|
284
|
+
if not shm_name.startswith(prefix):
|
|
285
|
+
continue
|
|
286
|
+
candidate = '/' + shm_name
|
|
287
|
+
p = c_shm_pid(PyUnicode_AsUTF8(candidate))
|
|
288
|
+
if p <= 0:
|
|
289
|
+
continue
|
|
290
|
+
try:
|
|
291
|
+
os.kill(p, 0)
|
|
292
|
+
except ProcessLookupError:
|
|
293
|
+
out.append(candidate)
|
|
294
|
+
except PermissionError:
|
|
295
|
+
continue
|
|
296
|
+
except OSError:
|
|
297
|
+
continue
|
|
298
|
+
return out
|
|
299
|
+
|
|
300
|
+
cpdef void cleanup_dangling(self, str shm_prefix=None):
|
|
301
|
+
c_shm_clear_dangling(PyUnicode_AsUTF8(shm_prefix) if shm_prefix else NULL)
|
|
302
|
+
|
|
303
|
+
def pages(self):
|
|
304
|
+
if not self.ctx:
|
|
305
|
+
raise RuntimeError(f'Uninitialized <{self.__class__.__name__}>')
|
|
306
|
+
cdef shm_page_ctx* page = self.ctx.active_page
|
|
307
|
+
while page:
|
|
308
|
+
yield SharedMemoryPage.c_from_header(page)
|
|
309
|
+
page = page.prev
|
|
310
|
+
|
|
311
|
+
def allocated(self):
|
|
312
|
+
if not self.ctx:
|
|
313
|
+
raise RuntimeError(f'Uninitialized <{self.__class__.__name__}>')
|
|
314
|
+
cdef shm_page_ctx* page = self.ctx.active_page
|
|
315
|
+
cdef shm_memory_block* block
|
|
316
|
+
while page:
|
|
317
|
+
block = page.shm_page.allocated
|
|
318
|
+
while block:
|
|
319
|
+
yield SharedMemoryBlock.c_from_header(block, False)
|
|
320
|
+
block = block.next_allocated
|
|
321
|
+
page = page.prev
|
|
322
|
+
|
|
323
|
+
def free_list(self):
|
|
324
|
+
if not self.ctx:
|
|
325
|
+
raise RuntimeError(f'Uninitialized <{self.__class__.__name__}>')
|
|
326
|
+
cdef shm_memory_block* block = self.ctx.shm_allocator.free_list
|
|
327
|
+
while block:
|
|
328
|
+
yield SharedMemoryBlock.c_from_header(block, False)
|
|
329
|
+
block = block.next_free
|
|
330
|
+
|
|
331
|
+
property name:
|
|
332
|
+
def __get__(self) -> str:
|
|
333
|
+
if not self.ctx or not self.ctx.shm_allocator:
|
|
334
|
+
return None
|
|
335
|
+
return PyUnicode_FromString(self.ctx.shm_allocator.shm_name)
|
|
336
|
+
|
|
337
|
+
property pid:
|
|
338
|
+
def __get__(self):
|
|
339
|
+
if not self.ctx or not self.ctx.shm_allocator:
|
|
340
|
+
return -1
|
|
341
|
+
return <pid_t> self.ctx.shm_allocator.pid
|
|
342
|
+
|
|
343
|
+
property region:
|
|
344
|
+
def __get__(self):
|
|
345
|
+
if not self.ctx or not self.ctx.shm_allocator:
|
|
346
|
+
return -1
|
|
347
|
+
return <uintptr_t> self.ctx.shm_allocator.region
|
|
348
|
+
|
|
349
|
+
property region_addr:
|
|
350
|
+
def __get__(self):
|
|
351
|
+
if not self.ctx or not self.ctx.shm_allocator:
|
|
352
|
+
return None
|
|
353
|
+
return f'{<uintptr_t> self.ctx.shm_allocator.region:#0x}'
|
|
354
|
+
|
|
355
|
+
property region_size:
|
|
356
|
+
def __get__(self):
|
|
357
|
+
if not self.ctx or not self.ctx.shm_allocator:
|
|
358
|
+
return -1
|
|
359
|
+
return <size_t> self.ctx.shm_allocator.region_size
|
|
360
|
+
|
|
361
|
+
property mapped_size:
|
|
362
|
+
def __get__(self):
|
|
363
|
+
if not self.ctx or not self.ctx.shm_allocator:
|
|
364
|
+
return -1
|
|
365
|
+
return <size_t> self.ctx.shm_allocator.mapped_size
|
|
366
|
+
|
|
367
|
+
property mapped_pages:
|
|
368
|
+
def __get__(self):
|
|
369
|
+
if not self.ctx or not self.ctx.shm_allocator:
|
|
370
|
+
return -1
|
|
371
|
+
return <size_t> self.ctx.shm_allocator.mapped_pages
|
|
372
|
+
|
|
373
|
+
property active_page:
|
|
374
|
+
def __get__(self):
|
|
375
|
+
if not self.ctx:
|
|
376
|
+
return None
|
|
377
|
+
return SharedMemoryPage.c_from_header(self.ctx.active_page)
|
|
378
|
+
|
|
379
|
+
property autopage_capacity:
|
|
380
|
+
def __get__(self):
|
|
381
|
+
if not self.ctx or not self.ctx.shm_allocator:
|
|
382
|
+
raise RuntimeError(f'Uninitialized <{self.__class__.__name__}>')
|
|
383
|
+
return <size_t> self.ctx.shm_allocator.autopage_capacity
|
|
384
|
+
|
|
385
|
+
def __set__(self, size_t value):
|
|
386
|
+
if not self.ctx or not self.ctx.shm_allocator:
|
|
387
|
+
raise RuntimeError(f'Uninitialized <{self.__class__.__name__}>')
|
|
388
|
+
self.ctx.shm_allocator.autopage_capacity = value
|
|
389
|
+
|
|
390
|
+
property autopage_capacity_max:
|
|
391
|
+
def __get__(self):
|
|
392
|
+
if not self.ctx or not self.ctx.shm_allocator:
|
|
393
|
+
raise RuntimeError(f'Uninitialized <{self.__class__.__name__}>')
|
|
394
|
+
return <size_t> self.ctx.shm_allocator.autopage_capacity_max
|
|
395
|
+
|
|
396
|
+
def __set__(self, size_t value):
|
|
397
|
+
if not self.ctx or not self.ctx.shm_allocator:
|
|
398
|
+
raise RuntimeError(f'Uninitialized <{self.__class__.__name__}>')
|
|
399
|
+
self.ctx.shm_allocator.autopage_capacity_max = value
|
|
400
|
+
|
|
401
|
+
property autopage_alignment:
|
|
402
|
+
def __get__(self):
|
|
403
|
+
if not self.ctx or not self.ctx.shm_allocator:
|
|
404
|
+
raise RuntimeError(f'Uninitialized <{self.__class__.__name__}>')
|
|
405
|
+
return <size_t> self.ctx.shm_allocator.autopage_alignment
|
|
406
|
+
|
|
407
|
+
def __set__(self, size_t value):
|
|
408
|
+
if not self.ctx or not self.ctx.shm_allocator:
|
|
409
|
+
raise RuntimeError(f'Uninitialized <{self.__class__.__name__}>')
|
|
410
|
+
self.ctx.shm_allocator.autopage_alignment = value
|
|
411
|
+
|
|
412
|
+
property shm_prefix:
|
|
413
|
+
def __get__(self) -> str:
|
|
414
|
+
if not self.ctx or not self.ctx.shm_allocator:
|
|
415
|
+
return None
|
|
416
|
+
return PyUnicode_FromString(self.ctx.shm_allocator.shm_prefix)
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
cdef SharedMemoryAllocator ALLOCATOR = SharedMemoryAllocator.c_from_header(C_SHM_COMP, False)
|
|
420
|
+
cdef shm_allocator_ctx* C_ALLOCATOR = C_SHM_COMP
|
|
421
|
+
|
|
422
|
+
globals()['ALLOCATOR'] = ALLOCATOR
|
|
423
|
+
globals()['AP_SHM_AUTOPAGE_CAPACITY'] = AP_SHM_AUTOPAGE_CAPACITY
|
|
424
|
+
globals()['AP_SHM_AUTOPAGE_CAPACITY_MAX'] = AP_SHM_AUTOPAGE_CAPACITY_MAX
|
|
425
|
+
globals()['AP_SHM_AUTOPAGE_ALIGNMENT'] = AP_SHM_AUTOPAGE_ALIGNMENT
|
|
426
|
+
globals()['AP_SHM_ALLOCATOR_PREFIX'] = PyUnicode_FromString(AP_SHM_ALLOCATOR_PREFIX)
|
|
427
|
+
globals()['AP_SHM_NAME_LEN'] = AP_SHM_NAME_LEN
|
|
428
|
+
globals()['AP_SHM_PREFIX_MAX'] = AP_SHM_PREFIX_MAX
|
|
429
|
+
globals()['AP_SHM_ALLOCATOR_DEFAULT_REGION_SIZE'] = AP_SHM_ALLOCATOR_DEFAULT_REGION_SIZE
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
def cleanup():
|
|
433
|
+
global ALLOCATOR, C_ALLOCATOR
|
|
434
|
+
ALLOCATOR.owner = True
|
|
435
|
+
globals()['ALLOCATOR'] = ALLOCATOR = None
|
|
436
|
+
C_ALLOCATOR = NULL
|
|
437
|
+
c_shm_clear_dangling(NULL)
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
import atexit
|
|
441
|
+
|
|
442
|
+
atexit.register(cleanup)
|