You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

core_memusage.h 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (c) 2015-2016 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #ifndef STARWELS_CORE_MEMUSAGE_H
  5. #define STARWELS_CORE_MEMUSAGE_H
  6. #include "primitives/transaction.h"
  7. #include "primitives/block.h"
  8. #include "memusage.h"
  9. static inline size_t RecursiveDynamicUsage(const CScript& script) {
  10. return memusage::DynamicUsage(script);
  11. }
  12. static inline size_t RecursiveDynamicUsage(const COutPoint& out) {
  13. return 0;
  14. }
  15. static inline size_t RecursiveDynamicUsage(const CTxIn& in) {
  16. size_t mem = RecursiveDynamicUsage(in.scriptSig) + RecursiveDynamicUsage(in.prevout) + memusage::DynamicUsage(in.scriptWitness.stack);
  17. for (std::vector<std::vector<unsigned char> >::const_iterator it = in.scriptWitness.stack.begin(); it != in.scriptWitness.stack.end(); it++) {
  18. mem += memusage::DynamicUsage(*it);
  19. }
  20. return mem;
  21. }
  22. static inline size_t RecursiveDynamicUsage(const CTxOut& out) {
  23. return RecursiveDynamicUsage(out.scriptPubKey);
  24. }
  25. static inline size_t RecursiveDynamicUsage(const CTransaction& tx) {
  26. size_t mem = memusage::DynamicUsage(tx.vin) + memusage::DynamicUsage(tx.vout);
  27. for (std::vector<CTxIn>::const_iterator it = tx.vin.begin(); it != tx.vin.end(); it++) {
  28. mem += RecursiveDynamicUsage(*it);
  29. }
  30. for (std::vector<CTxOut>::const_iterator it = tx.vout.begin(); it != tx.vout.end(); it++) {
  31. mem += RecursiveDynamicUsage(*it);
  32. }
  33. return mem;
  34. }
  35. static inline size_t RecursiveDynamicUsage(const CMutableTransaction& tx) {
  36. size_t mem = memusage::DynamicUsage(tx.vin) + memusage::DynamicUsage(tx.vout);
  37. for (std::vector<CTxIn>::const_iterator it = tx.vin.begin(); it != tx.vin.end(); it++) {
  38. mem += RecursiveDynamicUsage(*it);
  39. }
  40. for (std::vector<CTxOut>::const_iterator it = tx.vout.begin(); it != tx.vout.end(); it++) {
  41. mem += RecursiveDynamicUsage(*it);
  42. }
  43. return mem;
  44. }
  45. static inline size_t RecursiveDynamicUsage(const CBlock& block) {
  46. size_t mem = memusage::DynamicUsage(block.vtx);
  47. for (const auto& tx : block.vtx) {
  48. mem += memusage::DynamicUsage(tx) + RecursiveDynamicUsage(*tx);
  49. }
  50. return mem;
  51. }
  52. static inline size_t RecursiveDynamicUsage(const CBlockLocator& locator) {
  53. return memusage::DynamicUsage(locator.vHave);
  54. }
  55. template<typename X>
  56. static inline size_t RecursiveDynamicUsage(const std::shared_ptr<X>& p) {
  57. return p ? memusage::DynamicUsage(p) + RecursiveDynamicUsage(*p) : 0;
  58. }
  59. #endif // STARWELS_CORE_MEMUSAGE_H