_PyLong_New(Py_ssize_t size)
{
assert(size >= 0);
- PyLongObject *result;
+ PyLongObject *result = NULL;
if (size > (Py_ssize_t)MAX_LONG_DIGITS) {
PyErr_SetString(PyExc_OverflowError,
"too many digits in integer");
/* Fast operations for single digit integers (including zero)
* assume that there is always at least one digit present. */
Py_ssize_t ndigits = size ? size : 1;
- /* Number of bytes needed is: offsetof(PyLongObject, ob_digit) +
- sizeof(digit)*size. Previous incarnations of this code used
- sizeof() instead of the offsetof, but this risks being
- incorrect in the presence of padding between the header
- and the digits. */
- result = PyObject_Malloc(offsetof(PyLongObject, long_value.ob_digit) +
- ndigits*sizeof(digit));
- if (!result) {
- PyErr_NoMemory();
- return NULL;
+
+ if (ndigits == 1) {
+ result = (PyLongObject *)_Py_FREELIST_POP(PyLongObject, ints);
+ }
+ if (result == NULL) {
+ /* Number of bytes needed is: offsetof(PyLongObject, ob_digit) +
+ sizeof(digit)*size. Previous incarnations of this code used
+ sizeof() instead of the offsetof, but this risks being
+ incorrect in the presence of padding between the header
+ and the digits. */
+ result = PyObject_Malloc(offsetof(PyLongObject, long_value.ob_digit) +
+ ndigits*sizeof(digit));
+ if (!result) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+ _PyObject_Init((PyObject*)result, &PyLong_Type);
}
_PyLong_SetSignAndDigitCount(result, size != 0, size);
- _PyObject_Init((PyObject*)result, &PyLong_Type);
/* The digit has to be initialized explicitly to avoid
* use-of-uninitialized-value. */
result->long_value.ob_digit[0] = 0;