1818#define NEED_OPCODE_TABLES
1919#include "pycore_opcode_utils.h"
2020#undef NEED_OPCODE_TABLES
21+ #include "pycore_c_array.h" // _Py_c_array_t
2122#include "pycore_compile.h"
2223#include "pycore_instruction_sequence.h" // _PyInstructionSequence_NewLabel()
2324#include "pycore_intrinsics.h"
@@ -100,40 +101,48 @@ static const int compare_masks[] = {
100101 [Py_GE ] = COMPARISON_GREATER_THAN | COMPARISON_EQUALS ,
101102};
102103
103- /*
104- * Resize the array if index is out of range.
105- *
106- * idx: the index we want to access
107- * arr: pointer to the array
108- * alloc: pointer to the capacity of the array
109- * default_alloc: initial number of items
110- * item_size: size of each item
111- *
112- */
104+
105+ int
106+ _Py_CArray_Init (_Py_c_array_t * array , int item_size , int initial_num_entries ) {
107+ memset (array , 0 , sizeof (_Py_c_array_t ));
108+ array -> item_size = item_size ;
109+ array -> initial_num_entries = initial_num_entries ;
110+ return 0 ;
111+ }
112+
113+ void
114+ _Py_CArray_Fini (_Py_c_array_t * array )
115+ {
116+ if (array -> array ) {
117+ PyMem_Free (array -> array );
118+ array -> allocated_entries = 0 ;
119+ }
120+ }
121+
113122int
114- _PyCompile_EnsureArrayLargeEnough (int idx , void * * array , int * alloc ,
115- int default_alloc , size_t item_size )
123+ _Py_CArray_EnsureCapacity (_Py_c_array_t * c_array , int idx )
116124{
117- void * arr = * array ;
125+ void * arr = c_array -> array ;
126+ int alloc = c_array -> allocated_entries ;
118127 if (arr == NULL ) {
119- int new_alloc = default_alloc ;
128+ int new_alloc = c_array -> initial_num_entries ;
120129 if (idx >= new_alloc ) {
121- new_alloc = idx + default_alloc ;
130+ new_alloc = idx + c_array -> initial_num_entries ;
122131 }
123- arr = PyMem_Calloc (new_alloc , item_size );
132+ arr = PyMem_Calloc (new_alloc , c_array -> item_size );
124133 if (arr == NULL ) {
125134 PyErr_NoMemory ();
126135 return ERROR ;
127136 }
128- * alloc = new_alloc ;
137+ alloc = new_alloc ;
129138 }
130- else if (idx >= * alloc ) {
131- size_t oldsize = * alloc * item_size ;
132- int new_alloc = * alloc << 1 ;
139+ else if (idx >= alloc ) {
140+ size_t oldsize = alloc * c_array -> item_size ;
141+ int new_alloc = alloc << 1 ;
133142 if (idx >= new_alloc ) {
134- new_alloc = idx + default_alloc ;
143+ new_alloc = idx + c_array -> initial_num_entries ;
135144 }
136- size_t newsize = new_alloc * item_size ;
145+ size_t newsize = new_alloc * c_array -> item_size ;
137146
138147 if (oldsize > (SIZE_MAX >> 1 )) {
139148 PyErr_NoMemory ();
@@ -146,12 +155,13 @@ _PyCompile_EnsureArrayLargeEnough(int idx, void **array, int *alloc,
146155 PyErr_NoMemory ();
147156 return ERROR ;
148157 }
149- * alloc = new_alloc ;
158+ alloc = new_alloc ;
150159 arr = tmp ;
151160 memset ((char * )arr + oldsize , 0 , newsize - oldsize );
152161 }
153162
154- * array = arr ;
163+ c_array -> array = arr ;
164+ c_array -> allocated_entries = alloc ;
155165 return SUCCESS ;
156166}
157167
0 commit comments