allocator.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 JSON_ALLOCATOR_H_INCLUDED
  6. #define JSON_ALLOCATOR_H_INCLUDED
  7. #include <cstring>
  8. #include <memory>
  9. #if !defined(__SUNPRO_CC)
  10. #pragma pack(push, 8)
  11. #endif
  12. namespace Json {
  13. template <typename T> class SecureAllocator {
  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. // allocate using "global operator new"
  28. return static_cast<pointer>(::operator new(n * sizeof(T)));
  29. }
  30. /**
  31. * Release memory which was allocated for N items at pointer P.
  32. *
  33. * The memory block is filled with zeroes before being released.
  34. * The pointer argument is tagged as "volatile" to prevent the
  35. * compiler optimizing out this critical step.
  36. */
  37. void deallocate(volatile pointer p, size_type n) {
  38. std::memset(p, 0, n * sizeof(T));
  39. // free using "global operator delete"
  40. ::operator delete(p);
  41. }
  42. /**
  43. * Construct an item in-place at pointer P.
  44. */
  45. template <typename... Args> void construct(pointer p, Args&&... args) {
  46. // construct using "placement new" and "perfect forwarding"
  47. ::new (static_cast<void*>(p)) T(std::forward<Args>(args)...);
  48. }
  49. size_type max_size() const { return size_t(-1) / sizeof(T); }
  50. pointer address(reference x) const { return std::addressof(x); }
  51. const_pointer address(const_reference x) const { return std::addressof(x); }
  52. /**
  53. * Destroy an item in-place at pointer P.
  54. */
  55. void destroy(pointer p) {
  56. // destroy using "explicit destructor"
  57. p->~T();
  58. }
  59. // Boilerplate
  60. SecureAllocator() {}
  61. template <typename U> SecureAllocator(const SecureAllocator<U>&) {}
  62. template <typename U> struct rebind { using other = SecureAllocator<U>; };
  63. };
  64. template <typename T, typename U>
  65. bool operator==(const SecureAllocator<T>&, const SecureAllocator<U>&) {
  66. return true;
  67. }
  68. template <typename T, typename U>
  69. bool operator!=(const SecureAllocator<T>&, const SecureAllocator<U>&) {
  70. return false;
  71. }
  72. } // namespace Json
  73. #if !defined(__SUNPRO_CC)
  74. #pragma pack(pop)
  75. #endif
  76. #endif // JSON_ALLOCATOR_H_INCLUDED