allocator.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #if !defined(__SUNPRO_CC)
  10. #pragma pack(push, 8)
  11. #endif
  12. namespace Json {
  13. template<typename T>
  14. class SecureAllocator {
  15. public:
  16. // Type definitions
  17. using value_type = T;
  18. using pointer = T*;
  19. using const_pointer = const T*;
  20. using reference = T&;
  21. using const_reference = const T&;
  22. using size_type = std::size_t;
  23. using difference_type = std::ptrdiff_t;
  24. /**
  25. * Allocate memory for N items using the standard allocator.
  26. */
  27. pointer allocate(size_type n) {
  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. std::memset(p, 0, n * sizeof(T));
  40. // free using "global operator delete"
  41. ::operator delete(p);
  42. }
  43. /**
  44. * Construct an item in-place at pointer P.
  45. */
  46. template<typename... Args>
  47. void construct(pointer p, Args&&... args) {
  48. // construct using "placement new" and "perfect forwarding"
  49. ::new (static_cast<void*>(p)) T(std::forward<Args>(args)...);
  50. }
  51. size_type max_size() const {
  52. return size_t(-1) / sizeof(T);
  53. }
  54. pointer address( reference x ) const {
  55. return std::addressof(x);
  56. }
  57. const_pointer address( const_reference x ) const {
  58. return std::addressof(x);
  59. }
  60. /**
  61. * Destroy an item in-place at pointer P.
  62. */
  63. void destroy(pointer p) {
  64. // destroy using "explicit destructor"
  65. p->~T();
  66. }
  67. // Boilerplate
  68. SecureAllocator() {}
  69. template<typename U> SecureAllocator(const SecureAllocator<U>&) {}
  70. template<typename U> struct rebind { using other = SecureAllocator<U>; };
  71. };
  72. template<typename T, typename U>
  73. bool operator==(const SecureAllocator<T>&, const SecureAllocator<U>&) {
  74. return true;
  75. }
  76. template<typename T, typename U>
  77. bool operator!=(const SecureAllocator<T>&, const SecureAllocator<U>&) {
  78. return false;
  79. }
  80. } //namespace Json
  81. #if !defined(__SUNPRO_CC)
  82. #pragma pack(pop)
  83. #endif
  84. #endif // CPPTL_JSON_ALLOCATOR_H_INCLUDED