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 | |
Jordan Bayles | 9704ced | 2019-11-14 09:38:11 -0800 | [diff] [blame] | 6 | #ifndef JSON_ALLOCATOR_H_INCLUDED |
| 7 | #define JSON_ALLOCATOR_H_INCLUDED |
dawesc | f8674c6 | 2016-03-06 11:42:11 -0600 | [diff] [blame] | 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 { |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 15 | template <typename T> class SecureAllocator { |
| 16 | public: |
| 17 | // Type definitions |
| 18 | using value_type = T; |
| 19 | using pointer = T*; |
| 20 | using const_pointer = const T*; |
| 21 | using reference = T&; |
| 22 | using const_reference = const T&; |
| 23 | using size_type = std::size_t; |
| 24 | using difference_type = std::ptrdiff_t; |
dawesc | f8674c6 | 2016-03-06 11:42:11 -0600 | [diff] [blame] | 25 | |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 26 | /** |
| 27 | * Allocate memory for N items using the standard allocator. |
| 28 | */ |
| 29 | pointer allocate(size_type n) { |
| 30 | // allocate using "global operator new" |
| 31 | return static_cast<pointer>(::operator new(n * sizeof(T))); |
| 32 | } |
dawesc | f8674c6 | 2016-03-06 11:42:11 -0600 | [diff] [blame] | 33 | |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 34 | /** |
| 35 | * Release memory which was allocated for N items at pointer P. |
| 36 | * |
| 37 | * The memory block is filled with zeroes before being released. |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 38 | */ |
Christian Ledergerber | 30170d6 | 2020-10-13 17:55:58 +0200 | [diff] [blame^] | 39 | void deallocate(pointer p, size_type n) { |
| 40 | // memset_s is used because memset may be optimized away by the compiler |
| 41 | memset_s(p, n * sizeof(T), 0, n * sizeof(T)); |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 42 | // free using "global operator delete" |
| 43 | ::operator delete(p); |
| 44 | } |
dawesc | f8674c6 | 2016-03-06 11:42:11 -0600 | [diff] [blame] | 45 | |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 46 | /** |
| 47 | * Construct an item in-place at pointer P. |
| 48 | */ |
| 49 | template <typename... Args> void construct(pointer p, Args&&... args) { |
| 50 | // construct using "placement new" and "perfect forwarding" |
| 51 | ::new (static_cast<void*>(p)) T(std::forward<Args>(args)...); |
| 52 | } |
dawesc | f8674c6 | 2016-03-06 11:42:11 -0600 | [diff] [blame] | 53 | |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 54 | size_type max_size() const { return size_t(-1) / sizeof(T); } |
dawesc | f8674c6 | 2016-03-06 11:42:11 -0600 | [diff] [blame] | 55 | |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 56 | pointer address(reference x) const { return std::addressof(x); } |
dawesc | f8674c6 | 2016-03-06 11:42:11 -0600 | [diff] [blame] | 57 | |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 58 | const_pointer address(const_reference x) const { return std::addressof(x); } |
dawesc | f8674c6 | 2016-03-06 11:42:11 -0600 | [diff] [blame] | 59 | |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 60 | /** |
| 61 | * Destroy an item in-place at pointer P. |
| 62 | */ |
| 63 | void destroy(pointer p) { |
| 64 | // destroy using "explicit destructor" |
| 65 | p->~T(); |
| 66 | } |
dawesc | f8674c6 | 2016-03-06 11:42:11 -0600 | [diff] [blame] | 67 | |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 68 | // Boilerplate |
| 69 | SecureAllocator() {} |
| 70 | template <typename U> SecureAllocator(const SecureAllocator<U>&) {} |
| 71 | template <typename U> struct rebind { using other = SecureAllocator<U>; }; |
dawesc | f8674c6 | 2016-03-06 11:42:11 -0600 | [diff] [blame] | 72 | }; |
| 73 | |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 74 | template <typename T, typename U> |
dawesc | f8674c6 | 2016-03-06 11:42:11 -0600 | [diff] [blame] | 75 | bool operator==(const SecureAllocator<T>&, const SecureAllocator<U>&) { |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 76 | return true; |
dawesc | f8674c6 | 2016-03-06 11:42:11 -0600 | [diff] [blame] | 77 | } |
| 78 | |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 79 | template <typename T, typename U> |
dawesc | f8674c6 | 2016-03-06 11:42:11 -0600 | [diff] [blame] | 80 | bool operator!=(const SecureAllocator<T>&, const SecureAllocator<U>&) { |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 81 | return false; |
dawesc | f8674c6 | 2016-03-06 11:42:11 -0600 | [diff] [blame] | 82 | } |
| 83 | |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 84 | } // namespace Json |
dawesc | f8674c6 | 2016-03-06 11:42:11 -0600 | [diff] [blame] | 85 | |
Sergiy80 | d6e666f | 2016-12-03 22:29:14 +0200 | [diff] [blame] | 86 | #pragma pack(pop) |
| 87 | |
Jordan Bayles | 9704ced | 2019-11-14 09:38:11 -0800 | [diff] [blame] | 88 | #endif // JSON_ALLOCATOR_H_INCLUDED |