Commit 3d6d1b58 authored by Robert Haas's avatar Robert Haas

Move out-of-memory error checks from aset.c to mcxt.c

This potentially allows us to add mcxt.c interfaces that do something
other than throw an error when memory cannot be allocated.  We'll
handle adding those interfaces in a separate commit.

Michael Paquier, with minor changes by me
parent 1c993b3a
...@@ -642,8 +642,8 @@ AllocSetDelete(MemoryContext context) ...@@ -642,8 +642,8 @@ AllocSetDelete(MemoryContext context)
/* /*
* AllocSetAlloc * AllocSetAlloc
* Returns pointer to allocated memory of given size; memory is added * Returns pointer to allocated memory of given size or NULL if
* to the set. * request could not be completed; memory is added to the set.
* *
* No request may exceed: * No request may exceed:
* MAXALIGN_DOWN(SIZE_MAX) - ALLOC_BLOCKHDRSZ - ALLOC_CHUNKHDRSZ * MAXALIGN_DOWN(SIZE_MAX) - ALLOC_BLOCKHDRSZ - ALLOC_CHUNKHDRSZ
...@@ -671,13 +671,7 @@ AllocSetAlloc(MemoryContext context, Size size) ...@@ -671,13 +671,7 @@ AllocSetAlloc(MemoryContext context, Size size)
blksize = chunk_size + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ; blksize = chunk_size + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ;
block = (AllocBlock) malloc(blksize); block = (AllocBlock) malloc(blksize);
if (block == NULL) if (block == NULL)
{ return NULL;
MemoryContextStats(TopMemoryContext);
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory"),
errdetail("Failed on request of size %zu.", size)));
}
block->aset = set; block->aset = set;
block->freeptr = block->endptr = ((char *) block) + blksize; block->freeptr = block->endptr = ((char *) block) + blksize;
...@@ -865,13 +859,7 @@ AllocSetAlloc(MemoryContext context, Size size) ...@@ -865,13 +859,7 @@ AllocSetAlloc(MemoryContext context, Size size)
} }
if (block == NULL) if (block == NULL)
{ return NULL;
MemoryContextStats(TopMemoryContext);
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory"),
errdetail("Failed on request of size %zu.", size)));
}
block->aset = set; block->aset = set;
block->freeptr = ((char *) block) + ALLOC_BLOCKHDRSZ; block->freeptr = ((char *) block) + ALLOC_BLOCKHDRSZ;
...@@ -1002,9 +990,10 @@ AllocSetFree(MemoryContext context, void *pointer) ...@@ -1002,9 +990,10 @@ AllocSetFree(MemoryContext context, void *pointer)
/* /*
* AllocSetRealloc * AllocSetRealloc
* Returns new pointer to allocated memory of given size; this memory * Returns new pointer to allocated memory of given size or NULL if
* is added to the set. Memory associated with given pointer is copied * request could not be completed; this memory is added to the set.
* into the new memory, and the old memory is freed. * Memory associated with given pointer is copied into the new memory,
* and the old memory is freed.
* *
* Without MEMORY_CONTEXT_CHECKING, we don't know the old request size. This * Without MEMORY_CONTEXT_CHECKING, we don't know the old request size. This
* makes our Valgrind client requests less-precise, hazarding false negatives. * makes our Valgrind client requests less-precise, hazarding false negatives.
...@@ -1107,13 +1096,7 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size) ...@@ -1107,13 +1096,7 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
blksize = chksize + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ; blksize = chksize + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ;
block = (AllocBlock) realloc(block, blksize); block = (AllocBlock) realloc(block, blksize);
if (block == NULL) if (block == NULL)
{ return NULL;
MemoryContextStats(TopMemoryContext);
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory"),
errdetail("Failed on request of size %zu.", size)));
}
block->freeptr = block->endptr = ((char *) block) + blksize; block->freeptr = block->endptr = ((char *) block) + blksize;
/* Update pointers since block has likely been moved */ /* Update pointers since block has likely been moved */
...@@ -1179,6 +1162,10 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size) ...@@ -1179,6 +1162,10 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
/* allocate new chunk */ /* allocate new chunk */
newPointer = AllocSetAlloc((MemoryContext) set, size); newPointer = AllocSetAlloc((MemoryContext) set, size);
/* leave immediately if request was not completed */
if (newPointer == NULL)
return NULL;
/* /*
* AllocSetAlloc() just made the region NOACCESS. Change it to * AllocSetAlloc() just made the region NOACCESS. Change it to
* UNDEFINED for the moment; memcpy() will then transfer definedness * UNDEFINED for the moment; memcpy() will then transfer definedness
......
...@@ -623,6 +623,15 @@ MemoryContextAlloc(MemoryContext context, Size size) ...@@ -623,6 +623,15 @@ MemoryContextAlloc(MemoryContext context, Size size)
context->isReset = false; context->isReset = false;
ret = (*context->methods->alloc) (context, size); ret = (*context->methods->alloc) (context, size);
if (ret == NULL)
{
MemoryContextStats(TopMemoryContext);
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory"),
errdetail("Failed on request of size %zu.", size)));
}
VALGRIND_MEMPOOL_ALLOC(context, ret, size); VALGRIND_MEMPOOL_ALLOC(context, ret, size);
return ret; return ret;
...@@ -649,6 +658,15 @@ MemoryContextAllocZero(MemoryContext context, Size size) ...@@ -649,6 +658,15 @@ MemoryContextAllocZero(MemoryContext context, Size size)
context->isReset = false; context->isReset = false;
ret = (*context->methods->alloc) (context, size); ret = (*context->methods->alloc) (context, size);
if (ret == NULL)
{
MemoryContextStats(TopMemoryContext);
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory"),
errdetail("Failed on request of size %zu.", size)));
}
VALGRIND_MEMPOOL_ALLOC(context, ret, size); VALGRIND_MEMPOOL_ALLOC(context, ret, size);
MemSetAligned(ret, 0, size); MemSetAligned(ret, 0, size);
...@@ -677,6 +695,15 @@ MemoryContextAllocZeroAligned(MemoryContext context, Size size) ...@@ -677,6 +695,15 @@ MemoryContextAllocZeroAligned(MemoryContext context, Size size)
context->isReset = false; context->isReset = false;
ret = (*context->methods->alloc) (context, size); ret = (*context->methods->alloc) (context, size);
if (ret == NULL)
{
MemoryContextStats(TopMemoryContext);
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory"),
errdetail("Failed on request of size %zu.", size)));
}
VALGRIND_MEMPOOL_ALLOC(context, ret, size); VALGRIND_MEMPOOL_ALLOC(context, ret, size);
MemSetLoop(ret, 0, size); MemSetLoop(ret, 0, size);
...@@ -699,6 +726,15 @@ palloc(Size size) ...@@ -699,6 +726,15 @@ palloc(Size size)
CurrentMemoryContext->isReset = false; CurrentMemoryContext->isReset = false;
ret = (*CurrentMemoryContext->methods->alloc) (CurrentMemoryContext, size); ret = (*CurrentMemoryContext->methods->alloc) (CurrentMemoryContext, size);
if (ret == NULL)
{
MemoryContextStats(TopMemoryContext);
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory"),
errdetail("Failed on request of size %zu.", size)));
}
VALGRIND_MEMPOOL_ALLOC(CurrentMemoryContext, ret, size); VALGRIND_MEMPOOL_ALLOC(CurrentMemoryContext, ret, size);
return ret; return ret;
...@@ -719,6 +755,15 @@ palloc0(Size size) ...@@ -719,6 +755,15 @@ palloc0(Size size)
CurrentMemoryContext->isReset = false; CurrentMemoryContext->isReset = false;
ret = (*CurrentMemoryContext->methods->alloc) (CurrentMemoryContext, size); ret = (*CurrentMemoryContext->methods->alloc) (CurrentMemoryContext, size);
if (ret == NULL)
{
MemoryContextStats(TopMemoryContext);
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory"),
errdetail("Failed on request of size %zu.", size)));
}
VALGRIND_MEMPOOL_ALLOC(CurrentMemoryContext, ret, size); VALGRIND_MEMPOOL_ALLOC(CurrentMemoryContext, ret, size);
MemSetAligned(ret, 0, size); MemSetAligned(ret, 0, size);
...@@ -789,6 +834,12 @@ repalloc(void *pointer, Size size) ...@@ -789,6 +834,12 @@ repalloc(void *pointer, Size size)
Assert(!context->isReset); Assert(!context->isReset);
ret = (*context->methods->realloc) (context, pointer, size); ret = (*context->methods->realloc) (context, pointer, size);
if (ret == NULL)
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory"),
errdetail("Failed on request of size %zu.", size)));
VALGRIND_MEMPOOL_CHANGE(context, pointer, ret, size); VALGRIND_MEMPOOL_CHANGE(context, pointer, ret, size);
return ret; return ret;
...@@ -814,6 +865,15 @@ MemoryContextAllocHuge(MemoryContext context, Size size) ...@@ -814,6 +865,15 @@ MemoryContextAllocHuge(MemoryContext context, Size size)
context->isReset = false; context->isReset = false;
ret = (*context->methods->alloc) (context, size); ret = (*context->methods->alloc) (context, size);
if (ret == NULL)
{
MemoryContextStats(TopMemoryContext);
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory"),
errdetail("Failed on request of size %zu.", size)));
}
VALGRIND_MEMPOOL_ALLOC(context, ret, size); VALGRIND_MEMPOOL_ALLOC(context, ret, size);
return ret; return ret;
...@@ -854,6 +914,12 @@ repalloc_huge(void *pointer, Size size) ...@@ -854,6 +914,12 @@ repalloc_huge(void *pointer, Size size)
Assert(!context->isReset); Assert(!context->isReset);
ret = (*context->methods->realloc) (context, pointer, size); ret = (*context->methods->realloc) (context, pointer, size);
if (ret == NULL)
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory"),
errdetail("Failed on request of size %zu.", size)));
VALGRIND_MEMPOOL_CHANGE(context, pointer, ret, size); VALGRIND_MEMPOOL_CHANGE(context, pointer, ret, size);
return ret; return ret;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment