|
| 1 | +#include <cstdlib> |
| 2 | +#include <memory> |
| 3 | +#include <memory_resource> |
| 4 | +#include <scoped_allocator> |
| 5 | + |
| 6 | +class C1 { |
| 7 | +public: |
| 8 | + C1() {} |
| 9 | +}; |
| 10 | + |
| 11 | +// Item 1: Any non-placement form of new or delete |
| 12 | + |
| 13 | +void use_of_new() { |
| 14 | + C1 x1; // compliant: no use of new |
| 15 | + C1 x2{}; // compliant: no use of new |
| 16 | + C1 *x3 = new C1; // non_compliant: use of new |
| 17 | + C1 *x4 = new (&x1) C1; // compliant: placement new (but violates Rule 21.6.3) |
| 18 | +} |
| 19 | + |
| 20 | +void use_of_delete() { |
| 21 | + C1 *x1 = new C1; // non_compliant: use of new |
| 22 | + delete x1; // non_compliant: use of delete |
| 23 | +} |
| 24 | + |
| 25 | +// Item 2: Any of the functions malloc, calloc, realloc, aligned_alloc, free |
| 26 | + |
| 27 | +void use_of_malloc() { |
| 28 | + C1 *x1 = static_cast<C1 *>( |
| 29 | + std::malloc(sizeof(C1))); // non_compliant: use of malloc |
| 30 | + C1 *x2 = static_cast<C1 *>( |
| 31 | + malloc(sizeof(C1))); // non_compliant: use of malloc from global namespace |
| 32 | +} |
| 33 | + |
| 34 | +void use_of_calloc() { |
| 35 | + C1 *x1 = static_cast<C1 *>( |
| 36 | + std::calloc(1, sizeof(C1))); // non_compliant: use of calloc |
| 37 | + C1 *x2 = static_cast<C1 *>(calloc( |
| 38 | + 1, sizeof(C1))); // non_compliant: use of calloc from global namespace |
| 39 | +} |
| 40 | + |
| 41 | +void use_of_realloc() { |
| 42 | + void *p = std::malloc(sizeof(C1)); // non_compliant: use of malloc |
| 43 | + void *q1 = std::realloc(p, sizeof(C1) * 2); // non_compliant: use of realloc |
| 44 | + void *q2 = realloc( |
| 45 | + p, sizeof(C1) * 2); // non_compliant: use of realloc from global namespace |
| 46 | +} |
| 47 | + |
| 48 | +void use_of_aligned_alloc() { |
| 49 | + void *x1 = std::aligned_alloc( |
| 50 | + alignof(C1), sizeof(C1)); // non_compliant: use of aligned_alloc |
| 51 | + void *x2 = aligned_alloc( |
| 52 | + alignof(C1), |
| 53 | + sizeof(C1)); // non_compliant: use of aligned_alloc from global namespace |
| 54 | +} |
| 55 | + |
| 56 | +void use_of_free() { |
| 57 | + C1 *c1 = static_cast<C1 *>( |
| 58 | + std::malloc(sizeof(C1))); // non_compliant: use of malloc |
| 59 | + std::free(c1); // non_compliant: use of free |
| 60 | + free(c1); // non_compliant: use of free from global namespace |
| 61 | +} |
| 62 | + |
| 63 | +// Item 3: Any member function named allocate or deallocate enclosed by |
| 64 | +// namespace std |
| 65 | + |
| 66 | +void use_of_std_allocator() { |
| 67 | + std::allocator<C1> alloc; |
| 68 | + C1 *p1 = alloc.allocate(1); // non_compliant: std::allocator::allocate |
| 69 | + alloc.deallocate(p1, 1); // non_compliant: std::allocator::deallocate |
| 70 | +} |
| 71 | + |
| 72 | +void use_of_allocator_traits() { |
| 73 | + std::allocator<C1> alloc; |
| 74 | + using Traits = std::allocator_traits<std::allocator<C1>>; |
| 75 | + |
| 76 | + C1 *p1 = Traits::allocate( |
| 77 | + alloc, 1); // non_compliant: std::allocator_traits::allocate |
| 78 | + Traits::deallocate(alloc, p1, |
| 79 | + 1); // non_compliant: std::allocator_traits::deallocate |
| 80 | +} |
| 81 | + |
| 82 | +void use_of_memory_resource(std::pmr::memory_resource &mr) { |
| 83 | + void *p1 = |
| 84 | + mr.allocate(sizeof(C1)); // non_compliant: memory_resource::allocate |
| 85 | + mr.deallocate(p1, sizeof(C1)); // non_compliant: memory_resource::deallocate |
| 86 | + |
| 87 | + void *p2 = mr.allocate( |
| 88 | + sizeof(C1), |
| 89 | + alignof(C1)); // non_compliant: memory_resource::allocate (with alignment) |
| 90 | + mr.deallocate( |
| 91 | + p2, sizeof(C1), |
| 92 | + alignof( |
| 93 | + C1)); // non_compliant: memory_resource::deallocate (with alignment) |
| 94 | +} |
| 95 | + |
| 96 | +void use_of_polymorphic_allocator() { |
| 97 | + std::pmr::polymorphic_allocator<C1> alloc; |
| 98 | + C1 *p1 = alloc.allocate(1); // non_compliant: polymorphic_allocator::allocate |
| 99 | + alloc.deallocate(p1, 1); // non_compliant: polymorphic_allocator::deallocate |
| 100 | +} |
| 101 | + |
| 102 | +void use_of_monotonic_buffer_resource() { |
| 103 | + char buffer[1024]; |
| 104 | + std::pmr::monotonic_buffer_resource mr{buffer, sizeof(buffer)}; |
| 105 | + |
| 106 | + void *p1 = mr.allocate( |
| 107 | + sizeof(C1)); // non_compliant: monotonic_buffer_resource::allocate |
| 108 | + mr.deallocate( |
| 109 | + p1, sizeof(C1)); // non_compliant: monotonic_buffer_resource::deallocate |
| 110 | +} |
| 111 | + |
| 112 | +void use_of_pool_resources() { |
| 113 | + std::pmr::unsynchronized_pool_resource unsync_pool; |
| 114 | + void *p1 = unsync_pool.allocate( |
| 115 | + sizeof(C1)); // non_compliant: unsynchronized_pool_resource::allocate |
| 116 | + unsync_pool.deallocate( |
| 117 | + p1, |
| 118 | + sizeof(C1)); // non_compliant: unsynchronized_pool_resource::deallocate |
| 119 | + |
| 120 | + std::pmr::synchronized_pool_resource sync_pool; |
| 121 | + void *p2 = sync_pool.allocate( |
| 122 | + sizeof(C1)); // non_compliant: synchronized_pool_resource::allocate |
| 123 | + sync_pool.deallocate( |
| 124 | + p2, sizeof(C1)); // non_compliant: synchronized_pool_resource::deallocate |
| 125 | +} |
| 126 | + |
| 127 | +void use_of_scoped_allocator_adaptor() { |
| 128 | + using Alloc = std::scoped_allocator_adaptor<std::allocator<C1>>; |
| 129 | + Alloc alloc; |
| 130 | + |
| 131 | + C1 *p1 = |
| 132 | + alloc.allocate(1); // non_compliant: scoped_allocator_adaptor::allocate |
| 133 | + alloc.deallocate(p1, |
| 134 | + 1); // non_compliant: scoped_allocator_adaptor::deallocate |
| 135 | +} |
| 136 | + |
| 137 | +// Item 4: std::unique_ptr::release |
| 138 | + |
| 139 | +void use_of_unique_ptr_release() { |
| 140 | + auto p1 = std::make_unique<C1>(); // compliant: smart pointer creation |
| 141 | + C1 *raw1 = p1.release(); // non_compliant: std::unique_ptr::release |
| 142 | + delete raw1; // non_compliant: use of delete |
| 143 | + |
| 144 | + auto p2 = |
| 145 | + std::make_unique<C1[]>(10); // compliant: smart pointer array creation |
| 146 | + C1 *raw2 = |
| 147 | + p2.release(); // non_compliant: std::unique_ptr::release (array form) |
| 148 | + delete[] raw2; // non_compliant: use of delete[] |
| 149 | +} |
| 150 | + |
| 151 | +void delete_via_get() { |
| 152 | + auto p1 = std::make_unique<C1>(); |
| 153 | + C1 *raw = p1.get(); // compliant: get() is fine |
| 154 | + delete raw; // non_compliant: use of delete (causes double-free!) |
| 155 | +} |
| 156 | + |
| 157 | +int main() { return 0; } |
0 commit comments