Commit 3b609241 authored by Mark Florisson's avatar Mark Florisson

Do not deallocate cython.array buffer if user-allocated

parent e71755fd
...@@ -33,8 +33,9 @@ cdef class array: ...@@ -33,8 +33,9 @@ cdef class array:
Py_ssize_t itemsize Py_ssize_t itemsize
unicode mode unicode mode
bytes _format bytes _format
void (*callback_free_data)(char *data) void (*callback_free_data)(void *data)
# cdef object _memview # cdef object _memview
cdef bint free_data
def __cinit__(array self, tuple shape, Py_ssize_t itemsize, format, def __cinit__(array self, tuple shape, Py_ssize_t itemsize, format,
mode=u"c", bint allocate_buffer=True): mode=u"c", bint allocate_buffer=True):
...@@ -95,6 +96,7 @@ cdef class array: ...@@ -95,6 +96,7 @@ cdef class array:
mode = decode('ASCII') mode = decode('ASCII')
self.mode = mode self.mode = mode
self.free_data = allocate_buffer
if allocate_buffer: if allocate_buffer:
self.data = <char *>malloc(self.len) self.data = <char *>malloc(self.len)
if not self.data: if not self.data:
...@@ -127,7 +129,7 @@ cdef class array: ...@@ -127,7 +129,7 @@ cdef class array:
def __dealloc__(array self): def __dealloc__(array self):
if self.callback_free_data != NULL: if self.callback_free_data != NULL:
self.callback_free_data(self.data) self.callback_free_data(self.data)
else: elif self.free_data:
free(self.data) free(self.data)
self.data = NULL self.data = NULL
......
from libc.stdlib cimport malloc, free from libc.stdlib cimport malloc, free
cimport cython cimport cython
cdef void callback(char *data): cdef void callback(void *data):
print "callback called" print "callback called"
free(data)
def create_array(shape, mode, use_callback=False): def create_array(shape, mode, use_callback=False):
cdef cython.array result = cython.array(shape, itemsize=sizeof(int), cdef cython.array result = cython.array(shape, itemsize=sizeof(int),
......
...@@ -215,7 +215,7 @@ cdef extern from "bufaccess.h": ...@@ -215,7 +215,7 @@ cdef extern from "bufaccess.h":
ctypedef unsigned int td_h_ushort # Defined as unsigned short ctypedef unsigned int td_h_ushort # Defined as unsigned short
ctypedef td_h_short td_h_cy_short ctypedef td_h_short td_h_cy_short
cdef void dealloc_callback(char *data): cdef void dealloc_callback(void *data):
print "deallocating..." print "deallocating..."
def index(cython.array array): def index(cython.array array):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment