2017-09-15 17:41:09 +00:00
|
|
|
#ifndef __TRACYSLAB_HPP__
|
|
|
|
#define __TRACYSLAB_HPP__
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-09-16 23:22:46 +00:00
|
|
|
#include "TracyMemory.hpp"
|
|
|
|
|
2017-09-15 17:41:09 +00:00
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
|
|
|
template<size_t BlockSize>
|
|
|
|
class Slab
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Slab()
|
|
|
|
: m_ptr( new char[BlockSize] )
|
|
|
|
, m_offset( 0 )
|
2017-09-22 20:25:18 +00:00
|
|
|
, m_buffer( { m_ptr } )
|
2019-02-15 01:28:31 +00:00
|
|
|
, m_usage( BlockSize )
|
2017-09-16 23:22:46 +00:00
|
|
|
{
|
|
|
|
memUsage.fetch_add( BlockSize, std::memory_order_relaxed );
|
|
|
|
}
|
2017-09-15 17:41:09 +00:00
|
|
|
|
|
|
|
~Slab()
|
|
|
|
{
|
2019-02-15 01:28:31 +00:00
|
|
|
memUsage.fetch_sub( m_usage, std::memory_order_relaxed );
|
2017-09-15 17:41:09 +00:00
|
|
|
for( auto& v : m_buffer )
|
|
|
|
{
|
|
|
|
delete[] v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 00:47:16 +00:00
|
|
|
tracy_force_inline void* AllocRaw( size_t size )
|
2017-09-15 17:41:09 +00:00
|
|
|
{
|
|
|
|
assert( size <= BlockSize );
|
|
|
|
if( m_offset + size > BlockSize )
|
|
|
|
{
|
2018-04-30 00:47:16 +00:00
|
|
|
DoAlloc();
|
2017-09-15 17:41:09 +00:00
|
|
|
}
|
|
|
|
void* ret = m_ptr + m_offset;
|
|
|
|
m_offset += size;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-21 10:54:12 +00:00
|
|
|
template<typename T>
|
|
|
|
T* AllocInit()
|
|
|
|
{
|
2017-11-19 12:53:39 +00:00
|
|
|
const auto size = sizeof( T );
|
|
|
|
assert( size <= BlockSize );
|
|
|
|
if( m_offset + size > BlockSize )
|
|
|
|
{
|
2018-04-30 00:47:16 +00:00
|
|
|
DoAlloc();
|
2017-11-19 12:53:39 +00:00
|
|
|
}
|
|
|
|
void* ret = m_ptr + m_offset;
|
|
|
|
new( ret ) T;
|
|
|
|
m_offset += size;
|
|
|
|
return (T*)ret;
|
2017-10-21 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
2019-09-29 18:18:16 +00:00
|
|
|
template<typename T>
|
|
|
|
T* AllocInit( size_t sz )
|
|
|
|
{
|
|
|
|
const auto size = sizeof( T ) * sz;
|
|
|
|
assert( size <= BlockSize );
|
|
|
|
if( m_offset + size > BlockSize )
|
|
|
|
{
|
|
|
|
DoAlloc();
|
|
|
|
}
|
|
|
|
void* ret = m_ptr + m_offset;
|
|
|
|
T* ptr = (T*)ret;
|
|
|
|
for( size_t i=0; i<sz; i++ )
|
|
|
|
{
|
|
|
|
new( ptr ) T;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
m_offset += size;
|
|
|
|
return (T*)ret;
|
|
|
|
}
|
|
|
|
|
2017-09-15 17:54:43 +00:00
|
|
|
template<typename T>
|
2018-04-30 00:47:16 +00:00
|
|
|
tracy_force_inline T* Alloc()
|
2017-09-15 17:54:43 +00:00
|
|
|
{
|
2017-10-21 10:54:12 +00:00
|
|
|
return (T*)AllocRaw( sizeof( T ) );
|
2017-09-15 17:54:43 +00:00
|
|
|
}
|
|
|
|
|
2017-10-21 11:01:57 +00:00
|
|
|
template<typename T>
|
2018-04-30 00:47:16 +00:00
|
|
|
tracy_force_inline T* Alloc( size_t size )
|
2017-10-21 11:01:57 +00:00
|
|
|
{
|
|
|
|
return (T*)AllocRaw( sizeof( T ) * size );
|
|
|
|
}
|
|
|
|
|
2018-04-30 00:47:16 +00:00
|
|
|
tracy_force_inline void Unalloc( size_t size )
|
2017-09-15 17:41:09 +00:00
|
|
|
{
|
|
|
|
assert( size <= m_offset );
|
|
|
|
m_offset -= size;
|
|
|
|
}
|
|
|
|
|
2019-02-15 00:48:33 +00:00
|
|
|
tracy_force_inline void* AllocBig( size_t size )
|
|
|
|
{
|
|
|
|
if( m_offset + size <= BlockSize )
|
|
|
|
{
|
|
|
|
void* ret = m_ptr + m_offset;
|
|
|
|
m_offset += size;
|
|
|
|
return ret;
|
|
|
|
}
|
2019-02-15 01:04:37 +00:00
|
|
|
else if( size <= BlockSize && BlockSize - m_offset <= 1024 )
|
|
|
|
{
|
|
|
|
DoAlloc();
|
|
|
|
void* ret = m_ptr + m_offset;
|
|
|
|
m_offset += size;
|
|
|
|
return ret;
|
|
|
|
}
|
2019-02-15 00:48:33 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
memUsage.fetch_add( size );
|
2019-02-15 01:28:31 +00:00
|
|
|
m_usage += size;
|
2019-02-15 00:48:33 +00:00
|
|
|
auto ret = new char[size];
|
|
|
|
m_buffer.emplace_back( ret );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-15 17:41:09 +00:00
|
|
|
void Reset()
|
|
|
|
{
|
|
|
|
if( m_buffer.size() > 1 )
|
|
|
|
{
|
2019-02-15 01:28:31 +00:00
|
|
|
memUsage.fetch_sub( m_usage - BlockSize, std::memory_order_relaxed );
|
|
|
|
m_usage = BlockSize;
|
2017-09-15 17:41:09 +00:00
|
|
|
for( int i=1; i<m_buffer.size(); i++ )
|
|
|
|
{
|
|
|
|
delete[] m_buffer[i];
|
|
|
|
}
|
|
|
|
m_ptr = m_buffer[0];
|
|
|
|
m_buffer.clear();
|
|
|
|
m_buffer.emplace_back( m_ptr );
|
|
|
|
}
|
|
|
|
m_offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Slab( const Slab& ) = delete;
|
|
|
|
Slab( Slab&& ) = delete;
|
|
|
|
|
|
|
|
Slab& operator=( const Slab& ) = delete;
|
|
|
|
Slab& operator=( Slab&& ) = delete;
|
|
|
|
|
|
|
|
private:
|
2018-04-30 00:47:16 +00:00
|
|
|
void DoAlloc()
|
|
|
|
{
|
|
|
|
m_ptr = new char[BlockSize];
|
|
|
|
m_offset = 0;
|
|
|
|
m_buffer.emplace_back( m_ptr );
|
|
|
|
memUsage.fetch_add( BlockSize, std::memory_order_relaxed );
|
2019-02-15 01:28:31 +00:00
|
|
|
m_usage += BlockSize;
|
2018-04-30 00:47:16 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 17:41:09 +00:00
|
|
|
char* m_ptr;
|
2017-11-19 12:38:55 +00:00
|
|
|
uint32_t m_offset;
|
2017-09-15 17:41:09 +00:00
|
|
|
std::vector<char*> m_buffer;
|
2019-02-15 01:28:31 +00:00
|
|
|
size_t m_usage;
|
2017-09-15 17:41:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|