少年修仙传客户端基础资源
hch
2024-04-01 d01413b00ef631ac20347716b23818b0b811f65f
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
#pragma once
 
#include <stack>
#include <cmath>
#include <vector>
 
#include "../CommonDef.h"
 
namespace hybridclr
{
    namespace transform
    {
        const size_t kMinBlockSize = 8 * 1024;
 
        class TemporaryMemoryArena
        {
        public:
 
            TemporaryMemoryArena() : _buf(nullptr), _size(0), _pos(0)
            {
                Begin();
            }
            ~TemporaryMemoryArena()
            {
                End();
            }
 
            static size_t AligndSize(size_t size)
            {
                return (size + 7) & ~7;
            }
 
            template<typename T>
            T* AllocIR()
            {
                const size_t aligndSize = AligndSize(sizeof(T));
                if (_pos + aligndSize <= _size)
                {
                    T* ir = (T*)(_buf + _pos);
                    *ir = {};
                    _pos += aligndSize;
                    return ir;
                }
 
                RequireSize(aligndSize);
 
                T* ir = (T*)(_buf + _pos);
                *ir = {};
                _pos += aligndSize;
                return ir;
            }
 
            template<typename T>
            T* NewAny()
            {
                const size_t needSize = AligndSize(sizeof(T));
                if (_pos + needSize <= _size)
                {
                    T* ir = new (_buf + _pos) T();
                    *ir = {};
                    _pos += needSize;
                    return ir;
                }
 
                RequireSize(needSize);
 
                T* ir = new (_buf + _pos) T();
                *ir = {};
                _pos += needSize;
                return ir;
            }
 
            template<typename T>
            T* NewNAny(int n)
            {
                if (n > 0)
                {
                    size_t bytes = AligndSize(sizeof(T) * n);
                    if (_pos + bytes > _size)
                    {
                        RequireSize(bytes);
                    }
                    T* ret = new (_buf + _pos) T[n];
                    _pos += bytes;
                    return ret;
                }
                else
                {
                    return nullptr;
                }
            }
 
            void Begin();
 
            void End();
 
        private:
            struct Block
            {
                void* data;
                size_t size;
            };
 
            void RequireSize(size_t size)
            {
                if (_buf)
                {
                    _useOuts.push_back({ (void*)_buf, _size});
                }
 
                Block newBlock = AllocBlock(std::max(size, kMinBlockSize));
                _buf = (byte*)newBlock.data;
                _size = newBlock.size;
                _pos = 0;
            }
 
            static Block AllocBlock(size_t size);
 
            std::vector<Block> _useOuts;
 
            byte* _buf;
            size_t _size;
            size_t _pos;
        };
    }
}