1 // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors 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 JSON_ALLOCATOR_H_INCLUDED 7 #define JSON_ALLOCATOR_H_INCLUDED 8 9 #include <cstring> 10 #include <memory> 11 12 #pragma pack(push, 8) 13 14 namespace Json { 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; 25 26 /** 27 * Allocate memory for N items using the standard allocator. 28 */ allocate(size_type n)29 pointer allocate(size_type n) { 30 // allocate using "global operator new" 31 return static_cast<pointer>(::operator new(n * sizeof(T))); 32 } 33 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. 38 * The pointer argument is tagged as "volatile" to prevent the 39 * compiler optimizing out this critical step. 40 */ deallocate(volatile pointer p,size_type n)41 void deallocate(volatile pointer p, size_type n) { 42 std::memset(p, 0, n * sizeof(T)); 43 // free using "global operator delete" 44 ::operator delete(p); 45 } 46 47 /** 48 * Construct an item in-place at pointer P. 49 */ construct(pointer p,Args &&...args)50 template <typename... Args> void construct(pointer p, Args&&... args) { 51 // construct using "placement new" and "perfect forwarding" 52 ::new (static_cast<void*>(p)) T(std::forward<Args>(args)...); 53 } 54 max_size()55 size_type max_size() const { return size_t(-1) / sizeof(T); } 56 address(reference x)57 pointer address(reference x) const { return std::addressof(x); } 58 address(const_reference x)59 const_pointer address(const_reference x) const { return std::addressof(x); } 60 61 /** 62 * Destroy an item in-place at pointer P. 63 */ destroy(pointer p)64 void destroy(pointer p) { 65 // destroy using "explicit destructor" 66 p->~T(); 67 } 68 69 // Boilerplate SecureAllocator()70 SecureAllocator() {} SecureAllocator(const SecureAllocator<U> &)71 template <typename U> SecureAllocator(const SecureAllocator<U>&) {} 72 template <typename U> struct rebind { using other = SecureAllocator<U>; }; 73 }; 74 75 template <typename T, typename U> 76 bool operator==(const SecureAllocator<T>&, const SecureAllocator<U>&) { 77 return true; 78 } 79 80 template <typename T, typename U> 81 bool operator!=(const SecureAllocator<T>&, const SecureAllocator<U>&) { 82 return false; 83 } 84 85 } // namespace Json 86 87 #pragma pack(pop) 88 89 #endif // JSON_ALLOCATOR_H_INCLUDED 90