Refactor: Removed begin/end_ptr functions.

This commit is contained in:
Karl-Johan Alm 2016-12-09 12:01:37 +09:00
parent 86017842d6
commit 8c1dbc5e9d
11 changed files with 32 additions and 61 deletions

View file

@ -22,7 +22,7 @@ static void RIPEMD160(benchmark::State& state)
uint8_t hash[CRIPEMD160::OUTPUT_SIZE]; uint8_t hash[CRIPEMD160::OUTPUT_SIZE];
std::vector<uint8_t> in(BUFFER_SIZE,0); std::vector<uint8_t> in(BUFFER_SIZE,0);
while (state.KeepRunning()) while (state.KeepRunning())
CRIPEMD160().Write(begin_ptr(in), in.size()).Finalize(hash); CRIPEMD160().Write(in.data(), in.size()).Finalize(hash);
} }
static void SHA1(benchmark::State& state) static void SHA1(benchmark::State& state)
@ -30,7 +30,7 @@ static void SHA1(benchmark::State& state)
uint8_t hash[CSHA1::OUTPUT_SIZE]; uint8_t hash[CSHA1::OUTPUT_SIZE];
std::vector<uint8_t> in(BUFFER_SIZE,0); std::vector<uint8_t> in(BUFFER_SIZE,0);
while (state.KeepRunning()) while (state.KeepRunning())
CSHA1().Write(begin_ptr(in), in.size()).Finalize(hash); CSHA1().Write(in.data(), in.size()).Finalize(hash);
} }
static void SHA256(benchmark::State& state) static void SHA256(benchmark::State& state)
@ -38,7 +38,7 @@ static void SHA256(benchmark::State& state)
uint8_t hash[CSHA256::OUTPUT_SIZE]; uint8_t hash[CSHA256::OUTPUT_SIZE];
std::vector<uint8_t> in(BUFFER_SIZE,0); std::vector<uint8_t> in(BUFFER_SIZE,0);
while (state.KeepRunning()) while (state.KeepRunning())
CSHA256().Write(begin_ptr(in), in.size()).Finalize(hash); CSHA256().Write(in.data(), in.size()).Finalize(hash);
} }
static void SHA256_32b(benchmark::State& state) static void SHA256_32b(benchmark::State& state)
@ -46,7 +46,7 @@ static void SHA256_32b(benchmark::State& state)
std::vector<uint8_t> in(32,0); std::vector<uint8_t> in(32,0);
while (state.KeepRunning()) { while (state.KeepRunning()) {
for (int i = 0; i < 1000000; i++) { for (int i = 0; i < 1000000; i++) {
CSHA256().Write(begin_ptr(in), in.size()).Finalize(&in[0]); CSHA256().Write(in.data(), in.size()).Finalize(&in[0]);
} }
} }
} }
@ -56,7 +56,7 @@ static void SHA512(benchmark::State& state)
uint8_t hash[CSHA512::OUTPUT_SIZE]; uint8_t hash[CSHA512::OUTPUT_SIZE];
std::vector<uint8_t> in(BUFFER_SIZE,0); std::vector<uint8_t> in(BUFFER_SIZE,0);
while (state.KeepRunning()) while (state.KeepRunning())
CSHA512().Write(begin_ptr(in), in.size()).Finalize(hash); CSHA512().Write(in.data(), in.size()).Finalize(hash);
} }
static void SipHash_32b(benchmark::State& state) static void SipHash_32b(benchmark::State& state)

View file

@ -91,7 +91,7 @@ static void VerifyScriptBench(benchmark::State& state)
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << txSpend; stream << txSpend;
int csuccess = bitcoinconsensus_verify_script_with_amount( int csuccess = bitcoinconsensus_verify_script_with_amount(
begin_ptr(txCredit.vout[0].scriptPubKey), txCredit.vout[0].scriptPubKey.data(),
txCredit.vout[0].scriptPubKey.size(), txCredit.vout[0].scriptPubKey.size(),
txCredit.vout[0].nValue, txCredit.vout[0].nValue,
(const unsigned char*)&stream[0], stream.size(), 0, flags, nullptr); (const unsigned char*)&stream[0], stream.size(), 0, flags, nullptr);

View file

@ -292,7 +292,7 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
vSocks5Init.push_back(0x01); // # METHODS vSocks5Init.push_back(0x01); // # METHODS
vSocks5Init.push_back(0x00); // X'00' NO AUTHENTICATION REQUIRED vSocks5Init.push_back(0x00); // X'00' NO AUTHENTICATION REQUIRED
} }
ssize_t ret = send(hSocket, (const char*)begin_ptr(vSocks5Init), vSocks5Init.size(), MSG_NOSIGNAL); ssize_t ret = send(hSocket, (const char*)vSocks5Init.data(), vSocks5Init.size(), MSG_NOSIGNAL);
if (ret != (ssize_t)vSocks5Init.size()) { if (ret != (ssize_t)vSocks5Init.size()) {
CloseSocket(hSocket); CloseSocket(hSocket);
return error("Error sending to proxy"); return error("Error sending to proxy");
@ -317,7 +317,7 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
vAuth.insert(vAuth.end(), auth->username.begin(), auth->username.end()); vAuth.insert(vAuth.end(), auth->username.begin(), auth->username.end());
vAuth.push_back(auth->password.size()); vAuth.push_back(auth->password.size());
vAuth.insert(vAuth.end(), auth->password.begin(), auth->password.end()); vAuth.insert(vAuth.end(), auth->password.begin(), auth->password.end());
ret = send(hSocket, (const char*)begin_ptr(vAuth), vAuth.size(), MSG_NOSIGNAL); ret = send(hSocket, (const char*)vAuth.data(), vAuth.size(), MSG_NOSIGNAL);
if (ret != (ssize_t)vAuth.size()) { if (ret != (ssize_t)vAuth.size()) {
CloseSocket(hSocket); CloseSocket(hSocket);
return error("Error sending authentication to proxy"); return error("Error sending authentication to proxy");
@ -347,7 +347,7 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
vSocks5.insert(vSocks5.end(), strDest.begin(), strDest.end()); vSocks5.insert(vSocks5.end(), strDest.begin(), strDest.end());
vSocks5.push_back((port >> 8) & 0xFF); vSocks5.push_back((port >> 8) & 0xFF);
vSocks5.push_back((port >> 0) & 0xFF); vSocks5.push_back((port >> 0) & 0xFF);
ret = send(hSocket, (const char*)begin_ptr(vSocks5), vSocks5.size(), MSG_NOSIGNAL); ret = send(hSocket, (const char*)vSocks5.data(), vSocks5.size(), MSG_NOSIGNAL);
if (ret != (ssize_t)vSocks5.size()) { if (ret != (ssize_t)vSocks5.size()) {
CloseSocket(hSocket); CloseSocket(hSocket);
return error("Error sending to proxy"); return error("Error sending to proxy");

View file

@ -117,7 +117,7 @@ static std::string DummyAddress(const CChainParams &params)
std::vector<unsigned char> sourcedata = params.Base58Prefix(CChainParams::PUBKEY_ADDRESS); std::vector<unsigned char> sourcedata = params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
sourcedata.insert(sourcedata.end(), dummydata, dummydata + sizeof(dummydata)); sourcedata.insert(sourcedata.end(), dummydata, dummydata + sizeof(dummydata));
for(int i=0; i<256; ++i) { // Try every trailing byte for(int i=0; i<256; ++i) { // Try every trailing byte
std::string s = EncodeBase58(begin_ptr(sourcedata), end_ptr(sourcedata)); std::string s = EncodeBase58(sourcedata.data(), sourcedata.data() + sourcedata.size());
if (!CBitcoinAddress(s).IsValid()) if (!CBitcoinAddress(s).IsValid())
return s; return s;
sourcedata[sourcedata.size()-1] += 1; sourcedata[sourcedata.size()-1] += 1;

View file

@ -11,7 +11,6 @@
#include "compat.h" // for Windows API #include "compat.h" // for Windows API
#include <wincrypt.h> #include <wincrypt.h>
#endif #endif
#include "serialize.h" // for begin_ptr(vec)
#include "util.h" // for LogPrint() #include "util.h" // for LogPrint()
#include "utilstrencodings.h" // for GetTime() #include "utilstrencodings.h" // for GetTime()
@ -72,15 +71,15 @@ static void RandAddSeedPerfmon()
const size_t nMaxSize = 10000000; // Bail out at more than 10MB of performance data const size_t nMaxSize = 10000000; // Bail out at more than 10MB of performance data
while (true) { while (true) {
nSize = vData.size(); nSize = vData.size();
ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, begin_ptr(vData), &nSize); ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, vData.data(), &nSize);
if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize) if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
break; break;
vData.resize(std::max((vData.size() * 3) / 2, nMaxSize)); // Grow size of buffer exponentially vData.resize(std::max((vData.size() * 3) / 2, nMaxSize)); // Grow size of buffer exponentially
} }
RegCloseKey(HKEY_PERFORMANCE_DATA); RegCloseKey(HKEY_PERFORMANCE_DATA);
if (ret == ERROR_SUCCESS) { if (ret == ERROR_SUCCESS) {
RAND_add(begin_ptr(vData), nSize, nSize / 100.0); RAND_add(vData.data(), nSize, nSize / 100.0);
memory_cleanse(begin_ptr(vData), nSize); memory_cleanse(vData.data(), nSize);
LogPrint("rand", "%s: %lu bytes\n", __func__, nSize); LogPrint("rand", "%s: %lu bytes\n", __func__, nSize);
} else { } else {
static bool warned = false; // Warn only once static bool warned = false; // Warn only once

View file

@ -856,15 +856,15 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, un
valtype& vch = stacktop(-1); valtype& vch = stacktop(-1);
valtype vchHash((opcode == OP_RIPEMD160 || opcode == OP_SHA1 || opcode == OP_HASH160) ? 20 : 32); valtype vchHash((opcode == OP_RIPEMD160 || opcode == OP_SHA1 || opcode == OP_HASH160) ? 20 : 32);
if (opcode == OP_RIPEMD160) if (opcode == OP_RIPEMD160)
CRIPEMD160().Write(begin_ptr(vch), vch.size()).Finalize(begin_ptr(vchHash)); CRIPEMD160().Write(vch.data(), vch.size()).Finalize(vchHash.data());
else if (opcode == OP_SHA1) else if (opcode == OP_SHA1)
CSHA1().Write(begin_ptr(vch), vch.size()).Finalize(begin_ptr(vchHash)); CSHA1().Write(vch.data(), vch.size()).Finalize(vchHash.data());
else if (opcode == OP_SHA256) else if (opcode == OP_SHA256)
CSHA256().Write(begin_ptr(vch), vch.size()).Finalize(begin_ptr(vchHash)); CSHA256().Write(vch.data(), vch.size()).Finalize(vchHash.data());
else if (opcode == OP_HASH160) else if (opcode == OP_HASH160)
CHash160().Write(begin_ptr(vch), vch.size()).Finalize(begin_ptr(vchHash)); CHash160().Write(vch.data(), vch.size()).Finalize(vchHash.data());
else if (opcode == OP_HASH256) else if (opcode == OP_HASH256)
CHash256().Write(begin_ptr(vch), vch.size()).Finalize(begin_ptr(vchHash)); CHash256().Write(vch.data(), vch.size()).Finalize(vchHash.data());
popstack(stack); popstack(stack);
stack.push_back(vchHash); stack.push_back(vchHash);
} }

View file

@ -59,34 +59,6 @@ inline T* NCONST_PTR(const T* val)
return const_cast<T*>(val); return const_cast<T*>(val);
} }
/**
* Important: Do not use the following functions in new code, but use v.data()
* and v.data() + v.size() respectively directly. They were once introduced to
* have a compatible, safe way to get the begin and end pointer of a vector.
* However with C++11 the language has built-in functionality for this and it's
* more readable to just use that.
*/
template <typename V>
inline typename V::value_type* begin_ptr(V& v)
{
return v.data();
}
template <typename V>
inline const typename V::value_type* begin_ptr(const V& v)
{
return v.data();
}
template <typename V>
inline typename V::value_type* end_ptr(V& v)
{
return v.data() + v.size();
}
template <typename V>
inline const typename V::value_type* end_ptr(const V& v)
{
return v.data() + v.size();
}
/* /*
* Lowest-level serialization and conversion. * Lowest-level serialization and conversion.
* @note Sizes of these types are verified in the tests * @note Sizes of these types are verified in the tests
@ -390,14 +362,14 @@ public:
template <class T, class TAl> template <class T, class TAl>
explicit CFlatData(std::vector<T,TAl> &v) explicit CFlatData(std::vector<T,TAl> &v)
{ {
pbegin = (char*)begin_ptr(v); pbegin = (char*)v.data();
pend = (char*)end_ptr(v); pend = (char*)(v.data() + v.size());
} }
template <unsigned int N, typename T, typename S, typename D> template <unsigned int N, typename T, typename S, typename D>
explicit CFlatData(prevector<N, T, S, D> &v) explicit CFlatData(prevector<N, T, S, D> &v)
{ {
pbegin = (char*)begin_ptr(v); pbegin = (char*)v.data();
pend = (char*)end_ptr(v); pend = (char*)(v.data() + v.size());
} }
char* begin() { return pbegin; } char* begin() { return pbegin; }
const char* begin() const { return pbegin; } const char* begin() const { return pbegin; }

View file

@ -39,7 +39,7 @@ BOOST_AUTO_TEST_CASE(base58_EncodeBase58)
std::vector<unsigned char> sourcedata = ParseHex(test[0].get_str()); std::vector<unsigned char> sourcedata = ParseHex(test[0].get_str());
std::string base58string = test[1].get_str(); std::string base58string = test[1].get_str();
BOOST_CHECK_MESSAGE( BOOST_CHECK_MESSAGE(
EncodeBase58(begin_ptr(sourcedata), end_ptr(sourcedata)) == base58string, EncodeBase58(sourcedata.data(), sourcedata.data() + sourcedata.size()) == base58string,
strTest); strTest);
} }
} }

View file

@ -176,10 +176,10 @@ void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, const CScript
int libconsensus_flags = flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL; int libconsensus_flags = flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL;
if (libconsensus_flags == flags) { if (libconsensus_flags == flags) {
if (flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS) { if (flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS) {
BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script_with_amount(begin_ptr(scriptPubKey), scriptPubKey.size(), txCredit.vout[0].nValue, (const unsigned char*)&stream[0], stream.size(), 0, libconsensus_flags, NULL) == expect, message); BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script_with_amount(scriptPubKey.data(), scriptPubKey.size(), txCredit.vout[0].nValue, (const unsigned char*)&stream[0], stream.size(), 0, libconsensus_flags, NULL) == expect, message);
} else { } else {
BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script_with_amount(begin_ptr(scriptPubKey), scriptPubKey.size(), 0, (const unsigned char*)&stream[0], stream.size(), 0, libconsensus_flags, NULL) == expect, message); BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script_with_amount(scriptPubKey.data(), scriptPubKey.size(), 0, (const unsigned char*)&stream[0], stream.size(), 0, libconsensus_flags, NULL) == expect, message);
BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script(begin_ptr(scriptPubKey), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size(), 0, libconsensus_flags, NULL) == expect,message); BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size(), 0, libconsensus_flags, NULL) == expect,message);
} }
} }
#endif #endif

View file

@ -501,10 +501,10 @@ static std::vector<uint8_t> ComputeResponse(const std::string &key, const std::v
{ {
CHMAC_SHA256 computeHash((const uint8_t*)key.data(), key.size()); CHMAC_SHA256 computeHash((const uint8_t*)key.data(), key.size());
std::vector<uint8_t> computedHash(CHMAC_SHA256::OUTPUT_SIZE, 0); std::vector<uint8_t> computedHash(CHMAC_SHA256::OUTPUT_SIZE, 0);
computeHash.Write(begin_ptr(cookie), cookie.size()); computeHash.Write(cookie.data(), cookie.size());
computeHash.Write(begin_ptr(clientNonce), clientNonce.size()); computeHash.Write(clientNonce.data(), clientNonce.size());
computeHash.Write(begin_ptr(serverNonce), serverNonce.size()); computeHash.Write(serverNonce.data(), serverNonce.size());
computeHash.Finalize(begin_ptr(computedHash)); computeHash.Finalize(computedHash.data());
return computedHash; return computedHash;
} }

View file

@ -704,13 +704,13 @@ void ShrinkDebugFile()
// Restart the file with some of the end // Restart the file with some of the end
std::vector <char> vch(200000,0); std::vector <char> vch(200000,0);
fseek(file, -((long)vch.size()), SEEK_END); fseek(file, -((long)vch.size()), SEEK_END);
int nBytes = fread(begin_ptr(vch), 1, vch.size(), file); int nBytes = fread(vch.data(), 1, vch.size(), file);
fclose(file); fclose(file);
file = fopen(pathLog.string().c_str(), "w"); file = fopen(pathLog.string().c_str(), "w");
if (file) if (file)
{ {
fwrite(begin_ptr(vch), 1, nBytes, file); fwrite(vch.data(), 1, nBytes, file);
fclose(file); fclose(file);
} }
} }