Devin Jeanpierre | 59e4d35 | 2017-07-21 03:44:36 -0700 | [diff] [blame] | 1 | // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors |
dawesc | f8674c6 | 2016-03-06 11:42:11 -0600 | [diff] [blame] | 2 | // Distributed under MIT license, or public domain if desired and |
| 3 | // recognized in your jurisdiction. |
| 4 | // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE |
| 5 | |
| 6 | #ifndef CPPTL_JSON_ALLOCATOR_H_INCLUDED |
| 7 | #define CPPTL_JSON_ALLOCATOR_H_INCLUDED |
| 8 | |
| 9 | #include <cstring> |
| 10 | #include <memory> |
| 11 | |
Sergiy80 | d6e666f | 2016-12-03 22:29:14 +0200 | [diff] [blame] | 12 | #pragma pack(push, 8) |
| 13 | |
dawesc | f8674c6 | 2016-03-06 11:42:11 -0600 | [diff] [blame] | 14 | namespace Json { |
| 15 | template<typename T> |
| 16 | class SecureAllocator { |
| 17 | public: |
| 18 | // Type definitions |
| 19 | using value_type = T; |
| 20 | using pointer = T*; |
| 21 | using const_pointer = const T*; |
| 22 | using reference = T&; |
| 23 | using const_reference = const T&; |
| 24 | using size_type = std::size_t; |
| 25 | using difference_type = std::ptrdiff_t; |
| 26 | |
| 27 | /** |
| 28 | * Allocate memory for N items using the standard allocator. |
| 29 | */ |
| 30 | pointer allocate(size_type n) { |
| 31 | // allocate using "global operator new" |
| 32 | return static_cast<pointer>(::operator new(n * sizeof(T))); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Release memory which was allocated for N items at pointer P. |
| 37 | * |
| 38 | * The memory block is filled with zeroes before being released. |
| 39 | * The pointer argument is tagged as "volatile" to prevent the |
| 40 | * compiler optimizing out this critical step. |
| 41 | */ |
| 42 | void deallocate(volatile pointer p, size_type n) { |
| 43 | std::memset(p, 0, n * sizeof(T)); |
| 44 | // free using "global operator delete" |
| 45 | ::operator delete(p); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Construct an item in-place at pointer P. |
| 50 | */ |
| 51 | template<typename... Args> |
| 52 | void construct(pointer p, Args&&... args) { |
| 53 | // construct using "placement new" and "perfect forwarding" |
| 54 | ::new (static_cast<void*>(p)) T(std::forward<Args>(args)...); |
| 55 | } |
| 56 | |
| 57 | size_type max_size() const { |
| 58 | return size_t(-1) / sizeof(T); |
| 59 | } |
| 60 | |
| 61 | pointer address( reference x ) const { |
| 62 | return std::addressof(x); |
| 63 | } |
| 64 | |
| 65 | const_pointer address( const_reference x ) const { |
| 66 | return std::addressof(x); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Destroy an item in-place at pointer P. |
| 71 | */ |
| 72 | void destroy(pointer p) { |
| 73 | // destroy using "explicit destructor" |
| 74 | p->~T(); |
| 75 | } |
| 76 | |
| 77 | // Boilerplate |
| 78 | SecureAllocator() {} |
| 79 | template<typename U> SecureAllocator(const SecureAllocator<U>&) {} |
| 80 | template<typename U> struct rebind { using other = SecureAllocator<U>; }; |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | template<typename T, typename U> |
| 85 | bool operator==(const SecureAllocator<T>&, const SecureAllocator<U>&) { |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | template<typename T, typename U> |
| 90 | bool operator!=(const SecureAllocator<T>&, const SecureAllocator<U>&) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | } //namespace Json |
| 95 | |
Sergiy80 | d6e666f | 2016-12-03 22:29:14 +0200 | [diff] [blame] | 96 | #pragma pack(pop) |
| 97 | |
dawesc | f8674c6 | 2016-03-06 11:42:11 -0600 | [diff] [blame] | 98 | #endif // CPPTL_JSON_ALLOCATOR_H_INCLUDED |