object-introspection/types/small_vec_type.toml
Jake Hillion d71307cb43 oil: change std::stack reference to a std::function (#345)
Summary:

Previously on large types OIL would have problems with corrupting the `std::stack<exporter::inst::Inst>` that is passed to the processors. This change hides the implementation of the stack from the processors by wrapping the call to emplace in a `std::function` written by the non-generated code, which solves the test case I've seen for this crashing. It also allows us to easily change the stack implementation in future - I plan to change it to a `std::stack<T, std::vector<T>>` in a follow up.

Reviewed By: tyroguru

Differential Revision: D49273116
2023-09-14 16:57:45 +01:00

131 lines
3.9 KiB
TOML

[info]
type_name = "folly::small_vector"
ctype = "SMALL_VEC_TYPE"
header = "folly/small_vector.h"
stub_template_params = [2]
# Old:
typeName = "folly::small_vector<"
ns = ["folly::small_vector_policy::policy_size_type", "folly::small_vector"]
numTemplateParams = 1
replaceTemplateParamIndex = []
[codegen]
decl = """
template <class V, std::size_t N, class P>
void getSizeType(const %1%<V, N, P> &container, size_t& returnArg);
"""
func = """
template <class V, std::size_t N, class P>
void getSizeType(const %1%<V, N, P> &container, size_t& returnArg)
{
SAVE_SIZE(sizeof(%1%<V, N, P>));
bool dataInlined = ((uintptr_t)container.data() >= (uintptr_t)&container) &&
((uintptr_t)container.data() < (uintptr_t)(&container + sizeof(%1%<V, N, P>)));
if (dataInlined) {
// Don't double count inlined elements
SAVE_SIZE(-(container.size() * sizeof(V)));
} else {
// Account for wasted space in the buffer
SAVE_SIZE((container.capacity() - container.size()) * sizeof(V));
}
SAVE_DATA((uintptr_t)(N));
SAVE_DATA((uintptr_t)container.capacity());
SAVE_DATA((uintptr_t)container.size());
for (auto & it: container) {
getSizeType(it, returnArg);
}
}
"""
handler = """
template <typename DB, typename T0, std::size_t N0, typename T1>
struct TypeHandler<DB, %1%<T0, N0, T1>> {
using type = types::st::Pair<
DB, types::st::VarInt<DB>,
types::st::Pair<
DB, types::st::VarInt<DB>,
types::st::List<DB, typename TypeHandler<DB, T0>::type>>>;
static types::st::Unit<DB> getSizeType(
const %1%<T0, N0, T1> & container,
typename TypeHandler<DB, %1%<T0, N0, T1>>::type returnArg) {
auto tail = returnArg.write(N0)
.write(container.capacity())
.write(container.size());
for (auto& it : container) {
tail = tail.delegate([&it](auto ret) {
return OIInternal::getSizeType<DB>(it, ret);
});
}
return tail.finish();
}
};
"""
traversal_func = """
// If `container.data()` pointer is within the container struct,
// then the container's storage is inlined and doesn't uses the heap.
// TODO: Is there an API to get this information?
bool uses_intern_storage =
(uintptr_t)&container <= (uintptr_t)container.data() &&
(uintptr_t)container.data() < ((uintptr_t)&container + sizeof(container));
auto tail = returnArg
.write((uintptr_t)uses_intern_storage)
.write(container.capacity())
.write(container.size());
for (auto &&it: container) {
tail = tail.delegate([&it](typename TypeHandler<DB, T0>::type ret) {
return OIInternal::getSizeType<DB>(it, ret);
});
}
return tail.finish();
"""
[[codegen.processor]]
type = "types::st::VarInt<DB>"
func = """
// Using the container's pointer to temporarily store the uses_intern_storage boolean.
// TODO: Is there another way to pass a value across processors?
el.pointer = std::get<ParsedData::VarInt>(d.val).value;
"""
[[codegen.processor]]
type = "types::st::VarInt<DB>"
func = """
el.container_stats.emplace(result::Element::ContainerStats {
.capacity = std::get<ParsedData::VarInt>(d.val).value,
});
"""
[[codegen.processor]]
type = "types::st::List<DB, typename TypeHandler<DB, T0>::type>"
func = """
// Reading the `uses_intern_storage` boolean that was stored in `pointer` by the processor above.
bool uses_intern_storage = std::exchange(el.pointer.value(), (uintptr_t)0);
auto list = std::get<ParsedData::List>(d.val);
el.container_stats->length = list.length;
if (uses_intern_storage) {
// The storage is inlined, so don't double count for items using the intern storage.
el.exclusive_size -= list.length * sizeof(T0);
} else {
// The storage is heap allocated, so add any unused capacity.
el.exclusive_size += (el.container_stats->capacity - el.container_stats->length) * sizeof(T0);
}
static constexpr auto childField = make_field<DB, T0>("[]");
for (size_t i = 0; i < list.length; i++)
stack_ins(childField);
"""