mirror of
https://github.com/explosion/spaCy.git
synced 2024-11-10 19:57:17 +03:00
* Add memory management code
This commit is contained in:
parent
0152831c89
commit
5a20dfc03e
5
setup.py
5
setup.py
|
@ -56,9 +56,8 @@ exts = [
|
|||
include_dirs=includes),
|
||||
Extension("spacy.tokens", ["spacy/tokens.pyx"], language="c++",
|
||||
include_dirs=includes),
|
||||
|
||||
|
||||
|
||||
Extension("spacy.memory", ["spacy/memory.pyx"], language="c++",
|
||||
include_dirs=includes)
|
||||
]
|
||||
|
||||
|
||||
|
|
11
spacy/memory.pxd
Normal file
11
spacy/memory.pxd
Normal file
|
@ -0,0 +1,11 @@
|
|||
from libcpp.vector cimport vector
|
||||
|
||||
cdef class Pool:
|
||||
cdef vector[void*] _addresses
|
||||
|
||||
cdef void* alloc(self, size_t number, size_t size) except NULL
|
||||
cdef void* realloc(self, void* addr, size_t n) except NULL
|
||||
|
||||
|
||||
cdef class Address:
|
||||
cdef size_t addr
|
39
spacy/memory.pyx
Normal file
39
spacy/memory.pyx
Normal file
|
@ -0,0 +1,39 @@
|
|||
from cpython.mem cimport PyMem_Malloc, PyMem_Realloc, PyMem_Free
|
||||
from libc.string cimport memset
|
||||
|
||||
from libcpp.vector cimport vector
|
||||
|
||||
|
||||
cdef class Pool:
|
||||
def __cinit__(self):
|
||||
pass
|
||||
|
||||
def __dealloc__(self):
|
||||
cdef void* addr
|
||||
for addr in self._addresses:
|
||||
PyMem_Free(<void*>addr)
|
||||
|
||||
cdef void* alloc(self, size_t number, size_t size) except NULL:
|
||||
cdef void* addr = PyMem_Malloc(number * size)
|
||||
memset(addr, 0, number * size)
|
||||
self._addresses.push_back(addr)
|
||||
return addr
|
||||
|
||||
cdef void* realloc(self, void* addr, size_t n) except NULL:
|
||||
cdef size_t i
|
||||
for i in range(self.size()):
|
||||
if self._addresses[i] == addr:
|
||||
self._addresses[i] = PyMem_Realloc(addr, n)
|
||||
return self._addresses[i]
|
||||
else:
|
||||
raise MemoryError("Address to realloc not found in pool")
|
||||
|
||||
|
||||
cdef class Address:
|
||||
def __cinit__(self, size_t number, size_t size):
|
||||
cdef void* addr = PyMem_Malloc(number * size)
|
||||
memset(addr, 0, number * size)
|
||||
self.addr = <size_t>addr
|
||||
|
||||
def __dealloc__(self):
|
||||
PyMem_Free(<void*>self.addr)
|
Loading…
Reference in New Issue
Block a user