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