Remove headers.h
[novacoin.git] / src / allocators.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_ALLOCATORS_H
6 #define BITCOIN_ALLOCATORS_H
7
8 #include <string>
9
10 #ifdef WIN32
11 #ifdef _WIN32_WINNT
12 #undef _WIN32_WINNT
13 #endif
14 #define _WIN32_WINNT 0x0501
15 #define WIN32_LEAN_AND_MEAN 1
16 #ifndef NOMINMAX
17 #define NOMINMAX
18 #endif
19 #include <windows.h>
20 // This is used to attempt to keep keying material out of swap
21 // Note that VirtualLock does not provide this as a guarantee on Windows,
22 // but, in practice, memory that has been VirtualLock'd almost never gets written to
23 // the pagefile except in rare circumstances where memory is extremely low.
24 #define mlock(p, n) VirtualLock((p), (n));
25 #define munlock(p, n) VirtualUnlock((p), (n));
26 #else
27 #include <sys/mman.h>
28 #include <limits.h>
29 /* This comes from limits.h if it's not defined there set a sane default */
30 #ifndef PAGESIZE
31 #include <unistd.h>
32 #define PAGESIZE sysconf(_SC_PAGESIZE)
33 #endif
34 #define mlock(a,b) \
35   mlock(((void *)(((size_t)(a)) & (~((PAGESIZE)-1)))),\
36   (((((size_t)(a)) + (b) - 1) | ((PAGESIZE) - 1)) + 1) - (((size_t)(a)) & (~((PAGESIZE) - 1))))
37 #define munlock(a,b) \
38   munlock(((void *)(((size_t)(a)) & (~((PAGESIZE)-1)))),\
39   (((((size_t)(a)) + (b) - 1) | ((PAGESIZE) - 1)) + 1) - (((size_t)(a)) & (~((PAGESIZE) - 1))))
40 #endif
41
42 //
43 // Allocator that locks its contents from being paged
44 // out of memory and clears its contents before deletion.
45 //
46 template<typename T>
47 struct secure_allocator : public std::allocator<T>
48 {
49     // MSVC8 default copy constructor is broken
50     typedef std::allocator<T> base;
51     typedef typename base::size_type size_type;
52     typedef typename base::difference_type  difference_type;
53     typedef typename base::pointer pointer;
54     typedef typename base::const_pointer const_pointer;
55     typedef typename base::reference reference;
56     typedef typename base::const_reference const_reference;
57     typedef typename base::value_type value_type;
58     secure_allocator() throw() {}
59     secure_allocator(const secure_allocator& a) throw() : base(a) {}
60     template <typename U>
61     secure_allocator(const secure_allocator<U>& a) throw() : base(a) {}
62     ~secure_allocator() throw() {}
63     template<typename _Other> struct rebind
64     { typedef secure_allocator<_Other> other; };
65
66     T* allocate(std::size_t n, const void *hint = 0)
67     {
68         T *p;
69         p = std::allocator<T>::allocate(n, hint);
70         if (p != NULL)
71             mlock(p, sizeof(T) * n);
72         return p;
73     }
74
75     void deallocate(T* p, std::size_t n)
76     {
77         if (p != NULL)
78         {
79             memset(p, 0, sizeof(T) * n);
80             munlock(p, sizeof(T) * n);
81         }
82         std::allocator<T>::deallocate(p, n);
83     }
84 };
85
86
87 //
88 // Allocator that clears its contents before deletion.
89 //
90 template<typename T>
91 struct zero_after_free_allocator : public std::allocator<T>
92 {
93     // MSVC8 default copy constructor is broken
94     typedef std::allocator<T> base;
95     typedef typename base::size_type size_type;
96     typedef typename base::difference_type  difference_type;
97     typedef typename base::pointer pointer;
98     typedef typename base::const_pointer const_pointer;
99     typedef typename base::reference reference;
100     typedef typename base::const_reference const_reference;
101     typedef typename base::value_type value_type;
102     zero_after_free_allocator() throw() {}
103     zero_after_free_allocator(const zero_after_free_allocator& a) throw() : base(a) {}
104     template <typename U>
105     zero_after_free_allocator(const zero_after_free_allocator<U>& a) throw() : base(a) {}
106     ~zero_after_free_allocator() throw() {}
107     template<typename _Other> struct rebind
108     { typedef zero_after_free_allocator<_Other> other; };
109
110     void deallocate(T* p, std::size_t n)
111     {
112         if (p != NULL)
113             memset(p, 0, sizeof(T) * n);
114         std::allocator<T>::deallocate(p, n);
115     }
116 };
117
118 // This is exactly like std::string, but with a custom allocator.
119 // (secure_allocator<> is defined in serialize.h)
120 typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
121
122 #endif