container/test/memory_resource_logger.hpp
Ion Gaztañaga 33d2f0f7af Add Polymorphic Memory Resource utilities and rework the separately compiled library:
- Dlmalloc's based C function are boost_cont_xxx no longer exported, but wrapped into C++ linkage dlmalloc_xxx functions to effectively reuse Boost's dynamic library and autolink machinery instead of rewriting machinery to compile the C source file.
- Refactored scoped_allocator_adaptor's construct logic as it was shared with polymorphic allocator's one. Moved common logic to detail/dispatch_uses_allocator.hpp. Refactored also scoped_allocator_adaptor test utilities to be reused with polymorphic_allocator tests.
2015-09-07 19:16:46 +02:00

87 lines
2.3 KiB
C++

//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2015-2015. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/container for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#ifndef BOOST_CONTAINER_TEST_MEMORY_RESOURCE_TESTER_HPP
#define BOOST_CONTAINER_TEST_MEMORY_RESOURCE_TESTER_HPP
#include <boost/container/pmr/memory_resource.hpp>
#include <boost/container/vector.hpp>
#include <cstdlib>
class memory_resource_logger
: public boost::container::pmr::memory_resource
{
public:
struct allocation_info
{
char *address;
std::size_t bytes;
std::size_t alignment;
};
boost::container::vector<allocation_info> m_info;
unsigned m_mismatches;
explicit memory_resource_logger()
: m_info()
, m_mismatches()
{}
virtual ~memory_resource_logger()
{ this->reset(); }
virtual void* do_allocate(std::size_t bytes, std::size_t alignment)
{
char *addr =(char*)std::malloc(bytes);
if(!addr){
throw std::bad_alloc();
}
allocation_info info;
info.address = addr;
info.bytes = bytes;
info.alignment = alignment;
m_info.push_back(info);
return addr;
}
virtual void do_deallocate(void* p, std::size_t bytes, std::size_t alignment)
{
std::size_t i = 0, max = m_info.size();
while(i != max && m_info[i].address != p){
++i;
}
if(i == max){
++m_mismatches;
}
else{
const allocation_info &info = m_info[i];
m_mismatches += info.bytes != bytes || info.alignment != alignment;
std::free(p);
m_info.erase(m_info.nth(i));
}
}
virtual bool do_is_equal(const boost::container::pmr::memory_resource& other) const BOOST_NOEXCEPT
{
return static_cast<const memory_resource *>(this) == &other;
}
void reset()
{
while(!m_info.empty()){
std::free(m_info.back().address);
m_info.pop_back();
}
m_mismatches = 0u;
}
};
#endif //#ifndef BOOST_CONTAINER_TEST_MEMORY_RESOURCE_TESTER_HPP