1 // Copyright (c) 2015 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(*static_cast<const CScriptBase
*>(&script
));
16 static inline size_t RecursiveDynamicUsage(const COutPoint
& out
) {
20 static inline size_t RecursiveDynamicUsage(const CTxIn
& in
) {
21 return RecursiveDynamicUsage(in
.scriptSig
) + RecursiveDynamicUsage(in
.prevout
);
24 static inline size_t RecursiveDynamicUsage(const CTxOut
& out
) {
25 return RecursiveDynamicUsage(out
.scriptPubKey
);
28 static inline size_t RecursiveDynamicUsage(const CScriptWitness
& scriptWit
) {
29 size_t mem
= memusage::DynamicUsage(scriptWit
.stack
);
30 for (std::vector
<std::vector
<unsigned char> >::const_iterator it
= scriptWit
.stack
.begin(); it
!= scriptWit
.stack
.end(); it
++) {
31 mem
+= memusage::DynamicUsage(*it
);
36 static inline size_t RecursiveDynamicUsage(const CTxInWitness
& txinwit
) {
37 return RecursiveDynamicUsage(txinwit
.scriptWitness
);
40 static inline size_t RecursiveDynamicUsage(const CTxWitness
& txwit
) {
41 size_t mem
= memusage::DynamicUsage(txwit
.vtxinwit
);
42 for (std::vector
<CTxInWitness
>::const_iterator it
= txwit
.vtxinwit
.begin(); it
!= txwit
.vtxinwit
.end(); it
++) {
43 mem
+= RecursiveDynamicUsage(*it
);
48 static inline size_t RecursiveDynamicUsage(const CTransaction
& tx
) {
49 size_t mem
= memusage::DynamicUsage(tx
.vin
) + memusage::DynamicUsage(tx
.vout
) + RecursiveDynamicUsage(tx
.wit
);
50 for (std::vector
<CTxIn
>::const_iterator it
= tx
.vin
.begin(); it
!= tx
.vin
.end(); it
++) {
51 mem
+= RecursiveDynamicUsage(*it
);
53 for (std::vector
<CTxOut
>::const_iterator it
= tx
.vout
.begin(); it
!= tx
.vout
.end(); it
++) {
54 mem
+= RecursiveDynamicUsage(*it
);
59 static inline size_t RecursiveDynamicUsage(const CMutableTransaction
& tx
) {
60 size_t mem
= memusage::DynamicUsage(tx
.vin
) + memusage::DynamicUsage(tx
.vout
) + RecursiveDynamicUsage(tx
.wit
);
61 for (std::vector
<CTxIn
>::const_iterator it
= tx
.vin
.begin(); it
!= tx
.vin
.end(); it
++) {
62 mem
+= RecursiveDynamicUsage(*it
);
64 for (std::vector
<CTxOut
>::const_iterator it
= tx
.vout
.begin(); it
!= tx
.vout
.end(); it
++) {
65 mem
+= RecursiveDynamicUsage(*it
);
70 static inline size_t RecursiveDynamicUsage(const CBlock
& block
) {
71 size_t mem
= memusage::DynamicUsage(block
.vtx
);
72 for (const auto& tx
: block
.vtx
) {
73 mem
+= memusage::DynamicUsage(tx
) + RecursiveDynamicUsage(*tx
);
78 static inline size_t RecursiveDynamicUsage(const CBlockLocator
& locator
) {
79 return memusage::DynamicUsage(locator
.vHave
);
82 #endif // BITCOIN_CORE_MEMUSAGE_H