HiInspired by [1], I took a stab at converting src/fe_utils to the new pg_malloc macros.
Some mostly useless metrics: 31/34 converted, 7 casts removed. meson test looks good for me. 1. https://www.postgresql.org/message-id/flat/CAHut%2BPvpGPDLhkHAoxw_g3jdrYxA1m16a8uagbgH3TGWSKtXNQ%40mail.gmail.com best regards, Henrik
From 6ad0e5be35029a1aa32452cfeba5fe0ec55b1f5d Mon Sep 17 00:00:00 2001 From: Henrik TJ <[email protected]> Date: Fri, 13 Feb 2026 15:51:47 +0100 Subject: [PATCH] fe_utils: Use pg_malloc_object() and pg_malloc_array() Use more high-level allocation routines in src/fe_utils. Similar to 31d3847a37be and 6736dea14af --- src/fe_utils/conditional.c | 4 ++-- src/fe_utils/print.c | 48 +++++++++++++++++++------------------- src/fe_utils/psqlscan.l | 8 +++---- src/fe_utils/simple_list.c | 4 ++-- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/fe_utils/conditional.c b/src/fe_utils/conditional.c index deba61e0d81..537c76ed3cd 100644 --- a/src/fe_utils/conditional.c +++ b/src/fe_utils/conditional.c @@ -17,7 +17,7 @@ ConditionalStack conditional_stack_create(void) { - ConditionalStack cstack = pg_malloc(sizeof(ConditionalStackData)); + ConditionalStack cstack = pg_malloc_object(ConditionalStackData); cstack->head = NULL; return cstack; @@ -52,7 +52,7 @@ conditional_stack_destroy(ConditionalStack cstack) void conditional_stack_push(ConditionalStack cstack, ifState new_state) { - IfStackElem *p = (IfStackElem *) pg_malloc(sizeof(IfStackElem)); + IfStackElem *p = pg_malloc_object(IfStackElem); p->if_state = new_state; p->query_len = -1; diff --git a/src/fe_utils/print.c b/src/fe_utils/print.c index ef52cdee9b7..12d969e8666 100644 --- a/src/fe_utils/print.c +++ b/src/fe_utils/print.c @@ -344,7 +344,7 @@ format_numeric_locale(const char *my_str) return pg_strdup(my_str); new_len = strlen(my_str) + additional_numeric_locale_len(my_str); - new_str = pg_malloc(new_len + 1); + new_str = pg_malloc_array(char, (new_len + 1)); new_str_pos = 0; int_len = integer_digits(my_str); @@ -692,18 +692,18 @@ print_aligned_text(const printTableContent *cont, FILE *fout, bool is_pager) if (cont->ncolumns > 0) { col_count = cont->ncolumns; - width_header = pg_malloc0(col_count * sizeof(*width_header)); - width_average = pg_malloc0(col_count * sizeof(*width_average)); - max_width = pg_malloc0(col_count * sizeof(*max_width)); - width_wrap = pg_malloc0(col_count * sizeof(*width_wrap)); - max_nl_lines = pg_malloc0(col_count * sizeof(*max_nl_lines)); - curr_nl_line = pg_malloc0(col_count * sizeof(*curr_nl_line)); - col_lineptrs = pg_malloc0(col_count * sizeof(*col_lineptrs)); - max_bytes = pg_malloc0(col_count * sizeof(*max_bytes)); - format_buf = pg_malloc0(col_count * sizeof(*format_buf)); - header_done = pg_malloc0(col_count * sizeof(*header_done)); - bytes_output = pg_malloc0(col_count * sizeof(*bytes_output)); - wrap = pg_malloc0(col_count * sizeof(*wrap)); + width_header = pg_malloc0_array(unsigned int, col_count); + width_average = pg_malloc0_array(unsigned int, col_count); + max_width = pg_malloc0_array(unsigned int, col_count); + width_wrap = pg_malloc0_array(unsigned int, col_count); + max_nl_lines = pg_malloc0_array(unsigned int, col_count); + curr_nl_line = pg_malloc0_array(unsigned int, col_count); + col_lineptrs = pg_malloc0_array(struct lineptr *, col_count); + max_bytes = pg_malloc0_array(unsigned int, col_count); + format_buf = pg_malloc0_array(unsigned char *, col_count); + header_done = pg_malloc0_array(bool, col_count); + bytes_output = pg_malloc0_array(int, col_count); + wrap = pg_malloc0_array(printTextLineWrap, col_count); } else { @@ -798,10 +798,10 @@ print_aligned_text(const printTableContent *cont, FILE *fout, bool is_pager) for (i = 0; i < col_count; i++) { /* Add entry for ptr == NULL array termination */ - col_lineptrs[i] = pg_malloc0((max_nl_lines[i] + 1) * - sizeof(**col_lineptrs)); + col_lineptrs[i] = pg_malloc0_array(struct lineptr, + (max_nl_lines[i] + 1)); - format_buf[i] = pg_malloc(max_bytes[i] + 1); + format_buf[i] = pg_malloc_array(unsigned char, (max_bytes[i] + 1)); col_lineptrs[i]->ptr = format_buf[i]; } @@ -1409,8 +1409,8 @@ print_aligned_vertical(const printTableContent *cont, * We now have all the information we need to setup the formatting * structures */ - dlineptr = pg_malloc((sizeof(*dlineptr)) * (dheight + 1)); - hlineptr = pg_malloc((sizeof(*hlineptr)) * (hheight + 1)); + dlineptr = pg_malloc_array(struct lineptr, (dheight + 1)); + hlineptr = pg_malloc_array(struct lineptr, (hheight + 1)); dlineptr->ptr = pg_malloc(dformatsize); hlineptr->ptr = pg_malloc(hformatsize); @@ -3198,7 +3198,7 @@ printTableInit(printTableContent *const content, const printTableOpt *opt, content->ncolumns = ncolumns; content->nrows = nrows; - content->headers = pg_malloc0((ncolumns + 1) * sizeof(*content->headers)); + content->headers = pg_malloc0_array(const char *, (ncolumns + 1)); total_cells = (uint64) ncolumns * nrows; /* Catch possible overflow. Using >= here allows adding 1 below */ @@ -3209,12 +3209,12 @@ printTableInit(printTableContent *const content, const printTableOpt *opt, SIZE_MAX / sizeof(*content->cells)); exit(EXIT_FAILURE); } - content->cells = pg_malloc0((total_cells + 1) * sizeof(*content->cells)); + content->cells = pg_malloc0_array(const char *, (total_cells + 1)); content->cellmustfree = NULL; content->footers = NULL; - content->aligns = pg_malloc0((ncolumns + 1) * sizeof(*content->align)); + content->aligns = pg_malloc0_array(char, (ncolumns + 1)); content->header = content->headers; content->cell = content->cells; @@ -3305,7 +3305,7 @@ printTableAddCell(printTableContent *const content, char *cell, { if (content->cellmustfree == NULL) content->cellmustfree = - pg_malloc0((total_cells + 1) * sizeof(bool)); + pg_malloc0_array(bool, (total_cells + 1)); content->cellmustfree[content->cellsadded] = true; } @@ -3330,7 +3330,7 @@ printTableAddFooter(printTableContent *const content, const char *footer) { printTableFooter *f; - f = pg_malloc0(sizeof(*f)); + f = pg_malloc0_object(printTableFooter); f->data = pg_strdup(footer); if (content->footers == NULL) @@ -3477,7 +3477,7 @@ count_table_lines(const printTableContent *cont, * Scan all column headers and determine their heights. Cache the values * since vertical mode repeats the headers for every record. */ - header_height = (int *) pg_malloc(cont->ncolumns * sizeof(int)); + header_height = pg_malloc_array(int, cont->ncolumns); for (i = 0; i < cont->ncolumns; i++) { pg_wcssize((const unsigned char *) cont->headers[i], diff --git a/src/fe_utils/psqlscan.l b/src/fe_utils/psqlscan.l index 5d74714ffc6..e78952d448d 100644 --- a/src/fe_utils/psqlscan.l +++ b/src/fe_utils/psqlscan.l @@ -1002,7 +1002,7 @@ psql_scan_create(const PsqlScanCallbacks *callbacks) { PsqlScanState state; - state = (PsqlScanStateData *) pg_malloc0(sizeof(PsqlScanStateData)); + state = pg_malloc0_object(PsqlScanStateData); state->callbacks = callbacks; @@ -1376,7 +1376,7 @@ psqlscan_push_new_buffer(PsqlScanState state, const char *newstr, { StackElem *stackelem; - stackelem = (StackElem *) pg_malloc(sizeof(StackElem)); + stackelem = pg_malloc_object(StackElem); /* * In current usage, the passed varname points at the current flex input @@ -1479,7 +1479,7 @@ psqlscan_prepare_buffer(PsqlScanState state, const char *txt, int len, char *newtxt; /* Flex wants two \0 characters after the actual data */ - newtxt = pg_malloc(len + 2); + newtxt = pg_malloc_array(char, (len + 2)); *txtcopy = newtxt; newtxt[len] = newtxt[len + 1] = YY_END_OF_BUFFER_CHAR; @@ -1548,7 +1548,7 @@ psqlscan_emit(PsqlScanState state, const char *txt, int len) char * psqlscan_extract_substring(PsqlScanState state, const char *txt, int len) { - char *result = (char *) pg_malloc(len + 1); + char *result = pg_malloc_array(char, (len + 1)); if (state->safe_encoding) memcpy(result, txt, len); diff --git a/src/fe_utils/simple_list.c b/src/fe_utils/simple_list.c index ec698fc6728..03c11bae7c8 100644 --- a/src/fe_utils/simple_list.c +++ b/src/fe_utils/simple_list.c @@ -27,7 +27,7 @@ simple_oid_list_append(SimpleOidList *list, Oid val) { SimpleOidListCell *cell; - cell = (SimpleOidListCell *) pg_malloc(sizeof(SimpleOidListCell)); + cell = pg_malloc_object(SimpleOidListCell); cell->next = NULL; cell->val = val; @@ -163,7 +163,7 @@ simple_ptr_list_append(SimplePtrList *list, void *ptr) { SimplePtrListCell *cell; - cell = (SimplePtrListCell *) pg_malloc(sizeof(SimplePtrListCell)); + cell = pg_malloc_object(SimplePtrListCell); cell->next = NULL; cell->ptr = ptr; -- 2.53.0
