Path: blob/master/thirdparty/openxr/src/external/jsoncpp/include/json/allocator.h
9913 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#pragma pack(push)12#pragma pack()1314namespace Json {15template <typename T> class SecureAllocator {16public:17// Type definitions18using value_type = T;19using pointer = T*;20using const_pointer = const T*;21using reference = T&;22using const_reference = const T&;23using size_type = std::size_t;24using difference_type = std::ptrdiff_t;2526/**27* Allocate memory for N items using the standard allocator.28*/29pointer allocate(size_type n) {30// allocate using "global operator new"31return static_cast<pointer>(::operator new(n * sizeof(T)));32}3334/**35* Release memory which was allocated for N items at pointer P.36*37* The memory block is filled with zeroes before being released.38*/39void deallocate(pointer p, size_type n) {40// memset_s is used because memset may be optimized away by the compiler41memset_s(p, n * sizeof(T), 0, n * sizeof(T));42// free using "global operator delete"43::operator delete(p);44}4546/**47* Construct an item in-place at pointer P.48*/49template <typename... Args> void construct(pointer p, Args&&... args) {50// construct using "placement new" and "perfect forwarding"51::new (static_cast<void*>(p)) T(std::forward<Args>(args)...);52}5354size_type max_size() const { return size_t(-1) / sizeof(T); }5556pointer address(reference x) const { return std::addressof(x); }5758const_pointer address(const_reference x) const { return std::addressof(x); }5960/**61* Destroy an item in-place at pointer P.62*/63void destroy(pointer p) {64// destroy using "explicit destructor"65p->~T();66}6768// Boilerplate69SecureAllocator() {}70template <typename U> SecureAllocator(const SecureAllocator<U>&) {}71template <typename U> struct rebind {72using other = SecureAllocator<U>;73};74};7576template <typename T, typename U>77bool operator==(const SecureAllocator<T>&, const SecureAllocator<U>&) {78return true;79}8081template <typename T, typename U>82bool operator!=(const SecureAllocator<T>&, const SecureAllocator<U>&) {83return false;84}8586} // namespace Json8788#pragma pack(pop)8990#endif // JSON_ALLOCATOR_H_INCLUDED919293