Skip to content

Commit

Permalink
fix flatmap element space should align with usertype (#2288)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehds authored Jul 14, 2023
1 parent 491591c commit 2729272
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/butil/containers/flat_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
#include <stdint.h>
#include <functional>
#include <iostream> // std::ostream
#include <type_traits> // std::aligned_storage
#include "butil/type_traits.h"
#include "butil/logging.h"
#include "butil/find_cstr.h"
Expand Down Expand Up @@ -251,22 +252,23 @@ class FlatMap {

struct Bucket {
explicit Bucket(const _K& k) : next(NULL)
{ new (element_spaces) Element(k); }
{ new (&element_spaces) Element(k); }
Bucket(const Bucket& other) : next(NULL)
{ new (element_spaces) Element(other.element()); }
{ new (&element_spaces) Element(other.element()); }
bool is_valid() const { return next != (const Bucket*)-1UL; }
void set_invalid() { next = (Bucket*)-1UL; }
// NOTE: Only be called when is_valid() is true.
Element& element() {
void* spaces = element_spaces; // Suppress strict-aliasing
void* spaces = &element_spaces; // Suppress strict-aliasing
return *reinterpret_cast<Element*>(spaces);
}
const Element& element() const {
const void* spaces = element_spaces;
const void* spaces = &element_spaces;
return *reinterpret_cast<const Element*>(spaces);
}
Bucket* next;
char element_spaces[sizeof(Element)];
Bucket *next;
typename std::aligned_storage<sizeof(Element), alignof(Element)>::type
element_spaces;
};

allocator_type& get_allocator() { return _pool.get_allocator(); }
Expand Down

0 comments on commit 2729272

Please sign in to comment.