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.

zeroafterfree.h 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2013 The Bitcoin Core developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #ifndef BITCOIN_SUPPORT_ALLOCATORS_ZEROAFTERFREE_H
  6. #define BITCOIN_SUPPORT_ALLOCATORS_ZEROAFTERFREE_H
  7. #include "support/cleanse.h"
  8. #include <memory>
  9. #include <vector>
  10. template <typename T>
  11. struct zero_after_free_allocator : public std::allocator<T> {
  12. // MSVC8 default copy constructor is broken
  13. typedef std::allocator<T> base;
  14. typedef typename base::size_type size_type;
  15. typedef typename base::difference_type difference_type;
  16. typedef typename base::pointer pointer;
  17. typedef typename base::const_pointer const_pointer;
  18. typedef typename base::reference reference;
  19. typedef typename base::const_reference const_reference;
  20. typedef typename base::value_type value_type;
  21. zero_after_free_allocator() throw() {}
  22. zero_after_free_allocator(const zero_after_free_allocator& a) throw() : base(a) {}
  23. template <typename U>
  24. zero_after_free_allocator(const zero_after_free_allocator<U>& a) throw() : base(a)
  25. {
  26. }
  27. ~zero_after_free_allocator() throw() {}
  28. template <typename _Other>
  29. struct rebind {
  30. typedef zero_after_free_allocator<_Other> other;
  31. };
  32. void deallocate(T* p, std::size_t n)
  33. {
  34. if (p != NULL)
  35. memory_cleanse(p, sizeof(T) * n);
  36. std::allocator<T>::deallocate(p, n);
  37. }
  38. };
  39. // Byte-vector that clears its contents before deletion.
  40. typedef std::vector<char, zero_after_free_allocator<char> > CSerializeData;
  41. #endif // BITCOIN_SUPPORT_ALLOCATORS_ZEROAFTERFREE_H