Phil Burk | fd911c1 | 2017-01-03 17:15:39 -0800 | [diff] [blame] | 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 | #define LOG_TAG "FifoControllerBase" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <stdint.h> |
| 22 | #include "FifoControllerBase.h" |
| 23 | |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 24 | using namespace android; // TODO just import names needed |
| 25 | |
Phil Burk | fd911c1 | 2017-01-03 17:15:39 -0800 | [diff] [blame] | 26 | FifoControllerBase::FifoControllerBase(fifo_frames_t capacity, fifo_frames_t threshold) |
| 27 | : mCapacity(capacity) |
| 28 | , mThreshold(threshold) |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | FifoControllerBase::~FifoControllerBase() { |
| 33 | } |
| 34 | |
| 35 | fifo_frames_t FifoControllerBase::getFullFramesAvailable() { |
Cindy Zhou | 03a2061 | 2019-11-05 16:18:31 -0800 | [diff] [blame] | 36 | fifo_frames_t temp = 0; |
| 37 | __builtin_sub_overflow(getWriteCounter(), getReadCounter(), &temp); |
| 38 | return temp; |
Phil Burk | fd911c1 | 2017-01-03 17:15:39 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | fifo_frames_t FifoControllerBase::getReadIndex() { |
| 42 | // % works with non-power of two sizes |
Phil Burk | 58f7ff5 | 2018-12-03 14:16:46 -0800 | [diff] [blame] | 43 | return (fifo_frames_t) ((uint64_t)getReadCounter() % mCapacity); |
Phil Burk | fd911c1 | 2017-01-03 17:15:39 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void FifoControllerBase::advanceReadIndex(fifo_frames_t numFrames) { |
Cindy Zhou | 03a2061 | 2019-11-05 16:18:31 -0800 | [diff] [blame] | 47 | fifo_counter_t temp = 0; |
| 48 | __builtin_add_overflow(getReadCounter(), numFrames, &temp); |
| 49 | setReadCounter(temp); |
Phil Burk | fd911c1 | 2017-01-03 17:15:39 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | fifo_frames_t FifoControllerBase::getEmptyFramesAvailable() { |
| 53 | return (int32_t)(mThreshold - getFullFramesAvailable()); |
| 54 | } |
| 55 | |
| 56 | fifo_frames_t FifoControllerBase::getWriteIndex() { |
| 57 | // % works with non-power of two sizes |
Phil Burk | 58f7ff5 | 2018-12-03 14:16:46 -0800 | [diff] [blame] | 58 | return (fifo_frames_t) ((uint64_t)getWriteCounter() % mCapacity); |
Phil Burk | fd911c1 | 2017-01-03 17:15:39 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void FifoControllerBase::advanceWriteIndex(fifo_frames_t numFrames) { |
Cindy Zhou | 03a2061 | 2019-11-05 16:18:31 -0800 | [diff] [blame] | 62 | fifo_counter_t temp = 0; |
| 63 | __builtin_add_overflow(getWriteCounter(), numFrames, &temp); |
| 64 | setWriteCounter(temp); |
Phil Burk | fd911c1 | 2017-01-03 17:15:39 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void FifoControllerBase::setThreshold(fifo_frames_t threshold) { |
Phil Burk | 5c4f826 | 2017-11-17 12:16:22 -0800 | [diff] [blame] | 68 | if (threshold > mCapacity) { |
| 69 | threshold = mCapacity; |
| 70 | } else if (threshold < 0) { |
| 71 | threshold = 0; |
| 72 | } |
Phil Burk | fd911c1 | 2017-01-03 17:15:39 -0800 | [diff] [blame] | 73 | mThreshold = threshold; |
| 74 | } |