* [PATCH][_Hahstable] Use RAII to guard node pointer while constructing
@ 2023-11-08 20:00 François Dumont
2023-11-09 0:57 ` Jonathan Wakely
0 siblings, 1 reply; 2+ messages in thread
From: François Dumont @ 2023-11-08 20:00 UTC (permalink / raw)
To: libstdc++; +Cc: gcc-patches
[-- Attachment #1: Type: text/plain, Size: 511 bytes --]
Another proposal to use RAII rather than __try/__catch block.
libstdc++: [_Hashtable] Use RAII type to guard node while constructing value
libstdc++-v3/ChangeLog:
* include/bits/hashtable_policy.h
(struct _NodePtrGuard<_HashtableAlloc, _NodePtr>): New.
(_ReuseAllocNode::operator()(_Args&&...)): Use latter to guard
allocated node
pointer while constructing in place the value_type instance.
Tested under Linux x64, ok to commit ?
François
[-- Attachment #2: raii_reuse_alloc_nodeptr.txt --]
[-- Type: text/plain, Size: 1553 bytes --]
diff --git a/libstdc++-v3/include/bits/hashtable_policy.h b/libstdc++-v3/include/bits/hashtable_policy.h
index cd8943d8d05..c67eebd3b2b 100644
--- a/libstdc++-v3/include/bits/hashtable_policy.h
+++ b/libstdc++-v3/include/bits/hashtable_policy.h
@@ -173,6 +173,19 @@ namespace __detail
{ return __node_gen(std::forward<_Kt>(__k)); }
};
+ template<typename _HashtableAlloc, typename _NodePtr>
+ struct _NodePtrGuard
+ {
+ _HashtableAlloc& _M_h;
+ _NodePtr _M_ptr;
+
+ ~_NodePtrGuard()
+ {
+ if (_M_ptr)
+ _M_h._M_deallocate_node_ptr(_M_ptr);
+ }
+ };
+
template<typename _NodeAlloc>
struct _Hashtable_alloc;
@@ -201,27 +214,20 @@ namespace __detail
__node_ptr
operator()(_Args&&... __args) const
{
- if (_M_nodes)
- {
+ if (!_M_nodes)
+ return _M_h._M_allocate_node(std::forward<_Args>(__args)...);
+
__node_ptr __node = _M_nodes;
_M_nodes = _M_nodes->_M_next();
__node->_M_nxt = nullptr;
auto& __a = _M_h._M_node_allocator();
__node_alloc_traits::destroy(__a, __node->_M_valptr());
- __try
- {
+ _NodePtrGuard<__hashtable_alloc, __node_ptr> __guard { _M_h, __node };
__node_alloc_traits::construct(__a, __node->_M_valptr(),
std::forward<_Args>(__args)...);
- }
- __catch(...)
- {
- _M_h._M_deallocate_node_ptr(__node);
- __throw_exception_again;
- }
+ __guard._M_ptr = nullptr;
return __node;
}
- return _M_h._M_allocate_node(std::forward<_Args>(__args)...);
- }
private:
mutable __node_ptr _M_nodes;
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH][_Hahstable] Use RAII to guard node pointer while constructing
2023-11-08 20:00 [PATCH][_Hahstable] Use RAII to guard node pointer while constructing François Dumont
@ 2023-11-09 0:57 ` Jonathan Wakely
0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2023-11-09 0:57 UTC (permalink / raw)
To: François Dumont; +Cc: libstdc++, gcc-patches
On Wed, 8 Nov 2023 at 20:00, François Dumont <frs.dumont@gmail.com> wrote:
>
> Another proposal to use RAII rather than __try/__catch block.
>
> libstdc++: [_Hashtable] Use RAII type to guard node while constructing value
>
> libstdc++-v3/ChangeLog:
>
> * include/bits/hashtable_policy.h
> (struct _NodePtrGuard<_HashtableAlloc, _NodePtr>): New.
> (_ReuseAllocNode::operator()(_Args&&...)): Use latter to guard
> allocated node
> pointer while constructing in place the value_type instance.
>
> Tested under Linux x64, ok to commit ?
Looks good, OK.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2023-11-09 0:57 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-08 20:00 [PATCH][_Hahstable] Use RAII to guard node pointer while constructing François Dumont
2023-11-09 0:57 ` Jonathan Wakely
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).