aboutsummaryrefslogtreecommitdiff
path: root/include/std/buffer.h
blob: 3c2d79c99731c98e2f22adff24f0d4ef27bc7143 (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
#ifndef AMALGAM_BUFFER_H
#define AMALGAM_BUFFER_H

#include "types.h"
#include "misc.h"
#include "defs.h"

#define BUFFER_OK 0
#define BUFFER_ALLOC_FAIL -1

typedef struct {
    char* data;
    usize size;
    usize capacity;

    ArenaAllocator *allocator;
    usize allocator_index;
} Buffer;

CHECK_RESULT int buffer_init(Buffer *self, struct ArenaAllocator *allocator);
void buffer_deinit(Buffer *self);

/*
@data can't be null. Use @buffer_append_empty if you want to change the size
of the buffer without adding data
*/
CHECK_RESULT int buffer_append(Buffer *self, const void *data, usize size);
CHECK_RESULT int buffer_append_empty(Buffer *self, usize size);
/* Expand buffer capacity without changing size */
CHECK_RESULT int buffer_expand(Buffer *self, usize size);
void* buffer_get(Buffer *self, usize index, usize type_size);
CHECK_RESULT int buffer_pop(Buffer *self, void *data, usize size);
/* Set buffer size to 0, doesn't change the capacity */
void buffer_clear(Buffer *self);
/* Also changes size if @new_capacity is less than buffer size */
CHECK_RESULT int buffer_set_capacity(Buffer *self, usize new_capacity);
void* buffer_begin(Buffer *self);
void* buffer_end(Buffer *self);
usize __buffer_get_size(Buffer *self, usize type_size);
#define buffer_get_size(self, type) __buffer_get_size((self), sizeof(type))

#endif