• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 
16 #include "circle_stream_buffer.h"
17 
18 #include "devicestatus_define.h"
19 
20 namespace OHOS {
21 namespace Msdp {
CopyDataToBegin()22 void CircleStreamBuffer::CopyDataToBegin()
23 {
24     int32_t residualSize = ResidualSize();
25     if (residualSize > 0 && rPos_ > 0) {
26         int32_t pos = 0;
27         for (int32_t i = rPos_; i <= wPos_;) {
28             szBuff_[pos++] = szBuff_[i++];
29         }
30     }
31     FI_HILOGD("ResidualSize:%{public}d rPos:%{public}d wPos:%{public}d", residualSize, rPos_, wPos_);
32     rPos_ = 0;
33     wPos_ = residualSize;
34 }
35 
CheckWrite(size_t size)36 bool CircleStreamBuffer::CheckWrite(size_t size)
37 {
38     int32_t bufferSize = static_cast<int32_t>(size);
39     int32_t availableSize = GetAvailableBufSize();
40     if (bufferSize > availableSize && rPos_ > 0) {
41         CopyDataToBegin();
42         availableSize = GetAvailableBufSize();
43     }
44     return (availableSize >= bufferSize);
45 }
46 
Write(const char * buf,size_t size)47 bool CircleStreamBuffer::Write(const char *buf, size_t size)
48 {
49     if (!CheckWrite(size)) {
50         FI_HILOGE("Out of buffer memory, availableSize:%{public}d, size:%{public}zu,"
51             "residualSize:%{public}d, rPos:%{public}d, wPos:%{public}d",
52             GetAvailableBufSize(), size, ResidualSize(), rPos_, wPos_);
53         return false;
54     }
55     return StreamBuffer::Write(buf, size);
56 }
57 } // namespace Msdp
58 } // namespace OHOS