allocator.h 2.5 KB

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