Properly track memory usage in slab allocator.

This commit is contained in:
Bartosz Taudul 2019-02-15 02:28:31 +01:00
parent 23d12d2633
commit 8fd685c877

View File

@ -17,13 +17,14 @@ public:
: m_ptr( new char[BlockSize] ) : m_ptr( new char[BlockSize] )
, m_offset( 0 ) , m_offset( 0 )
, m_buffer( { m_ptr } ) , m_buffer( { m_ptr } )
, m_usage( BlockSize )
{ {
memUsage.fetch_add( BlockSize, std::memory_order_relaxed ); memUsage.fetch_add( BlockSize, std::memory_order_relaxed );
} }
~Slab() ~Slab()
{ {
memUsage.fetch_sub( BlockSize * m_buffer.size(), std::memory_order_relaxed ); memUsage.fetch_sub( m_usage, std::memory_order_relaxed );
for( auto& v : m_buffer ) for( auto& v : m_buffer )
{ {
delete[] v; delete[] v;
@ -93,6 +94,7 @@ public:
else else
{ {
memUsage.fetch_add( size ); memUsage.fetch_add( size );
m_usage += size;
auto ret = new char[size]; auto ret = new char[size];
m_buffer.emplace_back( ret ); m_buffer.emplace_back( ret );
return ret; return ret;
@ -103,7 +105,8 @@ public:
{ {
if( m_buffer.size() > 1 ) if( m_buffer.size() > 1 )
{ {
memUsage.fetch_sub( BlockSize * ( m_buffer.size() - 1 ), std::memory_order_relaxed ); memUsage.fetch_sub( m_usage - BlockSize, std::memory_order_relaxed );
m_usage = BlockSize;
for( int i=1; i<m_buffer.size(); i++ ) for( int i=1; i<m_buffer.size(); i++ )
{ {
delete[] m_buffer[i]; delete[] m_buffer[i];
@ -128,11 +131,13 @@ private:
m_offset = 0; m_offset = 0;
m_buffer.emplace_back( m_ptr ); m_buffer.emplace_back( m_ptr );
memUsage.fetch_add( BlockSize, std::memory_order_relaxed ); memUsage.fetch_add( BlockSize, std::memory_order_relaxed );
m_usage += BlockSize;
} }
char* m_ptr; char* m_ptr;
uint32_t m_offset; uint32_t m_offset;
std::vector<char*> m_buffer; std::vector<char*> m_buffer;
size_t m_usage;
}; };
} }