From 0e31285fe88dbe817932fc9a96d13b84e59b55ce Mon Sep 17 00:00:00 2001 From: Serge Pavlov Date: Tue, 29 May 2018 05:39:08 +0000 Subject: Use uniform mechanism for OOM errors handling In r325551 many calls of malloc/calloc/realloc were replaces with calls of their safe counterparts defined in the namespace llvm. There functions generate crash if memory cannot be allocated, such behavior facilitates handling of out of memory errors on Windows. If the result of *alloc function were checked for success, the function was not replaced with the safe variant. In these cases the calling function made the error handling, like: T *NewElts = static_cast(malloc(NewCapacity*sizeof(T))); if (NewElts == nullptr) report_bad_alloc_error("Allocation of SmallVector element failed."); Actually knowledge about the function where OOM occurred is useless. Moreover having a single entry point for OOM handling is convenient for investigation of memory problems. This change removes custom OOM errors handling and replaces them with calls to functions `llvm::safe_*alloc`. Declarations of `safe_*alloc` are moved to a separate include file, to avoid cyclic dependency in SmallVector.h Differential Revision: https://reviews.llvm.org/D47440 llvm-svn: 333390 --- llvm/lib/Support/SmallVector.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'llvm/lib/Support/SmallVector.cpp') diff --git a/llvm/lib/Support/SmallVector.cpp b/llvm/lib/Support/SmallVector.cpp index 74313151c76..ccab4a125aa 100644 --- a/llvm/lib/Support/SmallVector.cpp +++ b/llvm/lib/Support/SmallVector.cpp @@ -25,17 +25,13 @@ void SmallVectorBase::grow_pod(void *FirstEl, size_t MinSizeInBytes, void *NewElts; if (BeginX == FirstEl) { - NewElts = malloc(NewCapacityInBytes); - if (NewElts == nullptr) - report_bad_alloc_error("Allocation of SmallVector element failed."); + NewElts = safe_malloc(NewCapacityInBytes); // Copy the elements over. No need to run dtors on PODs. memcpy(NewElts, this->BeginX, CurSizeBytes); } else { // If this wasn't grown from the inline copy, grow the allocated space. - NewElts = realloc(this->BeginX, NewCapacityInBytes); - if (NewElts == nullptr) - report_bad_alloc_error("Reallocation of SmallVector element failed."); + NewElts = safe_realloc(this->BeginX, NewCapacityInBytes); } this->EndX = (char*)NewElts+CurSizeBytes; -- cgit v1.2.3