1 // Copyright (c) 2015-2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_CORE_MEMUSAGE_H
6 #define BITCOIN_CORE_MEMUSAGE_H
8 #include <primitives/transaction.h>
9 #include <primitives/block.h>
12 static inline size_t RecursiveDynamicUsage(const CScript
& script
) {
13 return memusage::DynamicUsage(script
);
16 static inline size_t RecursiveDynamicUsage(const COutPoint
& out
) {
20 static inline size_t RecursiveDynamicUsage(const CTxIn
& in
) {
21 size_t mem
= RecursiveDynamicUsage(in
.scriptSig
) + RecursiveDynamicUsage(in
.prevout
) + memusage::DynamicUsage(in
.scriptWitness
.stack
);
22 for (std::vector
<std::vector
<unsigned char> >::const_iterator it
= in
.scriptWitness
.stack
.begin(); it
!= in
.scriptWitness
.stack
.end(); it
++) {
23 mem
+= memusage::DynamicUsage(*it
);
28 static inline size_t RecursiveDynamicUsage(const CTxOut
& out
) {
29 return RecursiveDynamicUsage(out
.scriptPubKey
);
32 static inline size_t RecursiveDynamicUsage(const CTransaction
& tx
) {
33 size_t mem
= memusage::DynamicUsage(tx
.vin
) + memusage::DynamicUsage(tx
.vout
);
34 for (std::vector
<CTxIn
>::const_iterator it
= tx
.vin
.begin(); it
!= tx
.vin
.end(); it
++) {
35 mem
+= RecursiveDynamicUsage(*it
);
37 for (std::vector
<CTxOut
>::const_iterator it
= tx
.vout
.begin(); it
!= tx
.vout
.end(); it
++) {
38 mem
+= RecursiveDynamicUsage(*it
);
43 static inline size_t RecursiveDynamicUsage(const CMutableTransaction
& tx
) {
44 size_t mem
= memusage::DynamicUsage(tx
.vin
) + memusage::DynamicUsage(tx
.vout
);
45 for (std::vector
<CTxIn
>::const_iterator it
= tx
.vin
.begin(); it
!= tx
.vin
.end(); it
++) {
46 mem
+= RecursiveDynamicUsage(*it
);
48 for (std::vector
<CTxOut
>::const_iterator it
= tx
.vout
.begin(); it
!= tx
.vout
.end(); it
++) {
49 mem
+= RecursiveDynamicUsage(*it
);
54 static inline size_t RecursiveDynamicUsage(const CBlock
& block
) {
55 size_t mem
= memusage::DynamicUsage(block
.vtx
);
56 for (const auto& tx
: block
.vtx
) {
57 mem
+= memusage::DynamicUsage(tx
) + RecursiveDynamicUsage(*tx
);
62 static inline size_t RecursiveDynamicUsage(const CBlockLocator
& locator
) {
63 return memusage::DynamicUsage(locator
.vHave
);
67 static inline size_t RecursiveDynamicUsage(const std::shared_ptr
<X
>& p
) {
68 return p
? memusage::DynamicUsage(p
) + RecursiveDynamicUsage(*p
) : 0;
71 #endif // BITCOIN_CORE_MEMUSAGE_H