JsonCpp project page | Classes | Namespace | JsonCpp home page

allocator.h
Go to the documentation of this file.
// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
// Distributed under MIT license, or public domain if desired and
// recognized in your jurisdiction.
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
#ifndef JSON_ALLOCATOR_H_INCLUDED
#define JSON_ALLOCATOR_H_INCLUDED
#include <algorithm>
#include <cstring>
#include <memory>
#pragma pack(push)
#pragma pack()
namespace Json {
template <typename T> class SecureAllocator {
public:
// Type definitions
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
pointer allocate(size_type n) {
// allocate using "global operator new"
return static_cast<pointer>(::operator new(n * sizeof(T)));
}
void deallocate(pointer p, size_type n) {
// These constructs will not be removed by the compiler during optimization,
// unlike memset.
#if defined(HAVE_MEMSET_S)
memset_s(p, n * sizeof(T), 0, n * sizeof(T));
#elif defined(_WIN32)
RtlSecureZeroMemory(p, n * sizeof(T));
#else
std::fill_n(reinterpret_cast<volatile unsigned char*>(p), n, 0);
#endif
// free using "global operator delete"
::operator delete(p);
}
template <typename... Args> void construct(pointer p, Args&&... args) {
// construct using "placement new" and "perfect forwarding"
::new (static_cast<void*>(p)) T(std::forward<Args>(args)...);
}
size_type max_size() const { return size_t(-1) / sizeof(T); }
pointer address(reference x) const { return std::addressof(x); }
const_pointer address(const_reference x) const { return std::addressof(x); }
void destroy(pointer p) {
// destroy using "explicit destructor"
p->~T();
}
// Boilerplate
SecureAllocator() {}
template <typename U> SecureAllocator(const SecureAllocator<U>&) {}
template <typename U> struct rebind {
using other = SecureAllocator<U>;
};
};
template <typename T, typename U>
bool operator==(const SecureAllocator<T>&, const SecureAllocator<U>&) {
return true;
}
template <typename T, typename U>
bool operator!=(const SecureAllocator<T>&, const SecureAllocator<U>&) {
return false;
}
} // namespace Json
#pragma pack(pop)
#endif // JSON_ALLOCATOR_H_INCLUDED
Json::SecureAllocator
Definition allocator.h:17
Json::SecureAllocator::max_size
size_type max_size() const
Definition allocator.h:64
Json::SecureAllocator::address
const_pointer address(const_reference x) const
Definition allocator.h:68
Json::SecureAllocator::address
pointer address(reference x) const
Definition allocator.h:66
Json::SecureAllocator::const_reference
const T & const_reference
Definition allocator.h:24
Json::SecureAllocator::difference_type
std::ptrdiff_t difference_type
Definition allocator.h:26
Json::SecureAllocator::pointer
T * pointer
Definition allocator.h:21
Json::SecureAllocator::const_pointer
const T * const_pointer
Definition allocator.h:22
Json::SecureAllocator::reference
T & reference
Definition allocator.h:23
Json::SecureAllocator::size_type
std::size_t size_type
Definition allocator.h:25
Json::SecureAllocator::destroy
void destroy(pointer p)
Destroy an item in-place at pointer P.
Definition allocator.h:73
Json::SecureAllocator::deallocate
void deallocate(pointer p, size_type n)
Release memory which was allocated for N items at pointer P.
Definition allocator.h:41
Json::SecureAllocator::allocate
pointer allocate(size_type n)
Allocate memory for N items using the standard allocator.
Definition allocator.h:31
Json::SecureAllocator::SecureAllocator
SecureAllocator()
Definition allocator.h:79
Json::SecureAllocator::construct
void construct(pointer p, Args &&... args)
Construct an item in-place at pointer P.
Definition allocator.h:59
Json::SecureAllocator::value_type
T value_type
Definition allocator.h:20
Json::SecureAllocator::SecureAllocator
SecureAllocator(const SecureAllocator< U > &)
Definition allocator.h:80
Json
JSON (JavaScript Object Notation).
Definition allocator.h:16
Json::operator==
bool operator==(const SecureAllocator< T > &, const SecureAllocator< U > &)
Definition allocator.h:87
Json::operator!=
bool operator!=(const SecureAllocator< T > &, const SecureAllocator< U > &)
Definition allocator.h:92
Json::SecureAllocator::rebind
Definition allocator.h:81
Json::SecureAllocator::rebind::other
SecureAllocator< U > other
Definition allocator.h:82