[PATCH] Use returns_nonnull in BumpPtrAllocator and MallocAllocator to avoid null-check in placement new

Hans Wennborg hans at chromium.org
Thu Aug 21 10:19:26 PDT 2014


Closed by commit rL216192 (authored by @hans).

REPOSITORY
  rL LLVM

http://reviews.llvm.org/D4989

Files:
  llvm/trunk/include/llvm/Support/Allocator.h
  llvm/trunk/include/llvm/Support/Compiler.h

Index: llvm/trunk/include/llvm/Support/Allocator.h
===================================================================
--- llvm/trunk/include/llvm/Support/Allocator.h
+++ llvm/trunk/include/llvm/Support/Allocator.h
@@ -90,7 +90,10 @@
 public:
   void Reset() {}
 
-  void *Allocate(size_t Size, size_t /*Alignment*/) { return malloc(Size); }
+  LLVM_ATTRIBUTE_RETURNS_NONNULL void *Allocate(size_t Size,
+                                                size_t /*Alignment*/) {
+    return malloc(Size);
+  }
 
   // Pull in base class overloads.
   using AllocatorBase<MallocAllocator>::Allocate;
@@ -200,7 +203,7 @@
   }
 
   /// \brief Allocate space at the specified alignment.
-  void *Allocate(size_t Size, size_t Alignment) {
+  LLVM_ATTRIBUTE_RETURNS_NONNULL void *Allocate(size_t Size, size_t Alignment) {
     assert(Alignment > 0 && "0-byte alignnment is not allowed. Use 1 instead.");
 
     // Keep track of how many bytes we've allocated.
Index: llvm/trunk/include/llvm/Support/Compiler.h
===================================================================
--- llvm/trunk/include/llvm/Support/Compiler.h
+++ llvm/trunk/include/llvm/Support/Compiler.h
@@ -236,6 +236,12 @@
 #define LLVM_ATTRIBUTE_NORETURN
 #endif
 
+#if __has_attribute(returns_nonnull) || __GNUC_PREREQ(4, 9)
+#define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
+#else
+#define LLVM_ATTRIBUTE_RETURNS_NONNULL
+#endif
+
 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
 /// pedantic diagnostics.
 #ifdef __GNUC__
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4989.12786.patch
Type: text/x-patch
Size: 1539 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140821/794434b8/attachment.bin>


More information about the llvm-commits mailing list