Wallet.h cleanup
[novacoin.git] / src / allocators.h
index 99afa10..c787b35 100644 (file)
@@ -5,16 +5,20 @@
 #ifndef BITCOIN_ALLOCATORS_H
 #define BITCOIN_ALLOCATORS_H
 
-#include <string.h>
+#include <openssl/crypto.h> // for OPENSSL_cleanse()
+
+#include <cstring>
 #include <string>
-#include <boost/thread/mutex.hpp>
+#include <mutex>
+#include <cassert>
 #include <map>
+#include <vector>
 
 #ifdef WIN32
 #ifdef _WIN32_WINNT
 #undef _WIN32_WINNT
 #endif
-#define _WIN32_WINNT 0x0501
+#define _WIN32_WINNT 0x0601
 #define WIN32_LEAN_AND_MEAN 1
 #ifndef NOMINMAX
 #define NOMINMAX
@@ -26,7 +30,7 @@
 // the pagefile except in rare circumstances where memory is extremely low.
 #else
 #include <sys/mman.h>
-#include <limits.h> // for PAGESIZE
+#include <climits> // for PAGESIZE
 #include <unistd.h> // for sysconf
 #endif
 
@@ -55,7 +59,7 @@ public:
     // For all pages in affected range, increase lock count
     void LockRange(void *p, size_t size)
     {
-        boost::mutex::scoped_lock lock(mutex);
+        std::scoped_lock lock(mutex);
         if(!size) return;
         const size_t base_addr = reinterpret_cast<size_t>(p);
         const size_t start_page = base_addr & page_mask;
@@ -78,7 +82,7 @@ public:
     // For all pages in affected range, decrease lock count
     void UnlockRange(void *p, size_t size)
     {
-        boost::mutex::scoped_lock lock(mutex);
+        std::scoped_lock lock(mutex);
         if(!size) return;
         const size_t base_addr = reinterpret_cast<size_t>(p);
         const size_t start_page = base_addr & page_mask;
@@ -101,13 +105,13 @@ public:
     // Get number of locked pages for diagnostics
     int GetLockedPageCount()
     {
-        boost::mutex::scoped_lock lock(mutex);
+        std::scoped_lock lock(mutex);
         return histogram.size();
     }
 
 private:
     Locker locker;
-    boost::mutex mutex;
+    std::mutex mutex;
     size_t page_size, page_mask;
     // map of page base address to lock count
     typedef std::map<size_t,int> Histogram;
@@ -143,7 +147,7 @@ public:
     bool Lock(const void *addr, size_t len)
     {
 #ifdef WIN32
-        return VirtualLock(const_cast<void*>(addr), len);
+        return VirtualLock(const_cast<void*>(addr), len) != 0;
 #else
         return mlock(addr, len) == 0;
 #endif
@@ -154,7 +158,7 @@ public:
     bool Unlock(const void *addr, size_t len)
     {
 #ifdef WIN32
-        return VirtualUnlock(const_cast<void*>(addr), len);
+        return VirtualUnlock(const_cast<void*>(addr), len) != 0;
 #else
         return munlock(addr, len) == 0;
 #endif
@@ -212,7 +216,7 @@ struct secure_allocator : public std::allocator<T>
     {
         if (p != NULL)
         {
-            memset(p, 0, sizeof(T) * n);
+            OPENSSL_cleanse(p, sizeof(T) * n);
             LockedPageManager::instance.UnlockRange(p, sizeof(T) * n);
         }
         std::allocator<T>::deallocate(p, n);
@@ -246,7 +250,7 @@ struct zero_after_free_allocator : public std::allocator<T>
     void deallocate(T* p, std::size_t n)
     {
         if (p != NULL)
-            memset(p, 0, sizeof(T) * n);
+            OPENSSL_cleanse(p, sizeof(T) * n);
         std::allocator<T>::deallocate(p, n);
     }
 };
@@ -254,4 +258,7 @@ struct zero_after_free_allocator : public std::allocator<T>
 // This is exactly like std::string, but with a custom allocator.
 typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
 
+// Byte-vector that clears its contents before deletion.
+typedef std::vector<char, zero_after_free_allocator<char> > CSerializeData;
+
 #endif