aboutsummaryrefslogtreecommitdiff
path: root/src/std
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2020-01-18 10:01:27 +0100
committerdec05eba <dec05eba@protonmail.com>2020-01-18 10:05:55 +0100
commit50c928d224bff0af322f23a7d2b842cd54aa2e68 (patch)
tree4e02b167c89c2ff109f6dfd1d2734309ea0192db /src/std
parent1dbef1bfdefe8d7967a360f00d350db307d344e2 (diff)
Start on bytecode, move object files to build directory
Diffstat (limited to 'src/std')
-rw-r--r--src/std/buffer.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/std/buffer.c b/src/std/buffer.c
new file mode 100644
index 0000000..3fc5184
--- /dev/null
+++ b/src/std/buffer.c
@@ -0,0 +1,42 @@
+#include "../../include/std/buffer.h"
+#include <stdlib.h>
+#include <string.h>
+
+void tsl_buffer_init(TslBuffer *self) {
+ self->data = NULL;
+ self->size = 0;
+ self->capacity = 0;
+}
+
+void tsl_buffer_deinit(TslBuffer *self) {
+ free(self->data);
+}
+
+static int tsl_buffer_ensure_capacity(TslBuffer *self, size_t new_size) {
+ void *new_ptr;
+ if(new_size <= self->capacity)
+ return 1;
+
+ if(self->capacity != 0) {
+ size_t new_capacity = self->capacity;
+ while(new_capacity < new_size) {
+ new_capacity *= 2;
+ }
+ new_size = new_capacity;
+ }
+ new_ptr = realloc(self->data, new_size);
+ if(!new_ptr)
+ return 0;
+
+ self->data = new_ptr;
+ self->capacity = new_size;
+ return 1;
+}
+
+int tsl_buffer_append(TslBuffer *self, void *data, size_t size) {
+ if(!tsl_buffer_ensure_capacity(self, self->size + size))
+ return 1;
+ memcpy((char*)self->data + self->size, data, size);
+ self->size += size;
+ return 0;
+}