Skip to content

Commit 93d018c

Browse files
committed
tidy up API
1 parent 122dbf1 commit 93d018c

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

Include/internal/pycore_c_array.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ extern "C" {
1313
/* Utility for a number of growing arrays */
1414

1515
typedef struct {
16-
void **array; /* pointer to the array */
17-
int *allocated_entries; /* pointer to the capacity of the array */
16+
void *array; /* pointer to the array */
17+
int allocated_entries; /* pointer to the capacity of the array */
1818
size_t item_size; /* size of each element */
1919
int initial_num_entries; /* initial allocation size */
2020
} _Py_c_array_t;
2121

22+
23+
int _Py_CArray_Init(_Py_c_array_t* array, int item_size, int initial_num_entries);
24+
void _Py_CArray_Fini(_Py_c_array_t* array);
25+
2226
/* If idx is out of bounds:
2327
* If arr->array is NULL, allocate arr->initial_num_entries slots.
2428
* Otherwise, double its size.

Python/codegen.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,28 @@ static const int compare_masks[] = {
111111
};
112112

113113

114+
int
115+
_Py_CArray_Init(_Py_c_array_t* array, int item_size, int initial_num_entries) {
116+
memset(array, 0, sizeof(_Py_c_array_t));
117+
array->item_size = item_size;
118+
array->initial_num_entries = initial_num_entries;
119+
return 0;
120+
}
121+
122+
void
123+
_Py_CArray_Fini(_Py_c_array_t* array)
124+
{
125+
if (array->array) {
126+
PyMem_Free(array->array);
127+
array->allocated_entries = 0;
128+
}
129+
}
130+
114131
int
115132
_Py_CArray_EnsureCapacity(_Py_c_array_t *c_array, int idx)
116133
{
117-
void *arr = *c_array->array;
118-
int alloc = *c_array->allocated_entries;
134+
void *arr = c_array->array;
135+
int alloc = c_array->allocated_entries;
119136
if (arr == NULL) {
120137
int new_alloc = c_array->initial_num_entries;
121138
if (idx >= new_alloc) {
@@ -152,8 +169,8 @@ _Py_CArray_EnsureCapacity(_Py_c_array_t *c_array, int idx)
152169
memset((char *)arr + oldsize, 0, newsize - oldsize);
153170
}
154171

155-
*c_array->array = arr;
156-
*c_array->allocated_entries = alloc;
172+
c_array->array = arr;
173+
c_array->allocated_entries = alloc;
157174
return SUCCESS;
158175
}
159176

Python/flowgraph.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,15 @@ basicblock_next_instr(basicblock *b)
143143
{
144144
assert(b != NULL);
145145
_Py_c_array_t array = {
146-
.array = (void**)&b->b_instr,
147-
.allocated_entries = &b->b_ialloc,
146+
.array = (void*)b->b_instr,
147+
.allocated_entries = b->b_ialloc,
148148
.item_size = sizeof(cfg_instr),
149149
.initial_num_entries = DEFAULT_BLOCK_SIZE,
150150
};
151151

152152
RETURN_IF_ERROR(_Py_CArray_EnsureCapacity(&array, b->b_iused + 1));
153+
b->b_instr = array.array;
154+
b->b_ialloc = array.allocated_entries;
153155
return b->b_iused++;
154156
}
155157

Python/instruction_sequence.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ instr_sequence_next_inst(instr_sequence *seq) {
3939

4040

4141
_Py_c_array_t array = {
42-
.array = (void**)&seq->s_instrs,
43-
.allocated_entries = &seq->s_allocated,
42+
.array = (void*)seq->s_instrs,
43+
.allocated_entries = seq->s_allocated,
4444
.item_size = sizeof(instruction),
4545
.initial_num_entries = INITIAL_INSTR_SEQUENCE_SIZE,
4646
};
4747

4848
RETURN_IF_ERROR(_Py_CArray_EnsureCapacity(&array, seq->s_used + 1));
49+
seq->s_instrs = array.array;
50+
seq->s_allocated = array.allocated_entries;
4951

5052
assert(seq->s_allocated >= 0);
5153
assert(seq->s_used < seq->s_allocated);
@@ -64,13 +66,15 @@ _PyInstructionSequence_UseLabel(instr_sequence *seq, int lbl)
6466
{
6567
int old_size = seq->s_labelmap_size;
6668
_Py_c_array_t array = {
67-
.array = (void**)&seq->s_labelmap,
68-
.allocated_entries = &seq->s_labelmap_size,
69+
.array = (void*)seq->s_labelmap,
70+
.allocated_entries = seq->s_labelmap_size,
6971
.item_size = sizeof(int),
7072
.initial_num_entries = INITIAL_INSTR_SEQUENCE_LABELS_MAP_SIZE,
7173
};
7274

7375
RETURN_IF_ERROR(_Py_CArray_EnsureCapacity(&array, lbl));
76+
seq->s_labelmap = array.array;
77+
seq->s_labelmap_size = array.allocated_entries;
7478

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

0 commit comments

Comments
 (0)