Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3d6d1b5

Browse files
committed
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
1 parent 1c993b3 commit 3d6d1b5

File tree

2 files changed

+79
-26
lines changed

2 files changed

+79
-26
lines changed

src/backend/utils/mmgr/aset.c

+13-26
Original file line numberDiff line numberDiff line change
@@ -642,8 +642,8 @@ AllocSetDelete(MemoryContext context)
642642

643643
/*
644644
* AllocSetAlloc
645-
* Returns pointer to allocated memory of given size; memory is added
646-
* to the set.
645+
* Returns pointer to allocated memory of given size or NULL if
646+
* request could not be completed; memory is added to the set.
647647
*
648648
* No request may exceed:
649649
* MAXALIGN_DOWN(SIZE_MAX) - ALLOC_BLOCKHDRSZ - ALLOC_CHUNKHDRSZ
@@ -671,13 +671,7 @@ AllocSetAlloc(MemoryContext context, Size size)
671671
blksize = chunk_size + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ;
672672
block = (AllocBlock) malloc(blksize);
673673
if (block == NULL)
674-
{
675-
MemoryContextStats(TopMemoryContext);
676-
ereport(ERROR,
677-
(errcode(ERRCODE_OUT_OF_MEMORY),
678-
errmsg("out of memory"),
679-
errdetail("Failed on request of size %zu.", size)));
680-
}
674+
return NULL;
681675
block->aset = set;
682676
block->freeptr = block->endptr = ((char *) block) + blksize;
683677

@@ -865,13 +859,7 @@ AllocSetAlloc(MemoryContext context, Size size)
865859
}
866860

867861
if (block == NULL)
868-
{
869-
MemoryContextStats(TopMemoryContext);
870-
ereport(ERROR,
871-
(errcode(ERRCODE_OUT_OF_MEMORY),
872-
errmsg("out of memory"),
873-
errdetail("Failed on request of size %zu.", size)));
874-
}
862+
return NULL;
875863

876864
block->aset = set;
877865
block->freeptr = ((char *) block) + ALLOC_BLOCKHDRSZ;
@@ -1002,9 +990,10 @@ AllocSetFree(MemoryContext context, void *pointer)
1002990

1003991
/*
1004992
* AllocSetRealloc
1005-
* Returns new pointer to allocated memory of given size; this memory
1006-
* is added to the set. Memory associated with given pointer is copied
1007-
* into the new memory, and the old memory is freed.
993+
* Returns new pointer to allocated memory of given size or NULL if
994+
* request could not be completed; this memory is added to the set.
995+
* Memory associated with given pointer is copied into the new memory,
996+
* and the old memory is freed.
1008997
*
1009998
* Without MEMORY_CONTEXT_CHECKING, we don't know the old request size. This
1010999
* makes our Valgrind client requests less-precise, hazarding false negatives.
@@ -1107,13 +1096,7 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
11071096
blksize = chksize + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ;
11081097
block = (AllocBlock) realloc(block, blksize);
11091098
if (block == NULL)
1110-
{
1111-
MemoryContextStats(TopMemoryContext);
1112-
ereport(ERROR,
1113-
(errcode(ERRCODE_OUT_OF_MEMORY),
1114-
errmsg("out of memory"),
1115-
errdetail("Failed on request of size %zu.", size)));
1116-
}
1099+
return NULL;
11171100
block->freeptr = block->endptr = ((char *) block) + blksize;
11181101

11191102
/* Update pointers since block has likely been moved */
@@ -1179,6 +1162,10 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
11791162
/* allocate new chunk */
11801163
newPointer = AllocSetAlloc((MemoryContext) set, size);
11811164

