summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support
diff options
context:
space:
mode:
authorSerge Pavlov <sepavloff@gmail.com>2018-06-09 05:19:45 +0000
committerSerge Pavlov <sepavloff@gmail.com>2018-06-09 05:19:45 +0000
commit15681ad00b83c606dbb13af8782610a61bf39687 (patch)
treeb3d4599dbf9dd2a083456965ddaa085b17f8ccd1 /llvm/lib/Support
parent61998289f93772f354a29417ce76323b13bd4736 (diff)
downloadbcm5719-llvm-15681ad00b83c606dbb13af8782610a61bf39687.tar.gz
bcm5719-llvm-15681ad00b83c606dbb13af8782610a61bf39687.zip
Use uniform mechanism for OOM errors handling
This is a recommit of r333506, which was reverted in r333518. The original commit message is below. 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<T*>(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: 334344
Diffstat (limited to 'llvm/lib/Support')
-rw-r--r--llvm/lib/Support/FoldingSet.cpp7
-rw-r--r--llvm/lib/Support/Mutex.cpp5
-rw-r--r--llvm/lib/Support/SmallPtrSet.cpp20
-rw-r--r--llvm/lib/Support/SmallVector.cpp8
-rw-r--r--llvm/lib/Support/StringMap.cpp8
5 files changed, 12 insertions, 36 deletions
diff --git a/llvm/lib/Support/FoldingSet.cpp b/llvm/lib/Support/FoldingSet.cpp
index 94237954903..ec7d57586e8 100644
--- a/llvm/lib/Support/FoldingSet.cpp
+++ b/llvm/lib/Support/FoldingSet.cpp
@@ -214,11 +214,8 @@ static void **GetBucketFor(unsigned Hash, void **Buckets, unsigned NumBuckets) {
/// AllocateBuckets - Allocated initialized bucket memory.
static void **AllocateBuckets(unsigned NumBuckets) {
- void **Buckets = static_cast<void**>(calloc(NumBuckets+1, sizeof(void*)));
-
- if (Buckets == nullptr)
- report_bad_alloc_error("Allocation of Buckets failed.");
-
+ void **Buckets = static_cast<void**>(safe_calloc(NumBuckets + 1,
+ sizeof(void*)));
// Set the very last bucket to be a non-null "pointer".
Buckets[NumBuckets] = reinterpret_cast<void*>(-1);
return Buckets;
diff --git a/llvm/lib/Support/Mutex.cpp b/llvm/lib/Support/Mutex.cpp
index c70125f108e..7138c7a4b98 100644
--- a/llvm/lib/Support/Mutex.cpp
+++ b/llvm/lib/Support/Mutex.cpp
@@ -47,10 +47,7 @@ MutexImpl::MutexImpl( bool recursive)
{
// Declare the pthread_mutex data structures
pthread_mutex_t* mutex =
- static_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t)));
-
- if (mutex == nullptr)
- report_bad_alloc_error("Mutex allocation failed");
+ static_cast<pthread_mutex_t*>(safe_malloc(sizeof(pthread_mutex_t)));
pthread_mutexattr_t attr;
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);
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;
diff --git a/llvm/lib/Support/StringMap.cpp b/llvm/lib/Support/StringMap.cpp
index 79262cc6d3a..c1f707ce50a 100644
--- a/llvm/lib/Support/StringMap.cpp
+++ b/llvm/lib/Support/StringMap.cpp
@@ -59,10 +59,8 @@ void StringMapImpl::init(unsigned InitSize) {
NumTombstones = 0;
TheTable = static_cast<StringMapEntryBase **>(
- std::calloc(NewNumBuckets+1,
+ safe_calloc(NewNumBuckets+1,
sizeof(StringMapEntryBase **) + sizeof(unsigned)));
- if (TheTable == nullptr)
- report_bad_alloc_error("Allocation of StringMap table failed.");
// Set the member only if TheTable was successfully allocated
NumBuckets = NewNumBuckets;
@@ -220,9 +218,7 @@ unsigned StringMapImpl::RehashTable(unsigned BucketNo) {
// Allocate one extra bucket which will always be non-empty. This allows the
// iterators to stop at end.
auto NewTableArray = static_cast<StringMapEntryBase **>(
- std::calloc(NewSize+1, sizeof(StringMapEntryBase *) + sizeof(unsigned)));
- if (NewTableArray == nullptr)
- report_bad_alloc_error("Allocation of StringMap hash table failed.");
+ safe_calloc(NewSize+1, sizeof(StringMapEntryBase *) + sizeof(unsigned)));
unsigned *NewHashArray = (unsigned *)(NewTableArray + NewSize + 1);
NewTableArray[NewSize] = (StringMapEntryBase*)2;
OpenPOWER on IntegriCloud