diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2023-08-22 21:48:40 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2023-08-22 21:48:40 +0000 |
commit | da2a54fc4abdd31a783ed63292ea7f9c3c5cc551 (patch) | |
tree | d742251dc927e091a738c790c6ca6b2982982f49 /vespalib | |
parent | 9b45904e3da537e47a06509cdd32ad5b24aa0b11 (diff) |
Avoid dynamic_cast by adding an interface to get allocated size
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/vespa/vespalib/util/stash.cpp | 11 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/stash.h | 8 |
2 files changed, 10 insertions, 9 deletions
diff --git a/vespalib/src/vespa/vespalib/util/stash.cpp b/vespalib/src/vespa/vespalib/util/stash.cpp index 9e982588294..870340adefe 100644 --- a/vespalib/src/vespa/vespalib/util/stash.cpp +++ b/vespalib/src/vespa/vespalib/util/stash.cpp @@ -132,12 +132,11 @@ Stash::get_memory_usage() const used += chunk->used; } for (auto cleanup = _cleanup; cleanup; cleanup = cleanup->next) { - if (auto memory = dynamic_cast<stash::DeleteMemory *>(cleanup)) { - allocated += memory->allocated; - used += memory->allocated; - } + auto extra = cleanup->allocated(); + allocated += extra; + used += extra; } - return MemoryUsage(allocated, used, 0, 0); -}; + return {allocated, used, 0, 0}; +} } // namespace vespalib diff --git a/vespalib/src/vespa/vespalib/util/stash.h b/vespalib/src/vespa/vespalib/util/stash.h index 5f2ad27bbcd..990f69fe4ed 100644 --- a/vespalib/src/vespa/vespalib/util/stash.h +++ b/vespalib/src/vespa/vespalib/util/stash.h @@ -15,15 +15,17 @@ struct Cleanup { Cleanup * const next; explicit Cleanup(Cleanup *next_in) noexcept : next(next_in) {} virtual void cleanup() = 0; + virtual size_t allocated() const noexcept { return 0; } protected: virtual ~Cleanup() = default; }; // used as header for memory allocated outside the stash -struct DeleteMemory : public Cleanup { - explicit DeleteMemory(size_t sz, Cleanup *next_in) noexcept : Cleanup(next_in), allocated(sz) {} +struct DeleteMemory final : public Cleanup { + explicit DeleteMemory(size_t sz, Cleanup *next_in) noexcept : Cleanup(next_in), _allocated(sz) {} void cleanup() override { free((void*)this); } - size_t allocated; + size_t allocated() const noexcept override { return _allocated; } + size_t _allocated; }; // used as prefix for objects to be destructed |