• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef HIPERF_RING_BUFFER_H
16 #define HIPERF_RING_BUFFER_H
17 
18 #include <memory>
19 
20 namespace OHOS {
21 namespace Developtools {
22 namespace HiPerf {
23 class RingBuffer {
24 public:
25     // little endian, perf_event_header.type is less than 0xff, so set it
26     static constexpr uint8_t MARGIN_BYTE = 0xFF;
27 
28     RingBuffer(size_t size);
29     ~RingBuffer();
30     // get size of the writable space
31     size_t GetFreeSize() const;
32 
33     // before writing data to rbuff, alloc space first
34     uint8_t *AllocForWrite(size_t writeSize);
35     // after writing data, move head pointer
36     void EndWrite();
37     // get data from buff, return nullptr if no readable data
38     uint8_t *GetReadData();
39     // after reading, move tail pointer
40     void EndRead();
41 
42 private:
43     std::unique_ptr<uint8_t[]> buf_ = nullptr;
44     const size_t size_;
45     std::atomic_size_t head_ = 0; // write after this, always increase
46     std::atomic_size_t tail_ = 0; // read from this, always increase
47     size_t writeSize_ = 0;
48     size_t readSize_ = 0;
49 };
50 } // namespace HiPerf
51 } // namespace Developtools
52 } // namespace OHOS
53 #endif