Skip to content

Commit a579a8e

Browse files
committed
[libc++] Add ABI flag to make __tree nodes more compact
1 parent 9286b54 commit a579a8e

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

libcxx/include/__configuration/abi.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
# define _LIBCPP_ABI_OPTIMIZED_FUNCTION
7676
# define _LIBCPP_ABI_REGEX_CONSTANTS_NONZERO
7777
# define _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
78+
# define _LIBCPP_ABI_TREE_POINTER_INT_PAIR
7879
# define _LIBCPP_ABI_USE_WRAP_ITER_IN_STD_ARRAY
7980
# define _LIBCPP_ABI_USE_WRAP_ITER_IN_STD_STRING_VIEW
8081
# define _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
@@ -98,6 +99,8 @@
9899
# endif
99100
#endif
100101

102+
#define _LIBCPP_ABI_TREE_POINTER_INT_PAIR
103+
101104
// We had some bugs where we use [[no_unique_address]] together with construct_at,
102105
// which causes UB as the call on construct_at could write to overlapping subobjects
103106
//

libcxx/include/__tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <__utility/forward.h>
3737
#include <__utility/move.h>
3838
#include <__utility/pair.h>
39+
#include <__utility/pointer_int_pair.h>
3940
#include <__utility/swap.h>
4041
#include <__utility/try_key_extraction.h>
4142
#include <limits>
@@ -575,6 +576,8 @@ public:
575576
using pointer = __rebind_pointer_t<_VoidPtr, __tree_node_base>;
576577
using __end_node_pointer _LIBCPP_NODEBUG = __rebind_pointer_t<_VoidPtr, __tree_end_node<pointer> >;
577578

579+
static_assert(is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value);
580+
578581
pointer __right_;
579582

580583
private:
@@ -595,6 +598,43 @@ public:
595598
__tree_node_base& operator=(__tree_node_base const&) = delete;
596599
};
597600

601+
#ifdef _LIBCPP_ABI_TREE_POINTER_INT_PAIR
602+
template <>
603+
class __tree_node_base<void*> : public __tree_end_node<__tree_node_base<void*>*> {
604+
public:
605+
using pointer = __tree_node_base<void*>*;
606+
using __end_node_pointer _LIBCPP_NODEBUG = __tree_end_node<__tree_node_base<void*>*>*;
607+
608+
pointer __right_;
609+
610+
private:
611+
using __pair_t = __pointer_int_pair<__end_node_pointer, bool, __integer_width(1)>;
612+
613+
__pair_t __parent_and_color_;
614+
615+
public:
616+
_LIBCPP_HIDE_FROM_ABI pointer __parent_unsafe() const {
617+
return static_cast<pointer>(__parent_and_color_.__get_ptr());
618+
}
619+
620+
_LIBCPP_HIDE_FROM_ABI void __set_parent(pointer __ptr) { __set_parent(static_cast<__end_node_pointer>(__ptr)); }
621+
622+
_LIBCPP_HIDE_FROM_ABI void __set_parent(__end_node_pointer __ptr) {
623+
__parent_and_color_ = __pair_t(__ptr, __parent_and_color_.__get_value());
624+
}
625+
626+
_LIBCPP_HIDE_FROM_ABI __end_node_pointer __get_parent() const { return __parent_and_color_.__get_ptr(); }
627+
_LIBCPP_HIDE_FROM_ABI __tree_color __get_color() const {
628+
return static_cast<__tree_color>(__parent_and_color_.__get_value());
629+
}
630+
_LIBCPP_HIDE_FROM_ABI void __set_color(__tree_color __color) {
631+
__parent_and_color_ = __pair_t(__parent_and_color_.__get_ptr(), __color == __tree_color::__black);
632+
}
633+
};
634+
#endif // _LIBCPP_ABI_TREE_POINTER_INT_PAIR
635+
636+
static_assert(sizeof(__tree_node_base<void*>) == 24);
637+
598638
template <class _Tp, class _VoidPtr>
599639
class _LIBCPP_STANDALONE_DEBUG __tree_node : public __tree_node_base<_VoidPtr> {
600640
public:

0 commit comments

Comments
 (0)