Skip to content

Commit d8315d1

Browse files
committed
Remove include of windows.h from allocators.h
Create an allocators.cpp, and move all of the #ifdef WIN32 code and the #include of windows.h into it. Two motives for this cleanup: 1. I'm getting a weird error in windows.h in my smartfee branch. 2. allocators.h is included (indirectly) just about everywhere, so this should speed up Windows compiles quite a lot.
1 parent 26002aa commit d8315d1

File tree

3 files changed

+68
-54
lines changed

3 files changed

+68
-54
lines changed

src/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ obj/build.h: FORCE
3131
$(abs_top_srcdir)
3232
version.o: obj/build.h
3333

34-
libbitcoin_a_SOURCES = addrman.cpp alert.cpp bitcoinrpc.cpp bloom.cpp \
34+
libbitcoin_a_SOURCES = addrman.cpp alert.cpp allocators.cpp bitcoinrpc.cpp bloom.cpp \
3535
chainparams.cpp checkpoints.cpp core.cpp crypter.cpp db.cpp hash.cpp \
3636
init.cpp key.cpp keystore.cpp leveldb.cpp main.cpp miner.cpp \
3737
netbase.cpp net.cpp noui.cpp protocol.cpp rpcblockchain.cpp rpcdump.cpp \

src/allocators.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) 2009-2013 The Bitcoin developers
2+
// Distributed under the MIT/X11 software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include "allocators.h"
6+
7+
#ifdef WIN32
8+
#ifdef _WIN32_WINNT
9+
#undef _WIN32_WINNT
10+
#endif
11+
#define _WIN32_WINNT 0x0501
12+
#define WIN32_LEAN_AND_MEAN 1
13+
#ifndef NOMINMAX
14+
#define NOMINMAX
15+
#endif
16+
#include <windows.h>
17+
// This is used to attempt to keep keying material out of swap
18+
// Note that VirtualLock does not provide this as a guarantee on Windows,
19+
// but, in practice, memory that has been VirtualLock'd almost never gets written to
20+
// the pagefile except in rare circumstances where memory is extremely low.
21+
#else
22+
#include <sys/mman.h>
23+
#include <limits.h> // for PAGESIZE
24+
#include <unistd.h> // for sysconf
25+
#endif
26+
27+
/** Determine system page size in bytes */
28+
static inline size_t GetSystemPageSize()
29+
{
30+
size_t page_size;
31+
#if defined(WIN32)
32+
SYSTEM_INFO sSysInfo;
33+
GetSystemInfo(&sSysInfo);
34+
page_size = sSysInfo.dwPageSize;
35+
#elif defined(PAGESIZE) // defined in limits.h
36+
page_size = PAGESIZE;
37+
#else // assume some POSIX OS
38+
page_size = sysconf(_SC_PAGESIZE);
39+
#endif
40+
return page_size;
41+
}
42+
43+
bool MemoryPageLocker::Lock(const void *addr, size_t len)
44+
{
45+
#ifdef WIN32
46+
return VirtualLock(const_cast<void*>(addr), len);
47+
#else
48+
return mlock(addr, len) == 0;
49+
#endif
50+
}
51+
52+
bool MemoryPageLocker::Unlock(const void *addr, size_t len)
53+
{
54+
#ifdef WIN32
55+
return VirtualUnlock(const_cast<void*>(addr), len);
56+
#else
57+
return munlock(addr, len) == 0;
58+
#endif
59+
}
60+
61+
LockedPageManager::LockedPageManager() : LockedPageManagerBase<MemoryPageLocker>(GetSystemPageSize())
62+
{
63+
}
64+

src/allocators.h

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,6 @@
1111
#include <map>
1212
#include <openssl/crypto.h> // for OPENSSL_cleanse()
1313

14-
#ifdef WIN32
15-
#ifdef _WIN32_WINNT
16-
#undef _WIN32_WINNT
17-
#endif
18-
#define _WIN32_WINNT 0x0501
19-
#define WIN32_LEAN_AND_MEAN 1
20-
#ifndef NOMINMAX
21-
#define NOMINMAX
22-
#endif
23-
#include <windows.h>
24-
// This is used to attempt to keep keying material out of swap
25-
// Note that VirtualLock does not provide this as a guarantee on Windows,
26-
// but, in practice, memory that has been VirtualLock'd almost never gets written to
27-
// the pagefile except in rare circumstances where memory is extremely low.
28-
#else
29-
#include <sys/mman.h>
30-
#include <limits.h> // for PAGESIZE
31-
#include <unistd.h> // for sysconf
32-
#endif
3314

3415
/**
3516
* Thread-safe class to keep track of locked (ie, non-swappable) memory pages.
@@ -115,21 +96,6 @@ template <class Locker> class LockedPageManagerBase
11596
Histogram histogram;
11697
};
11798

118-
/** Determine system page size in bytes */
119-
static inline size_t GetSystemPageSize()
120-
{
121-
size_t page_size;
122-
#if defined(WIN32)
123-
SYSTEM_INFO sSysInfo;
124-
GetSystemInfo(&sSysInfo);
125-
page_size = sSysInfo.dwPageSize;
126-
#elif defined(PAGESIZE) // defined in limits.h
127-
page_size = PAGESIZE;
128-
#else // assume some POSIX OS
129-
page_size = sysconf(_SC_PAGESIZE);
130-
#endif
131-
return page_size;
132-
}
13399

134100
/**
135101
* OS-dependent memory page locking/unlocking.
@@ -141,25 +107,11 @@ class MemoryPageLocker
141107
/** Lock memory pages.
142108
* addr and len must be a multiple of the system page size
143109
*/
144-
bool Lock(const void *addr, size_t len)
145-
{
146-
#ifdef WIN32
147-
return VirtualLock(const_cast<void*>(addr), len);
148-
#else
149-
return mlock(addr, len) == 0;
150-
#endif
151-
}
110+
bool Lock(const void *addr, size_t len);
152111
/** Unlock memory pages.
153112
* addr and len must be a multiple of the system page size
154113
*/
155-
bool Unlock(const void *addr, size_t len)
156-
{
157-
#ifdef WIN32
158-
return VirtualUnlock(const_cast<void*>(addr), len);
159-
#else
160-
return munlock(addr, len) == 0;
161-
#endif
162-
}
114+
bool Unlock(const void *addr, size_t len);
163115
};
164116

165117
/**
@@ -171,9 +123,7 @@ class LockedPageManager: public LockedPageManagerBase<MemoryPageLocker>
171123
public:
172124
static LockedPageManager instance; // instantiated in util.cpp
173125
private:
174-
LockedPageManager():
175-
LockedPageManagerBase<MemoryPageLocker>(GetSystemPageSize())
176-
{}
126+
LockedPageManager();
177127
};
178128

179129
//

0 commit comments

Comments
 (0)