about summary refs log tree commit diff
path: root/src/rt/circular_buffer.h
blob: c54ee8a33e41e8587611972739661b1789e5ce69 (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
 *
 */

#ifndef CIRCULAR_BUFFER_H
#define CIRCULAR_BUFFER_H

#include "rust_globals.h"
#include "rust_kernel.h"

class
circular_buffer : public kernel_owned<circular_buffer> {
    static const size_t INITIAL_CIRCULAR_BUFFER_SIZE_IN_UNITS = 8;

public:
    rust_kernel *kernel;
    // Size of the data unit in bytes.
    const size_t unit_sz;
    circular_buffer(rust_kernel *kernel, 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_t initial_size();
    void grow();
    void shrink();

    // Size of the buffer in bytes.
    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;
};

//
// Local Variables:
// mode: C++
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:
//

#endif /* CIRCULAR_BUFFER_H */