• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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 #include <algorithm>
18 #include <cassert>
19 #include <stdint.h>
20 
21 #include "FifoControllerBase.h"
22 
23 namespace oboe {
24 
FifoControllerBase(uint32_t capacityInFrames)25 FifoControllerBase::FifoControllerBase(uint32_t capacityInFrames)
26         : mTotalFrames(capacityInFrames)
27 {
28     // Avoid ridiculously large buffers and the arithmetic wraparound issues that can follow.
29     assert(capacityInFrames <= (UINT32_MAX / 4));
30 }
31 
getFullFramesAvailable() const32 uint32_t FifoControllerBase::getFullFramesAvailable() const {
33     uint64_t writeCounter =  getWriteCounter();
34     uint64_t readCounter = getReadCounter();
35     if (readCounter > writeCounter) {
36         return 0;
37     }
38     uint64_t delta = writeCounter - readCounter;
39     if (delta >= mTotalFrames) {
40         return mTotalFrames;
41     }
42     // delta is now guaranteed to fit within the range of a uint32_t
43     return static_cast<uint32_t>(delta);
44 }
45 
getReadIndex() const46 uint32_t FifoControllerBase::getReadIndex() const {
47     // % works with non-power of two sizes
48     return static_cast<uint32_t>(getReadCounter() % mTotalFrames);
49 }
50 
advanceReadIndex(uint32_t numFrames)51 void FifoControllerBase::advanceReadIndex(uint32_t numFrames) {
52     incrementReadCounter(numFrames);
53 }
54 
getEmptyFramesAvailable() const55 uint32_t FifoControllerBase::getEmptyFramesAvailable() const {
56     return static_cast<uint32_t>(mTotalFrames - getFullFramesAvailable());
57 }
58 
getWriteIndex() const59 uint32_t FifoControllerBase::getWriteIndex() const {
60     // % works with non-power of two sizes
61     return static_cast<uint32_t>(getWriteCounter() % mTotalFrames);
62 }
63 
advanceWriteIndex(uint32_t numFrames)64 void FifoControllerBase::advanceWriteIndex(uint32_t numFrames) {
65     incrementWriteCounter(numFrames);
66 }
67 
68 } // namespace oboe
69