From dc8f650026640160d55f2e5771ac72c23fc799fb Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Thu, 6 Mar 2025 21:33:36 +0000 Subject: [PATCH 1/5] gh-130080: move _Py_EnsureArrayLargeEnough to a separate header so it can be used outside of the compiler --- Include/internal/pycore_c_array.h | 36 +++++++++++++++++++++++++++++++ Include/internal/pycore_compile.h | 9 +------- Python/codegen.c | 4 ++-- Python/flowgraph.c | 3 ++- Python/instruction_sequence.c | 23 ++++++++++---------- 5 files changed, 53 insertions(+), 22 deletions(-) create mode 100644 Include/internal/pycore_c_array.h diff --git a/Include/internal/pycore_c_array.h b/Include/internal/pycore_c_array.h new file mode 100644 index 00000000000000..aa2d8a7f8dc530 --- /dev/null +++ b/Include/internal/pycore_c_array.h @@ -0,0 +1,36 @@ +#ifndef Py_INTERNAL_C_ARRAY_H +#define Py_INTERNAL_C_ARRAY_H +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef Py_BUILD_CORE +# error "this header requires Py_BUILD_CORE define" +#endif + + +/* Utility for a number of growing arrays + * + * idx: index we need to write to + * array: pointer to the array + * alloc: pointer to array capacity + * delta: array growth + * item_size: size of each array entry + * + * If *array is NULL, allocate default_alloc entries. + * Otherwise, grow the array by default_alloc entries. + * + * return 0 if successful and -1 (with exception set) otherwise. + */ +int _Py_EnsureArrayLargeEnough( + int idx, + void **array, + int *alloc, + int delta, + size_t item_size); + + +#ifdef __cplusplus +} +#endif +#endif /* !Py_INTERNAL_C_ARRAY_H */ diff --git a/Include/internal/pycore_compile.h b/Include/internal/pycore_compile.h index 9f0ca33892a43b..bbb64858cfa15a 100644 --- a/Include/internal/pycore_compile.h +++ b/Include/internal/pycore_compile.h @@ -11,6 +11,7 @@ extern "C" { #include #include "pycore_ast.h" // mod_ty +#include "pycore_c_array.h" // _Py_EnsureArrayLargeEnough() #include "pycore_symtable.h" // _Py_SourceLocation #include "pycore_instruction_sequence.h" @@ -174,14 +175,6 @@ int _PyCodegen_Expression(struct _PyCompiler *c, expr_ty e); int _PyCodegen_Body(struct _PyCompiler *c, _Py_SourceLocation loc, asdl_stmt_seq *stmts, bool is_interactive); -/* Utility for a number of growing arrays used in the compiler */ -int _PyCompile_EnsureArrayLargeEnough( - int idx, - void **array, - int *alloc, - int default_alloc, - size_t item_size); - int _PyCompile_ConstCacheMergeOne(PyObject *const_cache, PyObject **obj); PyCodeObject *_PyCompile_OptimizeAndAssemble(struct _PyCompiler *c, int addNone); diff --git a/Python/codegen.c b/Python/codegen.c index 8f1a2983007ce4..f50e38793a8709 100644 --- a/Python/codegen.c +++ b/Python/codegen.c @@ -120,8 +120,8 @@ static const int compare_masks[] = { * */ int -_PyCompile_EnsureArrayLargeEnough(int idx, void **array, int *alloc, - int default_alloc, size_t item_size) +_Py_EnsureArrayLargeEnough(int idx, void **array, int *alloc, + int default_alloc, size_t item_size) { void *arr = *array; if (arr == NULL) { diff --git a/Python/flowgraph.c b/Python/flowgraph.c index fb3c73a059a589..2a8258075f46b6 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -1,5 +1,6 @@ #include "Python.h" #include "opcode.h" +#include "pycore_c_array.h" // _Py_EnsureArrayLargeEnough #include "pycore_flowgraph.h" #include "pycore_compile.h" #include "pycore_intrinsics.h" @@ -142,7 +143,7 @@ basicblock_next_instr(basicblock *b) { assert(b != NULL); RETURN_IF_ERROR( - _PyCompile_EnsureArrayLargeEnough( + _Py_EnsureArrayLargeEnough( b->b_iused + 1, (void**)&b->b_instr, &b->b_ialloc, diff --git a/Python/instruction_sequence.c b/Python/instruction_sequence.c index ed40c06715f1f3..5361443bdb8958 100644 --- a/Python/instruction_sequence.c +++ b/Python/instruction_sequence.c @@ -7,7 +7,8 @@ #include "Python.h" -#include "pycore_compile.h" // _PyCompile_EnsureArrayLargeEnough +#include "pycore_c_array.h" // _Py_EnsureArrayLargeEnough +#include "pycore_compile.h" // _PyInstruction #include "pycore_opcode_utils.h" #include "pycore_opcode_metadata.h" // OPCODE_HAS_ARG, etc @@ -37,11 +38,11 @@ instr_sequence_next_inst(instr_sequence *seq) { assert(seq->s_instrs != NULL || seq->s_used == 0); RETURN_IF_ERROR( - _PyCompile_EnsureArrayLargeEnough(seq->s_used + 1, - (void**)&seq->s_instrs, - &seq->s_allocated, - INITIAL_INSTR_SEQUENCE_SIZE, - sizeof(instruction))); + _Py_EnsureArrayLargeEnough(seq->s_used + 1, + (void**)&seq->s_instrs, + &seq->s_allocated, + INITIAL_INSTR_SEQUENCE_SIZE, + sizeof(instruction))); assert(seq->s_allocated >= 0); assert(seq->s_used < seq->s_allocated); return seq->s_used++; @@ -59,11 +60,11 @@ _PyInstructionSequence_UseLabel(instr_sequence *seq, int lbl) { int old_size = seq->s_labelmap_size; RETURN_IF_ERROR( - _PyCompile_EnsureArrayLargeEnough(lbl, - (void**)&seq->s_labelmap, - &seq->s_labelmap_size, - INITIAL_INSTR_SEQUENCE_LABELS_MAP_SIZE, - sizeof(int))); + _Py_EnsureArrayLargeEnough(lbl, + (void**)&seq->s_labelmap, + &seq->s_labelmap_size, + INITIAL_INSTR_SEQUENCE_LABELS_MAP_SIZE, + sizeof(int))); for(int i = old_size; i < seq->s_labelmap_size; i++) { seq->s_labelmap[i] = -111; /* something weird, for debugging */ From 344888d645456635d732d6a38a6f59295d79f7bb Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 7 Mar 2025 11:26:06 +0000 Subject: [PATCH 2/5] implement review feedback and fix comment --- Include/internal/pycore_c_array.h | 16 +++++++++------- Include/internal/pycore_compile.h | 1 - Python/codegen.c | 10 +++++----- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Include/internal/pycore_c_array.h b/Include/internal/pycore_c_array.h index aa2d8a7f8dc530..4ee7595de70458 100644 --- a/Include/internal/pycore_c_array.h +++ b/Include/internal/pycore_c_array.h @@ -1,5 +1,6 @@ #ifndef Py_INTERNAL_C_ARRAY_H #define Py_INTERNAL_C_ARRAY_H + #ifdef __cplusplus extern "C" { #endif @@ -18,19 +19,20 @@ extern "C" { * item_size: size of each array entry * * If *array is NULL, allocate default_alloc entries. - * Otherwise, grow the array by default_alloc entries. + * Otherwise, double the array size if idx is out of range. * - * return 0 if successful and -1 (with exception set) otherwise. + * Return 0 if successful and -1 (with exception set) otherwise. */ int _Py_EnsureArrayLargeEnough( - int idx, - void **array, - int *alloc, - int delta, - size_t item_size); + int idx, + void **array, + int *alloc, + int initial_size, + size_t item_size); #ifdef __cplusplus } #endif + #endif /* !Py_INTERNAL_C_ARRAY_H */ diff --git a/Include/internal/pycore_compile.h b/Include/internal/pycore_compile.h index bbb64858cfa15a..4ed050ac7ce01a 100644 --- a/Include/internal/pycore_compile.h +++ b/Include/internal/pycore_compile.h @@ -11,7 +11,6 @@ extern "C" { #include #include "pycore_ast.h" // mod_ty -#include "pycore_c_array.h" // _Py_EnsureArrayLargeEnough() #include "pycore_symtable.h" // _Py_SourceLocation #include "pycore_instruction_sequence.h" diff --git a/Python/codegen.c b/Python/codegen.c index f50e38793a8709..f3a0c57477a2f5 100644 --- a/Python/codegen.c +++ b/Python/codegen.c @@ -115,19 +115,19 @@ static const int compare_masks[] = { * idx: the index we want to access * arr: pointer to the array * alloc: pointer to the capacity of the array - * default_alloc: initial number of items + * initial_size: initial number of items * item_size: size of each item * */ int _Py_EnsureArrayLargeEnough(int idx, void **array, int *alloc, - int default_alloc, size_t item_size) + int initial_size, size_t item_size) { void *arr = *array; if (arr == NULL) { - int new_alloc = default_alloc; + int new_alloc = initial_size; if (idx >= new_alloc) { - new_alloc = idx + default_alloc; + new_alloc = idx + initial_size; } arr = PyMem_Calloc(new_alloc, item_size); if (arr == NULL) { @@ -140,7 +140,7 @@ _Py_EnsureArrayLargeEnough(int idx, void **array, int *alloc, size_t oldsize = *alloc * item_size; int new_alloc = *alloc << 1; if (idx >= new_alloc) { - new_alloc = idx + default_alloc; + new_alloc = idx + initial_size; } size_t newsize = new_alloc * item_size; From 469a42abc8ff59d97e77c5204365ea53d80790f3 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 7 Mar 2025 23:54:41 +0000 Subject: [PATCH 3/5] add _Py_c_array_t struct --- Include/internal/pycore_c_array.h | 29 ++++++++++------------ Python/codegen.c | 41 +++++++++++++------------------ Python/flowgraph.c | 17 +++++++------ Python/instruction_sequence.c | 32 ++++++++++++++---------- 4 files changed, 58 insertions(+), 61 deletions(-) diff --git a/Include/internal/pycore_c_array.h b/Include/internal/pycore_c_array.h index 4ee7595de70458..11baedce03bb79 100644 --- a/Include/internal/pycore_c_array.h +++ b/Include/internal/pycore_c_array.h @@ -10,25 +10,22 @@ extern "C" { #endif -/* Utility for a number of growing arrays - * - * idx: index we need to write to - * array: pointer to the array - * alloc: pointer to array capacity - * delta: array growth - * item_size: size of each array entry - * - * If *array is NULL, allocate default_alloc entries. - * Otherwise, double the array size if idx is out of range. +/* Utility for a number of growing arrays */ + +typedef struct { + void **array; /* pointer to the array */ + int *allocated_entries; /* pointer to the capacity of the array */ + size_t item_size; /* size of each element */ + int initial_num_entries; /* initial allocation size */ +} _Py_c_array_t; + +/* If idx is out of bouds: + * If arr->array is NULL, allocate arr->initial_num_entries slots. + * Otherwise, double its size. * * Return 0 if successful and -1 (with exception set) otherwise. */ -int _Py_EnsureArrayLargeEnough( - int idx, - void **array, - int *alloc, - int initial_size, - size_t item_size); +int _Py_c_array_EnsureCapacity(_Py_c_array_t *c_array, int idx); #ifdef __cplusplus diff --git a/Python/codegen.c b/Python/codegen.c index f3a0c57477a2f5..7225e4f49209b2 100644 --- a/Python/codegen.c +++ b/Python/codegen.c @@ -18,6 +18,7 @@ #define NEED_OPCODE_TABLES #include "pycore_opcode_utils.h" #undef NEED_OPCODE_TABLES +#include "pycore_c_array.h" // _Py_c_array_t #include "pycore_compile.h" #include "pycore_instruction_sequence.h" // _PyInstructionSequence_NewLabel() #include "pycore_intrinsics.h" @@ -109,40 +110,31 @@ static const int compare_masks[] = { [Py_GE] = COMPARISON_GREATER_THAN | COMPARISON_EQUALS, }; -/* - * Resize the array if index is out of range. - * - * idx: the index we want to access - * arr: pointer to the array - * alloc: pointer to the capacity of the array - * initial_size: initial number of items - * item_size: size of each item - * - */ + int -_Py_EnsureArrayLargeEnough(int idx, void **array, int *alloc, - int initial_size, size_t item_size) +_Py_c_array_EnsureCapacity(_Py_c_array_t *c_array, int idx) { - void *arr = *array; + void *arr = *c_array->array; + int alloc = *c_array->allocated_entries; if (arr == NULL) { - int new_alloc = initial_size; + int new_alloc = c_array->initial_num_entries; if (idx >= new_alloc) { - new_alloc = idx + initial_size; + new_alloc = idx + c_array->initial_num_entries; } - arr = PyMem_Calloc(new_alloc, item_size); + arr = PyMem_Calloc(new_alloc, c_array->item_size); if (arr == NULL) { PyErr_NoMemory(); return ERROR; } - *alloc = new_alloc; + alloc = new_alloc; } - else if (idx >= *alloc) { - size_t oldsize = *alloc * item_size; - int new_alloc = *alloc << 1; + else if (idx >= alloc) { + size_t oldsize = alloc * c_array->item_size; + int new_alloc = alloc << 1; if (idx >= new_alloc) { - new_alloc = idx + initial_size; + new_alloc = idx + c_array->initial_num_entries; } - size_t newsize = new_alloc * item_size; + size_t newsize = new_alloc * c_array->item_size; if (oldsize > (SIZE_MAX >> 1)) { PyErr_NoMemory(); @@ -155,12 +147,13 @@ _Py_EnsureArrayLargeEnough(int idx, void **array, int *alloc, PyErr_NoMemory(); return ERROR; } - *alloc = new_alloc; + alloc = new_alloc; arr = tmp; memset((char *)arr + oldsize, 0, newsize - oldsize); } - *array = arr; + *c_array->array = arr; + *c_array->allocated_entries = alloc; return SUCCESS; } diff --git a/Python/flowgraph.c b/Python/flowgraph.c index 2a8258075f46b6..d274abf576a41a 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -1,6 +1,6 @@ #include "Python.h" #include "opcode.h" -#include "pycore_c_array.h" // _Py_EnsureArrayLargeEnough +#include "pycore_c_array.h" // _Py_c_array_EnsureCapacity #include "pycore_flowgraph.h" #include "pycore_compile.h" #include "pycore_intrinsics.h" @@ -142,13 +142,14 @@ static int basicblock_next_instr(basicblock *b) { assert(b != NULL); - RETURN_IF_ERROR( - _Py_EnsureArrayLargeEnough( - b->b_iused + 1, - (void**)&b->b_instr, - &b->b_ialloc, - DEFAULT_BLOCK_SIZE, - sizeof(cfg_instr))); + _Py_c_array_t array = { + .array = (void**)&b->b_instr, + .allocated_entries = &b->b_ialloc, + .item_size = sizeof(cfg_instr), + .initial_num_entries = DEFAULT_BLOCK_SIZE, + }; + + RETURN_IF_ERROR(_Py_c_array_EnsureCapacity(&array, b->b_iused + 1)); return b->b_iused++; } diff --git a/Python/instruction_sequence.c b/Python/instruction_sequence.c index 5361443bdb8958..b68f21e1673661 100644 --- a/Python/instruction_sequence.c +++ b/Python/instruction_sequence.c @@ -7,7 +7,7 @@ #include "Python.h" -#include "pycore_c_array.h" // _Py_EnsureArrayLargeEnough +#include "pycore_c_array.h" // _Py_c_array_EnsureCapacity #include "pycore_compile.h" // _PyInstruction #include "pycore_opcode_utils.h" #include "pycore_opcode_metadata.h" // OPCODE_HAS_ARG, etc @@ -37,12 +37,16 @@ static int instr_sequence_next_inst(instr_sequence *seq) { assert(seq->s_instrs != NULL || seq->s_used == 0); - RETURN_IF_ERROR( - _Py_EnsureArrayLargeEnough(seq->s_used + 1, - (void**)&seq->s_instrs, - &seq->s_allocated, - INITIAL_INSTR_SEQUENCE_SIZE, - sizeof(instruction))); + + _Py_c_array_t array = { + .array = (void**)&seq->s_instrs, + .allocated_entries = &seq->s_allocated, + .item_size = sizeof(instruction), + .initial_num_entries = INITIAL_INSTR_SEQUENCE_SIZE, + }; + + RETURN_IF_ERROR(_Py_c_array_EnsureCapacity(&array, seq->s_used + 1)); + assert(seq->s_allocated >= 0); assert(seq->s_used < seq->s_allocated); return seq->s_used++; @@ -59,12 +63,14 @@ int _PyInstructionSequence_UseLabel(instr_sequence *seq, int lbl) { int old_size = seq->s_labelmap_size; - RETURN_IF_ERROR( - _Py_EnsureArrayLargeEnough(lbl, - (void**)&seq->s_labelmap, - &seq->s_labelmap_size, - INITIAL_INSTR_SEQUENCE_LABELS_MAP_SIZE, - sizeof(int))); + _Py_c_array_t array = { + .array = (void**)&seq->s_labelmap, + .allocated_entries = &seq->s_labelmap_size, + .item_size = sizeof(int), + .initial_num_entries = INITIAL_INSTR_SEQUENCE_LABELS_MAP_SIZE, + }; + + RETURN_IF_ERROR(_Py_c_array_EnsureCapacity(&array, lbl)); for(int i = old_size; i < seq->s_labelmap_size; i++) { seq->s_labelmap[i] = -111; /* something weird, for debugging */ From 122dbf15e985f1c7faec65f29ce4edaf2e5b5d73 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Mon, 10 Mar 2025 17:24:03 +0000 Subject: [PATCH 4/5] rename function --- Include/internal/pycore_c_array.h | 4 ++-- Python/codegen.c | 2 +- Python/flowgraph.c | 4 ++-- Python/instruction_sequence.c | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Include/internal/pycore_c_array.h b/Include/internal/pycore_c_array.h index 11baedce03bb79..5632fb4e573e90 100644 --- a/Include/internal/pycore_c_array.h +++ b/Include/internal/pycore_c_array.h @@ -19,13 +19,13 @@ typedef struct { int initial_num_entries; /* initial allocation size */ } _Py_c_array_t; -/* If idx is out of bouds: +/* If idx is out of bounds: * If arr->array is NULL, allocate arr->initial_num_entries slots. * Otherwise, double its size. * * Return 0 if successful and -1 (with exception set) otherwise. */ -int _Py_c_array_EnsureCapacity(_Py_c_array_t *c_array, int idx); +int _Py_CArray_EnsureCapacity(_Py_c_array_t *c_array, int idx); #ifdef __cplusplus diff --git a/Python/codegen.c b/Python/codegen.c index a561991d3619a3..a95f38b4f7f099 100644 --- a/Python/codegen.c +++ b/Python/codegen.c @@ -112,7 +112,7 @@ static const int compare_masks[] = { int -_Py_c_array_EnsureCapacity(_Py_c_array_t *c_array, int idx) +_Py_CArray_EnsureCapacity(_Py_c_array_t *c_array, int idx) { void *arr = *c_array->array; int alloc = *c_array->allocated_entries; diff --git a/Python/flowgraph.c b/Python/flowgraph.c index e24e8c81d8f116..29c8cea387d019 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -1,6 +1,6 @@ #include "Python.h" #include "opcode.h" -#include "pycore_c_array.h" // _Py_c_array_EnsureCapacity +#include "pycore_c_array.h" // _Py_CArray_EnsureCapacity #include "pycore_flowgraph.h" #include "pycore_compile.h" #include "pycore_intrinsics.h" @@ -149,7 +149,7 @@ basicblock_next_instr(basicblock *b) .initial_num_entries = DEFAULT_BLOCK_SIZE, }; - RETURN_IF_ERROR(_Py_c_array_EnsureCapacity(&array, b->b_iused + 1)); + RETURN_IF_ERROR(_Py_CArray_EnsureCapacity(&array, b->b_iused + 1)); return b->b_iused++; } diff --git a/Python/instruction_sequence.c b/Python/instruction_sequence.c index b68f21e1673661..0a3c0de3af575f 100644 --- a/Python/instruction_sequence.c +++ b/Python/instruction_sequence.c @@ -7,7 +7,7 @@ #include "Python.h" -#include "pycore_c_array.h" // _Py_c_array_EnsureCapacity +#include "pycore_c_array.h" // _Py_CArray_EnsureCapacity #include "pycore_compile.h" // _PyInstruction #include "pycore_opcode_utils.h" #include "pycore_opcode_metadata.h" // OPCODE_HAS_ARG, etc @@ -45,7 +45,7 @@ instr_sequence_next_inst(instr_sequence *seq) { .initial_num_entries = INITIAL_INSTR_SEQUENCE_SIZE, }; - RETURN_IF_ERROR(_Py_c_array_EnsureCapacity(&array, seq->s_used + 1)); + RETURN_IF_ERROR(_Py_CArray_EnsureCapacity(&array, seq->s_used + 1)); assert(seq->s_allocated >= 0); assert(seq->s_used < seq->s_allocated); @@ -70,7 +70,7 @@ _PyInstructionSequence_UseLabel(instr_sequence *seq, int lbl) .initial_num_entries = INITIAL_INSTR_SEQUENCE_LABELS_MAP_SIZE, }; - RETURN_IF_ERROR(_Py_c_array_EnsureCapacity(&array, lbl)); + RETURN_IF_ERROR(_Py_CArray_EnsureCapacity(&array, lbl)); for(int i = old_size; i < seq->s_labelmap_size; i++) { seq->s_labelmap[i] = -111; /* something weird, for debugging */ From 93d018cbbaf89fac18d57e942ce3240d58fff205 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Mon, 10 Mar 2025 23:25:43 +0000 Subject: [PATCH 5/5] tidy up API --- Include/internal/pycore_c_array.h | 8 ++++++-- Python/codegen.c | 25 +++++++++++++++++++++---- Python/flowgraph.c | 6 ++++-- Python/instruction_sequence.c | 12 ++++++++---- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/Include/internal/pycore_c_array.h b/Include/internal/pycore_c_array.h index 5632fb4e573e90..7a10fc866c88f1 100644 --- a/Include/internal/pycore_c_array.h +++ b/Include/internal/pycore_c_array.h @@ -13,12 +13,16 @@ extern "C" { /* Utility for a number of growing arrays */ typedef struct { - void **array; /* pointer to the array */ - int *allocated_entries; /* pointer to the capacity of the array */ + void *array; /* pointer to the array */ + int allocated_entries; /* pointer to the capacity of the array */ size_t item_size; /* size of each element */ int initial_num_entries; /* initial allocation size */ } _Py_c_array_t; + +int _Py_CArray_Init(_Py_c_array_t* array, int item_size, int initial_num_entries); +void _Py_CArray_Fini(_Py_c_array_t* array); + /* If idx is out of bounds: * If arr->array is NULL, allocate arr->initial_num_entries slots. * Otherwise, double its size. diff --git a/Python/codegen.c b/Python/codegen.c index a95f38b4f7f099..76d9f8e8f6172d 100644 --- a/Python/codegen.c +++ b/Python/codegen.c @@ -111,11 +111,28 @@ static const int compare_masks[] = { }; +int +_Py_CArray_Init(_Py_c_array_t* array, int item_size, int initial_num_entries) { + memset(array, 0, sizeof(_Py_c_array_t)); + array->item_size = item_size; + array->initial_num_entries = initial_num_entries; + return 0; +} + +void +_Py_CArray_Fini(_Py_c_array_t* array) +{ + if (array->array) { + PyMem_Free(array->array); + array->allocated_entries = 0; + } +} + int _Py_CArray_EnsureCapacity(_Py_c_array_t *c_array, int idx) { - void *arr = *c_array->array; - int alloc = *c_array->allocated_entries; + void *arr = c_array->array; + int alloc = c_array->allocated_entries; if (arr == NULL) { int new_alloc = c_array->initial_num_entries; if (idx >= new_alloc) { @@ -152,8 +169,8 @@ _Py_CArray_EnsureCapacity(_Py_c_array_t *c_array, int idx) memset((char *)arr + oldsize, 0, newsize - oldsize); } - *c_array->array = arr; - *c_array->allocated_entries = alloc; + c_array->array = arr; + c_array->allocated_entries = alloc; return SUCCESS; } diff --git a/Python/flowgraph.c b/Python/flowgraph.c index 29c8cea387d019..44a09c99bf6c53 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -143,13 +143,15 @@ basicblock_next_instr(basicblock *b) { assert(b != NULL); _Py_c_array_t array = { - .array = (void**)&b->b_instr, - .allocated_entries = &b->b_ialloc, + .array = (void*)b->b_instr, + .allocated_entries = b->b_ialloc, .item_size = sizeof(cfg_instr), .initial_num_entries = DEFAULT_BLOCK_SIZE, }; RETURN_IF_ERROR(_Py_CArray_EnsureCapacity(&array, b->b_iused + 1)); + b->b_instr = array.array; + b->b_ialloc = array.allocated_entries; return b->b_iused++; } diff --git a/Python/instruction_sequence.c b/Python/instruction_sequence.c index 0a3c0de3af575f..4ca85eec345d38 100644 --- a/Python/instruction_sequence.c +++ b/Python/instruction_sequence.c @@ -39,13 +39,15 @@ instr_sequence_next_inst(instr_sequence *seq) { _Py_c_array_t array = { - .array = (void**)&seq->s_instrs, - .allocated_entries = &seq->s_allocated, + .array = (void*)seq->s_instrs, + .allocated_entries = seq->s_allocated, .item_size = sizeof(instruction), .initial_num_entries = INITIAL_INSTR_SEQUENCE_SIZE, }; RETURN_IF_ERROR(_Py_CArray_EnsureCapacity(&array, seq->s_used + 1)); + seq->s_instrs = array.array; + seq->s_allocated = array.allocated_entries; assert(seq->s_allocated >= 0); assert(seq->s_used < seq->s_allocated); @@ -64,13 +66,15 @@ _PyInstructionSequence_UseLabel(instr_sequence *seq, int lbl) { int old_size = seq->s_labelmap_size; _Py_c_array_t array = { - .array = (void**)&seq->s_labelmap, - .allocated_entries = &seq->s_labelmap_size, + .array = (void*)seq->s_labelmap, + .allocated_entries = seq->s_labelmap_size, .item_size = sizeof(int), .initial_num_entries = INITIAL_INSTR_SEQUENCE_LABELS_MAP_SIZE, }; RETURN_IF_ERROR(_Py_CArray_EnsureCapacity(&array, lbl)); + seq->s_labelmap = array.array; + seq->s_labelmap_size = array.allocated_entries; for(int i = old_size; i < seq->s_labelmap_size; i++) { seq->s_labelmap[i] = -111; /* something weird, for debugging */