tracy/server/TracySlab.hpp

160 lines
3.3 KiB
C++
Raw Permalink Normal View History

2017-09-15 17:41:09 +00:00
#ifndef __TRACYSLAB_HPP__
#define __TRACYSLAB_HPP__
#include <assert.h>
2022-04-26 19:23:22 +00:00
#include <stdint.h>
2017-09-15 17:41:09 +00:00
#include <vector>
2017-09-16 23:22:46 +00:00
#include "TracyMemory.hpp"
#include "../public/common/TracyForceInline.hpp"
2017-09-16 23:22:46 +00:00
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 } )
, m_usage( BlockSize )
2017-09-16 23:22:46 +00:00
{
memUsage += BlockSize;
2017-09-16 23:22:46 +00:00
}
2017-09-15 17:41:09 +00:00
~Slab()
{
memUsage -= m_usage;
2017-09-15 17:41:09 +00:00
for( auto& v : m_buffer )
{
delete[] v;
}
}
tracy_force_inline void* AllocRaw( size_t size )
2017-09-15 17:41:09 +00:00
{
assert( size <= BlockSize );
2020-02-23 18:43:00 +00:00
const auto offset = m_offset;
if( offset + size > BlockSize )
{
return DoAlloc( size );
}
else
{
void* ret = m_ptr + offset;
m_offset += size;
return ret;
}
2017-09-15 17:41:09 +00:00
}
template<typename T>
2019-10-29 00:19:40 +00:00
tracy_force_inline T* AllocInit()
{
2017-11-19 12:53:39 +00:00
const auto size = sizeof( T );
2020-02-23 18:16:33 +00:00
auto ret = AllocRaw( size );
2017-11-19 12:53:39 +00:00
new( ret ) T;
return (T*)ret;
}
template<typename T>
2019-10-29 00:19:40 +00:00
tracy_force_inline T* AllocInit( size_t sz )
{
const auto size = sizeof( T ) * sz;
2020-02-23 18:16:33 +00:00
auto ret = AllocRaw( size );
T* ptr = (T*)ret;
for( size_t i=0; i<sz; i++ )
{
new( ptr ) T;
ptr++;
}
return (T*)ret;
}
2017-09-15 17:54:43 +00:00
template<typename T>
tracy_force_inline T* Alloc()
2017-09-15 17:54:43 +00:00
{
return (T*)AllocRaw( sizeof( T ) );
2017-09-15 17:54:43 +00:00
}
template<typename T>
tracy_force_inline T* Alloc( size_t size )
{
return (T*)AllocRaw( sizeof( T ) * size );
}
tracy_force_inline void Unalloc( size_t size )
2017-09-15 17:41:09 +00:00
{
assert( size <= m_offset );
m_offset -= size;
}
tracy_force_inline void* AllocBig( size_t size )
{
2020-02-23 18:43:00 +00:00
const auto offset = m_offset;
if( offset + size <= BlockSize )
{
2020-02-23 18:43:00 +00:00
void* ret = m_ptr + offset;
m_offset += size;
return ret;
}
2020-02-23 18:43:00 +00:00
else if( size <= BlockSize && BlockSize - offset <= 1024 )
{
2020-02-23 18:43:00 +00:00
return DoAlloc( size );
}
else
{
memUsage += size;
m_usage += size;
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 )
{
memUsage -= m_usage - BlockSize;
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:
2020-02-23 18:43:00 +00:00
void* DoAlloc( uint32_t willUseBytes )
{
2020-02-23 18:43:00 +00:00
auto ptr = new char[BlockSize];
m_ptr = ptr;
m_offset = willUseBytes;
m_buffer.emplace_back( m_ptr );
memUsage += BlockSize;
m_usage += BlockSize;
2020-02-23 18:43:00 +00:00
return ptr;
}
2017-09-15 17:41:09 +00:00
char* m_ptr;
uint32_t m_offset;
2017-09-15 17:41:09 +00:00
std::vector<char*> m_buffer;
size_t m_usage;
2017-09-15 17:41:09 +00:00
};
}
#endif