1165+
/* leave immediately if request was not completed */
1166+
if (newPointer == NULL)
1167+
return NULL;
1168+
11821169
/*
11831170
* AllocSetAlloc() just made the region NOACCESS. Change it to
11841171
* UNDEFINED for the moment; memcpy() will then transfer definedness

src/backend/utils/mmgr/mcxt.c

+66
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,15 @@ MemoryContextAlloc(MemoryContext context, Size size)
623623
context->isReset = false;
624624

625625
ret = (*context->methods->alloc) (context, size);
626+
if (ret == NULL)
627+
{
628+
MemoryContextStats(TopMemoryContext);
629+
ereport(ERROR,
630+
(errcode(ERRCODE_OUT_OF_MEMORY),
631+
errmsg("out of memory"),
632+
errdetail("Failed on request of size %zu.", size)));
633+
}
634+
626635
VALGRIND_MEMPOOL_ALLOC(context, ret, size);
627636

628637
return ret;
@@ -649,6 +658,15 @@ MemoryContextAllocZero(MemoryContext context, Size size)
649658
context->isReset = false;
650659

651660
ret = (*context->methods->alloc) (context, size);
661+
if (ret == NULL)
662+
{
663+
MemoryContextStats(TopMemoryContext);
664+
ereport(ERROR,
665+
(errcode(ERRCODE_OUT_OF_MEMORY),
666+
errmsg("out of memory"),
667+
errdetail("Failed on request of size %zu.", size)));
668+
}
669+
652670
VALGRIND_MEMPOOL_ALLOC(context, ret, size);
653671

654672
MemSetAligned(ret, 0, size);
@@ -677,6 +695,15 @@ MemoryContextAllocZeroAligned(MemoryContext context, Size size)
677695
context->isReset = false;
678696

679697
ret = (*context->methods->alloc) (context, size);
698+
if (ret == NULL)
699+
{
700+
MemoryContextStats(TopMemoryContext);
701+
ereport(ERROR,
702+
(errcode(ERRCODE_OUT_OF_MEMORY),
703+
errmsg("out of memory"),
704+
errdetail("Failed on request of size %zu.", size)));
705+
}
706+
680707
VALGRIND_MEMPOOL_ALLOC(context, ret, size);
681708

682709
MemSetLoop(ret, 0, size);
@@ -699,6 +726,15 @@ palloc(Size size)
699726
CurrentMemoryContext->isReset = false;
700727

701728
ret = (*CurrentMemoryContext->methods->alloc) (CurrentMemoryContext, size);
729+
if (ret == NULL)
730+
{
731+
MemoryContextStats(TopMemoryContext);
732+
ereport(ERROR,
733+
(errcode(ERRCODE_OUT_OF_MEMORY),
734+
errmsg("out of memory"),
735+
errdetail("Failed on request of size %zu.", size)));
736+
}
737+
702738
VALGRIND_MEMPOOL_ALLOC(CurrentMemoryContext, ret, size);
703739

704740
return ret;
@@ -719,6 +755,15 @@ palloc0(Size size)
719755
CurrentMemoryContext->isReset = false;
720756

721757
ret = (*CurrentMemoryContext->methods->alloc) (CurrentMemoryContext, size);
758+
if (ret == NULL)
759+
{
760+
MemoryContextStats(TopMemoryContext);
761+
ereport(ERROR,
762+
(errcode(ERRCODE_OUT_OF_MEMORY),
763+
errmsg("out of memory"),
764+
errdetail("Failed on request of size %zu.", size)));
765+
}
766+
722767
VALGRIND_MEMPOOL_ALLOC(CurrentMemoryContext, ret, size);
723768

724769
MemSetAligned(ret, 0, size);
@@ -789,6 +834,12 @@ repalloc(void *pointer, Size size)
789834
Assert(!context->isReset);
790835

791836
ret = (*context->methods->realloc) (context, pointer, size);
837+
if (ret == NULL)
838+
ereport(ERROR,
839+
(errcode(ERRCODE_OUT_OF_MEMORY),
840+
errmsg("out of memory"),
841+
errdetail("Failed on request of size %zu.", size)));
842+
792843
VALGRIND_MEMPOOL_CHANGE(context, pointer, ret, size);
793844

794845
return ret;
@@ -814,6 +865,15 @@ MemoryContextAllocHuge(MemoryContext context, Size size)
814865
context->isReset = false;
815866

816867
ret = (*context->methods->alloc) (context, size);
868+
if (ret == NULL)
869+
{
870+
MemoryContextStats(TopMemoryContext);
871+
ereport(ERROR,
872+
(errcode(ERRCODE_OUT_OF_MEMORY),
873+
errmsg("out of memory"),
874+
errdetail("Failed on request of size %zu.", size)));
875+
}
876+
817877
VALGRIND_MEMPOOL_ALLOC(context, ret, size);
818878

819879
return ret;
@@ -854,6 +914,12 @@ repalloc_huge(void *pointer, Size size)
854914
Assert(!context->isReset);
855915

856916
ret = (*context->methods->realloc) (context, pointer, size);
917+
if (ret == NULL)
918+
ereport(ERROR,
919+
(errcode(ERRCODE_OUT_OF_MEMORY),
920+
errmsg("out of memory"),
921+
errdetail("Failed on request of size %zu.", size)));
922+
857923
VALGRIND_MEMPOOL_CHANGE(context, pointer, ret, size);
858924

859925
return ret;

0 commit comments

Comments
 (0)