From a2f6e131383bc550684f6a0c2187433a0aa47689 Mon Sep 17 00:00:00 2001 From: faluco Date: Tue, 13 Apr 2021 14:41:59 +0200 Subject: [PATCH] Add a tag to the string memory pool in fmt to discriminate between pool vs heap allocated buffers. Heap allocated buffers are used as a fallback mechanism when the buffer runs out of space. --- lib/include/srsran/srslog/bundled/fmt/core.h | 2 +- lib/src/srslog/bundled/fmt/format.cc | 22 ++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/include/srsran/srslog/bundled/fmt/core.h b/lib/include/srsran/srslog/bundled/fmt/core.h index 045ec79fa..d676f27e5 100644 --- a/lib/include/srsran/srslog/bundled/fmt/core.h +++ b/lib/include/srsran/srslog/bundled/fmt/core.h @@ -1277,7 +1277,7 @@ template const T& unwrap(const std::reference_wrapper& v) { class dynamic_arg_list { public: - static constexpr std::size_t max_pool_string_size = 140; + static constexpr std::size_t max_pool_string_size = 256; private: // Workaround for clang's -Wweak-vtables. Unlike for regular classes, for diff --git a/lib/src/srslog/bundled/fmt/format.cc b/lib/src/srslog/bundled/fmt/format.cc index a443544dd..ac61868ae 100644 --- a/lib/src/srslog/bundled/fmt/format.cc +++ b/lib/src/srslog/bundled/fmt/format.cc @@ -26,9 +26,11 @@ int format_float(char* buf, std::size_t size, const char* format, int precision, } #define NODE_POOL_SIZE (10000u) +static constexpr uint8_t memory_heap_tag = 0xAA; class dyn_node_pool { - using type = std::array; + /// The extra byte is used to store the memory tag at position 0 in the array. + using type = std::array; public: dyn_node_pool() { @@ -44,13 +46,18 @@ public: std::lock_guard lock(m); if (free_list.empty()) { - return nullptr; + // Tag that this allocation was performed by the heap. + auto *p = new type; + (*p)[0] = memory_heap_tag; + return p->data() + 1; } auto* p = free_list.back(); free_list.pop_back(); - return p; + // Tag that this allocation was performed by the pool. + p[0] = 0; + return p + 1; } void dealloc(void* p) { @@ -59,7 +66,14 @@ public: } std::lock_guard lock(m); - free_list.push_back(reinterpret_cast(p)); + uint8_t* base_ptr = reinterpret_cast(p) - 1; + if (*base_ptr == memory_heap_tag) { + // This pointer was allocated using the heap. + delete reinterpret_cast(base_ptr); + return; + } + + free_list.push_back(base_ptr); } private: