[libcxx-commits] [libcxx] [libc++] Add randomize unspecified stability in `__hash_table` (PR #105982)

Arvid Jonasson via libcxx-commits libcxx-commits at lists.llvm.org
Sun Aug 25 08:35:52 PDT 2024


================
@@ -1741,6 +1760,70 @@ void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__do_rehash(size_type __nbc) {
   }
 }
 
+template <class _Tp, class _Hash, class _Equal, class _Alloc>
+template <bool _UniqueKeys>
+void __hash_table<_Tp, _Hash, _Equal, _Alloc>::__debug_randomize_order() {
+#ifdef _LIBCPP_DEBUG_RANDOMIZE_UNSPECIFIED_STABILITY
+
+  struct __nh_vec {
+    using __nh_allocator = __rebind_alloc<__node_traits, __node_holder>;
+    using __pointer      = typename allocator_traits<__nh_allocator>::pointer;
+    __nh_allocator __nh_alloc;
+    __pointer __p                 = __pointer();
+    size_type __total_nodes       = 0;
+    size_type __initialized_nodes = 0;
+    __nh_vec(size_type __n) : __nh_alloc(), __p(__nh_alloc.allocate(__n)), __total_nodes(__n) {}
+    ~__nh_vec() {
+      if (__p) {
+        std::__destroy(__p, __p + __initialized_nodes);
+        __nh_alloc.deallocate(__p, __total_nodes);
+      }
+    }
+  };
+
+  struct __index_vec {
+    using __index_allocator = __rebind_alloc<__node_traits, size_type>;
+    using __pointer         = typename allocator_traits<__index_allocator>::pointer;
+    __index_allocator __index_alloc;
+    __pointer __p                   = __pointer();
+    size_type __total_indices       = 0;
+    size_type __initialized_indices = 0;
+    __index_vec(size_type __n) : __index_alloc(), __p(__index_alloc.allocate(__n)), __total_indices(__n) {}
+    ~__index_vec() {
+      if (__p) {
+        std::__destroy(__p, __p + __initialized_indices);
+        __index_alloc.deallocate(__p, __total_indices);
+      }
+    }
+  };
+
+  __nh_vec __nhv(size());
+  __index_vec __iv(size());
----------------
arvidjonasson wrote:

Ideally I'd like to use `unique_ptr<__node_holder[], decltype(deleter)>` and `unique_ptr<size_type[], decltype(deleter)>` (instead of `__nh_vec` and `__index_vec`) while still being able to use the user defined allocator. But `min_pointer<T>` is giving me problems (by not being convertible to `T*`). Is someone familiar with the matter?

https://github.com/llvm/llvm-project/pull/105982


More information about the libcxx-commits mailing list