@@ -158,7 +158,7 @@ class NodeBase
158158 max_disk_size + max_number_of_children * KECCAK256_SIZE;
159159
160160 template <node_type DestNodeType, node_type SrcNodeType>
161- friend DestNodeType::UniquePtr copy_node (SrcNodeType const *const from);
161+ friend DestNodeType::SharedPtr copy_node (SrcNodeType const *const from);
162162
163163 /* 16-bit mask for children */
164164 uint16_t mask{0 };
@@ -327,6 +327,16 @@ class Node final : public NodeBase
327327 std::forward<Args>(args)...);
328328 }
329329
330+ template <class ... Args>
331+ static SharedPtr make_shared (size_t bytes, Args &&...args)
332+ {
333+ allocators::variable_size_allocator<Node> alloc (bytes);
334+ return std::allocate_shared<Node>(
335+ alloc,
336+ prevent_public_construction_tag{},
337+ std::forward<Args>(args)...);
338+ }
339+
330340 SharedPtr *child_ptr (unsigned index) noexcept ;
331341 SharedPtr const *child_ptr (unsigned index) const noexcept ;
332342
@@ -351,6 +361,7 @@ class CacheNode final : public NodeBase
351361 using Deleter = allocators::unique_ptr_aliasing_allocator_deleter<
352362 &allocators::aliasing_allocator_pair<CacheNode>>;
353363 using UniquePtr = std::unique_ptr<CacheNode, Deleter>;
364+ using SharedPtr = std::shared_ptr<CacheNode>;
354365
355366 CacheNode (prevent_public_construction_tag)
356367 : NodeBase()
@@ -368,6 +379,16 @@ class CacheNode final : public NodeBase
368379 std::forward<Args>(args)...);
369380 }
370381
382+ template <class ... Args>
383+ static SharedPtr make_shared (size_t bytes, Args &&...args)
384+ {
385+ allocators::variable_size_allocator<CacheNode> alloc (bytes);
386+ return std::allocate_shared<CacheNode>(
387+ alloc,
388+ prevent_public_construction_tag{},
389+ std::forward<Args>(args)...);
390+ }
391+
371392 void *next (size_t const index) const noexcept ;
372393 void set_next (unsigned const index, void *const ptr) noexcept ;
373394
@@ -398,7 +419,7 @@ struct ChildData
398419
399420 bool is_valid () const ;
400421 void erase ();
401- void finalize (Node::UniquePtr , Compute &, bool cache);
422+ void finalize (Node::SharedPtr , Compute &, bool cache);
402423 void copy_old_child (Node *old, unsigned i);
403424};
404425
@@ -431,21 +452,21 @@ constexpr size_t MAX_VALUE_LEN_OF_LEAF =
431452 0 /* number_of_children */ , 0 /* child_data_size */ , 0 /* value_size */ ,
432453 KECCAK256_SIZE /* path_size */ , KECCAK256_SIZE /* data_size*/ );
433454
434- Node::UniquePtr make_node (
455+ Node::SharedPtr make_node (
435456 Node &from, NibblesView path, std::optional<byte_string_view> value,
436457 int64_t version);
437458
438- Node::UniquePtr make_node (
459+ Node::SharedPtr make_node (
439460 uint16_t mask, std::span<ChildData>, NibblesView path,
440461 std::optional<byte_string_view> value, size_t data_size, int64_t version);
441462
442- Node::UniquePtr make_node (
463+ Node::SharedPtr make_node (
443464 uint16_t mask, std::span<ChildData>, NibblesView path,
444465 std::optional<byte_string_view> value, byte_string_view data,
445466 int64_t version);
446467
447468// create node: either branch/extension, with or without leaf
448- Node::UniquePtr create_node_with_children (
469+ Node::SharedPtr create_node_with_children (
449470 Compute &, uint16_t mask, std::span<ChildData> children, NibblesView path,
450471 std::optional<byte_string_view> value, int64_t version);
451472
@@ -454,7 +475,7 @@ void serialize_node_to_buffer(
454475 uint32_t disk_size, unsigned offset = 0 );
455476
456477template <node_type NodeType>
457- inline NodeType::UniquePtr
478+ inline NodeType::SharedPtr
458479deserialize_node_from_buffer (unsigned char const *read_pos, size_t max_bytes)
459480{
460481 for (size_t n = 0 ; n < max_bytes; n += 64 ) {
@@ -473,7 +494,7 @@ deserialize_node_from_buffer(unsigned char const *read_pos, size_t max_bytes)
473494 if constexpr (std::same_as<NodeType, Node>) {
474495 auto const alloc_size = round_up_align<3 >(base_size) +
475496 number_of_children * sizeof (Node::SharedPtr);
476- auto node = NodeType::make (alloc_size);
497+ auto node = NodeType::make_shared (alloc_size);
477498 std::copy_n (read_pos, base_size, (unsigned char *)node.get ());
478499 for (unsigned i = 0 ; i < node->number_of_children (); ++i) {
479500 new (node->child_ptr (i)) Node::SharedPtr ();
@@ -484,7 +505,7 @@ deserialize_node_from_buffer(unsigned char const *read_pos, size_t max_bytes)
484505 else {
485506 auto const alloc_size = round_up_align<3 >(base_size) +
486507 number_of_children * sizeof (NodeType *);
487- auto node = NodeType::make (alloc_size);
508+ auto node = NodeType::make_shared (alloc_size);
488509 std::copy_n (read_pos, base_size, (unsigned char *)node.get ());
489510 std::memset (
490511 node->next_data_aligned (),
@@ -496,7 +517,7 @@ deserialize_node_from_buffer(unsigned char const *read_pos, size_t max_bytes)
496517}
497518
498519template <node_type DestNodeType, node_type SrcNodeType>
499- DestNodeType::UniquePtr copy_node (SrcNodeType const *const from)
520+ DestNodeType::SharedPtr copy_node (SrcNodeType const *const from)
500521{
501522 auto const number_of_children = from->number_of_children ();
502523 auto const base_size = static_cast <unsigned >(
@@ -505,7 +526,7 @@ DestNodeType::UniquePtr copy_node(SrcNodeType const *const from)
505526 if constexpr (std::same_as<DestNodeType, Node>) {
506527 auto const alloc_size = round_up_align<3 >(base_size) +
507528 number_of_children * sizeof (Node::SharedPtr);
508- auto node_copy = DestNodeType::make (alloc_size);
529+ auto node_copy = DestNodeType::make_shared (alloc_size);
509530 std::copy_n (
510531 (unsigned char *)from, base_size, (unsigned char *)node_copy.get ());
511532 for (unsigned i = 0 ; i < number_of_children; ++i) {
@@ -516,7 +537,7 @@ DestNodeType::UniquePtr copy_node(SrcNodeType const *const from)
516537 else {
517538 auto const next_ptrs_size = number_of_children * sizeof (void *);
518539 auto const alloc_size = round_up_align<3 >(base_size) + next_ptrs_size;
519- auto node_copy = DestNodeType::make (alloc_size);
540+ auto node_copy = DestNodeType::make_shared (alloc_size);
520541 std::copy_n (
521542 (unsigned char *)from, base_size, (unsigned char *)node_copy.get ());
522543 // reset all in memory children
0 commit comments