Skip to content

Commit dc8f650

Browse files
committed
gh-130080: move _Py_EnsureArrayLargeEnough to a separate header so it can be used outside of the compiler
1 parent c4d37ee commit dc8f650

File tree

5 files changed

+53
-22
lines changed

5 files changed

+53
-22
lines changed

Include/internal/pycore_c_array.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef Py_INTERNAL_C_ARRAY_H
2+
#define Py_INTERNAL_C_ARRAY_H
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
#ifndef Py_BUILD_CORE
8+
# error "this header requires Py_BUILD_CORE define"
9+
#endif
10+
11+
12+
/* Utility for a number of growing arrays
13+
*
14+
* idx: index we need to write to
15+
* array: pointer to the array
16+
* alloc: pointer to array capacity
17+
* delta: array growth
18+
* item_size: size of each array entry
19+
*
20+
* If *array is NULL, allocate default_alloc entries.
21+
* Otherwise, grow the array by default_alloc entries.
22+
*
23+
* return 0 if successful and -1 (with exception set) otherwise.
24+
*/
25+
int _Py_EnsureArrayLargeEnough(
26+
int idx,
27+
void **array,
28+
int *alloc,
29+
int delta,
30+
size_t item_size);
31+
32+
33+
#ifdef __cplusplus
34+
}
35+
#endif
36+
#endif /* !Py_INTERNAL_C_ARRAY_H */

Include/internal/pycore_compile.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ extern "C" {
1111
#include <stdbool.h>
1212

1313
#include "pycore_ast.h" // mod_ty
14+
#include "pycore_c_array.h" // _Py_EnsureArrayLargeEnough()
1415
#include "pycore_symtable.h" // _Py_SourceLocation
1516
#include "pycore_instruction_sequence.h"
1617

@@ -174,14 +175,6 @@ int _PyCodegen_Expression(struct _PyCompiler *c, expr_ty e);
174175
int _PyCodegen_Body(struct _PyCompiler *c, _Py_SourceLocation loc, asdl_stmt_seq *stmts,
175176
bool is_interactive);
176177

177-
/* Utility for a number of growing arrays used in the compiler */
178-
int _PyCompile_EnsureArrayLargeEnough(
179-
int idx,
180-
void **array,
181-
int *alloc,
182-
int default_alloc,
183-
size_t item_size);
184-
185178
int _PyCompile_ConstCacheMergeOne(PyObject *const_cache, PyObject **obj);
186179

187180
PyCodeObject *_PyCompile_OptimizeAndAssemble(struct _PyCompiler *c, int addNone);

Python/codegen.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ static const int compare_masks[] = {
120120
*
121121
*/
122122
int
123-
_PyCompile_EnsureArrayLargeEnough(int idx, void **array, int *alloc,
124-
int default_alloc, size_t item_size)
123+
_Py_EnsureArrayLargeEnough(int idx, void **array, int *alloc,
124+
int default_alloc, size_t item_size)
125125
{
126126
void *arr = *array;
127127
if (arr == NULL) {

Python/flowgraph.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Python.h"
22
#include "opcode.h"
3+
#include "pycore_c_array.h" // _Py_EnsureArrayLargeEnough
34
#include "pycore_flowgraph.h"
45
#include "pycore_compile.h"
56
#include "pycore_intrinsics.h"
@@ -142,7 +143,7 @@ basicblock_next_instr(basicblock *b)
142143
{
143144
assert(b != NULL);
144145
RETURN_IF_ERROR(
145-
_PyCompile_EnsureArrayLargeEnough(
146+
_Py_EnsureArrayLargeEnough(
146147
b->b_iused + 1,
147148
(void**)&b->b_instr,
148149
&b->b_ialloc,

Python/instruction_sequence.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
#include "Python.h"
99

10-
#include "pycore_compile.h" // _PyCompile_EnsureArrayLargeEnough
10+
#include "pycore_c_array.h" // _Py_EnsureArrayLargeEnough
11+
#include "pycore_compile.h" // _PyInstruction
1112
#include "pycore_opcode_utils.h"
1213
#include "pycore_opcode_metadata.h" // OPCODE_HAS_ARG, etc
1314

@@ -37,11 +38,11 @@ instr_sequence_next_inst(instr_sequence *seq) {
3738
assert(seq->s_instrs != NULL || seq->s_used == 0);
3839

3940
RETURN_IF_ERROR(
40-
_PyCompile_EnsureArrayLargeEnough(seq->s_used + 1,
41-
(void**)&seq->s_instrs,
42-
&seq->s_allocated,
43-
INITIAL_INSTR_SEQUENCE_SIZE,
44-
sizeof(instruction)));
41+
_Py_EnsureArrayLargeEnough(seq->s_used + 1,
42+
(void**)&seq->s_instrs,
43+
&seq->s_allocated,
44+
INITIAL_INSTR_SEQUENCE_SIZE,
45+
sizeof(instruction)));
4546
assert(seq->s_allocated >= 0);
4647
assert(seq->s_used < seq->s_allocated);
4748
return seq->s_used++;
@@ -59,11 +60,11 @@ _PyInstructionSequence_UseLabel(instr_sequence *seq, int lbl)
5960
{
6061
int old_size = seq->s_labelmap_size;
6162
RETURN_IF_ERROR(
62-
_PyCompile_EnsureArrayLargeEnough(lbl,
63-
(void**)&seq->s_labelmap,
64-
&seq->s_labelmap_size,
65-
INITIAL_INSTR_SEQUENCE_LABELS_MAP_SIZE,
66-
sizeof(int)));
63+
_Py_EnsureArrayLargeEnough(lbl,
64+
(void**)&seq->s_labelmap,
65+
&seq->s_labelmap_size,
66+
INITIAL_INSTR_SEQUENCE_LABELS_MAP_SIZE,
67+
sizeof(int)));
6768

6869
for(int i = old_size; i < seq->s_labelmap_size; i++) {
6970
seq->s_labelmap[i] = -111; /* something weird, for debugging */

0 commit comments

Comments
 (0)