Skip to content

Commit

Permalink
enforce alignment on channels::detail::AtomicQueue consumer types
Browse files Browse the repository at this point in the history
Summary:
Class template `folly::channels::detail::AtomicQueue` is parameterized by a consumer type template. The class holds pointers to consumers but, in the interest of minimizing memory usage, may steal the two lowest bits of the pointers.

A unit-test for `AtomicQueue` uses a consumer type holding only a `bool`, giving that consumer type alignment 1 and therefore pointers to it no bits allowable to steal.

Enforce a minimum alignment on the consumer type that allows stealing the two lowest bits of pointers, and update the unit-test.

Reviewed By: dmm-fb

Differential Revision: D64287317

fbshipit-source-id: fb54e15559f12d82717cbea20075c01adfa6e8d1
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Oct 13, 2024
1 parent 0aa6ed4 commit ad90720
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion folly/channels/detail/AtomicQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class AtomicQueue {
public:
using MessageQueue = Queue<Message>;

AtomicQueue() {}
AtomicQueue() { static_assert(alignof(Consumer) > kTypeMask); }
~AtomicQueue() {
auto storage = storage_.load(std::memory_order_acquire);
auto type = static_cast<Type>(storage & kTypeMask);
Expand Down
2 changes: 1 addition & 1 deletion folly/channels/detail/test/AtomicQueueTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ TEST(AtomicQueueTest, Basic) {
}

TEST(AtomicQueueTest, Canceled) {
struct Consumer {
struct alignas(int) Consumer {
void consume(int*) { ADD_FAILURE() << "consume() shouldn't be called"; }
void canceled(int* consumerParam) {
EXPECT_EQ(consumerParam, getConsumerParam());
Expand Down

0 comments on commit ad90720

Please sign in to comment.