forked from albertz/music-player-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Buffer.hpp
42 lines (32 loc) · 1.02 KB
/
Buffer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef MP_BUFFER_HPP
#define MP_BUFFER_HPP
#include <boost/atomic.hpp>
#include <stdint.h>
#include "LinkedList.hpp"
#define BUFFER_CHUNK_SIZE (1024 * 4)
struct Buffer {
struct Chunk {
uint8_t data[BUFFER_CHUNK_SIZE];
typedef uint16_t Idx;
boost::atomic<Idx> start, end;
uint8_t* pt() { return data + start; }
uint16_t size() const { assert(start <= end); return end - start; }
static uint16_t BufferSize() { return BUFFER_CHUNK_SIZE; }
uint16_t freeDataAvailable() { return BufferSize() - end; }
Chunk();
};
LinkedList<Chunk> chunks;
boost::atomic<size_t> _size;
Buffer();
// these are all not multithreading safe
size_t size() { return _size; }
void clear() { _size = 0; chunks.clear(); }
bool empty() { return size() == 0; }
// returns amount of data returned, i.e. <= target_size
// single consumer supported
size_t pop(uint8_t* target, size_t target_size, bool doCleanup = true);
// single producer supported
void push(const uint8_t* data, size_t size);
void cleanup();
};
#endif // BUFFER_HPP