1 /*
2  * Copyright (c) 2021, 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 "RingBuffer.h"
18 
19 #include <android-base/logging.h>
20 
21 #include <inttypes.h>  // for PRIu64 and friends
22 
23 #include <memory>
24 
25 namespace android {
26 namespace automotive {
27 namespace telemetry {
28 
RingBuffer(int32_t limit)29 RingBuffer::RingBuffer(int32_t limit) : mSizeLimit(limit) {}
30 
push(BufferedCarData && data)31 void RingBuffer::push(BufferedCarData&& data) {
32     mList.push_back(std::move(data));
33     while (mList.size() > mSizeLimit) {
34         mList.pop_front();
35         mTotalDroppedDataCount += 1;
36     }
37 }
38 
popBack()39 BufferedCarData RingBuffer::popBack() {
40     auto result = std::move(mList.back());
41     mList.pop_back();
42     return result;
43 }
44 
dump(int fd) const45 void RingBuffer::dump(int fd) const {
46     dprintf(fd, "    RingBuffer:\n");
47     dprintf(fd, "      mSizeLimit=%d\n", mSizeLimit);
48     dprintf(fd, "      mList.size=%zu\n", mList.size());
49     dprintf(fd, "      mTotalDroppedDataCount=%" PRIu64 "\n", mTotalDroppedDataCount);
50 }
51 
size() const52 int32_t RingBuffer::size() const {
53     return mList.size();
54 }
55 
56 }  // namespace telemetry
57 }  // namespace automotive
58 }  // namespace android
59