8000 Use Python's memory API (#185) · Triadai/msgpack-python@6b113a6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6b113a6

Browse files
committed
Use Python's memory API (msgpack#185)
1 parent 40ee322 commit 6b113a6

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

msgpack/_packer.pyx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ cdef class Packer(object):
8888

8989
def __cinit__(self):
9090
cdef int buf_size = 1024*1024
91-
self.pk.buf = <char*> malloc(buf_size);
91+
self.pk.buf = <char*> PyMem_Malloc(buf_size)
9292
if self.pk.buf == NULL:
9393
raise MemoryError("Unable to allocate internal buffer.")
9494
self.pk.buf_size = buf_size
@@ -97,8 +97,6 @@ cdef class Packer(object):
9797
def __init__(self, default=None, encoding='utf-8', unicode_errors='strict',
9898
use_single_float=False, bint autoreset=1, bint use_bin_type=0,
9999
bint strict_types=0):
100-
"""
101-
"""
102100
self.use_float = use_single_float
103101
self.strict_types = strict_types
104102
self.autoreset = autoreset
@@ -123,7 +121,8 @@ cdef class Packer(object):
123121
self.unicode_errors = PyBytes_AsString(self._berrors)
124122

125123
def __dealloc__(self):
126-
free(self.pk.buf);
124+
PyMem_Free(self.pk.buf)
125+
self.pk.buf = NULL
127126

128127
cdef int _pack(self, object o, int nest_limit=DEFAULT_RECURSE_LIMIT) except -1:
129128
cdef long long llval

msgpack/_unpacker.pyx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
# coding: utf-8
22
#cython: embedsignature=True
33

4-
from cpython cimport *
4+
from cpython.bytes cimport (
5+
PyBytes_AsString,
6+
PyBytes_FromStringAndSize,
7+
PyBytes_Size,
8+
)
9+
from cpython.buffer cimport (
10+
Py_buffer,
11+
PyBuffer_Release,
12+
PyObject_GetBuffer,
13+
PyBUF_SIMPLE,
14+
)
15+
from cpython.mem cimport PyMem_Malloc, PyMem_Free
16+
from cpython.object cimport PyCallable_Check
17+
518
cdef extern from "Python.h":
619
ctypedef struct PyObject
720
cdef int PyObject_AsReadBuffer(object o, const void** buff, Py_ssize_t* buf_len) except -1
@@ -256,7 +269,7 @@ cdef class Unpacker(object):
256269
self.buf = NULL
257270

258271
def __dealloc__(self):
259-
free(self.buf)
272+
PyMem_Free(self.buf)
260273
self.buf = NULL
261274

262275
def __init__(self, file_like=None, Py_ssize_t read_size=0, bint use_list=1,
@@ -289,7 +302,7 @@ cdef class Unpacker(object):
289302
read_size = min(max_buffer_size, 1024**2)
290303
self.max_buffer_size = max_buffer_size
291304
self.read_size = read_size
292-
self.buf = <char*>malloc(read_size)
305+
self.buf = <char*>PyMem_Malloc(read_size)
293306
if self.buf == NULL:
294307
raise MemoryError("Unable to allocate internal buffer.")
295308
self.buf_size = read_size
@@ -352,13 +365,13 @@ cdef class Unpacker(object):
352365
if new_size > self.max_buffer_size:
353366
raise BufferFull
354367
new_size = min(new_size*2, self.max_buffer_size)
355-
new_buf = <char*>malloc(new_size)
368+
new_buf = <char*>PyMem_Malloc(new_size)
356369
if new_buf == NULL:
357370
# self.buf still holds old buffer and will be freed during
358371
# obj destruction
359372
raise MemoryError("Unable to enlarge internal buffer.")
360373
memcpy(new_buf, buf + head, tail - head)
361-
free(buf)
374+
PyMem_Free(buf)
362375

363376
buf = new_buf
364377
buf_size = new_size

0 commit comments

Comments
 (0)
0