From 15681ad00b83c606dbb13af8782610a61bf39687 Mon Sep 17 00:00:00 2001 From: Serge Pavlov Date: Sat, 9 Jun 2018 05:19:45 +0000 Subject: 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(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 --- llvm/lib/Support/StringMap.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'llvm/lib/Support/StringMap.cpp') 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( - 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( - 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; -- cgit v1.2.3