Browse Source

adding destruction statistics and checks to pool_allocator

master
Eyck Jentzsch 1 week ago
parent
commit
113cbd9991
2 changed files with 24 additions and 20 deletions
  1. 1
    16
      incl/tlm/tlm_mm.h
  2. 23
    4
      incl/util/pool_allocator.h

+ 1
- 16
incl/tlm/tlm_mm.h View File

@@ -19,9 +19,6 @@

#include <tlm>
#include <util/pool_allocator.h>
#ifdef HAVE_GETENV
#include <cstdlib>
#endif

namespace tlm {

@@ -67,7 +64,7 @@ public:
/**
* @brief destructor
*/
~tlm_mm();
~tlm_mm() = default;
/**
* @brief get a plain tlm_payload_type without extensions
* @return the tlm_payload_type
@@ -102,18 +99,6 @@ template <typename TYPES> typename tlm_mm<TYPES>::payload_type* tlm_mm<TYPES>::a
return new(ptr) payload_type(this);
}

template <typename TYPES> inline tlm_mm<TYPES>::~tlm_mm() {
#ifdef HAVE_GETENV
auto* check = getenv("TLM_MM_CHECK");
if(check && strcasecmp(check, "INFO")) {
auto diff = allocator.get_capacity() - allocator.get_free_entries_count();
if(diff)
std::cout << __FUNCTION__ << ": detected memory leak upon destruction, " << diff << " of "
<< allocator.get_capacity() << " entries are not free'd" << std::endl;
}
#endif
}

template <typename TYPES> void tlm_mm<TYPES>::free(payload_type* trans) {
trans->reset();
trans->~payload_type();

+ 23
- 4
incl/util/pool_allocator.h View File

@@ -10,6 +10,9 @@

#include <deque>
#include <vector>
#ifdef HAVE_GETENV
#include <cstdlib>
#endif

namespace util {
template <typename T, unsigned CHUNK_SIZE = 4096> class pool_allocator {
@@ -24,14 +27,13 @@ public:

pool_allocator(pool_allocator&&) = delete;

~pool_allocator();

pool_allocator& operator=(const pool_allocator&) = delete;

pool_allocator& operator=(pool_allocator&&) = delete;

static pool_allocator& get() {
static pool_allocator inst;
return inst;
}
static pool_allocator& get();

size_t get_capacity();

@@ -44,6 +46,23 @@ private:
std::deque<void*> free_list;
};

template<typename T, unsigned CHUNK_SIZE> pool_allocator<T, CHUNK_SIZE>& pool_allocator<T, CHUNK_SIZE>::get() {
static pool_allocator inst;
return inst;
}

template <typename T, unsigned CHUNK_SIZE> pool_allocator<T, CHUNK_SIZE>::~pool_allocator() {
#ifdef HAVE_GETENV
auto* check = getenv("TLM_MM_CHECK");
if(check && strcasecmp(check, "INFO")) {
auto diff = get_capacity() - get_free_entries_count();
if(diff)
std::cout << __FUNCTION__ << ": detected memory leak upon destruction, " << diff << " of "
<< get_capacity() << " entries are not free'd" << std::endl;
}
#endif
}

template <typename T, unsigned CHUNK_SIZE> inline void* pool_allocator<T, CHUNK_SIZE>::allocate() {
if(!free_list.size())
resize();

Loading…
Cancel
Save