summaryrefslogtreecommitdiffstats
path: root/Modules/gcmodule.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2014-05-02 20:31:14 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2014-05-02 20:31:14 (GMT)
commitdb067af12a5ebb889874e47d8177f9c4a3dc9a68 (patch)
tree87cea7c1fb4a0905ac2632c03520c43f377b3584 /Modules/gcmodule.c
parentd50c3f3f3af54f3be46d26d53a1c10b7c15a7b2d (diff)
downloadcpython-db067af12a5ebb889874e47d8177f9c4a3dc9a68.zip
cpython-db067af12a5ebb889874e47d8177f9c4a3dc9a68.tar.gz
cpython-db067af12a5ebb889874e47d8177f9c4a3dc9a68.tar.bz2
Issue #21233: Add new C functions: PyMem_RawCalloc(), PyMem_Calloc(),
PyObject_Calloc(), _PyObject_GC_Calloc(). bytes(int) and bytearray(int) are now using ``calloc()`` instead of ``malloc()`` for large objects which is faster and use less memory (until the bytearray buffer is filled with data).
Diffstat (limited to 'Modules/gcmodule.c')
-rw-r--r--Modules/gcmodule.c24
1 files changed, 20 insertions, 4 deletions
diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c
index 6281a7c..cff5d09 100644
--- a/Modules/gcmodule.c
+++ b/Modules/gcmodule.c
@@ -1703,15 +1703,19 @@ PyObject_GC_UnTrack(void *op)
_PyObject_GC_UNTRACK(op);
}
-PyObject *
-_PyObject_GC_Malloc(size_t basicsize)
+static PyObject *
+_PyObject_GC_Alloc(int use_calloc, size_t basicsize)
{
PyObject *op;
PyGC_Head *g;
+ size_t size;
if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head))
return PyErr_NoMemory();
- g = (PyGC_Head *)PyObject_MALLOC(
- sizeof(PyGC_Head) + basicsize);
+ size = sizeof(PyGC_Head) + basicsize;
+ if (use_calloc)
+ g = (PyGC_Head *)PyObject_Calloc(1, size);
+ else
+ g = (PyGC_Head *)PyObject_Malloc(size);
if (g == NULL)
return PyErr_NoMemory();
g->gc.gc_refs = 0;
@@ -1731,6 +1735,18 @@ _PyObject_GC_Malloc(size_t basicsize)
}
PyObject *
+_PyObject_GC_Malloc(size_t basicsize)
+{
+ return _PyObject_GC_Alloc(0, basicsize);
+}
+
+PyObject *
+_PyObject_GC_Calloc(size_t basicsize)
+{
+ return _PyObject_GC_Alloc(1, basicsize);
+}
+
+PyObject *
_PyObject_GC_New(PyTypeObject *tp)
{
PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));