2019-04-06 19:29:58 +02:00
|
|
|
|
#pragma once
|
2016-04-02 00:18:20 +02:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Ring buffer memory helper :
|
|
|
|
|
|
* There are 2 "pointers" (offset inside a memory buffer to be provided by class derrivative)
|
|
|
|
|
|
* PUT pointer "points" to the start of allocatable space.
|
|
|
|
|
|
* GET pointer "points" to the start of memory in use by the GPU.
|
|
|
|
|
|
* Space between GET and PUT is used by the GPU ; this structure check that this memory is not overwritten.
|
|
|
|
|
|
* User has to update the GET pointer when synchronisation happens.
|
|
|
|
|
|
*/
|
|
|
|
|
|
struct data_heap
|
|
|
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Does alloc cross get position ?
|
|
|
|
|
|
*/
|
2018-04-29 08:41:51 +02:00
|
|
|
|
template<int Alignment>
|
2016-04-02 00:18:20 +02:00
|
|
|
|
bool can_alloc(size_t size) const
|
|
|
|
|
|
{
|
2018-04-29 08:41:51 +02:00
|
|
|
|
size_t alloc_size = align(size, Alignment);
|
|
|
|
|
|
size_t aligned_put_pos = align(m_put_pos, Alignment);
|
2016-04-02 00:18:20 +02:00
|
|
|
|
if (aligned_put_pos + alloc_size < m_size)
|
|
|
|
|
|
{
|
|
|
|
|
|
// range before get
|
|
|
|
|
|
if (aligned_put_pos + alloc_size < m_get_pos)
|
|
|
|
|
|
return true;
|
|
|
|
|
|
// range after get
|
|
|
|
|
|
if (aligned_put_pos > m_get_pos)
|
|
|
|
|
|
return true;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// ..]....[..get..
|
|
|
|
|
|
if (aligned_put_pos < m_get_pos)
|
|
|
|
|
|
return false;
|
|
|
|
|
|
// ..get..]...[...
|
|
|
|
|
|
// Actually all resources extending beyond heap space starts at 0
|
|
|
|
|
|
if (alloc_size > m_get_pos)
|
|
|
|
|
|
return false;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t m_size;
|
|
|
|
|
|
size_t m_put_pos; // Start of free space
|
2017-06-10 22:32:17 +02:00
|
|
|
|
size_t m_min_guard_size; //If an allocation touches the guard region, reset the heap to avoid going over budget
|
|
|
|
|
|
size_t m_current_allocated_size;
|
|
|
|
|
|
size_t m_largest_allocated_pool;
|
2017-10-21 23:12:32 +02:00
|
|
|
|
|
|
|
|
|
|
char* m_name;
|
2016-04-02 00:18:20 +02:00
|
|
|
|
public:
|
|
|
|
|
|
data_heap() = default;
|
|
|
|
|
|
~data_heap() = default;
|
|
|
|
|
|
data_heap(const data_heap&) = delete;
|
|
|
|
|
|
data_heap(data_heap&&) = delete;
|
|
|
|
|
|
|
|
|
|
|
|
size_t m_get_pos; // End of free space
|
|
|
|
|
|
|
2017-10-21 23:12:32 +02:00
|
|
|
|
void init(size_t heap_size, const char* buffer_name = "unnamed", size_t min_guard_size=0x10000)
|
2016-04-02 00:18:20 +02:00
|
|
|
|
{
|
2017-10-21 23:12:32 +02:00
|
|
|
|
m_name = const_cast<char*>(buffer_name);
|
|
|
|
|
|
|
2016-04-02 00:18:20 +02:00
|
|
|
|
m_size = heap_size;
|
|
|
|
|
|
m_put_pos = 0;
|
|
|
|
|
|
m_get_pos = heap_size - 1;
|
2017-06-10 22:32:17 +02:00
|
|
|
|
|
|
|
|
|
|
//allocation stats
|
|
|
|
|
|
m_min_guard_size = min_guard_size;
|
|
|
|
|
|
m_current_allocated_size = 0;
|
|
|
|
|
|
m_largest_allocated_pool = 0;
|
2016-04-02 00:18:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-04-29 08:41:51 +02:00
|
|
|
|
template<int Alignment>
|
2016-04-02 00:18:20 +02:00
|
|
|
|
size_t alloc(size_t size)
|
|
|
|
|
|
{
|
2018-04-29 08:41:51 +02:00
|
|
|
|
if (!can_alloc<Alignment>(size))
|
2017-06-19 12:47:38 +02:00
|
|
|
|
{
|
2017-10-21 23:12:32 +02:00
|
|
|
|
fmt::throw_exception("[%s] Working buffer not big enough, buffer_length=%d allocated=%d requested=%d guard=%d largest_pool=%d" HERE,
|
|
|
|
|
|
m_name, m_size, m_current_allocated_size, size, m_min_guard_size, m_largest_allocated_pool);
|
2017-06-19 12:47:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-04-29 08:41:51 +02:00
|
|
|
|
size_t alloc_size = align(size, Alignment);
|
|
|
|
|
|
size_t aligned_put_pos = align(m_put_pos, Alignment);
|
2017-06-10 22:32:17 +02:00
|
|
|
|
|
|
|
|
|
|
const size_t block_length = (aligned_put_pos - m_put_pos) + alloc_size;
|
|
|
|
|
|
m_current_allocated_size += block_length;
|
|
|
|
|
|
m_largest_allocated_pool = std::max(m_largest_allocated_pool, block_length);
|
|
|
|
|
|
|
2016-04-02 00:18:20 +02:00
|
|
|
|
if (aligned_put_pos + alloc_size < m_size)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_put_pos = aligned_put_pos + alloc_size;
|
|
|
|
|
|
return aligned_put_pos;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
m_put_pos = alloc_size;
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* return current putpos - 1
|
|
|
|
|
|
*/
|
|
|
|
|
|
size_t get_current_put_pos_minus_one() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return (m_put_pos - 1 > 0) ? m_put_pos - 1 : m_size - 1;
|
|
|
|
|
|
}
|
2017-06-10 22:32:17 +02:00
|
|
|
|
|
2019-04-06 19:29:58 +02:00
|
|
|
|
bool is_critical() const
|
2017-06-10 22:32:17 +02:00
|
|
|
|
{
|
|
|
|
|
|
const size_t guard_length = std::max(m_min_guard_size, m_largest_allocated_pool);
|
2018-02-03 14:42:02 +01:00
|
|
|
|
return (m_current_allocated_size + guard_length) >= m_size;
|
2017-06-10 22:32:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void reset_allocation_stats()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_current_allocated_size = 0;
|
|
|
|
|
|
m_largest_allocated_pool = 0;
|
|
|
|
|
|
m_get_pos = get_current_put_pos_minus_one();
|
|
|
|
|
|
}
|
2017-09-22 17:39:48 +02:00
|
|
|
|
|
|
|
|
|
|
// Updates the current_allocated_size metrics
|
|
|
|
|
|
void notify()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_get_pos == UINT64_MAX)
|
|
|
|
|
|
m_current_allocated_size = 0;
|
|
|
|
|
|
else if (m_get_pos < m_put_pos)
|
|
|
|
|
|
m_current_allocated_size = (m_put_pos - m_get_pos - 1);
|
|
|
|
|
|
else if (m_get_pos > m_put_pos)
|
|
|
|
|
|
m_current_allocated_size = (m_put_pos + (m_size - m_get_pos - 1));
|
|
|
|
|
|
else
|
|
|
|
|
|
fmt::throw_exception("m_put_pos == m_get_pos!" HERE);
|
|
|
|
|
|
}
|
2018-02-21 18:50:27 +01:00
|
|
|
|
|
|
|
|
|
|
size_t size() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_size;
|
|
|
|
|
|
}
|
2016-04-02 00:18:20 +02:00
|
|
|
|
};
|