Skip to content

Commit 1002cc5

Browse files
dawesccdunn2001
dawesc
authored andcommitted
allocator.h
1 parent 339d400 commit 1002cc5

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

Diff for: include/json/allocator.h

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright 2007-2010 Baptiste Lepilleur
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+
namespace Json {
13+
template<typename T>
14+
class SecureAllocator {
15+
public:
16+
// Type definitions
17+
using value_type = T;
18+
using pointer = T*;
19+
using const_pointer = const T*;
20+
using reference = T&;
21+
using const_reference = const T&;
22+
using size_type = std::size_t;
23+
using difference_type = std::ptrdiff_t;
24+
25+
/**
26+
* Allocate memory for N items using the standard allocator.
27+
*/
28+
pointer allocate(size_type n) {
29+
// allocate using "global operator new"
30+
return static_cast<pointer>(::operator new(n * sizeof(T)));
31+
}
32+
33+
/**
34+
* Release memory which was allocated for N items at pointer P.
35+
*
36+
* The memory block is filled with zeroes before being released.
37+
* The pointer argument is tagged as "volatile" to prevent the
38+
* compiler optimizing out this critical step.
39+
*/
40+
void deallocate(volatile pointer p, size_type n) {
41+
std::memset(p, 0, n * sizeof(T));
42+
// free using "global operator delete"
43+
::operator delete(p);
44+
}
45+
46+
/**
47+
* Construct an item in-place at pointer P.
48+
*/
49+
template<typename... Args>
50+
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 {
56+
return size_t(-1) / sizeof(T);
57+
}
58+
59+
pointer address( reference x ) const {
60+
return std::addressof(x);
61+
}
62+
63+
const_pointer address( const_reference x ) const {
64+
return std::addressof(x);
65+
}
66+
67+
/**
68+
* Destroy an item in-place at pointer P.
69+
*/
70+
void destroy(pointer p) {
71+
// destroy using "explicit destructor"
72+
p->~T();
73+
}
74+
75+
// Boilerplate
76+
SecureAllocator() {}
77+
template<typename U> SecureAllocator(const SecureAllocator<U>&) {}
78+
template<typename U> struct rebind { using other = SecureAllocator<U>; };
79+
};
80+
81+
82+
template<typename T, typename U>
83+
bool operator==(const SecureAllocator<T>&, const SecureAllocator<U>&) {
84+
return true;
85+
}
86+
87+
template<typename T, typename U>
88+
bool operator!=(const SecureAllocator<T>&, const SecureAllocator<U>&) {
89+
return false;
90+
}
91+
92+
} //namespace Json
93+
94+
#endif // CPPTL_JSON_ALLOCATOR_H_INCLUDED

Diff for: include/json/config.h

+18
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@
119119
# define JSON_USE_INT64_DOUBLE_CONVERSION 1
120120
#endif
121121

122+
// If non-zero, the library zeroes any memory that it has allocated before
123+
// it frees its
124+
#ifndef JSON_USE_SECURE_MEMORY
125+
#define JSON_USE_SECURE_MEMORY 0
126+
#endif
127+
128+
#if JSON_USE_SECURE_MEMORY
129+
#include "allocator.h" //typedef Allocator
130+
#endif
131+
122132
namespace Json {
123133
typedef int Int;
124134
typedef unsigned int UInt;
@@ -139,11 +149,19 @@ typedef Int64 LargestInt;
139149
typedef UInt64 LargestUInt;
140150
#define JSON_HAS_INT64
141151
#endif // if defined(JSON_NO_INT64)
152+
#if JSON_USE_SECURE_MEMORY
153+
#define JSONCPP_STRING std::basic_string<char, std::char_traits<char>, SecureAllocator<char> >
154+
#define JSONCPP_OSTRINGSTREAM std::basic_ostringstream<char, std::char_traits<char>, SecureAllocator<char> >
155+
#define JSONCPP_OSTREAM std::basic_ostream<char, std::char_traits<char>>
156+
#define JSONCPP_ISTRINGSTREAM std::basic_istringstream<char, std::char_traits<char>, SecureAllocator<char> >
157+
#define JSONCPP_ISTREAM std::istream
158+
#else
142159
#define JSONCPP_STRING std::string
143160
#define JSONCPP_OSTRINGSTREAM std::ostringstream
144161
#define JSONCPP_OSTREAM std::ostream
145162
#define JSONCPP_ISTRINGSTREAM std::istringstream
146163
#define JSONCPP_ISTREAM std::istream
164+
#endif // if JSON_USE_SECURE_MEMORY
147165
} // end namespace Json
148166

149167
#endif // JSON_CONFIG_H_INCLUDED

0 commit comments

Comments
 (0)