postgres.git / summary / log / commit / refs
commit bd4e2fd97d3db84bd970d6051f775b7ff2af0e9d
Author: Robert Haas <rhaas@postgresql.org>
Date: Fri Jan 30 17:56:48 2015 +0000
First released in: 9.5.0
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.
src/backend/utils/mmgr/mcxt.c | 40 ++++++++++++++++++++++++++++++++++++++++
src/include/utils/palloc.h | 9 +++++++++
2 files changed, 49 insertions(+)
diff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c
index c62922a187a..202bc785bcf 100644
--- a/src/backend/utils/mmgr/mcxt.c
+++ b/src/backend/utils/mmgr/mcxt.c
@@ -711,6 +711,46 @@ MemoryContextAllocZeroAligned(MemoryContext context, Size size)
return ret;
}
+/*
+ * MemoryContextAllocExtended
+ * Allocate space within the specified context using the given flags.
+ */
+void *
+MemoryContextAllocExtended(MemoryContext context, Size size, int flags)
+{
+ void *ret;
+
+ AssertArg(MemoryContextIsValid(context));
+ AssertNotInCriticalSection(context);
+
+ if (((flags & MCXT_ALLOC_HUGE) != 0 && !AllocHugeSizeIsValid(size)) ||
+ ((flags & MCXT_ALLOC_HUGE) == 0 && !AllocSizeIsValid(size)))
+ elog(ERROR, "invalid memory alloc request size %zu", size);
+
+ context->isReset = false;
+
+ ret = (*context->methods->alloc) (context, size);
+ if (ret == NULL)
+ {
+ if ((flags & MCXT_ALLOC_NO_OOM) == 0)
+ {
+ MemoryContextStats(TopMemoryContext);
+ ereport(ERROR,
+ (errcode(ERRCODE_OUT_OF_MEMORY),
+ errmsg("out of memory"),
+ errdetail("Failed on request of size %zu.", size)));
+ }
+ return NULL;
+ }
+
+ VALGRIND_MEMPOOL_ALLOC(context, ret, size);
+
+ if ((flags & MCXT_ALLOC_ZERO) != 0)
+ MemSetAligned(ret, 0, size);
+
+ return ret;
+}
+
void *
palloc(Size size)
{
diff --git a/src/include/utils/palloc.h b/src/include/utils/palloc.h
index ca03f2b3341..f586fd5535b 100644
--- a/src/include/utils/palloc.h
+++ b/src/include/utils/palloc.h
@@ -42,12 +42,21 @@ typedef struct MemoryContextData *MemoryContext;
*/
extern PGDLLIMPORT MemoryContext CurrentMemoryContext;
+/*
+ * Flags for MemoryContextAllocExtended.
+ */
+#define MCXT_ALLOC_HUGE 0x01 /* allow huge allocation (> 1 GB) */
+#define MCXT_ALLOC_NO_OOM 0x02 /* no failure if out-of-memory */
+#define MCXT_ALLOC_ZERO 0x04 /* zero allocated memory */
+
/*
* Fundamental memory-allocation operations (more are in utils/memutils.h)
*/
extern void *MemoryContextAlloc(MemoryContext context, Size size);
extern void *MemoryContextAllocZero(MemoryContext context, Size size);
extern void *MemoryContextAllocZeroAligned(MemoryContext context, Size size);
+extern void *MemoryContextAllocExtended(MemoryContext context,
+ Size size, int flags);
extern void *palloc(Size size);
extern void *palloc0(Size size);
[parent: 3d660d33aab2]