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 #ifndef DFX_RING_BUFFER_BLOCK_H 17 #define DFX_RING_BUFFER_BLOCK_H 18 19 #include <cstddef> 20 21 /** 22 * @brief A block represents a continuous section 23 * of the ring buffer. 24 * @tparam T The type of data stored in the ring buffer. 25 */ 26 template<class T> 27 class DfxRingBufferBlock { 28 public: DfxRingBufferBlock()29 DfxRingBufferBlock() : start_(NULL), length_(0) 30 { 31 } 32 ~DfxRingBufferBlock()33 ~DfxRingBufferBlock() 34 { 35 } 36 37 /** 38 * @brief Sets the block's starting 39 * position to a point in memory. 40 */ SetStart(T * start)41 void SetStart(T* start) 42 { 43 this->start_ = start; 44 } 45 46 /** 47 * @brief Sets the number of items in the 48 * block. 49 */ SetLength(unsigned int length)50 void SetLength(unsigned int length) 51 { 52 this->length_ = length; 53 } 54 55 /** 56 * @return The block's starting 57 * point in memory. 58 */ Start()59 T* Start() 60 { 61 return this->start_; 62 } 63 64 /** 65 * @return The number of items in the block. 66 */ Length()67 unsigned int Length() 68 { 69 return this->length_; 70 } 71 72 /** 73 * @param index The index of the item in the block. 74 * @return The item in the block at the index. 75 */ At(unsigned int index)76 T At(unsigned int index) 77 { 78 if (this->start_ == nullptr) { 79 return T(); 80 } 81 return this->start_[index]; 82 } 83 ElementSize()84 size_t ElementSize() 85 { 86 return sizeof(T); 87 } 88 89 private: 90 T* start_; 91 92 unsigned int length_; 93 }; 94 95 #endif 96