-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This builds up a basic char array object in C with some Python trappings for handling construction and cleanup of the object. Otherwise provides no interface for accessing this object in Python. Since it's purpose is merely to manage a block of memory requested by the user to be passed off and used by other objects that implement the buffer protocol, there is no need for it to have other functionality. It simply allocates memory from Python's memory allocator and frees it on cleanup. Implements the buffer protocol in Cython for this object. Thus allowing the memory allocated to be reused by NumPy arrays or other Python objects that support the buffer protocol.
- Loading branch information
Showing
3 changed files
with
207 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,93 @@ | ||
cimport cpython | ||
cimport cpython.buffer | ||
cimport cpython.bytes | ||
|
||
from cpython.buffer cimport ( | ||
PyObject_GetBuffer, | ||
PyBuffer_Release, | ||
PyBuffer_ToContiguous, | ||
) | ||
from cpython.bytes cimport ( | ||
PyBytes_FromStringAndSize, | ||
) | ||
|
||
cimport cython | ||
|
||
cimport pysharedmem | ||
|
||
include "version.pxi" | ||
|
||
|
||
cdef extern from "Python.h": | ||
IF PY_VERSION_HEX >= 0x03040000: | ||
void* PyMem_RawMalloc(size_t n) nogil | ||
void PyMem_RawFree(void *p) nogil | ||
ELSE: | ||
void* PyMem_RawMalloc "PyMem_Malloc" (size_t n) nogil | ||
void PyMem_RawFree "PyMem_Free" (void *p) nogil | ||
|
||
|
||
|
||
cdef class cbuffer: | ||
cdef char[::1] buf | ||
|
||
|
||
def __cinit__(self, Py_ssize_t length): | ||
self.buf = None | ||
|
||
if length <= 0: | ||
raise ValueError("length must be positive definite") | ||
|
||
cdef char* ptr = <char*>PyMem_RawMalloc(length * sizeof(char)) | ||
if not ptr: | ||
raise MemoryError("unable to allocate buffer") | ||
|
||
self.buf = <char[:length]>ptr | ||
|
||
|
||
def __getbuffer__(self, Py_buffer *buffer, int flags): | ||
buffer.buf = &self.buf[0] | ||
buffer.obj = self | ||
buffer.len = len(self.buf) | ||
buffer.readonly = 0 | ||
buffer.itemsize = self.buf.itemsize | ||
buffer.format = "c" | ||
buffer.ndim = self.buf.ndim | ||
buffer.shape = self.buf.shape | ||
buffer.strides = self.buf.strides | ||
buffer.suboffsets = NULL | ||
buffer.internal = NULL | ||
|
||
|
||
def __releasebuffer__(self, Py_buffer *buffer): | ||
pass | ||
|
||
|
||
def __reduce__(self): | ||
cdef bytes buf_bytes = PyBytes_FromStringAndSize( | ||
&self.buf[0], len(self.buf) | ||
) | ||
return (frombuffer, (buf_bytes,)) | ||
|
||
|
||
@cython.nonecheck(False) | ||
def __dealloc__(self): | ||
cdef char* ptr | ||
if self.buf is not None: | ||
ptr = &self.buf[0] | ||
self.buf = None | ||
PyMem_RawFree(<void*>ptr) | ||
|
||
|
||
def frombuffer(object src): | ||
cdef Py_buffer src_buf | ||
cdef cbuffer dest | ||
|
||
PyObject_GetBuffer(src, &src_buf, 0) | ||
|
||
dest = cbuffer(src_buf.len) | ||
PyBuffer_ToContiguous(<void*>&dest.buf[0], &src_buf, src_buf.len, 'C') | ||
|
||
PyBuffer_Release(&src_buf) | ||
|
||
return dest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters