JsonCpp project page | JsonCpp home page

allocator.h
Go to the documentation of this file.
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 CPPTL_JSON_ALLOCATOR_H_INCLUDED
7 #define CPPTL_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>
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
30 pointer allocate(size_type n) {
31 // allocate using "global operator new"
32 return static_cast<pointer>(::operator new(n * sizeof(T)));
33 }
34
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
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
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
96 #pragma pack(pop)
97
98 #endif // CPPTL_JSON_ALLOCATOR_H_INCLUDED
Json::SecureAllocator::const_reference
const T & const_reference
Definition: allocator.h:23
Json::SecureAllocator::construct
void construct(pointer p, Args &&... args)
Construct an item in-place at pointer P.
Definition: allocator.h:52
Json::SecureAllocator::rebind
Definition: allocator.h:80
Json::SecureAllocator::allocate
pointer allocate(size_type n)
Allocate memory for N items using the standard allocator.
Definition: allocator.h:30
Json::SecureAllocator::max_size
size_type max_size() const
Definition: allocator.h:57
Json::SecureAllocator::SecureAllocator
SecureAllocator(const SecureAllocator< U > &)
Definition: allocator.h:79
Json::SecureAllocator::destroy
void destroy(pointer p)
Destroy an item in-place at pointer P.
Definition: allocator.h:72
Json::SecureAllocator::SecureAllocator
SecureAllocator()
Definition: allocator.h:78
Json::SecureAllocator::pointer
T * pointer
Definition: allocator.h:20
Json
JSON (JavaScript Object Notation).
Definition: allocator.h:14
Json::operator==
bool operator==(const SecureAllocator< T > &, const SecureAllocator< U > &)
Definition: allocator.h:85
Json::SecureAllocator::const_pointer
const T * const_pointer
Definition: allocator.h:21
Json::SecureAllocator::value_type
T value_type
Definition: allocator.h:19
Json::SecureAllocator::address
pointer address(reference x) const
Definition: allocator.h:61
Json::SecureAllocator::difference_type
std::ptrdiff_t difference_type
Definition: allocator.h:25
Json::SecureAllocator
Definition: allocator.h:16
Json::SecureAllocator::reference
T & reference
Definition: allocator.h:22
Json::operator!=
bool operator!=(const SecureAllocator< T > &, const SecureAllocator< U > &)
Definition: allocator.h:90
Json::SecureAllocator::size_type
std::size_t size_type
Definition: allocator.h:24
Json::SecureAllocator::address
const_pointer address(const_reference x) const
Definition: allocator.h:65
Json::SecureAllocator::deallocate
void deallocate(volatile pointer p, size_type n)
Release memory which was allocated for N items at pointer P.
Definition: allocator.h:42