mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-02-06 09:50:01 +00:00
Use Python's memory API (#185)
This commit is contained in:
parent
40ee322440
commit
6b113a6fb3
2 changed files with 21 additions and 9 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue