148f512ceSopenharmony_ci/*
248f512ceSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
348f512ceSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
448f512ceSopenharmony_ci * you may not use this file except in compliance with the License.
548f512ceSopenharmony_ci * You may obtain a copy of the License at
648f512ceSopenharmony_ci *
748f512ceSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
848f512ceSopenharmony_ci *
948f512ceSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1048f512ceSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1148f512ceSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1248f512ceSopenharmony_ci * See the License for the specific language governing permissions and
1348f512ceSopenharmony_ci * limitations under the License.
1448f512ceSopenharmony_ci */
1548f512ceSopenharmony_ci#ifndef HIPERF_RING_BUFFER_H
1648f512ceSopenharmony_ci#define HIPERF_RING_BUFFER_H
1748f512ceSopenharmony_ci#include <memory>
1848f512ceSopenharmony_ci
1948f512ceSopenharmony_cinamespace OHOS {
2048f512ceSopenharmony_cinamespace Developtools {
2148f512ceSopenharmony_cinamespace HiPerf {
2248f512ceSopenharmony_ciclass RingBuffer {
2348f512ceSopenharmony_cipublic:
2448f512ceSopenharmony_ci    // little endian, perf_event_header.type is less than 0xff, so set it
2548f512ceSopenharmony_ci    static constexpr uint8_t MARGIN_BYTE = 0xFF;
2648f512ceSopenharmony_ci
2748f512ceSopenharmony_ci    explicit RingBuffer(size_t size);
2848f512ceSopenharmony_ci    ~RingBuffer();
2948f512ceSopenharmony_ci    // get size of the writable space
3048f512ceSopenharmony_ci    size_t GetFreeSize() const;
3148f512ceSopenharmony_ci
3248f512ceSopenharmony_ci    // before writing data to rbuff, alloc space first
3348f512ceSopenharmony_ci    uint8_t *AllocForWrite(size_t writeSize);
3448f512ceSopenharmony_ci    // after writing data, move head pointer
3548f512ceSopenharmony_ci    void EndWrite();
3648f512ceSopenharmony_ci    // get data from buff, return nullptr if no readable data
3748f512ceSopenharmony_ci    uint8_t *GetReadData();
3848f512ceSopenharmony_ci    // after reading, move tail pointer
3948f512ceSopenharmony_ci    void EndRead();
4048f512ceSopenharmony_ci
4148f512ceSopenharmony_ciprivate:
4248f512ceSopenharmony_ci    std::unique_ptr<uint8_t[]> buf_ = nullptr;
4348f512ceSopenharmony_ci    const size_t size_;
4448f512ceSopenharmony_ci    std::atomic_size_t head_ = 0; // write after this, always increase
4548f512ceSopenharmony_ci    std::atomic_size_t tail_ = 0; // read from this, always increase
4648f512ceSopenharmony_ci    size_t writeSize_ = 0;
4748f512ceSopenharmony_ci    size_t readSize_ = 0;
4848f512ceSopenharmony_ci};
4948f512ceSopenharmony_ci} // namespace HiPerf
5048f512ceSopenharmony_ci} // namespace Developtools
5148f512ceSopenharmony_ci} // namespace OHOS
5248f512ceSopenharmony_ci#endif // HIPERF_RING_BUFFER_H
53