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/SmallPtrSet.cpp | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) (limited to 'llvm/lib/Support/SmallPtrSet.cpp') diff --git a/llvm/lib/Support/SmallPtrSet.cpp b/llvm/lib/Support/SmallPtrSet.cpp index 119bb871d4c..fed4a17d663 100644 --- a/llvm/lib/Support/SmallPtrSet.cpp +++ b/llvm/lib/Support/SmallPtrSet.cpp @@ -32,9 +32,7 @@ void SmallPtrSetImplBase::shrink_and_clear() { NumNonEmpty = NumTombstones = 0; // Install the new array. Clear all the buckets to empty. - CurArray = (const void**)malloc(sizeof(void*) * CurArraySize); - if (CurArray == nullptr) - report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed."); + CurArray = (const void**)safe_malloc(sizeof(void*) * CurArraySize); memset(CurArray, -1, CurArraySize*sizeof(void*)); } @@ -100,9 +98,7 @@ void SmallPtrSetImplBase::Grow(unsigned NewSize) { bool WasSmall = isSmall(); // Install the new array. Clear all the buckets to empty. - const void **NewBuckets = (const void**) malloc(sizeof(void*) * NewSize); - if (NewBuckets == nullptr) - report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed."); + const void **NewBuckets = (const void**) safe_malloc(sizeof(void*) * NewSize); // Reset member only if memory was allocated successfully CurArray = NewBuckets; @@ -132,9 +128,7 @@ SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage, CurArray = SmallArray; // Otherwise, allocate new heap space (unless we were the same size) } else { - CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize); - if (CurArray == nullptr) - report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed."); + CurArray = (const void**)safe_malloc(sizeof(void*) * that.CurArraySize); } // Copy over the that array. @@ -163,16 +157,12 @@ void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) { // Otherwise, allocate new heap space (unless we were the same size) } else if (CurArraySize != RHS.CurArraySize) { if (isSmall()) - CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize); + CurArray = (const void**)safe_malloc(sizeof(void*) * RHS.CurArraySize); else { - const void **T = (const void**)realloc(CurArray, + const void **T = (const void**)safe_realloc(CurArray, sizeof(void*) * RHS.CurArraySize); - if (!T) - free(CurArray); CurArray = T; } - if (CurArray == nullptr) - report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed."); } CopyHelper(RHS); -- cgit v1.2.3