Skip to content

Commit dd2ad43

Browse files
ezbrcopybara-github
authored andcommitted
Change AlignedType to have a void* array member so that swisstable backing arrays end up in the pointer-containing partition for heap partitioning.
PiperOrigin-RevId: 834392373 Change-Id: I46c840995f68f96cdce10f7d3f3c7c43885a5ff8
1 parent 872cb63 commit dd2ad43

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

absl/container/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ cc_test(
429429
deps = [
430430
":container_memory",
431431
":test_instance_tracker",
432+
"//absl/base:config",
432433
"//absl/base:no_destructor",
433434
"//absl/meta:type_traits",
434435
"//absl/strings",

absl/container/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ absl_cc_test(
484484
COPTS
485485
${ABSL_TEST_COPTS}
486486
DEPS
487+
absl::config
487488
absl::container_memory
488489
absl::no_destructor
489490
absl::strings

absl/container/internal/container_memory.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ ABSL_NAMESPACE_BEGIN
4444
namespace container_internal {
4545

4646
template <size_t Alignment>
47-
struct alignas(Alignment) AlignedType {};
47+
struct alignas(Alignment) AlignedType {
48+
// When alignment is sufficient for the allocated memory to store pointers,
49+
// include a pointer member so that swisstable backing arrays end up in the
50+
// pointer-containing partition for heap partitioning.
51+
std::conditional_t<(Alignment < alignof(void*)), char, void*> pointer;
52+
};
4853

4954
// Allocates at least n bytes aligned to the specified alignment.
5055
// Alignment must be a power of 2. It must be positive.

absl/container/internal/container_memory_test.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "gmock/gmock.h"
2727
#include "gtest/gtest.h"
28+
#include "absl/base/config.h"
2829
#include "absl/base/no_destructor.h"
2930
#include "absl/container/internal/test_instance_tracker.h"
3031
#include "absl/meta/type_traits.h"
@@ -42,6 +43,16 @@ using ::testing::ElementsAre;
4243
using ::testing::Gt;
4344
using ::testing::Pair;
4445

46+
#if ABSL_HAVE_BUILTIN(__builtin_infer_alloc_token)
47+
TEST(Memory, AlignedTypeAllocToken) {
48+
#if defined(__wasm__)
49+
GTEST_SKIP() << "Fails on wasm due to lack of heap partitioning support.";
50+
#endif
51+
EXPECT_GT(__builtin_infer_alloc_token(sizeof(AlignedType<alignof(void*)>)),
52+
__builtin_infer_alloc_token(sizeof(int)));
53+
}
54+
#endif
55+
4556
TEST(Memory, AlignmentLargerThanBase) {
4657
std::allocator<int8_t> alloc;
4758
void* mem = Allocate<2>(&alloc, 3);

0 commit comments

Comments
 (0)