From: Peter Alexander Date: Thu, 19 Oct 2017 09:46:56 +0000 (-0700) Subject: Add ProducerConsumerQueue::capacity() X-Git-Tag: v2017.10.23.00~15 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=7111b632fca2d01ea33c974aa68ffa021b75ae24;hp=dce47b8a0cc0e90f93844e66651d68340ed2f940;p=folly.git 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 --- 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. +}