From f277f41d83d450a1f28d350bb7d6da075d1d2741 Mon Sep 17 00:00:00 2001 From: Anshul Gupta Date: Wed, 9 Apr 2025 13:24:53 -0700 Subject: Initial Commit Add cmake github workflow Install dependencies in gh action Fix missing directory Fix compiler errors Fix group name to gid conversion Force cjson to statically link Add header guards to query headers Add install and cpack to CMakeLists.txt Add config search and CLI arg parsing Improve docs for git.c Fix program continuing on -h flag --- src/buffer.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/buffer.c (limited to 'src/buffer.c') diff --git a/src/buffer.c b/src/buffer.c new file mode 100644 index 0000000..51a3e3c --- /dev/null +++ b/src/buffer.c @@ -0,0 +1,51 @@ +// +// Created by Anshul Gupta on 4/4/25. +// + +#include "buffer.h" + +#include +#include +#include + +#include "alloc.h" + +buffer_t buffer_new(size_t cap) +{ + buffer_t buf; + + if (cap == 0) + return (buffer_t) {NULL, 0, 0}; + + cap = (cap + 7) & ~7; // Align to 8 bytes + buf.data = gmalloc(cap); + if (!buf.data) + abort(); + + return (buffer_t) {buf.data, 0, cap}; +} + +void buffer_free(buffer_t buf) { gfree(buf.data); } + +void buffer_reserve(buffer_t *buf, size_t cap) +{ + if (buf->cap >= cap) + return; + + cap = (cap + 7) & ~7; // Align to 8 bytes + uint8_t *new_data = grealloc(buf->data, cap); + if (!new_data) + abort(); + buf->data = new_data; + buf->cap = cap; +} + +void buffer_append(buffer_t *buf, const void *data, size_t len) +{ + if (buf->len + len > buf->cap) + buffer_reserve(buf, buf->len + len); + + memcpy(buf->data + buf->len, data, len); + buf->len += len; + assert(buf->len <= buf->cap); +} -- cgit v1.2.3