1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
/*
Copyright (c) 2015, Arvid Norberg
All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef HANDLER_ALLOCATOR_HPP_INCLUDED
#define HANDLER_ALLOCATOR_HPP_INCLUDED
namespace sim
{
namespace aux
{
template <typename T>
struct malloc_allocator
{
using value_type = T;
using size_type = std::size_t;
friend bool operator==(malloc_allocator, malloc_allocator) { return true; }
friend bool operator!=(malloc_allocator, malloc_allocator) { return false; }
template <class U>
struct rebind { using other = malloc_allocator<U>; };
malloc_allocator() = default;
template <typename U>
malloc_allocator(malloc_allocator<U> const&) {}
T* allocate(std::size_t size) { return static_cast<T*>(std::malloc(size * sizeof(T))); }
void deallocate(T* pointer, std::size_t) { std::free(pointer); }
using is_always_equal = std::true_type;
};
// this is a handler wrapper that customizes the asio handler allocator to use
// malloc instead of new. The purpose is to distinguish allocations that are
// internal to the simulator and allocations part of the program under test.
template <typename Handler>
struct malloc_wrapper
{
malloc_wrapper(Handler h) : m_handler(std::move(h)) {}
template <typename... Args>
void operator()(Args&&... a)
{
m_handler(std::forward<Args>(a)...);
}
using allocator_type = malloc_allocator<malloc_wrapper<Handler>>;
allocator_type get_allocator() const noexcept
{ return allocator_type{}; }
private:
Handler m_handler;
};
template <typename T>
malloc_wrapper<T> make_malloc(T h)
{
return malloc_wrapper<T>(std::move(h));
}
}
}
#endif
|