From 7111b632fca2d01ea33c974aa68ffa021b75ae24 Mon Sep 17 00:00:00 2001 From: Peter Alexander Date: Thu, 19 Oct 2017 02:46:56 -0700 Subject: [PATCH] Add ProducerConsumerQueue::capacity() Summary: Simple addition. Easy to track externally, but might as well provide it in the class if it is readily available. Reviewed By: yfeldblum Differential Revision: D6093826 fbshipit-source-id: 9d8c02891b2cea9ce0d3f6ea78e1e0055b536eb8 --- folly/ProducerConsumerQueue.h | 5 +++++ folly/test/ProducerConsumerQueueTest.cpp | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/folly/ProducerConsumerQueue.h b/folly/ProducerConsumerQueue.h index fd9c94a0..8d2e47f0 100644 --- a/folly/ProducerConsumerQueue.h +++ b/folly/ProducerConsumerQueue.h @@ -167,6 +167,11 @@ struct ProducerConsumerQueue { return ret; } + // maximum number of items in the queue. + size_t capacity() const { + return size_ - 1; + } + private: char pad0_[CacheLocality::kFalseSharingRange]; const uint32_t size_; diff --git a/folly/test/ProducerConsumerQueueTest.cpp b/folly/test/ProducerConsumerQueueTest.cpp index a30b6e2f..b6dc84c1 100644 --- a/folly/test/ProducerConsumerQueueTest.cpp +++ b/folly/test/ProducerConsumerQueueTest.cpp @@ -288,3 +288,8 @@ TEST(PCQ, EmptyFull) { EXPECT_FALSE(queue.write(3)); EXPECT_EQ(queue.sizeGuess(), 2); } + +TEST(PCQ, Capacity) { + folly::ProducerConsumerQueue queue(3); + EXPECT_EQ(queue.capacity(), 2); // PCQ max size is buffer size - 1. +} -- 2.34.1