From 1b105f9472bdb9a68f709778afafb494997267bd Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Wed, 10 Dec 2025 07:36:46 +0900 Subject: Use palloc_object() and palloc_array() in backend code The idea is to encourage more the use of these new routines across the tree, as these offer stronger type safety guarantees than palloc(). This batch of changes includes most of the trivial changes suggested by the author for src/backend/. A total of 334 files are updated here. Among these files, 48 of them have their build change slightly; these are caused by line number changes as the new allocation formulas are simpler, shaving around 100 lines of code in total. Similar work has been done in 0c3c5c3b06a3 and 31d3847a37be. Author: David Geier Discussion: https://postgr.es/m/ad0748d4-3080-436e-b0bc-ac8f86a3466a@gmail.com --- src/backend/utils/adt/jsonpath_exec.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'src/backend/utils/adt/jsonpath_exec.c') diff --git a/src/backend/utils/adt/jsonpath_exec.c b/src/backend/utils/adt/jsonpath_exec.c index 3f92baf6e82..fc0e05e8789 100644 --- a/src/backend/utils/adt/jsonpath_exec.c +++ b/src/backend/utils/adt/jsonpath_exec.c @@ -775,7 +775,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp, break; } - v = hasNext ? &vbuf : palloc(sizeof(*v)); + v = hasNext ? &vbuf : palloc_object(JsonbValue); baseObject = cxt->baseObject; getJsonPathItem(cxt, jsp, v); @@ -1088,7 +1088,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp, case jpiType: { - JsonbValue *jbv = palloc(sizeof(*jbv)); + JsonbValue *jbv = palloc_object(JsonbValue); jbv->type = jbvString; jbv->val.string.val = pstrdup(JsonbTypeName(jb)); @@ -1118,7 +1118,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp, size = 1; } - jb = palloc(sizeof(*jb)); + jb = palloc_object(JsonbValue); jb->type = jbvNumeric; jb->val.numeric = int64_to_numeric(size); @@ -1249,7 +1249,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp, last = cxt->innermostArraySize - 1; - lastjbv = hasNext ? &tmpjbv : palloc(sizeof(*lastjbv)); + lastjbv = hasNext ? &tmpjbv : palloc_object(JsonbValue); lastjbv->type = jbvNumeric; lastjbv->val.numeric = int64_to_numeric(last); @@ -2162,7 +2162,7 @@ executeBinaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp, if (!jspGetNext(jsp, &elem) && !found) return jperOk; - lval = palloc(sizeof(*lval)); + lval = palloc_object(JsonbValue); lval->type = jbvNumeric; lval->val.numeric = res; @@ -2317,7 +2317,7 @@ executeNumericItemMethod(JsonPathExecContext *cxt, JsonPathItem *jsp, if (!jspGetNext(jsp, &next) && !found) return jperOk; - jb = palloc(sizeof(*jb)); + jb = palloc_object(JsonbValue); jb->type = jbvNumeric; jb->val.numeric = DatumGetNumeric(datum); @@ -2783,7 +2783,7 @@ executeDateTimeMethod(JsonPathExecContext *cxt, JsonPathItem *jsp, if (!hasNext && !found) return res; - jb = hasNext ? &jbvbuf : palloc(sizeof(*jb)); + jb = hasNext ? &jbvbuf : palloc_object(JsonbValue); jb->type = jbvDatetime; jb->val.datetime.value = value; @@ -3018,7 +3018,7 @@ GetJsonPathVar(void *cxt, char *varName, int varNameLen, return NULL; } - result = palloc(sizeof(JsonbValue)); + result = palloc_object(JsonbValue); if (var->isnull) { *baseObjectId = 0; @@ -3445,7 +3445,7 @@ compareNumeric(Numeric a, Numeric b) static JsonbValue * copyJsonbValue(JsonbValue *src) { - JsonbValue *dst = palloc(sizeof(*dst)); + JsonbValue *dst = palloc_object(JsonbValue); *dst = *src; @@ -4121,7 +4121,7 @@ JsonTableInitOpaque(TableFuncScanState *state, int natts) JsonExpr *je = castNode(JsonExpr, tf->docexpr); List *args = NIL; - cxt = palloc0(sizeof(JsonTableExecContext)); + cxt = palloc0_object(JsonTableExecContext); cxt->magic = JSON_TABLE_EXEC_CONTEXT_MAGIC; /* @@ -4140,7 +4140,7 @@ JsonTableInitOpaque(TableFuncScanState *state, int natts) { ExprState *state = lfirst_node(ExprState, exprlc); String *name = lfirst_node(String, namelc); - JsonPathVariable *var = palloc(sizeof(*var)); + JsonPathVariable *var = palloc_object(JsonPathVariable); var->name = pstrdup(name->sval); var->namelen = strlen(var->name); @@ -4158,8 +4158,7 @@ JsonTableInitOpaque(TableFuncScanState *state, int natts) } } - cxt->colplanstates = palloc(sizeof(JsonTablePlanState *) * - list_length(tf->colvalexprs)); + cxt->colplanstates = palloc_array(JsonTablePlanState *, list_length(tf->colvalexprs)); /* * Initialize plan for the root path and, recursively, also any child @@ -4197,7 +4196,7 @@ JsonTableInitPlan(JsonTableExecContext *cxt, JsonTablePlan *plan, JsonTablePlanState *parentstate, List *args, MemoryContext mcxt) { - JsonTablePlanState *planstate = palloc0(sizeof(*planstate)); + JsonTablePlanState *planstate = palloc0_object(JsonTablePlanState); planstate->plan = plan; planstate->parent = parentstate; -- cgit v1.2.3