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
58
59
|
#include "../include/buffer.h"
#include "../include/alloc.h"
#include "../include/mem.h"
#include <assert.h>
int buffer_init(Buffer *self, ScopedAllocator *allocator, usize initial_capacity) {
self->allocator = allocator;
self->size = 0;
self->capacity = initial_capacity;
return scoped_allocator_alloc(self->allocator, initial_capacity, (void**)&self->data);
}
<<<<<<< HEAD
void buffer_deinit(Buffer *self) {
am_free(self->data);
self->data = NULL;
self->size = 0;
self->capacity = 0;
}
static CHECK_RESULT int buffer_ensure_capacity_for(Buffer *self, usize size) {
usize capacity;
=======
static CHECK_RESULT int buffer_ensure_capacity_for(Buffer *self, usize size) {
>>>>>>> Fixed CHECK_RESULT macro, use scoped allocator
void *new_mem;
int alloc_result;
usize new_capacity;
new_capacity = self->size + size;
if(self->capacity >= new_capacity)
return BUFFER_OK;
/*
We are using alloc here instead of realloc because we are using scoped allocator
which doesn't reallocate. TODO: Verify if scoped allocator node size is enough to hold buffers...
*/
alloc_result = scoped_allocator_alloc(self->allocator, new_capacity, &new_mem);
if(alloc_result != ALLOC_OK)
return BUFFER_ALLOC_FAIL;
self->data = new_mem;
self->capacity = new_capacity;
return BUFFER_OK;
}
int buffer_append(Buffer *self, void *data, usize size) {
return_if_error(buffer_ensure_capacity_for(self, size));
am_memcpy(self->data + self->size, data, size);
self->size += size;
return BUFFER_OK;
}
void* buffer_get(Buffer *self, usize index, usize type_size) {
usize real_index;
real_index = index * type_size;
assert(real_index < self->size);
return &self->data[real_index];
}
|