Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TracySlab.hpp « server - github.com/wolfpld/tracy.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 26274c8c4b86828c327e80daf4205544887c0100 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#ifndef __TRACYSLAB_HPP__
#define __TRACYSLAB_HPP__

#include <assert.h>
#include <stdint.h>
#include <vector>

#include "TracyMemory.hpp"
#include "../public/common/TracyForceInline.hpp"

namespace tracy
{

template<size_t BlockSize>
class Slab
{
public:
    Slab()
        : m_ptr( new char[BlockSize] )
        , m_offset( 0 )
        , m_buffer( { m_ptr } )
        , m_usage( BlockSize )
    {
        memUsage += BlockSize;
    }

    ~Slab()
    {
        memUsage -= m_usage;
        for( auto& v : m_buffer )
        {
            delete[] v;
        }
    }

    tracy_force_inline void* AllocRaw( size_t size )
    {
        assert( size <= BlockSize );
        const auto offset = m_offset;
        if( offset + size > BlockSize )
        {
            return DoAlloc( size );
        }
        else
        {
            void* ret = m_ptr + offset;
            m_offset += size;
            return ret;
        }
    }

    template<typename T>
    tracy_force_inline T* AllocInit()
    {
        const auto size = sizeof( T );
        auto ret = AllocRaw( size );
        new( ret ) T;
        return (T*)ret;
    }

    template<typename T>
    tracy_force_inline T* AllocInit( size_t sz )
    {
        const auto size = sizeof( T ) * sz;
        auto ret = AllocRaw( size );
        T* ptr = (T*)ret;
        for( size_t i=0; i<sz; i++ )
        {
            new( ptr ) T;
            ptr++;
        }
        return (T*)ret;
    }

    template<typename T>
    tracy_force_inline T* Alloc()
    {
        return (T*)AllocRaw( sizeof( T ) );
    }

    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 )
    {
        assert( size <= m_offset );
        m_offset -= size;
    }

    tracy_force_inline void* AllocBig( size_t size )
    {
        const auto offset = m_offset;
        if( offset + size <= BlockSize )
        {
            void* ret = m_ptr + offset;
            m_offset += size;
            return ret;
        }
        else if( size <= BlockSize && BlockSize - offset <= 1024 )
        {
            return DoAlloc( size );
        }
        else
        {
            memUsage += size;
            m_usage += size;
            auto ret = new char[size];
            m_buffer.emplace_back( ret );
            return ret;
        }
    }

    void Reset()
    {
        if( m_buffer.size() > 1 )
        {
            memUsage -= m_usage - BlockSize;
            m_usage = BlockSize;
            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:
    void* DoAlloc( uint32_t willUseBytes )
    {
        auto ptr = new char[BlockSize];
        m_ptr = ptr;
        m_offset = willUseBytes;
        m_buffer.emplace_back( m_ptr );
        memUsage += BlockSize;
        m_usage += BlockSize;
        return ptr;
    }

    char* m_ptr;
    uint32_t m_offset;
    std::vector<char*> m_buffer;
    size_t m_usage;
};

}

#endif