-
Notifications
You must be signed in to change notification settings - Fork 524
/
Copy pathemitter.cpp
167 lines (141 loc) · 3.75 KB
/
emitter.cpp
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
160
161
162
163
164
165
166
167
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <executorch/devtools/etdump/emitter.h>
#include <cstdint>
#include <cstring>
#include <executorch/devtools/etdump/etdump_flatcc.h>
#include <executorch/runtime/platform/assert.h>
#include <flatcc/flatcc_builder.h>
using executorch::etdump::internal::ETDumpStaticAllocator;
namespace executorch {
namespace etdump {
namespace internal {
namespace {
int allocator_fn(
void* alloc_context,
flatcc_iovec_t* b,
size_t request,
int zero_fill,
int hint) {
void* p;
size_t n;
ETDumpStaticAllocator* state =
reinterpret_cast<ETDumpStaticAllocator*>(alloc_context);
// This allocator doesn't support freeing memory.
if (request == 0) {
if (b->iov_base) {
b->iov_base = nullptr;
b->iov_len = 0;
}
return 0;
}
switch (hint) {
case flatcc_builder_alloc_ds:
n = 256;
break;
case flatcc_builder_alloc_ht:
/* Should be exact size, or space size is just wasted. */
n = request;
break;
case flatcc_builder_alloc_fs:
n = sizeof(__flatcc_builder_frame_t) * 8;
break;
case flatcc_builder_alloc_us:
n = 64;
break;
case flatcc_builder_alloc_vd:
n = 64;
break;
default:
/*
* We have many small structures - vs stack for tables with few
* elements, and few offset fields in patch log. No need to
* overallocate in case of busy small messages.
*/
n = 32;
break;
}
while (n < request) {
n *= 2;
}
if (b->iov_base != nullptr) {
if (request > b->iov_len) {
// We don't support reallocating larger buffers.
if (((uintptr_t)b->iov_base + b->iov_len) ==
(uintptr_t)&state->data[state->allocated]) {
if ((state->allocated + n - b->iov_len) > state->data_size) {
return -1;
}
state->allocated += n - b->iov_len;
} else {
if ((state->allocated + n) > state->data_size) {
return -1;
}
memcpy((void*)&state->data[state->allocated], b->iov_base, b->iov_len);
b->iov_base = &state->data[state->allocated];
state->allocated += n;
}
if (zero_fill) {
memset((uint8_t*)b->iov_base + b->iov_len, 0, n - b->iov_len);
}
b->iov_len = n;
}
// Ignore request to resize buffers down.
return 0;
}
if ((state->allocated + n) > state->data_size) {
return -1;
}
p = &state->data[state->allocated];
state->allocated += n;
if (zero_fill) {
memset((void*)p, 0, n);
}
b->iov_base = p;
b->iov_len = n;
return 0;
}
// This emitter implementation emits to a fixed size buffer and will fail if it
// runs out of room on either end.
int emitter_fn(
void* emit_context,
const flatcc_iovec_t* iov,
int iov_count,
flatbuffers_soffset_t offset,
size_t len) {
ETDumpStaticAllocator* E =
reinterpret_cast<ETDumpStaticAllocator*>(emit_context);
uint8_t* p;
if (offset < 0) {
if (len > E->front_left) {
return -1;
}
E->front_cursor -= len;
E->front_left -= len;
p = E->front_cursor;
} else {
ET_CHECK_MSG(
0, "Moving the back pointer is currently not supported in ETDump.");
}
while (iov_count--) {
memcpy(p, iov->iov_base, iov->iov_len);
p += iov->iov_len;
++iov;
}
return 0;
}
} // namespace
int etdump_flatcc_custom_init(
flatcc_builder_t* builder,
struct ETDumpStaticAllocator* alloc) {
return flatcc_builder_custom_init(
builder, emitter_fn, alloc, allocator_fn, alloc);
}
} // namespace internal
} // namespace etdump
} // namespace executorch