mirror of
https://github.com/postgres/postgres.git
synced 2025-11-25 12:03:53 +03:00
Add palloc_extended for frontend and backend.
This commit also adds pg_malloc_extended for frontend. These interfaces can be used to control at a lower level memory allocation using an interface similar to MemoryContextAllocExtended. For example, the callers can specify MCXT_ALLOC_NO_OOM if they want to suppress the "out of memory" error while allocating the memory and handle a NULL return value. Michael Paquier, reviewed by me.
This commit is contained in:
@@ -864,6 +864,43 @@ palloc0(Size size)
|
||||
return ret;
|
||||
}
|
||||
|
||||
void *
|
||||
palloc_extended(Size size, int flags)
|
||||
{
|
||||
/* duplicates MemoryContextAllocExtended to avoid increased overhead */
|
||||
void *ret;
|
||||
|
||||
AssertArg(MemoryContextIsValid(CurrentMemoryContext));
|
||||
AssertNotInCriticalSection(CurrentMemoryContext);
|
||||
|
||||
if (((flags & MCXT_ALLOC_HUGE) != 0 && !AllocHugeSizeIsValid(size)) ||
|
||||
((flags & MCXT_ALLOC_HUGE) == 0 && !AllocSizeIsValid(size)))
|
||||
elog(ERROR, "invalid memory alloc request size %zu", size);
|
||||
|
||||
CurrentMemoryContext->isReset = false;
|
||||
|
||||
ret = (*CurrentMemoryContext->methods->alloc) (CurrentMemoryContext, 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(CurrentMemoryContext, ret, size);
|
||||
|
||||
if ((flags & MCXT_ALLOC_ZERO) != 0)
|
||||
MemSetAligned(ret, 0, size);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* pfree
|
||||
* Release an allocated chunk.
|
||||
|
||||
Reference in New Issue
Block a user