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

Commit4cf6e9e

Browse files
committed
Avoid corner cases where aset.c would unnecessarily make malloc()
requests of sizes that aren't powers of 2. Per observation fromDavid Schultz, 28-Aug.
1 parentc472b83 commit4cf6e9e

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Portions Copyright (c) 1994, Regents of the University of California
1212
*
1313
* IDENTIFICATION
14-
* $Header: /cvsroot/pgsql/src/backend/utils/mmgr/aset.c,v 1.52 2003/08/04 02:40:08 momjian Exp $
14+
* $Header: /cvsroot/pgsql/src/backend/utils/mmgr/aset.c,v 1.53 2003/09/13 22:25:38 tgl Exp $
1515
*
1616
* NOTE:
1717
*This is a new (Feb. 05, 1999) implementation of the allocation set
@@ -650,32 +650,29 @@ AllocSetAlloc(MemoryContext context, Size size)
650650
}
651651
else
652652
{
653-
/* Get size of prior block */
654-
blksize=set->blocks->endptr- ((char*)set->blocks);
655-
656653
/*
657-
* Special case: if very first allocation was for a large
658-
* chunk (or we have a small "keeper" block), could have an
659-
* undersized top block. Do something reasonable.
654+
* Use first power of 2 that is larger than previous block,
655+
* but not more than the allowed limit. (We don't simply double
656+
* the prior block size, because in some cases this could be a
657+
* funny size, eg if very first allocation was for an odd-sized
658+
* large chunk.)
660659
*/
661-
if (blksize<set->initBlockSize)
662-
blksize=set->initBlockSize;
663-
else
664-
{
665-
/* Crank it up, but not past max */
660+
Sizepblksize=set->blocks->endptr- ((char*)set->blocks);
661+
662+
blksize=set->initBlockSize;
663+
while (blksize <=pblksize)
666664
blksize <<=1;
667-
if (blksize>set->maxBlockSize)
668-
blksize=set->maxBlockSize;
669-
}
665+
if (blksize>set->maxBlockSize)
666+
blksize=set->maxBlockSize;
670667
}
671668

672669
/*
673670
* If initBlockSize is less than ALLOC_CHUNK_LIMIT, we could need
674-
* more space...
671+
* more space... but try to keep it a power of 2.
675672
*/
676673
required_size=chunk_size+ALLOC_BLOCKHDRSZ+ALLOC_CHUNKHDRSZ;
677-
if (blksize<required_size)
678-
blksize=required_size;
674+
while (blksize<required_size)
675+
blksize<<=1;
679676

680677
/* Try to allocate it */
681678
block= (AllocBlock)malloc(blksize);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp