Skip to content

Commit

Permalink
Cleanup code using forward declarations.
Browse files Browse the repository at this point in the history
Use misc methods of avoiding unnecesary header includes.
Replace int typedefs with int##_t from stdint.h.
Replace PRI64[xdu] with PRI[xdu]64 from inttypes.h.
Normalize QT_VERSION ifs where possible.
Resolve some indirect dependencies as direct ones.
Remove extern declarations from .cpp files.
  • Loading branch information
brandondahler committed Nov 10, 2013
1 parent 7c4c207 commit 51ed9ec
Show file tree
Hide file tree
Showing 177 changed files with 2,028 additions and 1,625 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ PKG_PROG_PKG_CONFIG
## compatibility with the legacy buildsystem.
##
CXXFLAGS="$CXXFLAGS -Wall -Wextra -Wformat -Wformat-security -Wno-unused-parameter"
CPPFLAGS="$CPPFLAGS -DBOOST_SPIRIT_THREADSAFE -DHAVE_BUILD_INFO"
CPPFLAGS="$CPPFLAGS -DBOOST_SPIRIT_THREADSAFE -DHAVE_BUILD_INFO -D__STDC_FORMAT_MACROS"

AC_LANG_PUSH([C++])

Expand Down
7 changes: 5 additions & 2 deletions share/qt/extract_strings_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ def parse_po(text):
messages = parse_po(out)

f = open(OUT_CPP, 'w')
f.write("""#include <QtGlobal>
f.write("""
#include <QtGlobal>
// Automatically generated by extract_strings.py
#ifdef __GNUC__
#define UNUSED __attribute__((unused))
Expand All @@ -70,5 +73,5 @@ def parse_po(text):
for (msgid, msgstr) in messages:
if msgid != EMPTY:
f.write('QT_TRANSLATE_NOOP("bitcoin-core", %s),\n' % ('\n'.join(msgid)))
f.write('};')
f.write('};\n')
f.close()
2 changes: 1 addition & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ BITCOIN_CORE_H = addrman.h alert.h allocators.h base58.h bignum.h \
bitcoinrpc.h bloom.h chainparams.h checkpoints.h checkqueue.h \
clientversion.h compat.h core.h crypter.h db.h hash.h init.h \
key.h keystore.h leveldbwrapper.h limitedmap.h main.h miner.h mruset.h \
netbase.h net.h protocol.h script.h serialize.h sync.h threadsafety.h \
netbase.h net.h noui.h protocol.h script.h serialize.h sync.h threadsafety.h \
txdb.h txmempool.h ui_interface.h uint256.h util.h version.h walletdb.h wallet.h

JSON_H = json/json_spirit.h json/json_spirit_error_position.h \
Expand Down
36 changes: 19 additions & 17 deletions src/addrman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "addrman.h"

#include "hash.h"
#include "serialize.h"

using namespace std;

Expand All @@ -12,12 +14,12 @@ int CAddrInfo::GetTriedBucket(const std::vector<unsigned char> &nKey) const
CDataStream ss1(SER_GETHASH, 0);
std::vector<unsigned char> vchKey = GetKey();
ss1 << nKey << vchKey;
uint64 hash1 = Hash(ss1.begin(), ss1.end()).Get64();
uint64_t hash1 = Hash(ss1.begin(), ss1.end()).Get64();

CDataStream ss2(SER_GETHASH, 0);
std::vector<unsigned char> vchGroupKey = GetGroup();
ss2 << nKey << vchGroupKey << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP);
uint64 hash2 = Hash(ss2.begin(), ss2.end()).Get64();
uint64_t hash2 = Hash(ss2.begin(), ss2.end()).Get64();
return hash2 % ADDRMAN_TRIED_BUCKET_COUNT;
}

Expand All @@ -27,15 +29,15 @@ int CAddrInfo::GetNewBucket(const std::vector<unsigned char> &nKey, const CNetAd
std::vector<unsigned char> vchGroupKey = GetGroup();
std::vector<unsigned char> vchSourceGroupKey = src.GetGroup();
ss1 << nKey << vchGroupKey << vchSourceGroupKey;
uint64 hash1 = Hash(ss1.begin(), ss1.end()).Get64();
uint64_t hash1 = Hash(ss1.begin(), ss1.end()).Get64();

CDataStream ss2(SER_GETHASH, 0);
ss2 << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP);
uint64 hash2 = Hash(ss2.begin(), ss2.end()).Get64();
uint64_t hash2 = Hash(ss2.begin(), ss2.end()).Get64();
return hash2 % ADDRMAN_NEW_BUCKET_COUNT;
}

bool CAddrInfo::IsTerrible(int64 nNow) const
bool CAddrInfo::IsTerrible(int64_t nNow) const
{
if (nLastTry && nLastTry >= nNow-60) // never remove things tried the last minute
return false;
Expand All @@ -55,12 +57,12 @@ bool CAddrInfo::IsTerrible(int64 nNow) const
return false;
}

double CAddrInfo::GetChance(int64 nNow) const
double CAddrInfo::GetChance(int64_t nNow) const
{
double fChance = 1.0;

int64 nSinceLastSeen = nNow - nTime;
int64 nSinceLastTry = nNow - nLastTry;
int64_t nSinceLastSeen = nNow - nTime;
int64_t nSinceLastTry = nNow - nLastTry;

if (nSinceLastSeen < 0) nSinceLastSeen = 0;
if (nSinceLastTry < 0) nSinceLastTry = 0;
Expand Down Expand Up @@ -129,7 +131,7 @@ int CAddrMan::SelectTried(int nKBucket)

// random shuffle the first few elements (using the entire list)
// find the least recently tried among them
int64 nOldest = -1;
int64_t nOldest = -1;
int nOldestPos = -1;
for (unsigned int i = 0; i < ADDRMAN_TRIED_ENTRIES_INSPECT_ON_EVICT && i < vTried.size(); i++)
{
Expand Down Expand Up @@ -259,7 +261,7 @@ void CAddrMan::MakeTried(CAddrInfo& info, int nId, int nOrigin)
return;
}

void CAddrMan::Good_(const CService &addr, int64 nTime)
void CAddrMan::Good_(const CService &addr, int64_t nTime)
{
int nId;
CAddrInfo *pinfo = Find(addr, &nId);
Expand Down Expand Up @@ -308,7 +310,7 @@ void CAddrMan::Good_(const CService &addr, int64 nTime)
MakeTried(info, nId, nUBucket);
}

bool CAddrMan::Add_(const CAddress &addr, const CNetAddr& source, int64 nTimePenalty)
bool CAddrMan::Add_(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty)
{
if (!addr.IsRoutable())
return false;
Expand All @@ -321,9 +323,9 @@ bool CAddrMan::Add_(const CAddress &addr, const CNetAddr& source, int64 nTimePen
{
// periodically update nTime
bool fCurrentlyOnline = (GetAdjustedTime() - addr.nTime < 24 * 60 * 60);
int64 nUpdateInterval = (fCurrentlyOnline ? 60 * 60 : 24 * 60 * 60);
int64_t nUpdateInterval = (fCurrentlyOnline ? 60 * 60 : 24 * 60 * 60);
if (addr.nTime && (!pinfo->nTime || pinfo->nTime < addr.nTime - nUpdateInterval - nTimePenalty))
pinfo->nTime = max((int64)0, addr.nTime - nTimePenalty);
pinfo->nTime = max((int64_t)0, addr.nTime - nTimePenalty);

// add services
pinfo->nServices |= addr.nServices;
Expand All @@ -348,7 +350,7 @@ bool CAddrMan::Add_(const CAddress &addr, const CNetAddr& source, int64 nTimePen
return false;
} else {
pinfo = Create(addr, source, &nId);
pinfo->nTime = max((int64)0, (int64)pinfo->nTime - nTimePenalty);
pinfo->nTime = max((int64_t)0, (int64_t)pinfo->nTime - nTimePenalty);
nNew++;
fNew = true;
}
Expand All @@ -365,7 +367,7 @@ bool CAddrMan::Add_(const CAddress &addr, const CNetAddr& source, int64 nTimePen
return fNew;
}

void CAddrMan::Attempt_(const CService &addr, int64 nTime)
void CAddrMan::Attempt_(const CService &addr, int64_t nTime)
{
CAddrInfo *pinfo = Find(addr);

Expand Down Expand Up @@ -504,7 +506,7 @@ void CAddrMan::GetAddr_(std::vector<CAddress> &vAddr)
}
}

void CAddrMan::Connected_(const CService &addr, int64 nTime)
void CAddrMan::Connected_(const CService &addr, int64_t nTime)
{
CAddrInfo *pinfo = Find(addr);

Expand All @@ -519,7 +521,7 @@ void CAddrMan::Connected_(const CService &addr, int64 nTime)
return;

// update info
int64 nUpdateInterval = 20 * 60;
int64_t nUpdateInterval = 20 * 60;
if (nTime - info.nTime > nUpdateInterval)
info.nTime = nTime;
}
33 changes: 17 additions & 16 deletions src/addrman.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
// Copyright (c) 2012 Pieter Wuille
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef _BITCOIN_ADDRMAN
#define _BITCOIN_ADDRMAN 1

#include "netbase.h"
#include "protocol.h"
#include "util.h"
#include "sync.h"

#include "util.h"

#include <map>
#include <set>
#include <stdint.h>
#include <vector>

#include <openssl/rand.h>


/** Extended statistics about a CAddress */
class CAddrInfo : public CAddress
{
Expand All @@ -24,10 +25,10 @@ class CAddrInfo : public CAddress
CNetAddr source;

// last successful connection by us
int64 nLastSuccess;
int64_t nLastSuccess;

// last try whatsoever by us:
// int64 CAddress::nLastTry
// int64_t CAddress::nLastTry

// connection attempts since last successful attempt
int nAttempts;
Expand Down Expand Up @@ -86,10 +87,10 @@ class CAddrInfo : public CAddress
}

// Determine whether the statistics about this entry are bad enough so that it can just be deleted
bool IsTerrible(int64 nNow = GetAdjustedTime()) const;
bool IsTerrible(int64_t nNow = GetAdjustedTime()) const;

// Calculate the relative chance this entry should be given when selecting nodes to connect to
double GetChance(int64 nNow = GetAdjustedTime()) const;
double GetChance(int64_t nNow = GetAdjustedTime()) const;

};

Expand Down Expand Up @@ -220,13 +221,13 @@ class CAddrMan
void MakeTried(CAddrInfo& info, int nId, int nOrigin);

// Mark an entry "good", possibly moving it from "new" to "tried".
void Good_(const CService &addr, int64 nTime);
void Good_(const CService &addr, int64_t nTime);

// Add an entry to the "new" table.
bool Add_(const CAddress &addr, const CNetAddr& source, int64 nTimePenalty);
bool Add_(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty);

// Mark an entry as attempted to connect.
void Attempt_(const CService &addr, int64 nTime);
void Attempt_(const CService &addr, int64_t nTime);

// Select an address to connect to.
// nUnkBias determines how much to favor new addresses over tried ones (min=0, max=100)
Expand All @@ -241,7 +242,7 @@ class CAddrMan
void GetAddr_(std::vector<CAddress> &vAddr);

// Mark an entry as currently-connected-to.
void Connected_(const CService &addr, int64 nTime);
void Connected_(const CService &addr, int64_t nTime);

public:

Expand Down Expand Up @@ -409,7 +410,7 @@ class CAddrMan
}

// Add a single address.
bool Add(const CAddress &addr, const CNetAddr& source, int64 nTimePenalty = 0)
bool Add(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty = 0)
{
bool fRet = false;
{
Expand All @@ -424,7 +425,7 @@ class CAddrMan
}

// Add multiple addresses.
bool Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64 nTimePenalty = 0)
bool Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty = 0)
{
int nAdd = 0;
{
Expand All @@ -440,7 +441,7 @@ class CAddrMan
}

// Mark an entry as accessible.
void Good(const CService &addr, int64 nTime = GetAdjustedTime())
void Good(const CService &addr, int64_t nTime = GetAdjustedTime())
{
{
LOCK(cs);
Expand All @@ -451,7 +452,7 @@ class CAddrMan
}

// Mark an entry as connection attempted to.
void Attempt(const CService &addr, int64 nTime = GetAdjustedTime())
void Attempt(const CService &addr, int64_t nTime = GetAdjustedTime())
{
{
LOCK(cs);
Expand Down Expand Up @@ -489,7 +490,7 @@ class CAddrMan
}

// Mark an entry as currently-connected-to.
void Connected(const CService &addr, int64 nTime = GetAdjustedTime())
void Connected(const CService &addr, int64_t nTime = GetAdjustedTime())
{
{
LOCK(cs);
Expand Down
21 changes: 12 additions & 9 deletions src/alert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
// Alert system
//

#include <algorithm>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/foreach.hpp>
#include <map>

#include "alert.h"

#include "key.h"
#include "net.h"
#include "sync.h"
#include "ui_interface.h"
#include "util.h"

#include <algorithm>
#include <inttypes.h>
#include <map>

#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/foreach.hpp>

using namespace std;

Expand Down Expand Up @@ -48,8 +51,8 @@ std::string CUnsignedAlert::ToString() const
return strprintf(
"CAlert(\n"
" nVersion = %d\n"
" nRelayUntil = %"PRI64d"\n"
" nExpiration = %"PRI64d"\n"
" nRelayUntil = %"PRId64"\n"
" nExpiration = %"PRId64"\n"
" nID = %d\n"
" nCancel = %d\n"
" setCancel = %s\n"
Expand Down
17 changes: 12 additions & 5 deletions src/alert.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@
#ifndef _BITCOINALERT_H_
#define _BITCOINALERT_H_ 1

#include "serialize.h"
#include "sync.h"

#include <map>
#include <set>
#include <stdint.h>
#include <string>

#include "uint256.h"
#include "util.h"

class CAlert;
class CNode;
class uint256;

extern std::map<uint256, CAlert> mapAlerts;
extern CCriticalSection cs_mapAlerts;

/** Alerts are for notifying old versions if they become too obsolete and
* need to upgrade. The message is displayed in the status bar.
Expand All @@ -24,8 +31,8 @@ class CUnsignedAlert
{
public:
int nVersion;
int64 nRelayUntil; // when newer nodes stop relaying to newer nodes
int64 nExpiration;
int64_t nRelayUntil; // when newer nodes stop relaying to newer nodes
int64_t nExpiration;
int nID;
int nCancel;
std::set<int> setCancel;
Expand Down
7 changes: 4 additions & 3 deletions src/allocators.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
// Copyright (c) 2009-2013 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_ALLOCATORS_H
#define BITCOIN_ALLOCATORS_H

#include <string.h>
#include <map>
#include <string>
#include <string.h>

#include <boost/thread/mutex.hpp>
#include <boost/thread/once.hpp>
#include <map>
#include <openssl/crypto.h> // for OPENSSL_cleanse()


/**
* Thread-safe class to keep track of locked (ie, non-swappable) memory pages.
*
Expand Down
Loading

0 comments on commit 51ed9ec

Please sign in to comment.