1800b99b8Sopenharmony_ci/* 2800b99b8Sopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License. 5800b99b8Sopenharmony_ci * You may obtain a copy of the License at 6800b99b8Sopenharmony_ci * 7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8800b99b8Sopenharmony_ci * 9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and 13800b99b8Sopenharmony_ci * limitations under the License. 14800b99b8Sopenharmony_ci */ 15800b99b8Sopenharmony_ci 16800b99b8Sopenharmony_ci#ifndef DFX_RING_BUFFER_BLOCK_H 17800b99b8Sopenharmony_ci#define DFX_RING_BUFFER_BLOCK_H 18800b99b8Sopenharmony_ci 19800b99b8Sopenharmony_ci#include <cstddef> 20800b99b8Sopenharmony_ci 21800b99b8Sopenharmony_ci/** 22800b99b8Sopenharmony_ci * @brief A block represents a continuous section 23800b99b8Sopenharmony_ci * of the ring buffer. 24800b99b8Sopenharmony_ci * @tparam T The type of data stored in the ring buffer. 25800b99b8Sopenharmony_ci */ 26800b99b8Sopenharmony_citemplate<class T> 27800b99b8Sopenharmony_ciclass DfxRingBufferBlock { 28800b99b8Sopenharmony_cipublic: 29800b99b8Sopenharmony_ci DfxRingBufferBlock() : start_(NULL), length_(0) 30800b99b8Sopenharmony_ci { 31800b99b8Sopenharmony_ci } 32800b99b8Sopenharmony_ci 33800b99b8Sopenharmony_ci ~DfxRingBufferBlock() 34800b99b8Sopenharmony_ci { 35800b99b8Sopenharmony_ci } 36800b99b8Sopenharmony_ci 37800b99b8Sopenharmony_ci /** 38800b99b8Sopenharmony_ci * @brief Sets the block's starting 39800b99b8Sopenharmony_ci * position to a point in memory. 40800b99b8Sopenharmony_ci */ 41800b99b8Sopenharmony_ci void SetStart(T* start) 42800b99b8Sopenharmony_ci { 43800b99b8Sopenharmony_ci this->start_ = start; 44800b99b8Sopenharmony_ci } 45800b99b8Sopenharmony_ci 46800b99b8Sopenharmony_ci /** 47800b99b8Sopenharmony_ci * @brief Sets the number of items in the 48800b99b8Sopenharmony_ci * block. 49800b99b8Sopenharmony_ci */ 50800b99b8Sopenharmony_ci void SetLength(unsigned int length) 51800b99b8Sopenharmony_ci { 52800b99b8Sopenharmony_ci this->length_ = length; 53800b99b8Sopenharmony_ci } 54800b99b8Sopenharmony_ci 55800b99b8Sopenharmony_ci /** 56800b99b8Sopenharmony_ci * @return The block's starting 57800b99b8Sopenharmony_ci * point in memory. 58800b99b8Sopenharmony_ci */ 59800b99b8Sopenharmony_ci T* Start() 60800b99b8Sopenharmony_ci { 61800b99b8Sopenharmony_ci return this->start_; 62800b99b8Sopenharmony_ci } 63800b99b8Sopenharmony_ci 64800b99b8Sopenharmony_ci /** 65800b99b8Sopenharmony_ci * @return The number of items in the block. 66800b99b8Sopenharmony_ci */ 67800b99b8Sopenharmony_ci unsigned int Length() 68800b99b8Sopenharmony_ci { 69800b99b8Sopenharmony_ci return this->length_; 70800b99b8Sopenharmony_ci } 71800b99b8Sopenharmony_ci 72800b99b8Sopenharmony_ci /** 73800b99b8Sopenharmony_ci * @param index The index of the item in the block. 74800b99b8Sopenharmony_ci * @return The item in the block at the index. 75800b99b8Sopenharmony_ci */ 76800b99b8Sopenharmony_ci T At(unsigned int index) 77800b99b8Sopenharmony_ci { 78800b99b8Sopenharmony_ci if (this->start_ == nullptr) { 79800b99b8Sopenharmony_ci return T(); 80800b99b8Sopenharmony_ci } 81800b99b8Sopenharmony_ci return this->start_[index]; 82800b99b8Sopenharmony_ci } 83800b99b8Sopenharmony_ci 84800b99b8Sopenharmony_ci size_t ElementSize() 85800b99b8Sopenharmony_ci { 86800b99b8Sopenharmony_ci return sizeof(T); 87800b99b8Sopenharmony_ci } 88800b99b8Sopenharmony_ci 89800b99b8Sopenharmony_ciprivate: 90800b99b8Sopenharmony_ci T* start_; 91800b99b8Sopenharmony_ci 92800b99b8Sopenharmony_ci unsigned int length_; 93800b99b8Sopenharmony_ci}; 94800b99b8Sopenharmony_ci 95800b99b8Sopenharmony_ci#endif 96