18
18
#define NEED_OPCODE_TABLES
19
19
#include "pycore_opcode_utils.h"
20
20
#undef NEED_OPCODE_TABLES
21
+ #include "pycore_c_array.h" // _Py_c_array_t
21
22
#include "pycore_compile.h"
22
23
#include "pycore_instruction_sequence.h" // _PyInstructionSequence_NewLabel()
23
24
#include "pycore_intrinsics.h"
@@ -100,40 +101,48 @@ static const int compare_masks[] = {
100
101
[Py_GE ] = COMPARISON_GREATER_THAN | COMPARISON_EQUALS ,
101
102
};
102
103
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
+
113
122
int
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 )
116
124
{
117
- void * arr = * array ;
125
+ void * arr = c_array -> array ;
126
+ int alloc = c_array -> allocated_entries ;
118
127
if (arr == NULL ) {
119
- int new_alloc = default_alloc ;
128
+ int new_alloc = c_array -> initial_num_entries ;
120
129
if (idx >= new_alloc ) {
121
- new_alloc = idx + default_alloc ;
130
+ new_alloc = idx + c_array -> initial_num_entries ;
122
131
}
123
- arr = PyMem_Calloc (new_alloc , item_size );
132
+ arr = PyMem_Calloc (new_alloc , c_array -> item_size );
124
133
if (arr == NULL ) {
125
134
PyErr_NoMemory ();
126
135
return ERROR ;
127
136
}
128
- * alloc = new_alloc ;
137
+ alloc = new_alloc ;
129
138
}
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 ;
133
142
if (idx >= new_alloc ) {
134
- new_alloc = idx + default_alloc ;
143
+ new_alloc = idx + c_array -> initial_num_entries ;
135
144
}
136
- size_t newsize = new_alloc * item_size ;
145
+ size_t newsize = new_alloc * c_array -> item_size ;
137
146
138
147
if (oldsize > (SIZE_MAX >> 1 )) {
139
148
PyErr_NoMemory ();
@@ -146,12 +155,13 @@ _PyCompile_EnsureArrayLargeEnough(int idx, void **array, int *alloc,
146
155
PyErr_NoMemory ();
147
156
return ERROR ;
148
157
}
149
- * alloc = new_alloc ;
158
+ alloc = new_alloc ;
150
159
arr = tmp ;
151
160
memset ((char * )arr + oldsize , 0 , newsize - oldsize );
152
161
}
153
162
154
- * array = arr ;
163
+ c_array -> array = arr ;
164
+ c_array -> allocated_entries = alloc ;
155
165
return SUCCESS ;
156
166
}
157
167
0 commit comments