gh-114569: Use PyMem_* APIs for non-PyObjects in compiler (#114587)

This commit is contained in:
Erlend E. Aasland 2024-01-30 00:04:34 +01:00 committed by GitHub
parent 3996cbdd33
commit 8612230c1c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 16 deletions

View file

@ -160,7 +160,7 @@ _PyCompile_EnsureArrayLargeEnough(int idx, void **array, int *alloc,
if (idx >= new_alloc) {
new_alloc = idx + default_alloc;
}
arr = PyObject_Calloc(new_alloc, item_size);
arr = PyMem_Calloc(new_alloc, item_size);
if (arr == NULL) {
PyErr_NoMemory();
return ERROR;
@ -181,7 +181,7 @@ _PyCompile_EnsureArrayLargeEnough(int idx, void **array, int *alloc,
}
assert(newsize > 0);
void *tmp = PyObject_Realloc(arr, newsize);
void *tmp = PyMem_Realloc(arr, newsize);
if (tmp == NULL) {
PyErr_NoMemory();
return ERROR;
@ -282,10 +282,10 @@ instr_sequence_insert_instruction(instr_sequence *seq, int pos,
static void
instr_sequence_fini(instr_sequence *seq) {
PyObject_Free(seq->s_labelmap);
PyMem_Free(seq->s_labelmap);
seq->s_labelmap = NULL;
PyObject_Free(seq->s_instrs);
PyMem_Free(seq->s_instrs);
seq->s_instrs = NULL;
}
@ -690,7 +690,7 @@ compiler_unit_free(struct compiler_unit *u)
Py_CLEAR(u->u_metadata.u_cellvars);
Py_CLEAR(u->u_metadata.u_fasthidden);
Py_CLEAR(u->u_private);
PyObject_Free(u);
PyMem_Free(u);
}
static int
@ -1262,8 +1262,7 @@ compiler_enter_scope(struct compiler *c, identifier name,
struct compiler_unit *u;
u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
struct compiler_unit));
u = (struct compiler_unit *)PyMem_Calloc(1, sizeof(struct compiler_unit));
if (!u) {
PyErr_NoMemory();
return ERROR;
@ -6657,7 +6656,7 @@ ensure_fail_pop(struct compiler *c, pattern_context *pc, Py_ssize_t n)
return SUCCESS;
}
Py_ssize_t needed = sizeof(jump_target_label) * size;
jump_target_label *resized = PyObject_Realloc(pc->fail_pop, needed);
jump_target_label *resized = PyMem_Realloc(pc->fail_pop, needed);
if (resized == NULL) {
PyErr_NoMemory();
return ERROR;
@ -6696,13 +6695,13 @@ emit_and_reset_fail_pop(struct compiler *c, location loc,
USE_LABEL(c, pc->fail_pop[pc->fail_pop_size]);
if (codegen_addop_noarg(INSTR_SEQUENCE(c), POP_TOP, loc) < 0) {
pc->fail_pop_size = 0;
PyObject_Free(pc->fail_pop);
PyMem_Free(pc->fail_pop);
pc->fail_pop = NULL;
return ERROR;
}
}
USE_LABEL(c, pc->fail_pop[0]);
PyObject_Free(pc->fail_pop);
PyMem_Free(pc->fail_pop);
pc->fail_pop = NULL;
return SUCCESS;
}
@ -7206,7 +7205,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
Py_DECREF(pc->stores);
*pc = old_pc;
Py_INCREF(pc->stores);
// Need to NULL this for the PyObject_Free call in the error block.
// Need to NULL this for the PyMem_Free call in the error block.
old_pc.fail_pop = NULL;
// No match. Pop the remaining copy of the subject and fail:
if (codegen_addop_noarg(INSTR_SEQUENCE(c), POP_TOP, LOC(p)) < 0 ||
@ -7252,7 +7251,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
diff:
compiler_error(c, LOC(p), "alternative patterns bind different names");
error:
PyObject_Free(old_pc.fail_pop);
PyMem_Free(old_pc.fail_pop);
Py_DECREF(old_pc.stores);
Py_XDECREF(control);
return ERROR;
@ -7453,7 +7452,7 @@ compiler_match(struct compiler *c, stmt_ty s)
pattern_context pc;
pc.fail_pop = NULL;
int result = compiler_match_inner(c, s, &pc);
PyObject_Free(pc.fail_pop);
PyMem_Free(pc.fail_pop);
return result;
}

View file

@ -162,7 +162,7 @@ basicblock_last_instr(const basicblock *b) {
static basicblock *
cfg_builder_new_block(cfg_builder *g)
{
basicblock *b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock));
basicblock *b = (basicblock *)PyMem_Calloc(1, sizeof(basicblock));
if (b == NULL) {
PyErr_NoMemory();
return NULL;
@ -437,10 +437,10 @@ _PyCfgBuilder_Free(cfg_builder *g)
basicblock *b = g->g_block_list;
while (b != NULL) {
if (b->b_instr) {
PyObject_Free((void *)b->b_instr);
PyMem_Free((void *)b->b_instr);
}
basicblock *next = b->b_list;
PyObject_Free((void *)b);
PyMem_Free((void *)b);
b = next;
}
PyMem_Free(g);