Path: blob/master/Utilities/cmjsoncpp/include/json/allocator.h
3156 views
// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors1// Distributed under MIT license, or public domain if desired and2// recognized in your jurisdiction.3// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE45#ifndef JSON_ALLOCATOR_H_INCLUDED6#define JSON_ALLOCATOR_H_INCLUDED78#include <cstring>9#include <memory>1011#if !defined(__SUNPRO_CC)12#pragma pack(push)13#pragma pack()14#endif1516namespace Json {17template <typename T> class SecureAllocator {18public:19// Type definitions20using value_type = T;21using pointer = T*;22using const_pointer = const T*;23using reference = T&;24using const_reference = const T&;25using size_type = std::size_t;26using difference_type = std::ptrdiff_t;2728/**29* Allocate memory for N items using the standard allocator.30*/31pointer allocate(size_type n) {32// allocate using "global operator new"33return static_cast<pointer>(::operator new(n * sizeof(T)));34}3536/**37* Release memory which was allocated for N items at pointer P.38*39* The memory block is filled with zeroes before being released.40*/41void deallocate(pointer p, size_type n) {42// memset_s is used because memset may be optimized away by the compiler43memset_s(p, n * sizeof(T), 0, n * sizeof(T));44// free using "global operator delete"45::operator delete(p);46}4748/**49* Construct an item in-place at pointer P.50*/51template <typename... Args> void construct(pointer p, Args&&... args) {52// construct using "placement new" and "perfect forwarding"53::new (static_cast<void*>(p)) T(std::forward<Args>(args)...);54}5556size_type max_size() const { return size_t(-1) / sizeof(T); }5758pointer address(reference x) const { return std::addressof(x); }5960const_pointer address(const_reference x) const { return std::addressof(x); }6162/**63* Destroy an item in-place at pointer P.64*/65void destroy(pointer p) {66// destroy using "explicit destructor"67p->~T();68}6970// Boilerplate71SecureAllocator() {}72template <typename U> SecureAllocator(const SecureAllocator<U>&) {}73template <typename U> struct rebind {74using other = SecureAllocator<U>;75};76};7778template <typename T, typename U>79bool operator==(const SecureAllocator<T>&, const SecureAllocator<U>&) {80return true;81}8283template <typename T, typename U>84bool operator!=(const SecureAllocator<T>&, const SecureAllocator<U>&) {85return false;86}8788} // namespace Json8990#if !defined(__SUNPRO_CC)91#pragma pack(pop)92#endif9394#endif // JSON_ALLOCATOR_H_INCLUDED959697