build/clang-plugin/tests/TestCustomHeap.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 #define MOZ_NONHEAP_CLASS __attribute__((annotate("moz_nonheap_class")))
michael@0 2 #define MOZ_HEAP_ALLOCATOR \
michael@0 3 _Pragma("GCC diagnostic push") \
michael@0 4 _Pragma("GCC diagnostic ignored \"-Wgcc-compat\"") \
michael@0 5 __attribute__((annotate("moz_heap_allocator"))) \
michael@0 6 _Pragma("GCC diagnostic pop")
michael@0 7
michael@0 8 #include <stdlib.h>
michael@0 9 #include <memory>
michael@0 10
michael@0 11 struct MOZ_NONHEAP_CLASS X {
michael@0 12 };
michael@0 13
michael@0 14 void *operator new(size_t x, int qual) MOZ_HEAP_ALLOCATOR {
michael@0 15 return ::operator new(x);
michael@0 16 }
michael@0 17
michael@0 18 template <typename T>
michael@0 19 T *customAlloc() MOZ_HEAP_ALLOCATOR {
michael@0 20 T *arg = static_cast<T*>(malloc(sizeof(T)));
michael@0 21 return new (arg) T();
michael@0 22 }
michael@0 23
michael@0 24 template <typename T>
michael@0 25 void misuseX(T q) {
michael@0 26 X *foo = customAlloc<X>(); // expected-error {{variable of type 'X' is not valid on the heap}}
michael@0 27 X *foo2 = new (100) X(); // expected-error {{variable of type 'X' is not valid on the heap}}
michael@0 28 }

mercurial