diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2023-08-23 13:51:32 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-23 13:51:32 +0200 |
commit | d469a69d95237c6c52930b008b91eb682f19b6bf (patch) | |
tree | 9a24e25b1f7f4b6b6850e04fc5baa14db4ea06d3 /vespalib | |
parent | 7134ffbf1d2f697949509e210d67ffd8d114973f (diff) | |
parent | 27339377a5fea608598960ec9d576673c2b64b55 (diff) |
Merge pull request #28116 from vespa-engine/balder/avoid-dynamic_cast
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 | 14 |
2 files changed, 14 insertions, 11 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..2be352e6381 100644 --- a/vespalib/src/vespa/vespalib/util/stash.h +++ b/vespalib/src/vespa/vespalib/util/stash.h @@ -15,25 +15,29 @@ 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 -template<typename T> struct DestructObject : public Cleanup { +template<typename T> +struct DestructObject final : public Cleanup { explicit DestructObject(Cleanup *next_in) noexcept : Cleanup(next_in) {} void cleanup() override { reinterpret_cast<T*>(this + 1)->~T(); } }; // used as prefix for arrays to be destructed -template<typename T> struct DestructArray : public Cleanup { +template<typename T> +struct DestructArray final : public Cleanup { size_t size; explicit DestructArray(Cleanup *next_in, size_t size_in) noexcept : Cleanup(next_in), size(size_in) {} |