Debian Codegraph @

Utilities/cmjsoncpp/include/json/allocator.h

raw
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 #if !defined(__SUNPRO_CC)
13 #pragma pack(push)
14 #pragma pack()
15 #endif
16
17 namespace Json {
18 template <typename T> class SecureAllocator {
19 public:
20 // Type definitions
21 using value_type = T;
22 using pointer = T*;
23 using const_pointer = const T*;
24 using reference = T&;
25 using const_reference = const T&;
26 using size_type = std::size_t;
27 using difference_type = std::ptrdiff_t;
28
29 /**
30 * Allocate memory for N items using the standard allocator.
31 */
32 pointer allocate(size_type n) {
33 // allocate using "global operator new"
34 return static_cast<pointer>(::operator new(n * sizeof(T)));
35 }
36
37 /**
38 * Release memory which was allocated for N items at pointer P.
39 *
40 * The memory block is filled with zeroes before being released.
41 */
42 void deallocate(pointer p, size_type n) {
43 // memset_s is used because memset may be optimized away by the compiler
44 memset_s(p, n * sizeof(T), 0, n * sizeof(T));
45 // free using "global operator delete"
46 ::operator delete(p);
47 }
48
49 /**
50 * Construct an item in-place at pointer P.
51 */
52 template <typename... Args> 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 { return size_t(-1) / sizeof(T); }
58
59 pointer address(reference x) const { return std::addressof(x); }
60
61 const_pointer address(const_reference x) const { return std::addressof(x); }
62
63 /**
64 * Destroy an item in-place at pointer P.
65 */
66 void destroy(pointer p) {
67 // destroy using "explicit destructor"
68 p->~T();
69 }
70
71 // Boilerplate
72 SecureAllocator() {}
73 template <typename U> SecureAllocator(const SecureAllocator<U>&) {}
74 template <typename U> struct rebind {
75 using other = SecureAllocator<U>;
76 };
77 };
78
79 template <typename T, typename U>
80 bool operator==(const SecureAllocator<T>&, const SecureAllocator<U>&) {
81 return true;
82 }
83
84 template <typename T, typename U>
85 bool operator!=(const SecureAllocator<T>&, const SecureAllocator<U>&) {
86 return false;
87 }
88
89 } // namespace Json
90
91 #if !defined(__SUNPRO_CC)
92 #pragma pack(pop)
93 #endif
94
95 #endif // JSON_ALLOCATOR_H_INCLUDED