Skip to content

Commit bd4e2fd

Browse files
committed
Provide a way to supress the "out of memory" error when allocating.
Using the new interface MemoryContextAllocExtended, callers can specify MCXT_ALLOC_NO_OOM if they are prepared to handle a NULL return value. Michael Paquier, reviewed and somewhat revised by me.
1 parent 3d660d3 commit bd4e2fd

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
return ret;
712712
}
713713

714+
/*
715+
* MemoryContextAllocExtended
716+
* Allocate space within the specified context using the given flags.
717+
*/
718+
void *
719+
MemoryContextAllocExtended(MemoryContext context, Size size, int flags)
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+
return NULL;
744+
}
745+
746+
VALGRIND_MEMPOOL_ALLOC(context, ret, size);
747+
748+
if ((flags & MCXT_ALLOC_ZERO) != 0)
749+
MemSetAligned(ret, 0, size);
750+
751+
return ret;
752+
}
753+
714754
void *
715755
palloc(Size size)
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
extern PGDLLIMPORT MemoryContext CurrentMemoryContext;
4444

45+
/*
46+
* Flags for MemoryContextAllocExtended.
47+
*/
48+
#define MCXT_ALLOC_HUGE 0x01 /* allow huge allocation (> 1 GB) */
49+
#define MCXT_ALLOC_NO_OOM 0x02 /* no failure if out-of-memory */
50+
#define MCXT_ALLOC_ZERO 0x04 /* zero allocated memory */
51+
4552
/*
4653
* Fundamental memory-allocation operations (more are in utils/memutils.h)
4754
*/
4855
extern void *MemoryContextAlloc(MemoryContext context, Size size);
4956
extern void *MemoryContextAllocZero(MemoryContext context, Size size);
5057
extern void *MemoryContextAllocZeroAligned(MemoryContext context, Size size);
58+
extern void *MemoryContextAllocExtended(MemoryContext context,
59+
Size size, int flags);
5160

5261
extern void *palloc(Size size);
5362
extern void *palloc0(Size size);

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