Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix flatmap element space should align with usertype #2288

Merged
merged 1 commit into from
Jul 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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