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