-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjitasm.CodeBuffer.h
71 lines (66 loc) · 1.62 KB
/
jitasm.CodeBuffer.h
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
#pragma once
#ifndef jitasm_CodeBuffer_h__
#define jitasm_CodeBuffer_h__
#include "jitasm.h"
namespace jitasm
{
template < typename Derived > class CodeBuffer$CRTP /* using Curiously Recurring Template Pattern */
{
protected:
void * buffaddr_;
size_t codesize_;
size_t buffsize_;
Derived & derived()
{
return *static_cast<Derived *>(this);
}
Derived const & derived() const
{
return *static_cast<Derived const *>(this);
}
public:
CodeBuffer$CRTP() : buffaddr_(nullptr), codesize_(0), buffsize_(0)
{
}
~CodeBuffer$CRTP()
{
ResetBuffer(0);
}
void * GetBufferPointer() const
{
return buffaddr_;
}
size_t GetBufferCapacity() const
{
return buffsize_;
}
size_t GetBufferSize() const
{
return codesize_;
}
bool ResetBuffer(size_t codesize)
{
bool result = true;
if (buffaddr_)
{
result = derived().FreeBuffer();
if (result)
{
buffaddr_ = nullptr;
codesize_ = 0;
buffsize_ = 0;
}
}
if (result && codesize)
{
result = derived().AllocateBuffer(codesize);
if (result)
{
codesize_ = codesize;
}
}
return result;
}
};
}
#endif // jitasm_CodeBuffer_h__