diff --git a/dev.oid.toml b/dev.oid.toml index 9e3ac26..bf70202 100644 --- a/dev.oid.toml +++ b/dev.oid.toml @@ -10,6 +10,7 @@ containers = [ "PWD/types/folly_iobuf_type.toml", "PWD/types/folly_iobuf_queue_type.toml", "PWD/types/set_type.toml", + "PWD/types/multi_set_type.toml", "PWD/types/unordered_set_type.toml", "PWD/types/seq_type.toml", "PWD/types/list_type.toml", diff --git a/oi/ContainerTypeEnum.h b/oi/ContainerTypeEnum.h index 664bc7e..a218754 100644 --- a/oi/ContainerTypeEnum.h +++ b/oi/ContainerTypeEnum.h @@ -20,6 +20,7 @@ X(ARRAY_TYPE) \ X(SMALL_VEC_TYPE) \ X(SET_TYPE) \ + X(MULTI_SET_TYPE) \ X(UNORDERED_SET_TYPE) \ X(SEQ_TYPE) \ X(LIST_TYPE) \ diff --git a/oi/TreeBuilder.cpp b/oi/TreeBuilder.cpp index 5fe924e..7aff17d 100644 --- a/oi/TreeBuilder.cpp +++ b/oi/TreeBuilder.cpp @@ -843,6 +843,7 @@ void TreeBuilder::processContainer(const Variable& variable, Node& node) { containerStats.length = next(); break; case RADIX_TREE_TYPE: + case MULTI_SET_TYPE: case MULTI_MAP_TYPE: case BY_MULTI_QRT_TYPE: containerStats.length = containerStats.capacity = next(); diff --git a/test/ci.oid.toml b/test/ci.oid.toml index 2b39fe1..5ac1a80 100644 --- a/test/ci.oid.toml +++ b/test/ci.oid.toml @@ -6,6 +6,7 @@ containers = [ "../types/folly_iobuf_type.toml", "../types/folly_iobuf_queue_type.toml", "../types/set_type.toml", + "../types/multi_set_type.toml", "../types/unordered_set_type.toml", "../types/seq_type.toml", "../types/list_type.toml", diff --git a/test/integration/std_multiset_custom_comparator.toml b/test/integration/std_multiset_custom_comparator.toml new file mode 100644 index 0000000..04d5da8 --- /dev/null +++ b/test/integration/std_multiset_custom_comparator.toml @@ -0,0 +1,73 @@ +definitions = ''' + struct CustomComparator { + bool operator()(const int& left, const int& right) const { + return left < right; + } + }; + + struct SmallSizedCustomComparator { + double a; + bool operator()(const int& left, const int& right) const { + return left < right; + } + }; + + struct BigSizedCustomComparator { + double d[1000]; + bool operator()(const int& left, const int& right) const { + return left < right; + } + }; + + struct Foo { + std::multiset m1; + std::multiset m2; + std::multiset m3; + std::multiset m4; + }; + +''' +includes = ["set", "functional"] + +[cases] + [cases.a] + param_types = ["const Foo&"] + setup = ''' + Foo foo; + + for (int i = 0; i < 3; i++) { + foo.m1.insert(i); + } + + for (int i = 0; i < 5; i++) { + foo.m2.insert(i); + } + + for (int i = 0; i < 7; i++) { + foo.m3.insert(i); + } + + for (int i = 0; i < 9; i++) { + foo.m4.insert(i); + } + + return {foo}; + ''' + expect_json = '''[{ + "staticSize":8184, + "dynamicSize":96, + "members":[ + {"name":"m1", "staticSize":48, "dynamicSize":12, "length":3, "capacity":3, "elementStaticSize":4}, + {"name":"m2", "staticSize":48, "dynamicSize":20, "length":5, "capacity":5, "elementStaticSize":4}, + {"name":"m3", "staticSize":48, "dynamicSize":28, "length":7, "capacity":7, "elementStaticSize":4}, + {"name":"m4", "staticSize":8040, "dynamicSize":36, "length":9, "capacity":9, "elementStaticSize":4} + ]}]''' + expect_json_v2 = '''[{ + "staticSize":8184, + "exclusiveSize": 0, + "members":[ + {"name":"m1", "staticSize":48, "exclusiveSize": 156, "length": 3, "capacity": 3}, + {"name":"m2", "staticSize":48, "exclusiveSize": 228, "length": 5, "capacity": 5}, + {"name":"m3", "staticSize":48, "exclusiveSize": 300, "length": 7, "capacity": 7}, + {"name":"m4", "staticSize":8040, "exclusiveSize": 8364, "length": 9, "capacity": 9} + ]}]''' diff --git a/types/multi_set_type.toml b/types/multi_set_type.toml new file mode 100644 index 0000000..d693cb4 --- /dev/null +++ b/types/multi_set_type.toml @@ -0,0 +1,149 @@ +[info] +type_name = "std::multiset" +stub_template_params = [1,2] +ctype = "MULTI_SET_TYPE" +header = "set" + +# Old: +typeName = "std::multiset<" +ns = ["namespace std"] +numTemplateParams = 1 +replaceTemplateParamIndex = [1] +allocatorIndex = 2 + +[codegen] +decl = """ +template +void getSizeType(const %1% &container, size_t& returnArg); +""" + +func = """ +template +void getSizeType(const %1% &container, size_t& returnArg) +{ + constexpr size_t nodeSize = sizeof(typename %1%::node_type); + size_t numElems = container.size(); + + SAVE_SIZE(sizeof(%1%) + (nodeSize * numElems)); + + SAVE_DATA((uintptr_t)numElems); + + // The double ampersand is needed otherwise this loop doesn't work with vector + for (auto&& it: container) { + getSizeType(it, returnArg); + } +} +""" + +handler = """ +template +struct TypeHandler> { + using type = types::st::List::type>; + + static types::st::Unit getSizeType( + const %1%& container, + typename TypeHandler>::type returnArg) { + constexpr size_t nodeSize = sizeof(typename %1%::node_type); + + auto tail = returnArg.write(container.size()); + + // The double ampersand is needed otherwise this loop doesn't work with + // vector + for (auto&& it : container) { + tail = tail.delegate([&it](auto ret) { + return OIInternal::getSizeType(it, ret); + }); + } + + return tail.finish(); + } +}; +""" + +traversal_func = """ +auto tail = returnArg.write((uintptr_t)&container) + .write(container.size()); + +// The double ampersand is needed otherwise this loop doesn't work with +// vector +for (auto&& it : container) { + tail = tail.delegate([&it](auto ret) { + return OIInternal::getSizeType(it, ret); + }); +} + +return tail.finish(); +""" + +[[codegen.processor]] +type = "types::st::VarInt" +func = """ +el.pointer = std::get(d.val).value; +""" + +[[codegen.processor]] +type = "types::st::List::type>" +func = """ +#ifdef __GLIBCXX__ +/* We don't have access to the _Rb_tree_node struct, so we manually re-create it + * to get the effective size of a multiset entry. Is there a better way to do this? + * + * https://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00716_source.html#l00216 + * From the source of , an _Rb_tree_node has the following members: + */ +struct OI_Rb_tree_node { + using _Rb_tree_color = int; // enum + using _Base_ptr = std::nullptr_t; // pointer + + _Rb_tree_color _M_color; // from parent _Rb_tree_node_base + _Base_ptr _M_parent; // from parent _Rb_tree_node_base + _Base_ptr _M_left; // from parent _Rb_tree_node_base + _Base_ptr _M_right; // from parent _Rb_tree_node_base + T0 _M_value; +}; + +static constexpr size_t element_size = sizeof(OI_Rb_tree_node); +#elif _LIBCPP_VERSION +static_assert(false && "libc++ is currently not supported"); + +/* We don't have access to the __tree_node struct, so we manually re-create it + * to get the effective size of a multiset entry. Is there a better way to do this? + * + * https://github.com/llvm/llvm-project/blob/1b10920164695a487669405223f8bbe93799430c/libcxx/include/__tree#L729-L781 + * From the source of <__tree>, a __tree_node has the following members: + */ +struct OI__tree_node { + using pointer = std::nullptr_t; // pointer + using __parent_pointer = std::nullptr_t; // pointer + + pointer __left_; // from parent __tree_end_node + pointer __right_; // from parent __tree_node_base + __parent_pointer __parent_; // from parent __tree_node_base + bool __is_black_; // from parent __tree_node_base + T0 __value_; +}; + +static constexpr size_t element_size = sizeof(OI__tree_node); +#else +static_assert(false && "No known element_size for multisets. See types/multi_set_type.toml"); +#endif + +static constexpr std::array names{"TODO"}; +static constexpr auto childField = inst::Field{ + sizeof(T0), + "[]", + names, + TypeHandler::fields, + TypeHandler::processors, +}; + +auto list = std::get(d.val); +el.container_stats.emplace(result::Element::ContainerStats { + .capacity = list.length, + .length = list.length, +}); +el.exclusive_size += el.container_stats->length * (element_size - sizeof(T0)); + +for (size_t i = 0; i < list.length; i++) + ins.emplace(childField); +"""