aboutsummaryrefslogtreecommitdiff
path: root/src/buffer.h
blob: a50cd859fa546864eb1af31cd1d42aa1c7d74c8e (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
#ifndef BUFFER_H
#define BUFFER_H

#include <stddef.h>

/*
    TODO: Optimize small size buffers by using data and size members (16 bytes on x86)
    instead of heap allocation
*/

typedef struct Buffer Buffer;
struct Buffer {
    void *data;
    size_t size;
    size_t capacity;
};

void buffer_init(Buffer *self);
void buffer_deinit(Buffer *self);

void buffer_append(Buffer *self, const void *data, size_t size);
/*
    This function changes the size of the buffer without changing the capacity
    and returns the value at the back of the buffer, which is valid until @tsl_buffer_append or
    @tsl_buffer_deinit is called.
    The buffer size has to be larger or equal to @size.
*/
void* buffer_pop(Buffer *self, size_t size);
void* buffer_begin(Buffer *self);
void* buffer_end(Buffer *self);
size_t buffer_get_size(Buffer *self, size_t type_size);

#endif