Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/dep/vixl/src/code-buffer-vixl.cc
4253 views
1
// Copyright 2017, VIXL authors
2
// All rights reserved.
3
//
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are met:
6
//
7
// * Redistributions of source code must retain the above copyright notice,
8
// this list of conditions and the following disclaimer.
9
// * Redistributions in binary form must reproduce the above copyright notice,
10
// this list of conditions and the following disclaimer in the documentation
11
// and/or other materials provided with the distribution.
12
// * Neither the name of ARM Limited nor the names of its contributors may be
13
// used to endorse or promote products derived from this software without
14
// specific prior written permission.
15
//
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27
#include "code-buffer-vixl.h"
28
#include "utils-vixl.h"
29
30
namespace vixl {
31
32
CodeBuffer::CodeBuffer() : buffer_(nullptr), cursor_(nullptr), dirty_(false), capacity_(0)
33
{
34
}
35
36
CodeBuffer::CodeBuffer(byte* buffer, size_t capacity)
37
: buffer_(reinterpret_cast<byte*>(buffer)),
38
cursor_(reinterpret_cast<byte*>(buffer)),
39
dirty_(false),
40
capacity_(capacity) {
41
VIXL_ASSERT(buffer_ != NULL);
42
}
43
44
45
CodeBuffer::~CodeBuffer() VIXL_NEGATIVE_TESTING_ALLOW_EXCEPTION {
46
VIXL_ASSERT(!IsDirty());
47
}
48
49
50
void CodeBuffer::EmitString(const char* string) {
51
const auto len = strlen(string) + 1;
52
VIXL_ASSERT(HasSpaceFor(len));
53
char* dst = reinterpret_cast<char*>(cursor_);
54
dirty_ = true;
55
memcpy(dst, string, len);
56
cursor_ = reinterpret_cast<byte*>(dst + len);
57
}
58
59
60
void CodeBuffer::EmitData(const void* data, size_t size) {
61
VIXL_ASSERT(HasSpaceFor(size));
62
dirty_ = true;
63
memcpy(cursor_, data, size);
64
cursor_ = cursor_ + size;
65
}
66
67
68
void CodeBuffer::UpdateData(size_t offset, const void* data, size_t size) {
69
dirty_ = true;
70
byte* dst = buffer_ + offset;
71
VIXL_ASSERT(dst + size <= cursor_);
72
memcpy(dst, data, size);
73
}
74
75
76
void CodeBuffer::Align() {
77
byte* end = AlignUp(cursor_, 4);
78
const size_t padding_size = end - cursor_;
79
VIXL_ASSERT(padding_size <= 4);
80
EmitZeroedBytes(static_cast<int>(padding_size));
81
}
82
83
void CodeBuffer::EmitZeroedBytes(int n) {
84
VIXL_ASSERT(HasSpaceFor(n));
85
dirty_ = true;
86
memset(cursor_, 0, n);
87
cursor_ += n;
88
}
89
90
void CodeBuffer::Reset() {
91
cursor_ = buffer_;
92
SetClean();
93
}
94
95
void CodeBuffer::Reset(byte* buffer, size_t capacity) {
96
buffer_ = buffer;
97
cursor_ = buffer;
98
capacity_ = capacity;
99
SetClean();
100
}
101
102
} // namespace vixl
103
104