|
1 |
| - |
2 | 1 | /* Complex object implementation */
|
3 | 2 |
|
4 | 3 | /* Borrows heavily from floatobject.c */
|
|
9 | 8 | #include "pycore_call.h" // _PyObject_CallNoArgs()
|
10 | 9 | #include "pycore_complexobject.h" // _PyComplex_FormatAdvancedWriter()
|
11 | 10 | #include "pycore_floatobject.h" // _Py_convert_int_to_double()
|
| 11 | +#include "pycore_freelist.h" // _Py_FREELIST_FREE(), _Py_FREELIST_POP() |
12 | 12 | #include "pycore_long.h" // _PyLong_GetZero()
|
13 | 13 | #include "pycore_object.h" // _PyObject_Init()
|
14 | 14 | #include "pycore_pymath.h" // _Py_ADJUST_ERANGE2()
|
@@ -410,16 +410,32 @@ complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval)
|
410 | 410 | PyObject *
|
411 | 411 | PyComplex_FromCComplex(Py_complex cval)
|
412 | 412 | {
|
413 |
| - /* Inline PyObject_New */ |
414 |
| - PyComplexObject *op = PyObject_Malloc(sizeof(PyComplexObject)); |
| 413 | + PyComplexObject *op = _Py_FREELIST_POP(PyComplexObject, complexes); |
| 414 | + |
415 | 415 | if (op == NULL) {
|
416 |
| - return PyErr_NoMemory(); |
| 416 | + /* Inline PyObject_New */ |
| 417 | + op = PyObject_Malloc(sizeof(PyComplexObject)); |
| 418 | + if (op == NULL) { |
| 419 | + return PyErr_NoMemory(); |
| 420 | + } |
| 421 | + _PyObject_Init((PyObject*)op, &PyComplex_Type); |
417 | 422 | }
|
418 |
| - _PyObject_Init((PyObject*)op, &PyComplex_Type); |
419 | 423 | op->cval = cval;
|
420 | 424 | return (PyObject *) op;
|
421 | 425 | }
|
422 | 426 |
|
| 427 | +static void |
| 428 | +complex_dealloc(PyObject *op) |
| 429 | +{ |
| 430 | + assert(PyComplex_Check(op)); |
| 431 | + if (PyComplex_CheckExact(op)) { |
| 432 | + _Py_FREELIST_FREE(complexes, op, PyObject_Free); |
| 433 | + } |
| 434 | + else { |
| 435 | + Py_TYPE(op)->tp_free(op); |
| 436 | + } |
| 437 | +} |
| 438 | + |
423 | 439 | static PyObject *
|
424 | 440 | complex_subtype_from_doubles(PyTypeObject *type, double real, double imag)
|
425 | 441 | {
|
@@ -1383,7 +1399,7 @@ PyTypeObject PyComplex_Type = {
|
1383 | 1399 | "complex",
|
1384 | 1400 | sizeof(PyComplexObject),
|
1385 | 1401 | 0,
|
1386 |
| - 0, /* tp_dealloc */ |
| 1402 | + complex_dealloc, /* tp_dealloc */ |
1387 | 1403 | 0, /* tp_vectorcall_offset */
|
1388 | 1404 | 0, /* tp_getattr */
|
1389 | 1405 | 0, /* tp_setattr */
|
|
0 commit comments