2020-02-08 10:50:48 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstddef>
|
2020-02-13 16:16:34 -05:00
|
|
|
#include <atomic>
|
2020-02-08 10:50:48 -05:00
|
|
|
|
|
|
|
namespace tc {
|
|
|
|
class ring_buffer {
|
|
|
|
public:
|
|
|
|
/* Attention: Actual size may be larger than given capacity */
|
|
|
|
explicit ring_buffer(size_t /* minimum capacity */);
|
|
|
|
~ring_buffer();
|
|
|
|
|
|
|
|
[[nodiscard]] size_t capacity() const;
|
|
|
|
[[nodiscard]] size_t fill_count() const;
|
|
|
|
[[nodiscard]] size_t free_count() const;
|
|
|
|
|
|
|
|
/* do not write more than the capacity! */
|
|
|
|
char* write_ptr();
|
|
|
|
void advance_write_ptr(size_t /* count */);
|
|
|
|
|
|
|
|
/* do not read more than the capacity! */
|
|
|
|
[[nodiscard]] const void* read_ptr() const;
|
|
|
|
void advance_read_ptr(size_t /* count */);
|
|
|
|
|
|
|
|
void clear();
|
2020-02-13 16:16:34 -05:00
|
|
|
[[nodiscard]] bool valid() const;
|
|
|
|
|
|
|
|
[[nodiscard]] inline operator bool() const { return this->valid(); }
|
2020-02-08 10:50:48 -05:00
|
|
|
private:
|
2020-02-13 16:16:34 -05:00
|
|
|
#ifndef HAVE_SOUNDIO
|
|
|
|
struct MirroredMemory {
|
|
|
|
size_t capacity;
|
|
|
|
char *address;
|
|
|
|
void *priv;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool allocate_memory(size_t requested_capacity);
|
|
|
|
void free_memory();
|
|
|
|
|
|
|
|
MirroredMemory memory{};
|
|
|
|
|
2020-04-20 15:32:05 -04:00
|
|
|
std::atomic_long write_offset{};
|
|
|
|
std::atomic_long read_offset{};
|
2020-02-13 16:16:34 -05:00
|
|
|
size_t _capacity{0}; /* for faster access */
|
|
|
|
#endif
|
|
|
|
|
2020-02-08 10:50:48 -05:00
|
|
|
void* handle{nullptr};
|
|
|
|
};
|
|
|
|
}
|