Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
38 changes: 38 additions & 0 deletions Include/internal/pycore_c_array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#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, double the array size if idx is out of range.
*
* 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);


#ifdef __cplusplus
}
#endif

#endif /* !Py_INTERNAL_C_ARRAY_H */
8 changes: 0 additions & 8 deletions Include/internal/pycore_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,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);
Expand Down
12 changes: 6 additions & 6 deletions Python/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
_PyCompile_EnsureArrayLargeEnough(int idx, void **array, int *alloc,
int default_alloc, size_t item_size)
_Py_EnsureArrayLargeEnough(int idx, void **array, int *alloc,
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) {
Expand All @@ -140,7 +140,7 @@ _PyCompile_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;

Expand Down
3 changes: 2 additions & 1 deletion Python/flowgraph.c
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 12 additions & 11 deletions Python/instruction_sequence.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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++;
Expand All @@ -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 */
Expand Down
Loading