JsonCpp project page Classes Namespace 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 JSON_ALLOCATOR_H_INCLUDED
7 #define JSON_ALLOCATOR_H_INCLUDED
8 
9 #include <cstring>
10 #include <memory>
11 
12 #pragma pack(push)
13 #pragma pack()
14 
15 namespace Json {
16 template <typename T> 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 
31  // allocate using "global operator new"
32  return static_cast<pointer>(::operator new(n * sizeof(T)));
33  }
34 
41  // memset_s is used because memset may be optimized away by the compiler
42  memset_s(p, n * sizeof(T), 0, n * sizeof(T));
43  // free using "global operator delete"
44  ::operator delete(p);
45  }
46 
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 
55  size_type max_size() const { return size_t(-1) / sizeof(T); }
56 
57  pointer address(reference x) const { return std::addressof(x); }
58 
59  const_pointer address(const_reference x) const { return std::addressof(x); }
60 
64  void destroy(pointer p) {
65  // destroy using "explicit destructor"
66  p->~T();
67  }
68 
69  // Boilerplate
71  template <typename U> SecureAllocator(const SecureAllocator<U>&) {}
72  template <typename U> struct rebind {
74  };
75 };
76 
77 template <typename T, typename U>
79  return true;
80 }
81 
82 template <typename T, typename U>
84  return false;
85 }
86 
87 } // namespace Json
88 
89 #pragma pack(pop)
90 
91 #endif // JSON_ALLOCATOR_H_INCLUDED
size_type max_size() const
Definition: allocator.h:55
const_pointer address(const_reference x) const
Definition: allocator.h:59
pointer address(reference x) const
Definition: allocator.h:57
const T & const_reference
Definition: allocator.h:23
std::ptrdiff_t difference_type
Definition: allocator.h:25
const T * const_pointer
Definition: allocator.h:21
std::size_t size_type
Definition: allocator.h:24
void destroy(pointer p)
Destroy an item in-place at pointer P.
Definition: allocator.h:64
void deallocate(pointer p, size_type n)
Release memory which was allocated for N items at pointer P.
Definition: allocator.h:40
pointer allocate(size_type n)
Allocate memory for N items using the standard allocator.
Definition: allocator.h:30
void construct(pointer p, Args &&... args)
Construct an item in-place at pointer P.
Definition: allocator.h:50
SecureAllocator(const SecureAllocator< U > &)
Definition: allocator.h:71
JSON (JavaScript Object Notation).
Definition: allocator.h:15
bool operator==(const SecureAllocator< T > &, const SecureAllocator< U > &)
Definition: allocator.h:78
bool operator!=(const SecureAllocator< T > &, const SecureAllocator< U > &)
Definition: allocator.h:83