diff options
author | INADA Naoki <methane@users.noreply.github.com> | 2016-04-30 17:07:14 +0900 |
---|---|---|
committer | INADA Naoki <methane@users.noreply.github.com> | 2016-04-30 17:07:14 +0900 |
commit | 6b113a6fb37ffb969e92429b06aab9ea9b8eeb4a (patch) | |
tree | a7dbd289fb4845909a17a2679e594afd27f3485d | |
parent | 40ee322440a018c9e09634aa2c190d1747d7f0bd (diff) | |
download | msgpack-python-6b113a6fb37ffb969e92429b06aab9ea9b8eeb4a.tar.gz |
Use Python's memory API (#185)
-rw-r--r-- | msgpack/_packer.pyx | 7 | ||||
-rw-r--r-- | msgpack/_unpacker.pyx | 23 |
2 files changed, 21 insertions, 9 deletions
diff --git a/msgpack/_packer.pyx b/msgpack/_packer.pyx index e923895..d491cc1 100644 --- a/msgpack/_packer.pyx +++ b/msgpack/_packer.pyx @@ -88,7 +88,7 @@ cdef class Packer(object): def __cinit__(self): cdef int buf_size = 1024*1024 - self.pk.buf = <char*> malloc(buf_size); + self.pk.buf = <char*> PyMem_Malloc(buf_size) if self.pk.buf == NULL: raise MemoryError("Unable to allocate internal buffer.") self.pk.buf_size = buf_size @@ -97,8 +97,6 @@ cdef class Packer(object): def __init__(self, default=None, encoding='utf-8', unicode_errors='strict', use_single_float=False, bint autoreset=1, bint use_bin_type=0, bint strict_types=0): - """ - """ self.use_float = use_single_float self.strict_types = strict_types self.autoreset = autoreset @@ -123,7 +121,8 @@ cdef class Packer(object): self.unicode_errors = PyBytes_AsString(self._berrors) def __dealloc__(self): - free(self.pk.buf); + PyMem_Free(self.pk.buf) + self.pk.buf = NULL cdef int _pack(self, object o, int nest_limit=DEFAULT_RECURSE_LIMIT) except -1: cdef long long llval diff --git a/msgpack/_unpacker.pyx b/msgpack/_unpacker.pyx index 0443505..23f6478 100644 --- a/msgpack/_unpacker.pyx +++ b/msgpack/_unpacker.pyx @@ -1,7 +1,20 @@ # coding: utf-8 #cython: embedsignature=True -from cpython cimport * +from cpython.bytes cimport ( + PyBytes_AsString, + PyBytes_FromStringAndSize, + PyBytes_Size, +) +from cpython.buffer cimport ( + Py_buffer, + PyBuffer_Release, + PyObject_GetBuffer, + PyBUF_SIMPLE, +) +from cpython.mem cimport PyMem_Malloc, PyMem_Free +from cpython.object cimport PyCallable_Check + cdef extern from "Python.h": ctypedef struct PyObject cdef int PyObject_AsReadBuffer(object o, const void** buff, Py_ssize_t* buf_len) except -1 @@ -256,7 +269,7 @@ cdef class Unpacker(object): self.buf = NULL def __dealloc__(self): - free(self.buf) + PyMem_Free(self.buf) self.buf = NULL def __init__(self, file_like=None, Py_ssize_t read_size=0, bint use_list=1, @@ -289,7 +302,7 @@ cdef class Unpacker(object): read_size = min(max_buffer_size, 1024**2) self.max_buffer_size = max_buffer_size self.read_size = read_size - self.buf = <char*>malloc(read_size) + self.buf = <char*>PyMem_Malloc(read_size) if self.buf == NULL: raise MemoryError("Unable to allocate internal buffer.") self.buf_size = read_size @@ -352,13 +365,13 @@ cdef class Unpacker(object): if new_size > self.max_buffer_size: raise BufferFull new_size = min(new_size*2, self.max_buffer_size) - new_buf = <char*>malloc(new_size) + new_buf = <char*>PyMem_Malloc(new_size) if new_buf == NULL: # self.buf still holds old buffer and will be freed during # obj destruction raise MemoryError("Unable to enlarge internal buffer.") memcpy(new_buf, buf + head, tail - head) - free(buf) + PyMem_Free(buf) buf = new_buf buf_size = new_size |