* Add memory management code

This commit is contained in:
Matthew Honnibal 2014-09-17 20:02:06 +02:00
parent 0152831c89
commit 5a20dfc03e
3 changed files with 52 additions and 3 deletions

View File

@ -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
View 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
View 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)