summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.dir-locals.el5
-rw-r--r--.gitignore2
-rw-r--r--LICENSE24
-rw-r--r--vec.h112
4 files changed, 143 insertions, 0 deletions
diff --git a/.dir-locals.el b/.dir-locals.el
new file mode 100644
index 0000000..380da03
--- /dev/null
+++ b/.dir-locals.el
@@ -0,0 +1,5 @@
+;;; Directory Local Variables -*- no-byte-compile: t; -*-
+;;; For more information see (info "(emacs) Directory Variables")
+
+((nil . ((+license/license-choice . "Unlicense")))
+ (c-mode . ((mode . clang-format))))
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..e1ab63f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+TAGS
+main* \ No newline at end of file
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..3c577b0
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,24 @@
+This is free and unencumbered software released into the public domain.
+
+Anyone is free to copy, modify, publish, use, compile, sell, or
+distribute this software, either in source code form or as a compiled
+binary, for any purpose, commercial or non-commercial, and by any
+means.
+
+In jurisdictions that recognize copyright laws, the author or authors
+of this software dedicate any and all copyright interest in the
+software to the public domain. We make this dedication for the benefit
+of the public at large and to the detriment of our heirs and
+successors. We intend this dedication to be an overt act of
+relinquishment in perpetuity of all present and future rights to this
+software under copyright law.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+
+For more information, please refer to <https://unlicense.org> \ No newline at end of file
diff --git a/vec.h b/vec.h
new file mode 100644
index 0000000..2f363d6
--- /dev/null
+++ b/vec.h
@@ -0,0 +1,112 @@
+/* Copyright (C) 2024 Aryadev Chavali
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Unlicense
+ * for details.
+
+ * You may distribute and modify this code under the terms of the
+ * Unlicense, which you should have received a copy of along with this
+ * program. If not, please go to <https://unlicense.org/>.
+
+ * Created: 2024-10-01
+ * Author: Aryadev Chavali
+ * Description: A dynamically sized container.
+ */
+
+#ifndef VEC_H
+#define VEC_H
+
+#include <stdint.h>
+
+typedef struct
+{
+ uint32_t size, capacity;
+ uint8_t bytes[];
+} vec_t;
+
+#define VEC_GET(P) (((vec_t *)(P)) - 1)
+#define VEC_SIZE(P) (VEC_GET(P)->size)
+#define VEC_CAP(P) (VEC_GET(P)->capacity)
+
+void vec_make(void **ptr, uint32_t size);
+void vec_free(void **ptr);
+void vec_append_byte(void **ptr, uint8_t byte);
+void vec_append(void **ptr, void *data, uint32_t size);
+void vec_clone(void **dest, void **src);
+void vec_ensure_remaining(void **ptr, uint32_t space);
+
+#define VEC_IMPL
+#ifdef VEC_IMPL
+
+#include <malloc.h>
+#include <string.h>
+
+#ifndef MAX
+#define MAX(A, B) ((A) > (B) ? (A) : (B))
+#endif
+
+#ifndef MIN
+#define MIN(A, B) ((A) < (B) ? (A) : (B))
+#endif
+
+void vec_make(void **ptr, uint32_t size)
+{
+ if (!ptr)
+ return;
+ vec_t *vector = calloc(1, sizeof(*vector) + size);
+ vector->size = 0;
+ vector->capacity = size;
+ *ptr = (vector + 1);
+}
+
+void vec_free(void **data)
+{
+ if (!data || !*data)
+ return;
+ free(VEC_GET(*data));
+ *data = NULL;
+}
+
+void vec_ensure_remaining(void **ptr, uint32_t space)
+{
+ if (!ptr || !*ptr)
+ return;
+ vec_t *vec = VEC_GET(*ptr);
+ if (vec->capacity - vec->size < space)
+ {
+ void *new_vec = NULL;
+ vec_make(&new_vec, MAX(vec->capacity * 2, vec->size + space));
+ VEC_SIZE(new_vec) = vec->size;
+ memcpy(new_vec, *ptr, vec->size);
+ vec_free(ptr);
+ *ptr = new_vec;
+ }
+}
+
+void vec_append_byte(void **ptr, uint8_t byte)
+{
+ vec_ensure_remaining(ptr, 1);
+ vec_t *vec = VEC_GET(*ptr);
+ vec->bytes[vec->size++] = byte;
+}
+
+void vec_append(void **ptr, void *data, uint32_t size)
+{
+ vec_ensure_remaining(ptr, size);
+ vec_t *vec = VEC_GET(*ptr);
+ memcpy(vec->bytes + vec->size, data, size);
+ vec->size += size;
+}
+
+void vec_clone(void **dest, void **src)
+{
+ if (!dest || !src || !*src)
+ return;
+ vec_make(dest, VEC_SIZE(*src));
+ memcpy(*dest, *src, VEC_SIZE(*src));
+ VEC_SIZE(*dest) = VEC_SIZE(*src);
+}
+#endif
+
+#endif