This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[Patch] Fix aliasing (/alignment) issue in ext/pb_assoc
- From: Paolo Carlini <pcarlini at suse dot de>
- To: libstdc++ <libstdc++ at gcc dot gnu dot org>
- Cc: Benjamin Kosnik <bkoz at redhat dot com>
- Date: Tue, 17 Jan 2006 18:39:44 +0100
- Subject: [Patch] Fix aliasing (/alignment) issue in ext/pb_assoc
Hi all, hi Benjamin,
as pointed out by Chris here:
http://gcc.gnu.org/ml/libstdc++/2005-12/msg00232.html
and discussed a bit here:
http://gcc.gnu.org/ml/libstdc++/2006-01/msg00013.html
We have got this aliasing/alignment issue, which, as far as I can see,
is a perfect candidate for a neat use of
tr1::aligned_storage/alignment_of. Indeed, the below appear to work
well, and the warning is gone. Any objections to this approach? Would be
for 4.1 branch too.
Thanks,
Paolo.
///////////////
2006-01-17 Paolo Carlini <pcarlini@suse.de>
* include/ext/pb_assoc/detail/value_type_adapter/
value_type_adapter.hpp: Include <tr1/type_traits>.
* include/ext/pb_assoc/detail/value_type_adapter/
it_value_type_traits.hpp (it_value_type_traits_<>::value_type_holder):
Use tr1::aligned_storage and tr1::alignment_of.
(it_value_type_traits_<>::buf_t): Remove.
(it_value_type_traits_<>::make_valid, recast): Adjust.
Index: include/ext/pb_assoc/detail/value_type_adapter/value_type_adapter.hpp
===================================================================
--- include/ext/pb_assoc/detail/value_type_adapter/value_type_adapter.hpp (revision 109795)
+++ include/ext/pb_assoc/detail/value_type_adapter/value_type_adapter.hpp (working copy)
@@ -1,6 +1,6 @@
// -*- C++ -*-
-// Copyright (C) 2005 Free Software Foundation, Inc.
+// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
@@ -51,6 +51,7 @@
#include <ext/pb_assoc/detail/type_utils.hpp>
#include <utility>
#include <algorithm>
+#include <tr1/type_traits> // for aligned_storage/alignment_of
namespace pb_assoc
{
Index: include/ext/pb_assoc/detail/value_type_adapter/it_value_type_traits.hpp
===================================================================
--- include/ext/pb_assoc/detail/value_type_adapter/it_value_type_traits.hpp (revision 109795)
+++ include/ext/pb_assoc/detail/value_type_adapter/it_value_type_traits.hpp (working copy)
@@ -1,6 +1,6 @@
// -*- C++ -*-
-// Copyright (C) 2005 Free Software Foundation, Inc.
+// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
@@ -94,16 +94,13 @@
value_type>::other::const_pointer
const_pointer;
- typedef
- typename Allocator_::template rebind<
- int* >::other::value_type
- buf_t;
-
struct value_type_holder
{
- buf_t m_a_key_buf[sizeof(key_ref_pair) / sizeof(buf_t) + 1];
+ typename std::tr1::aligned_storage<sizeof(key_ref_pair),
+ std::tr1::alignment_of<key_ref_pair>::value>::type m_a_key_buf;
- buf_t m_a_value_buf[sizeof(value_type) / sizeof(buf_t) + 1];
+ typename std::tr1::aligned_storage<sizeof(value_type),
+ std::tr1::alignment_of<value_type>::value>::type m_a_value_buf;
};
typedef
@@ -114,7 +111,7 @@
inline static pointer
recast(value_type_hoder_valerence r_holder)
{
- return reinterpret_cast<pointer>(r_holder.m_a_value_buf);
+ return reinterpret_cast<pointer>(&r_holder.m_a_value_buf);
}
inline static void
@@ -125,7 +122,7 @@
void* >::other::value_type
void_pointer;
- void_pointer p_target = r_holder.m_a_key_buf;
+ void_pointer p_target = &r_holder.m_a_key_buf;
new (p_target) key_ref_pair(r_bk, r_val.first);
@@ -135,9 +132,9 @@
key_ref_pair_pointer;
key_ref_pair_pointer p_key =
- reinterpret_cast<key_ref_pair_pointer>(r_holder.m_a_key_buf);
+ reinterpret_cast<key_ref_pair_pointer>(&r_holder.m_a_key_buf);
- p_target = r_holder.m_a_value_buf;
+ p_target = &r_holder.m_a_value_buf;
new (p_target) value_type(*p_key, r_val.second);
}
@@ -185,16 +182,13 @@
value_type>::other::const_pointer
const_pointer;
- typedef
- typename Allocator_::template rebind<
- int* >::other::value_type
- buf_t;
-
struct value_type_holder
{
- buf_t m_a_key_buf[sizeof(key_ref_pair) / sizeof(buf_t) + 1];
+ typename std::tr1::aligned_storage<sizeof(key_ref_pair),
+ std::tr1::alignment_of<key_ref_pair>::value>::type m_a_key_buf;
- buf_t m_a_value_buf[sizeof(value_type) / sizeof(buf_t) + 1];
+ typename std::tr1::aligned_storage<sizeof(value_type),
+ std::tr1::alignment_of<value_type>::value>::type m_a_value_buf;
};
typedef
@@ -205,7 +199,7 @@
inline static pointer
recast(value_type_hoder_valerence r_holder)
{
- return reinterpret_cast<pointer>(r_holder.m_a_value_buf);
+ return reinterpret_cast<pointer>(&r_holder.m_a_value_buf);
}
inline static void
@@ -216,7 +210,7 @@
void* >::other::value_type
void_pointer;
- void_pointer p_target = r_holder.m_a_value_buf;
+ void_pointer p_target = &r_holder.m_a_value_buf;
new (p_target) key_ref_pair(r_bk, r_val.first);
}