Skip to content

Commit c3a153a

Browse files
committed
Tweak palloc/repalloc to allow zero bytes to be requested, as per recent
proposal. Eliminate several dozen now-unnecessary hacks to avoid palloc(0). (It's likely there are more that I didn't find.)
1 parent 24a1e20 commit c3a153a

File tree

19 files changed

+113
-113
lines changed

19 files changed

+113
-113
lines changed

src/backend/access/nbtree/nbtree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Portions Copyright (c) 1994, Regents of the University of California
1313
*
1414
* IDENTIFICATION
15-
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtree.c,v 1.117 2004/06/02 17:28:17 tgl Exp $
15+
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtree.c,v 1.118 2004/06/05 19:48:07 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -693,7 +693,7 @@ btvacuumcleanup(PG_FUNCTION_ARGS)
693693
/* No point in remembering more than MaxFSMPages pages */
694694
maxFreePages = MaxFSMPages;
695695
if ((BlockNumber) maxFreePages > num_pages)
696-
maxFreePages = (int) num_pages + 1; /* +1 to avoid palloc(0) */
696+
maxFreePages = (int) num_pages;
697697
freePages = (BlockNumber *) palloc(maxFreePages * sizeof(BlockNumber));
698698
nFreePages = 0;
699699

src/backend/commands/analyze.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/analyze.c,v 1.73 2004/05/26 04:41:09 neilc Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/analyze.c,v 1.74 2004/06/05 19:48:07 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -226,9 +226,8 @@ analyze_rel(Oid relid, VacuumStmt *vacstmt)
226226
else
227227
{
228228
attr_cnt = onerel->rd_att->natts;
229-
/* +1 here is just to avoid palloc(0) with zero-column table */
230-
vacattrstats = (VacAttrStats **) palloc((attr_cnt + 1) *
231-
sizeof(VacAttrStats *));
229+
vacattrstats = (VacAttrStats **)
230+
palloc(attr_cnt * sizeof(VacAttrStats *));
232231
tcnt = 0;
233232
for (i = 1; i <= attr_cnt; i++)
234233
{
@@ -505,8 +504,8 @@ compute_index_stats(Relation onerel, double totalrows,
505504
estate);
506505

507506
/* Compute and save index expression values */
508-
exprvals = (Datum *) palloc((numrows * attr_cnt + 1) * sizeof(Datum));
509-
exprnulls = (bool *) palloc((numrows * attr_cnt + 1) * sizeof(bool));
507+
exprvals = (Datum *) palloc(numrows * attr_cnt * sizeof(Datum));
508+
exprnulls = (bool *) palloc(numrows * attr_cnt * sizeof(bool));
510509
numindexrows = 0;
511510
tcnt = 0;
512511
for (rowno = 0; rowno < numrows; rowno++)

src/backend/commands/copy.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.224 2004/05/26 04:41:10 neilc Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.225 2004/06/05 19:48:07 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1158,13 +1158,11 @@ CopyTo(Relation rel, List *attnumlist, bool binary, bool oids,
11581158

11591159
/*
11601160
* Get info about the columns we need to process.
1161-
*
1162-
* +1's here are to avoid palloc(0) in a zero-column table.
11631161
*/
1164-
out_functions = (FmgrInfo *) palloc((num_phys_attrs + 1) * sizeof(FmgrInfo));
1165-
elements = (Oid *) palloc((num_phys_attrs + 1) * sizeof(Oid));
1166-
isvarlena = (bool *) palloc((num_phys_attrs + 1) * sizeof(bool));
1167-
force_quote = (bool *) palloc((num_phys_attrs + 1) * sizeof(bool));
1162+
out_functions = (FmgrInfo *) palloc(num_phys_attrs * sizeof(FmgrInfo));
1163+
elements = (Oid *) palloc(num_phys_attrs * sizeof(Oid));
1164+
isvarlena = (bool *) palloc(num_phys_attrs * sizeof(bool));
1165+
force_quote = (bool *) palloc(num_phys_attrs * sizeof(bool));
11681166
foreach(cur, attnumlist)
11691167
{
11701168
int attnum = lfirst_int(cur);
@@ -1501,14 +1499,13 @@ CopyFrom(Relation rel, List *attnumlist, bool binary, bool oids,
15011499
* relation, including the input function, the element type (to pass
15021500
* to the input function), and info about defaults and constraints.
15031501
* (Which input function we use depends on text/binary format choice.)
1504-
* +1's here are to avoid palloc(0) in a zero-column table.
15051502
*/
1506-
in_functions = (FmgrInfo *) palloc((num_phys_attrs + 1) * sizeof(FmgrInfo));
1507-
elements = (Oid *) palloc((num_phys_attrs + 1) * sizeof(Oid));
1508-
defmap = (int *) palloc((num_phys_attrs + 1) * sizeof(int));
1509-
defexprs = (ExprState **) palloc((num_phys_attrs + 1) * sizeof(ExprState *));
1510-
constraintexprs = (ExprState **) palloc0((num_phys_attrs + 1) * sizeof(ExprState *));
1511-
force_notnull = (bool *) palloc((num_phys_attrs + 1) * sizeof(bool));
1503+
in_functions = (FmgrInfo *) palloc(num_phys_attrs * sizeof(FmgrInfo));
1504+
elements = (Oid *) palloc(num_phys_attrs * sizeof(Oid));
1505+
defmap = (int *) palloc(num_phys_attrs * sizeof(int));
1506+
defexprs = (ExprState **) palloc(num_phys_attrs * sizeof(ExprState *));
1507+
constraintexprs = (ExprState **) palloc0(num_phys_attrs * sizeof(ExprState *));
1508+
force_notnull = (bool *) palloc(num_phys_attrs * sizeof(bool));
15121509

15131510
for (attnum = 1; attnum <= num_phys_attrs; attnum++)
15141511
{
@@ -1635,8 +1632,8 @@ CopyFrom(Relation rel, List *attnumlist, bool binary, bool oids,
16351632
fmgr_info(in_func_oid, &oid_in_function);
16361633
}
16371634

1638-
values = (Datum *) palloc((num_phys_attrs + 1) * sizeof(Datum));
1639-
nulls = (char *) palloc((num_phys_attrs + 1) * sizeof(char));
1635+
values = (Datum *) palloc(num_phys_attrs * sizeof(Datum));
1636+
nulls = (char *) palloc(num_phys_attrs * sizeof(char));
16401637

16411638
/* Make room for a PARAM_EXEC value for domain constraint checks */
16421639
if (hasConstraints)

src/backend/commands/tablecmds.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.110 2004/06/04 20:35:21 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.111 2004/06/05 19:48:07 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -727,10 +727,10 @@ MergeAttributes(List *schema, List *supers, bool istemp,
727727
* newattno[] will contain the child-table attribute numbers for
728728
* the attributes of this parent table. (They are not the same
729729
* for parents after the first one, nor if we have dropped
730-
* columns.) +1 is to prevent error if parent has zero columns.
730+
* columns.)
731731
*/
732732
newattno = (AttrNumber *)
733-
palloc((tupleDesc->natts + 1) * sizeof(AttrNumber));
733+
palloc(tupleDesc->natts * sizeof(AttrNumber));
734734

735735
for (parent_attno = 1; parent_attno <= tupleDesc->natts;
736736
parent_attno++)

src/backend/commands/vacuum.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
*
1515
* IDENTIFICATION
16-
* $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.279 2004/05/31 19:24:05 tgl Exp $
16+
* $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.280 2004/06/05 19:48:07 tgl Exp $
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
@@ -2883,9 +2883,8 @@ vac_update_fsm(Relation onerel, VacPageList fraged_pages,
28832883
*/
28842884
threshold = GetAvgFSMRequestSize(&onerel->rd_node);
28852885

2886-
/* +1 to avoid palloc(0) */
28872886
pageSpaces = (PageFreeSpaceInfo *)
2888-
palloc((nPages + 1) * sizeof(PageFreeSpaceInfo));
2887+
palloc(nPages * sizeof(PageFreeSpaceInfo));
28892888
outPages = 0;
28902889

28912890
for (i = 0; i < nPages; i++)

src/backend/commands/vacuumlazy.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*
3232
*
3333
* IDENTIFICATION
34-
* $PostgreSQL: pgsql/src/backend/commands/vacuumlazy.c,v 1.41 2004/05/31 19:24:05 tgl Exp $
34+
* $PostgreSQL: pgsql/src/backend/commands/vacuumlazy.c,v 1.42 2004/06/05 19:48:07 tgl Exp $
3535
*
3636
*-------------------------------------------------------------------------
3737
*/
@@ -918,9 +918,6 @@ lazy_space_alloc(LVRelStats *vacrelstats, BlockNumber relblocks)
918918
/* No need to allocate more pages than the relation has blocks */
919919
if (relblocks < (BlockNumber) maxpages)
920920
maxpages = (int) relblocks;
921-
/* avoid palloc(0) */
922-
if (maxpages < 1)
923-
maxpages = 1;
924921

925922
vacrelstats->fs_is_heap = false;
926923
vacrelstats->num_free_pages = 0;

src/backend/executor/execQual.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/executor/execQual.c,v 1.162 2004/06/01 03:28:41 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/executor/execQual.c,v 1.163 2004/06/05 19:48:08 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -2129,8 +2129,6 @@ ExecEvalRow(RowExprState *rstate,
21292129

21302130
/* Allocate workspace */
21312131
nargs = list_length(rstate->args);
2132-
if (nargs == 0) /* avoid palloc(0) if no fields */
2133-
nargs = 1;
21342132
values = (Datum *) palloc(nargs * sizeof(Datum));
21352133
nulls = (char *) palloc(nargs * sizeof(char));
21362134

src/backend/optimizer/prep/prepunion.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
*
1616
* IDENTIFICATION
17-
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepunion.c,v 1.113 2004/06/05 01:55:04 tgl Exp $
17+
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepunion.c,v 1.114 2004/06/05 19:48:08 tgl Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -538,9 +538,9 @@ generate_append_tlist(List *colTypes, bool flag,
538538
* First extract typmods to use.
539539
*
540540
* If the inputs all agree on type and typmod of a particular column, use
541-
* that typmod; else use -1. (+1 here in case of zero columns.)
541+
* that typmod; else use -1.
542542
*/
543-
colTypmods = (int32 *) palloc(list_length(colTypes) * sizeof(int32) + 1);
543+
colTypmods = (int32 *) palloc(list_length(colTypes) * sizeof(int32));
544544

545545
foreach(planl, input_plans)
546546
{

src/backend/optimizer/util/clauses.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.173 2004/06/01 04:47:45 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.174 2004/06/05 19:48:08 tgl Exp $
1212
*
1313
* HISTORY
1414
* AUTHOR DATE MAJOR EVENT
@@ -2016,7 +2016,7 @@ inline_function(Oid funcid, Oid result_type, List *args,
20162016
* actual substitution of the inputs. So start building expression
20172017
* with inputs substituted.
20182018
*/
2019-
usecounts = (int *) palloc0((funcform->pronargs + 1) * sizeof(int));
2019+
usecounts = (int *) palloc0(funcform->pronargs * sizeof(int));
20202020
newexpr = substitute_actual_parameters(newexpr, funcform->pronargs,
20212021
args, usecounts);
20222022

src/backend/storage/freespace/freespace.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/storage/freespace/freespace.c,v 1.30 2004/01/26 22:35:32 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/storage/freespace/freespace.c,v 1.31 2004/06/05 19:48:08 tgl Exp $
1212
*
1313
*
1414
* NOTES:
@@ -893,7 +893,7 @@ LoadFreeSpaceMap(void)
893893
len = nPages * sizeof(IndexFSMPageData);
894894
else
895895
len = nPages * sizeof(FSMPageData);
896-
data = (char *) palloc(len + 1); /* +1 to avoid palloc(0) */
896+
data = (char *) palloc(len);
897897
if (fread(data, 1, len, fp) != len)
898898
{
899899
elog(LOG, "premature EOF in \"%s\"", cachefilename);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy