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