blob: 95ef8a5ec4d66b9aad6a0e0c1252c20a9a245791 [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.
Billy Donahueb5e1fe82018-05-20 16:55:27 -040038 */
Christian Ledergerber30170d62020-10-13 17:55:58 +020039 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 Donahueb5e1fe82018-05-20 16:55:27 -040042 // free using "global operator delete"
43 ::operator delete(p);
44 }
dawescf8674c62016-03-06 11:42:11 -060045
Billy Donahueb5e1fe82018-05-20 16:55:27 -040046 /**
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 }
dawescf8674c62016-03-06 11:42:11 -060053
Billy Donahueb5e1fe82018-05-20 16:55:27 -040054 size_type max_size() const { return size_t(-1) / sizeof(T); }
dawescf8674c62016-03-06 11:42:11 -060055
Billy Donahueb5e1fe82018-05-20 16:55:27 -040056 pointer address(reference x) const { return std::addressof(x); }
dawescf8674c62016-03-06 11:42:11 -060057
Billy Donahueb5e1fe82018-05-20 16:55:27 -040058 const_pointer address(const_reference x) const { return std::addressof(x); }
dawescf8674c62016-03-06 11:42:11 -060059
Billy Donahueb5e1fe82018-05-20 16:55:27 -040060 /**
61 * Destroy an item in-place at pointer P.
62 */
63 void destroy(pointer p) {
64 // destroy using "explicit destructor"
65 p->~T();
66 }
dawescf8674c62016-03-06 11:42:11 -060067
Billy Donahueb5e1fe82018-05-20 16:55:27 -040068 // Boilerplate
69 SecureAllocator() {}
70 template <typename U> SecureAllocator(const SecureAllocator<U>&) {}
71 template <typename U> struct rebind { using other = SecureAllocator<U>; };
dawescf8674c62016-03-06 11:42:11 -060072};
73
Billy Donahueb5e1fe82018-05-20 16:55:27 -040074template <typename T, typename U>
dawescf8674c62016-03-06 11:42:11 -060075bool operator==(const SecureAllocator<T>&, const SecureAllocator<U>&) {
Billy Donahueb5e1fe82018-05-20 16:55:27 -040076 return true;
dawescf8674c62016-03-06 11:42:11 -060077}
78
Billy Donahueb5e1fe82018-05-20 16:55:27 -040079template <typename T, typename U>
dawescf8674c62016-03-06 11:42:11 -060080bool operator!=(const SecureAllocator<T>&, const SecureAllocator<U>&) {
Billy Donahueb5e1fe82018-05-20 16:55:27 -040081 return false;
dawescf8674c62016-03-06 11:42:11 -060082}
83
Billy Donahueb5e1fe82018-05-20 16:55:27 -040084} // namespace Json
dawescf8674c62016-03-06 11:42:11 -060085
Sergiy80d6e666f2016-12-03 22:29:14 +020086#pragma pack(pop)
87
Jordan Bayles9704ced2019-11-14 09:38:11 -080088#endif // JSON_ALLOCATOR_H_INCLUDED