You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.6 KiB

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