Skip to content

Commit 3e46840

Browse files
author
Christopher Dawes
committed
Support for specifying allocator added
1 parent 9234cbb commit 3e46840

File tree

17 files changed

+7274
-6367
lines changed

17 files changed

+7274
-6367
lines changed

include/json/allocator.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2015 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 https://raw.githubusercontent.com/open-source-parsers/jsoncpp/master/LICENSE
5+
#ifndef CPPTL_JSON_ALLOCATOR_H_INCLUDED
6+
#define CPPTL_JSON_ALLOCATOR_H_INCLUDED
7+
8+
#include <cstring> // std::memset
9+
#include <cstddef> // std::size_t, std::ptrdiff_t
10+
#include <utility> // std::forward
11+
12+
#if !defined(JSON_IS_AMALGAMATION)
13+
#include "config.h"
14+
#endif // if !defined(JSON_IS_AMALGAMATION)
15+
16+
namespace Json {
17+
18+
/**
19+
* This class is used for de-allocating memory securely, allocation happens
20+
* in the normal manner however it's important that de-allocation explicitly
21+
* overwrites the memory with 0s to ensure the memory cannot be "sniffed"
22+
*/
23+
template<typename T>
24+
class JSON_API SecureAllocator {
25+
public:
26+
// Type definitions
27+
using value_type = T;
28+
using pointer = T*;
29+
using const_pointer = const T*;
30+
using reference = T&;
31+
using const_reference = const T&;
32+
using size_type = std::size_t;
33+
using difference_type = std::ptrdiff_t;
34+
35+
/**
36+
* Release memory which was allocated for N items at pointer P.
37+
*
38+
* The memory block is filled with zeroes before being released.
39+
* The pointer argument is tagged as "volatile" to prevent the
40+
* compiler optimizing out this critical step.
41+
*/
42+
void deallocate(volatile pointer p, size_type n) {
43+
std::fill_n((volatile char*)p, n * sizeof(value_type), 0);
44+
std::allocator<T>::deallocate(p, n);
45+
}
46+
};
47+
48+
template<typename T, typename U>
49+
bool operator==(const SecureAllocator<T>&, const SecureAllocator<U>&) {
50+
return true;
51+
}
52+
53+
template<typename T, typename U>
54+
bool operator!=(const SecureAllocator<T>&, const SecureAllocator<U>&) {
55+
return false;
56+
}
57+
58+
} //end namespace Json
59+
60+
#endif // CPPTL_JSON_ALLOCATOR_H_INCLUDED

include/json/config.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@
113113
#define JSONCPP_DEPRECATED(message)
114114
#endif // if !defined(JSONCPP_DEPRECATED)
115115

116+
//We may want to have different presets for the default allocator and
117+
//string
118+
//#define JSONCPP_DEFAULT_CHAR_TRAITS = std::char_traits<char>
119+
//#define JSONCPP_DEFAULT_ALLOCATOR = std::allocator<char>
120+
#define JSONCPP_DEFAULT_CHAR_TRAITS
121+
#define JSONCPP_DEFAULT_ALLOCATOR
122+
116123
namespace Json {
117124
typedef int Int;
118125
typedef unsigned int UInt;

include/json/forwards.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
namespace Json {
1414

1515
// writer.h
16+
template<typename _Traits, typename _Alloc>
1617
class FastWriter;
18+
template<typename _Traits, typename _Alloc>
1719
class StyledWriter;
1820

19-
// reader.h
21+
// reader_declaration.h
22+
template<typename _Traits, typename _Alloc>
2023
class Reader;
2124

2225
// features.h
@@ -25,11 +28,17 @@ class Features;
2528
// value.h
2629
typedef unsigned int ArrayIndex;
2730
class StaticString;
31+
template<typename _Traits, typename _Alloc>
2832
class Path;
33+
template<typename _Traits, typename _Alloc>
2934
class PathArgument;
35+
template<typename _Traits, typename _Alloc>
3036
class Value;
37+
template<typename _Traits, typename _Alloc>
3138
class ValueIteratorBase;
39+
template<typename _Traits, typename _Alloc>
3240
class ValueIterator;
41+
template<typename _Traits, typename _Alloc>
3342
class ValueConstIterator;
3443

3544
} // namespace Json

0 commit comments

Comments
 (0)