aboutsummaryrefslogtreecommitdiff
path: root/src/rt/circular_buffer.h
blob: 9ddaba42dc9f6df39d4d4bc8d4f71c3df18f4a71 (plain) (blame)
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 CIRCULAR_BUFFER_H
#define CIRCULAR_BUFFER_H

class
circular_buffer : public dom_owned<circular_buffer> {
    static const size_t INITIAL_CIRCULAR_BUFFFER_SIZE_IN_UNITS = 8;
    static const size_t MAX_CIRCULAR_BUFFFER_SIZE = 1 << 24;

public:
    rust_dom *dom;
    // Size of the data unit in bytes.
    const size_t unit_sz;
    circular_buffer(rust_dom *dom, size_t unit_sz);
    ~circular_buffer();
    void transfer(void *dst);
    void enqueue(void *src);
    void dequeue(void *dst);
    uint8_t *peek();
    bool is_empty();
    size_t size();

private:
    // Size of the buffer in bytes, should always be a power of two so that
    // modulo arithmetic (x % _buffer_sz) can optimized away with
    // (x & (_buffer_sz - 1)).
    size_t _buffer_sz;

    // Byte offset within the buffer where to read the next unit of data.
    size_t _next;

    // Number of bytes that have not been read from the buffer.
    size_t _unread;

    // The buffer itself.
    uint8_t *_buffer;
};

#endif /* CIRCULAR_BUFFER_H */