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

Commitbd4e2fd

Browse files
committed
Provide a way to supress the "out of memory" error when allocating.
Using the new interface MemoryContextAllocExtended, callers canspecify MCXT_ALLOC_NO_OOM if they are prepared to handle a NULLreturn value.Michael Paquier, reviewed and somewhat revised by me.
1 parent3d660d3 commitbd4e2fd

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,46 @@ MemoryContextAllocZeroAligned(MemoryContext context, Size size)
711711
returnret;
712712
}
713713

714+
/*
715+
* MemoryContextAllocExtended
716+
*Allocate space within the specified context using the given flags.
717+
*/
718+
void*
719+
MemoryContextAllocExtended(MemoryContextcontext,Sizesize,intflags)
720+
{
721+
void*ret;
722+
723+
AssertArg(MemoryContextIsValid(context));
724+
AssertNotInCriticalSection(context);
725+
726+
if (((flags&MCXT_ALLOC_HUGE)!=0&& !AllocHugeSizeIsValid(size))||
727+
((flags&MCXT_ALLOC_HUGE)==0&& !AllocSizeIsValid(size)))
728+
elog(ERROR,"invalid memory alloc request size %zu",size);
729+
730+
context->isReset= false;
731+
732+
ret= (*context->methods->alloc) (context,size);
733+
if (ret==NULL)
734+
{
735+
if ((flags&MCXT_ALLOC_NO_OOM)==0)
736+
{
737+
MemoryContextStats(TopMemoryContext);
738+
ereport(ERROR,
739+
(errcode(ERRCODE_OUT_OF_MEMORY),
740+
errmsg("out of memory"),
741+
errdetail("Failed on request of size %zu.",size)));
742+
}
743+
returnNULL;
744+
}
745+
746+
VALGRIND_MEMPOOL_ALLOC(context,ret,size);
747+
748+
if ((flags&MCXT_ALLOC_ZERO)!=0)
749+
MemSetAligned(ret,0,size);
750+
751+
returnret;
752+
}
753+
714754
void*
715755
palloc(Sizesize)
716756
{

‎src/include/utils/palloc.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,21 @@ typedef struct MemoryContextData *MemoryContext;
4242
*/
4343
externPGDLLIMPORTMemoryContextCurrentMemoryContext;
4444

45+
/*
46+
* Flags for MemoryContextAllocExtended.
47+
*/
48+
#defineMCXT_ALLOC_HUGE0x01/* allow huge allocation (> 1 GB) */
49+
#defineMCXT_ALLOC_NO_OOM0x02/* no failure if out-of-memory */
50+
#defineMCXT_ALLOC_ZERO0x04/* zero allocated memory */
51+
4552
/*
4653
* Fundamental memory-allocation operations (more are in utils/memutils.h)
4754
*/
4855
externvoid*MemoryContextAlloc(MemoryContextcontext,Sizesize);
4956
externvoid*MemoryContextAllocZero(MemoryContextcontext,Sizesize);
5057
externvoid*MemoryContextAllocZeroAligned(MemoryContextcontext,Sizesize);
58+
externvoid*MemoryContextAllocExtended(MemoryContextcontext,
59+
Sizesize,intflags);
5160

5261
externvoid*palloc(Sizesize);
5362
externvoid*palloc0(Sizesize);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp