-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmem_pool.py
53 lines (44 loc) · 1.31 KB
/
mem_pool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#
# mem_pool.py
#
# very primitive memory handling.
# For use with pre-allocated bytearray or with uctype
#
import uctypes
class MemPoolAllocException(BaseException):
pass
class MemPoolInvalidException(BaseException):
pass
POOL_DEF = {
"magic": 0 | uctypes.UINT32,
"addr": 4 | uctypes.UINT32,
"top": 8 | uctypes.UINT32,
}
POOL_MAGIC = 0x389ac54f
class MemPool():
#
# creates a memory pool or attaches to an existing one
#
def __init__(self, addr, size):
self._head = uctypes.struct(addr, POOL_DEF)
if self._head.magic != POOL_MAGIC:
print("create new pool, addr={:x}, size={}".format(addr, size))
self._head.magic = POOL_MAGIC
self._head.top = addr + size
self._head.addr = addr + uctypes.sizeof(POOL_DEF)
def _magic(self):
if self._head.magic != POOL_MAGIC:
raise MemPoolInvalidException
def addr(self):
self._magic()
return self._head.addr
def space(self):
self._magic()
return self._head.top - self._head.addr
def alloc(self, size):
self._magic()
new_addr = self._head.addr + size
if new_addr >= self._head.top:
raise MemPoolAllocException()
self._head.addr = new_addr
return self._head.addr