diff options
author | 2025-04-09 13:24:53 -0700 | |
---|---|---|
committer | 2025-04-11 00:15:18 -0700 | |
commit | f277f41d83d450a1f28d350bb7d6da075d1d2741 (patch) | |
tree | e197a7de7e26b47cec57a46acdde4b6c74889ad5 /src/buffer.c | |
download | github-mirror-f277f41d83d450a1f28d350bb7d6da075d1d2741.tar.gz github-mirror-f277f41d83d450a1f28d350bb7d6da075d1d2741.tar.zst github-mirror-f277f41d83d450a1f28d350bb7d6da075d1d2741.zip |
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
Diffstat (limited to 'src/buffer.c')
-rw-r--r-- | src/buffer.c | 51 |
1 files changed, 51 insertions, 0 deletions
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 <assert.h> +#include <stdlib.h> +#include <string.h> + +#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); +} |