Allow exact size allocations in Vector.

This commit is contained in:
Bartosz Taudul 2018-08-09 18:57:06 +02:00
parent 68dd90cb94
commit dbf4de0694

View File

@ -2,6 +2,7 @@
#define __TRACYVECTOR_HPP__
#include <assert.h>
#include <limits>
#include <stdint.h>
#include "../common/TracyForceInline.hpp"
@ -32,6 +33,8 @@ public:
, m_capacity( src.m_capacity )
{
src.m_ptr = nullptr;
src.m_size = 0;
src.m_capacity = 0;
}
Vector( const T& value )
@ -45,7 +48,14 @@ public:
~Vector()
{
memUsage.fetch_sub( Capacity() * sizeof( T ), std::memory_order_relaxed );
if( m_capacity == std::numeric_limits<uint8_t>::max() )
{
memUsage.fetch_sub( m_size * sizeof( T ) );
}
else
{
memUsage.fetch_sub( Capacity() * sizeof( T ), std::memory_order_relaxed );
}
delete[] m_ptr;
}
@ -57,13 +67,15 @@ public:
m_size = src.m_size;
m_capacity = src.m_capacity;
src.m_ptr = nullptr;
src.m_size = 0;
src.m_capacity = 0;
return *this;
}
tracy_force_inline bool empty() const { return m_size == 0; }
tracy_force_inline size_t size() const { return m_size; }
tracy_force_inline void set_size( size_t sz ) { m_size = sz; }
tracy_force_inline void set_size( size_t sz ) { assert( m_capacity != std::numeric_limits<uint8_t>::max() ); m_size = sz; }
tracy_force_inline T* data() { return m_ptr; }
tracy_force_inline const T* data() const { return m_ptr; };
@ -84,42 +96,49 @@ public:
tracy_force_inline void push_back( const T& v )
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
if( m_size == Capacity() ) AllocMore();
m_ptr[m_size++] = v;
}
tracy_force_inline void push_back_non_empty( const T& v )
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
if( m_size == CapacityNoNullptrCheck() ) AllocMore();
m_ptr[m_size++] = v;
}
tracy_force_inline void push_back_no_space_check( const T& v )
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
assert( m_size < Capacity() );
m_ptr[m_size++] = v;
}
tracy_force_inline void push_back( T&& v )
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
if( m_size == Capacity() ) AllocMore();
m_ptr[m_size++] = std::move( v );
}
tracy_force_inline T& push_next()
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
if( m_size == Capacity() ) AllocMore();
return m_ptr[m_size++];
}
tracy_force_inline T& push_next_no_space_check()
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
assert( m_size < Capacity() );
return m_ptr[m_size++];
}
T* insert( T* it, const T& v )
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
assert( it >= m_ptr && it <= m_ptr + m_size );
const auto dist = it - m_ptr;
if( m_size == Capacity() ) AllocMore();
@ -131,6 +150,7 @@ public:
T* insert( T* it, T&& v )
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
assert( it >= m_ptr && it <= m_ptr + m_size );
const auto dist = it - m_ptr;
if( m_size == Capacity() ) AllocMore();
@ -142,6 +162,7 @@ public:
void insert( T* it, T* begin, T* end )
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
assert( it >= m_ptr && it <= m_ptr + m_size );
const auto sz = end - begin;
const auto dist = it - m_ptr;
@ -153,6 +174,7 @@ public:
T* erase( T* it )
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
assert( it >= m_ptr && it <= m_ptr + m_size );
m_size--;
memmove( it, it+1, m_size * sizeof( T ) );
@ -161,6 +183,7 @@ public:
T* erase( T* begin, T* end )
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
assert( begin >= m_ptr && begin <= m_ptr + m_size );
assert( end >= m_ptr && end <= m_ptr + m_size );
assert( begin <= end );
@ -176,12 +199,14 @@ public:
tracy_force_inline void pop_back()
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
assert( m_size > 0 );
m_size--;
}
tracy_force_inline T& back_and_pop()
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
assert( m_size > 0 );
m_size--;
return m_ptr[m_size];
@ -195,6 +220,7 @@ public:
void reserve_non_zero( size_t cap )
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
cap--;
cap |= cap >> 1;
cap |= cap >> 2;
@ -209,18 +235,31 @@ public:
tracy_force_inline void reserve_and_use( size_t sz )
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
reserve( sz );
m_size = sz;
}
tracy_force_inline void reserve_exact( uint32_t sz )
{
assert( !m_ptr );
m_capacity = std::numeric_limits<uint8_t>::max();
m_size = sz;
m_ptr = new T[sz];
memUsage.fetch_add( sz * sizeof( T ) );
}
tracy_force_inline void clear()
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
m_size = 0;
}
private:
tracy_no_inline void AllocMore()
{
assert( m_capacity != std::numeric_limits<uint8_t>::max() );
if( m_ptr == nullptr )
{
memUsage.fetch_add( sizeof( T ), std::memory_order_relaxed );