1 /*
2  * Copyright (c) 2021-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 "dsched_data_buffer.h"
17 
18 #include "dtbschedmgr_log.h"
19 
20 namespace OHOS {
21 namespace DistributedSchedule {
DSchedDataBuffer(size_t capacity)22 DSchedDataBuffer::DSchedDataBuffer(size_t capacity)
23 {
24     if (capacity != 0 && capacity < DSCHED_MAX_BUFFER_SIZE) {
25         data_ = new uint8_t[capacity] {0};
26         if (data_ != nullptr) {
27             capacity_ = capacity;
28             rangeLength_ = capacity;
29         }
30     }
31 }
32 
~DSchedDataBuffer()33 DSchedDataBuffer::~DSchedDataBuffer()
34 {
35     if (data_ != nullptr) {
36         delete[] data_;
37         data_ = nullptr;
38     }
39 }
40 
Size()41 size_t DSchedDataBuffer::Size()
42 {
43     return rangeLength_;
44 }
45 
Offset()46 size_t DSchedDataBuffer::Offset()
47 {
48     return rangeOffset_;
49 }
50 
Capacity()51 size_t DSchedDataBuffer::Capacity()
52 {
53     return capacity_;
54 }
55 
Data()56 uint8_t *DSchedDataBuffer::Data()
57 {
58     if (data_ == nullptr) {
59         return nullptr;
60     }
61     return data_ + rangeOffset_;
62 }
63 
SetRange(size_t offset,size_t size)64 int32_t DSchedDataBuffer::SetRange(size_t offset, size_t size)
65 {
66     if (!(offset <= capacity_) || !(offset + size <= capacity_)) {
67         return -1;
68     }
69 
70     rangeOffset_ = offset;
71     rangeLength_ = size;
72     return ERR_OK;
73 }
74 }  // namespace DistributedSchedule
75 }  // namespace OHOS
76