• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CHRE_UTIL_BLOCKING_SEGMENTED_QUEUE_H_
18 #define CHRE_UTIL_BLOCKING_SEGMENTED_QUEUE_H_
19 
20 #include "chre/util/fixed_size_blocking_queue.h"
21 #include "chre/util/segmented_queue.h"
22 namespace chre {
23 /**
24  * Similar but memory efficient version of chre::FixedSizeBlockingQueue.
25  * This data structure achieves memory efficiency by using chre::SegmentedQueue
26  * as the data storage. @see BlockingQueueCore and FixedSizeBlockingQueue for
27  * more information.
28  *
29  * @tparam ElementType Type of element that will be stored.
30  * @tparam kBlockSize number of elements that a block can contain.
31  */
32 template <typename ElementType, size_t kBlockSize>
33 class BlockingSegmentedQueue
34     : public blocking_queue_internal::BlockingQueueCore<
35           ElementType, SegmentedQueue<ElementType, kBlockSize>> {
36   using Container = ::chre::SegmentedQueue<ElementType, kBlockSize>;
37   using BlockingQueue =
38       ::chre::blocking_queue_internal::BlockingQueueCore<ElementType,
39                                                          Container>;
40 
41  public:
42   typedef ElementType value_type;
43 
44   /**
45    * Create the blocking segmented queue object
46    *
47    * @param maxBlockCount the maximum number of block that the queue can hold.
48    * @param staticBlockCount the number of block that will be construct by
49    * constructor and will only be removed by destructor.
50    */
51   BlockingSegmentedQueue(size_t maxBlockCount, size_t staticBlockCount = 1)
BlockingQueue(maxBlockCount,staticBlockCount)52       : BlockingQueue(maxBlockCount, staticBlockCount) {}
53   /**
54    * @return size_t the number of block that the queue is holding.
55    */
block_count()56   size_t block_count() {
57     return Container::block_count();
58   }
59 
removeMatchedFromBack(typename Container::MatchingFunction * matchFunc,size_t maxNumOfElementsRemoved,typename Container::FreeFunction * freeFunction,void * extraDataForFreeFunction)60   size_t removeMatchedFromBack(typename Container::MatchingFunction *matchFunc,
61                                size_t maxNumOfElementsRemoved,
62                                typename Container::FreeFunction *freeFunction,
63                                void *extraDataForFreeFunction) {
64     LockGuard<Mutex> lock(BlockingQueue::mMutex);
65     return Container::removeMatchedFromBack(matchFunc, maxNumOfElementsRemoved,
66                                             freeFunction,
67                                             extraDataForFreeFunction);
68   }
69 };
70 }  // namespace chre
71 
72 #endif  // CHRE_UTIL_BLOCKING_SEGMENTED_QUEUE_H_
73