blob: 0f5c224b9d72f74ccb590e9cbd2b3f85ec187faf [file] [log] [blame]
Devin Jeanpierre59e4d352017-07-21 03:44:36 -07001// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
dawescf8674c62016-03-06 11:42:11 -06002// 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 Bayles9704ced2019-11-14 09:38:11 -08006#ifndef JSON_ALLOCATOR_H_INCLUDED
7#define JSON_ALLOCATOR_H_INCLUDED
dawescf8674c62016-03-06 11:42:11 -06008
9#include <cstring>
10#include <memory>
11
Sergiy80d6e666f2016-12-03 22:29:14 +020012#pragma pack(push, 8)
13
dawescf8674c62016-03-06 11:42:11 -060014namespace Json {
Billy Donahueb5e1fe82018-05-20 16:55:27 -040015template <typename T> class SecureAllocator {
16public:
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;
dawescf8674c62016-03-06 11:42:11 -060025
Billy Donahueb5e1fe82018-05-20 16:55:27 -040026 /**
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 }
dawescf8674c62016-03-06 11:42:11 -060033
Billy Donahueb5e1fe82018-05-20 16:55:27 -040034 /**
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 */
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 }
dawescf8674c62016-03-06 11:42:11 -060046
Billy Donahueb5e1fe82018-05-20 16:55:27 -040047 /**
48 * Construct an item in-place at pointer P.
49 */
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 }
dawescf8674c62016-03-06 11:42:11 -060054
Billy Donahueb5e1fe82018-05-20 16:55:27 -040055 size_type max_size() const { return size_t(-1) / sizeof(T); }
dawescf8674c62016-03-06 11:42:11 -060056
Billy Donahueb5e1fe82018-05-20 16:55:27 -040057 pointer address(reference x) const { return std::addressof(x); }
dawescf8674c62016-03-06 11:42:11 -060058
Billy Donahueb5e1fe82018-05-20 16:55:27 -040059 const_pointer address(const_reference x) const { return std::addressof(x); }
dawescf8674c62016-03-06 11:42:11 -060060
Billy Donahueb5e1fe82018-05-20 16:55:27 -040061 /**
62 * Destroy an item in-place at pointer P.
63 */
64 void destroy(pointer p) {
65 // destroy using "explicit destructor"
66 p->~T();
67 }
dawescf8674c62016-03-06 11:42:11 -060068
Billy Donahueb5e1fe82018-05-20 16:55:27 -040069 // Boilerplate
70 SecureAllocator() {}
71 template <typename U> SecureAllocator(const SecureAllocator<U>&) {}
72 template <typename U> struct rebind { using other = SecureAllocator<U>; };
dawescf8674c62016-03-06 11:42:11 -060073};
74
Billy Donahueb5e1fe82018-05-20 16:55:27 -040075template <typename T, typename U>
dawescf8674c62016-03-06 11:42:11 -060076bool operator==(const SecureAllocator<T>&, const SecureAllocator<U>&) {
Billy Donahueb5e1fe82018-05-20 16:55:27 -040077 return true;
dawescf8674c62016-03-06 11:42:11 -060078}
79
Billy Donahueb5e1fe82018-05-20 16:55:27 -040080template <typename T, typename U>
dawescf8674c62016-03-06 11:42:11 -060081bool operator!=(const SecureAllocator<T>&, const SecureAllocator<U>&) {
Billy Donahueb5e1fe82018-05-20 16:55:27 -040082 return false;
dawescf8674c62016-03-06 11:42:11 -060083}
84
Billy Donahueb5e1fe82018-05-20 16:55:27 -040085} // namespace Json
dawescf8674c62016-03-06 11:42:11 -060086
Sergiy80d6e666f2016-12-03 22:29:14 +020087#pragma pack(pop)
88
Jordan Bayles9704ced2019-11-14 09:38:11 -080089#endif // JSON_ALLOCATOR_H_INCLUDED