Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address code review
  • Loading branch information
corona10 committed Mar 9, 2024
commit 2252858efdd7c90b81d4b2ab8bb359530305eb0a
7 changes: 0 additions & 7 deletions Include/cpython/listobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ typedef struct {
Py_ssize_t allocated;
} PyListObject;

#ifdef Py_GIL_DISABLED
typedef struct {
Py_ssize_t allocated;
PyObject *ob_item[1];
} _PyListArray;
#endif

/* Cast argument to PyListObject* type. */
#define _PyList_CAST(op) \
(assert(PyList_Check(op)), _Py_CAST(PyListObject*, (op)))
Expand Down
26 changes: 10 additions & 16 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ get_list_freelist(void)
#endif

#ifdef Py_GIL_DISABLED
typedef struct {
Py_ssize_t allocated;
PyObject *ob_item[1];
} _PyListArray;

static _PyListArray *
list_allocate_array(size_t capacity)
{
if (capacity > PY_SSIZE_T_MAX/sizeof(PyObject*) - 1) {
return NULL;
}
_PyListArray *array = PyMem_Malloc(sizeof(_PyListArray) + (capacity - 1) * sizeof(PyObject *));
_PyListArray *array = PyMem_Malloc(sizeof(_PyListArray) + (capacity-1) * sizeof(PyObject *));
if (array == NULL) {
return NULL;
}
Expand All @@ -49,20 +54,16 @@ list_allocate_array(size_t capacity)
static Py_ssize_t
list_capacity(PyObject **items)
{
char *mem = (char *)items;
mem -= offsetof(_PyListArray, ob_item);
_PyListArray *array = (_PyListArray *)mem;
return _Py_atomic_load_ssize_relaxed(&array->allocated);
_PyListArray *array = _Py_CONTAINER_OF(items, _PyListArray, ob_item);
return array->allocated;
}
#endif

static void
free_list_items(PyObject** items, bool use_qsbr)
{
#ifdef Py_GIL_DISABLED
char *mem = (char *)items;
mem -= offsetof(_PyListArray, ob_item);
_PyListArray *array = (_PyListArray *)mem;
_PyListArray *array = _Py_CONTAINER_OF(items, _PyListArray, ob_item);
if (use_qsbr) {
_PyMem_FreeDelayed(array);
}
Expand Down Expand Up @@ -124,14 +125,7 @@ list_resize(PyListObject *self, Py_ssize_t newsize)
new_allocated = 0;

#ifdef Py_GIL_DISABLED
_PyListArray *array = NULL;
if (new_allocated <= (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
array = list_allocate_array(new_allocated);
}
else {
// integer overflow
array = NULL;
}
_PyListArray *array = list_allocate_array(new_allocated);
if (array == NULL) {
PyErr_NoMemory();
return -1;
Expand Down