Report fatal error in the case of out of memory
authorSerge Pavlov <sepavloff@gmail.com>
Tue, 20 Feb 2018 05:41:26 +0000 (05:41 +0000)
committerSerge Pavlov <sepavloff@gmail.com>
Tue, 20 Feb 2018 05:41:26 +0000 (05:41 +0000)
commit76d8ccee2e2aee1c7f7c6749ed8f555583ce3143
tree5f0e9f8eb634b9d9f52af47590724d14ba8b27dd
parentdb211892edde7dd6fafd108ecf770a2fecf8651d
Report fatal error in the case of out of memory

This is the second part of recommit of r325224. The previous part was
committed in r325426, which deals with C++ memory allocation. Solution
for C memory allocation involved functions `llvm::malloc` and similar.
This was a fragile solution because it caused ambiguity errors in some
cases. In this commit the new functions have names like `llvm::safe_malloc`.

The relevant part of original comment is below, updated for new function
names.

Analysis of fails in the case of out of memory errors can be tricky on
Windows. Such error emerges at the point where memory allocation function
fails, but manifests itself when null pointer is used. These two points
may be distant from each other. Besides, next runs may not exhibit
allocation error.

In some cases memory is allocated by a call to some of C allocation
functions, malloc, calloc and realloc. They are used for interoperability
with C code, when allocated object has variable size and when it is
necessary to avoid call of constructors. In many calls the result is not
checked for null pointer. To simplify checks, new functions are defined
in the namespace 'llvm': `safe_malloc`, `safe_calloc` and `safe_realloc`.
They behave as corresponding standard functions but produce fatal error if
allocation fails. This change replaces the standard functions like 'malloc'
in the cases when the result of the allocation function is not checked
for null pointer.

Finally, there are plain C code, that uses malloc and similar functions. If
the result is not checked, assert statement is added.

Differential Revision: https://reviews.llvm.org/D43010

llvm-svn: 325551
18 files changed:
llvm/include/llvm/ADT/BitVector.h
llvm/include/llvm/ADT/SparseMultiSet.h
llvm/include/llvm/ADT/SparseSet.h
llvm/include/llvm/Support/Allocator.h
llvm/include/llvm/Support/OnDiskHashTable.h
llvm/lib/CodeGen/InterferenceCache.cpp
llvm/lib/CodeGen/LiveIntervalUnion.cpp
llvm/lib/CodeGen/RegisterPressure.cpp
llvm/lib/ExecutionEngine/Interpreter/Execution.cpp
llvm/lib/Object/Object.cpp
llvm/lib/Support/RWMutex.cpp
llvm/lib/Support/StringMap.cpp
llvm/lib/Support/Unix/Signals.inc
llvm/lib/Support/Windows/RWMutex.inc
llvm/tools/llvm-c-test/attributes.c
llvm/tools/llvm-c-test/echo.cpp
llvm/unittests/Support/AllocatorTest.cpp
llvm/unittests/Support/ManagedStatic.cpp