1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <iostream>
16 #include <cstring>
17 #include <string>
18 #include <thread>
19
20 #include "log.h"
21 #include "securec.h"
22 #include "ring_buffer.h"
23
24 namespace Updater {
~RingBuffer()25 RingBuffer::~RingBuffer()
26 {
27 Release();
28 }
29
Init(uint32_t singleSize,uint32_t num)30 bool RingBuffer::Init(uint32_t singleSize, uint32_t num)
31 {
32 if (singleSize == 0 || num == 0 || (num & (num - 1)) != 0) { // power of 2
33 LOG(ERROR) << "singleSize:" << singleSize << " num:" << num << " error";
34 return false;
35 }
36 bufArray_ = new (std::nothrow) uint8_t* [num] {};
37 lenArray_ = new (std::nothrow) uint32_t [num] {};
38 if (bufArray_ == nullptr || lenArray_ == nullptr) {
39 LOG(ERROR) << "new buf or len " << num << " error";
40 return false;
41 }
42 for (uint32_t i = 0; i < num; i++) {
43 bufArray_[i] = new (std::nothrow) uint8_t [singleSize] {};
44 if (bufArray_[i] == nullptr) {
45 LOG(ERROR) << "new buf " << i << " size " << singleSize << " error";
46 return false;
47 }
48 }
49
50 writeIndex_ = 0;
51 readIndex_ = 0;
52 num_ = num;
53 singleSize_ = singleSize;
54 return true;
55 }
56
IsFull()57 bool RingBuffer::IsFull()
58 {
59 // writeIndex readIndex real size: 0 ~ num_ -1, logic size: 0 ~ 2num_ - 1
60 // when writeIndex_ - readIndex_ == n means full
61 return writeIndex_ == (readIndex_ ^ num_);
62 }
63
IsEmpty()64 bool RingBuffer::IsEmpty()
65 {
66 // writeIndex readIndex real size: 0 ~ num_ -1, logic size: 0 ~ 2num_ - 1
67 // when same means empty
68 return writeIndex_ == readIndex_;
69 }
70
Push(uint8_t * buf,uint32_t len)71 bool RingBuffer::Push(uint8_t *buf, uint32_t len)
72 {
73 if (buf == nullptr || len == 0 || len > singleSize_) {
74 LOG(ERROR) << "RingBuffer push error, len:" << len << " singleSize:" << singleSize_;
75 return false;
76 }
77 if (IsFull()) {
78 std::unique_lock<std::mutex> pushLock(notifyMtx_);
79 while (IsFull()) {
80 if (isStop_) {
81 LOG(WARNING) << "RingBuffer push stopped";
82 return false;
83 }
84 LOG(DEBUG) << "RingBuffer full, wait !!!";
85 notFull_.wait(pushLock);
86 }
87 }
88
89 uint32_t index = writeIndex_ & (num_ - 1);
90 if (memcpy_s(bufArray_[index], singleSize_, buf, len) != EOK) {
91 LOG(ERROR) << "memcpy error, len:" << len;
92 return false;
93 }
94 lenArray_[index] = len;
95 writeIndex_ = (writeIndex_ + 1) & (2 * num_ - 1); // 2: logic buffer size
96
97 std::unique_lock<std::mutex> popLock(notifyMtx_);
98 notEmpty_.notify_all();
99 return true;
100 }
101
Pop(uint8_t * buf,uint32_t maxLen,uint32_t & len)102 bool RingBuffer::Pop(uint8_t *buf, uint32_t maxLen, uint32_t &len)
103 {
104 if (buf == nullptr) {
105 LOG(ERROR) << "RingBuffer pop para error";
106 return false;
107 }
108 if (IsEmpty()) {
109 std::unique_lock<std::mutex> popLock(notifyMtx_);
110 while (IsEmpty()) {
111 if (isStop_) {
112 LOG(WARNING) << "RingBuffer pop stopped";
113 return false;
114 }
115 LOG(DEBUG) << "RingBuffer empty, wait !!!";
116 notEmpty_.wait(popLock);
117 }
118 }
119
120 uint32_t index = readIndex_ & (num_ - 1);
121 if (memcpy_s(buf, maxLen, bufArray_[index], lenArray_[index]) != EOK) {
122 LOG(ERROR) << "memcpy error, len:" << lenArray_[index];
123 return false;
124 }
125 len = lenArray_[index];
126 readIndex_ = (readIndex_ + 1) & (2 * num_ - 1); // 2: logic buffer size
127
128 std::unique_lock<std::mutex> popLock(notifyMtx_);
129 notFull_.notify_all();
130 return true;
131 }
132
Stop()133 void RingBuffer::Stop()
134 {
135 isStop_ = true;
136 notFull_.notify_all();
137 notEmpty_.notify_all();
138 }
139
Release()140 void RingBuffer::Release()
141 {
142 if (lenArray_ != nullptr) {
143 delete[] lenArray_;
144 lenArray_ = nullptr;
145 }
146
147 if (bufArray_ != nullptr) {
148 for (uint32_t i = 0; i < num_ && bufArray_[i] != nullptr; i++) {
149 delete[] bufArray_[i];
150 bufArray_[i] = nullptr;
151 }
152 delete[] bufArray_;
153 bufArray_ = nullptr;
154 }
155 }
156 } // namespace Updater
157