Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc3a153a

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 parent24a1e20 commitc3a153a

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
intattnum=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/0501:55:04 tgl Exp $
17+
* $PostgreSQL: pgsql/src/backend/optimizer/prep/prepunion.c,v 1.114 2004/06/0519: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
* AUTHORDATEMAJOR 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);

‎src/backend/storage/lmgr/deadlock.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
*
1414
* IDENTIFICATION
15-
* $PostgreSQL: pgsql/src/backend/storage/lmgr/deadlock.c,v 1.27 2003/12/01 21:59:25 momjian Exp $
15+
* $PostgreSQL: pgsql/src/backend/storage/lmgr/deadlock.c,v 1.28 2004/06/05 19:48:08 tgl Exp $
1616
*
1717
*Interface:
1818
*
@@ -147,11 +147,10 @@ InitDeadLockChecking(void)
147147
* We need to consider rearranging at most MaxBackends/2 wait queues
148148
* (since it takes at least two waiters in a queue to create a soft
149149
* edge), and the expanded form of the wait queues can't involve more
150-
* than MaxBackends total waiters.(But avoid palloc(0) if
151-
* MaxBackends = 1.)
150+
* than MaxBackends total waiters.
152151
*/
153152
waitOrders= (WAIT_ORDER*)
154-
palloc(((MaxBackends+1) /2)*sizeof(WAIT_ORDER));
153+
palloc((MaxBackends /2)*sizeof(WAIT_ORDER));
155154
waitOrderProcs= (PGPROC**)palloc(MaxBackends*sizeof(PGPROC*));
156155

157156
/*

‎src/backend/storage/lmgr/lock.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/storage/lmgr/lock.c,v 1.132 2004/05/28 05:13:05 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/storage/lmgr/lock.c,v 1.133 2004/06/05 19:48:08 tgl Exp $
1212
*
1313
* NOTES
1414
* Outside modules can create a lock table and acquire/release
@@ -1361,9 +1361,6 @@ GetLockStatusData(void)
13611361

13621362
data->nelements=i=proclockTable->hctl->nentries;
13631363

1364-
if (i==0)
1365-
i=1;/* avoid palloc(0) if empty table */
1366-
13671364
data->proclockaddrs= (SHMEM_OFFSET*)palloc(sizeof(SHMEM_OFFSET)*i);
13681365
data->proclocks= (PROCLOCK*)palloc(sizeof(PROCLOCK)*i);
13691366
data->procs= (PGPROC*)palloc(sizeof(PGPROC)*i);

‎src/backend/tcop/pquery.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/tcop/pquery.c,v 1.79 2004/05/26 18:54:08 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/tcop/pquery.c,v 1.80 2004/06/05 19:48:08 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -347,10 +347,9 @@ PortalSetResultFormat(Portal portal, int nFormats, int16 *formats)
347347
if (portal->tupDesc==NULL)
348348
return;
349349
natts=portal->tupDesc->natts;
350-
/* +1 avoids palloc(0) if no columns */
351350
portal->formats= (int16*)
352351
MemoryContextAlloc(PortalGetHeapMemory(portal),
353-
(natts+1)*sizeof(int16));
352+
natts*sizeof(int16));
354353
if (nFormats>1)
355354
{
356355
/* format specified for each column */

‎src/backend/utils/adt/name.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
*
1616
* IDENTIFICATION
17-
* $PostgreSQL: pgsql/src/backend/utils/adt/name.c,v 1.52 2004/05/30 23:40:35 neilc Exp $
17+
* $PostgreSQL: pgsql/src/backend/utils/adt/name.c,v 1.53 2004/06/05 19:48:09 tgl Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -358,9 +358,7 @@ current_schemas(PG_FUNCTION_ARGS)
358358
inti;
359359
ArrayType*array;
360360

361-
/* +1 here is just to avoid palloc(0) error */
362-
363-
names= (Datum*)palloc((list_length(search_path)+1)*sizeof(Datum));
361+
names= (Datum*)palloc(list_length(search_path)*sizeof(Datum));
364362
i=0;
365363
foreach(l,search_path)
366364
{

‎src/backend/utils/mmgr/README

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
$PostgreSQL: pgsql/src/backend/utils/mmgr/README,v 1.5 2003/11/29 19:52:04 pgsql Exp $
1+
$PostgreSQL: pgsql/src/backend/utils/mmgr/README,v 1.6 2004/06/05 19:48:09 tgl Exp $
22

33
Notes about memory allocation redesign
44
--------------------------------------
@@ -53,6 +53,26 @@ that can be reset or deleted at strategic times within a query, such as
5353
after each tuple.
5454

5555

56+
Some notes about the palloc API versus standard C library
57+
---------------------------------------------------------
58+
59+
The behavior of palloc and friends is similar to the standard C library's
60+
malloc and friends, but there are some deliberate differences too. Here
61+
are some notes to clarify the behavior.
62+
63+
* If out of memory, palloc and repalloc exit via elog(ERROR). They never
64+
return NULL, and it is not necessary or useful to test for such a result.
65+
66+
* palloc(0) is explicitly a valid operation. It does not return a NULL
67+
pointer, but a valid chunk of which no bytes may be used. (However, the
68+
chunk might later be repalloc'd larger; it can also be pfree'd without
69+
error.) (Note: this behavior is new in Postgres 7.5; earlier versions
70+
disallowed palloc(0). It seems more consistent to allow it, however.)
71+
Similarly, repalloc allows realloc'ing to zero size.
72+
73+
* pfree and repalloc do not accept a NULL pointer. This is intentional.
74+
75+
5676
pfree/repalloc no longer depend on CurrentMemoryContext
5777
-------------------------------------------------------
5878

‎src/backend/utils/mmgr/mcxt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
*
1616
* IDENTIFICATION
17-
* $PostgreSQL: pgsql/src/backend/utils/mmgr/mcxt.c,v 1.44 2003/11/29 19:52:04 pgsql Exp $
17+
* $PostgreSQL: pgsql/src/backend/utils/mmgr/mcxt.c,v 1.45 2004/06/05 19:48:09 tgl Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -565,7 +565,7 @@ pfree(void *pointer)
565565

566566
/*
567567
* repalloc
568-
*
568+
*Adjust the size of a previously allocated chunk.
569569
*/
570570
void*
571571
repalloc(void*pointer,Sizesize)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp