ESH

Executive Shell
git clone http://git.omkov.net/ESH
Log | Tree | Refs | README | Download

AuthorJamozed <[email protected]>
Date2021-09-11 01:55:29
Commitcc99c0486e7f3ee9c3ca00c82be7ecd6d810d47b
Parentfd110664b11917f1f75b936370d7e25b67d5b4e2

Add array utility

Diffstat

A src/util/array.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A src/util/array.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
A src/util/util.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
A src/util/util.h | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

4 files changed, 230 insertions, 0 deletions

diff --git a/src/util/array.c b/src/util/array.c
new file mode 100644
index 0000000..494acfd
--- /dev/null
+++ b/src/util/array.c
@@ -0,0 +1,64 @@
+// util/array.c
+// Array utility source file for ESH
+// Copyright (C) 2021, Jakob Wakeling
+// All rights reserved.
+
+/*
+OMKOV Permissive Licence, version 1.0
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal with
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimers.
+* Redistributions in binary form must reproduce the above copyright notice, this
+  list of conditions and the following disclaimers in the documentation and/or
+  other materials provided with the distribution.
+* Neither the names of the copyright holders, nor the names of its contributors
+  may be used to endorse or promote products derived from this Software without
+  specific prior written permission.
+
+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 COPYRIGHT
+HOLDERS OR CONTRIBUTORS 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 WITH THE SOFTWARE.
+*/
+
+#include "array.h"
+#include "util.h"
+
+#include <stdlib.h>
+
+/* Initialise an array. */
+array array_init() {
+	return assert_calloc(1, sizeof (struct array_));
+}
+
+/* Uninitialise an array. */
+void  array_free(array a) {
+	free(a);
+}
+
+/* Push a pointer to an array. */
+void array_push(array a, ptr data) {
+	a->data = assert_realloc(a->data, (a->size += 1));
+	a->data[a->size - 1] = data;
+}
+
+/* Pop a pointer from an array. */
+ptr array_pop(array a) {
+	ptr data = a->data[a->size - 1];
+	a->data = assert_realloc(a->data, (a->size -= 1));
+	return data;
+}
+
+/* Peek at a pointer on an array without popping. */
+ptr array_peek(array a) {
+	return a->data[a->size - 1];
+}
diff --git a/src/util/array.h b/src/util/array.h
new file mode 100644
index 0000000..f8981c3
--- /dev/null
+++ b/src/util/array.h
@@ -0,0 +1,49 @@
+// util/array.h
+// Array utility header file for ESH
+// Copyright (C) 2021, Jakob Wakeling
+// All rights reserved.
+
+/*
+OMKOV Permissive Licence, version 1.0
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal with
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimers.
+* Redistributions in binary form must reproduce the above copyright notice, this
+  list of conditions and the following disclaimers in the documentation and/or
+  other materials provided with the distribution.
+* Neither the names of the copyright holders, nor the names of its contributors
+  may be used to endorse or promote products derived from this Software without
+  specific prior written permission.
+
+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 COPYRIGHT
+HOLDERS OR CONTRIBUTORS 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 WITH THE SOFTWARE.
+*/
+
+#ifndef ESH_UTIL_ARRAY_H_9LJ2QOUZ
+#define ESH_UTIL_ARRAY_H_9LJ2QOUZ
+
+#include "util.h"
+
+typedef struct array_ *array;
+
+struct array_ { ptr *data; UINT size; };
+
+extern array array_init(UINT size);
+extern void  array_free(array a);
+
+extern void array_push(array a, ptr data);
+extern ptr  array_pop(array a);
+extern ptr  array_peek(array a);
+
+#endif // ESH_UTIL_ARRAY_H_9LJ2QOUZ
diff --git a/src/util/util.c b/src/util/util.c
new file mode 100644
index 0000000..a8b7572
--- /dev/null
+++ b/src/util/util.c
@@ -0,0 +1,54 @@
+// util/util.c
+// Common utility source file for ESH
+// Copyright (C) 2021, Jakob Wakeling
+// All rights reserved.
+
+/*
+OMKOV Permissive Licence, version 1.0
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal with
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimers.
+* Redistributions in binary form must reproduce the above copyright notice, this
+  list of conditions and the following disclaimers in the documentation and/or
+  other materials provided with the distribution.
+* Neither the names of the copyright holders, nor the names of its contributors
+  may be used to endorse or promote products derived from this Software without
+  specific prior written permission.
+
+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 COPYRIGHT
+HOLDERS OR CONTRIBUTORS 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 WITH THE SOFTWARE.
+*/
+
+#include "util.h"
+
+#include <assert.h>
+#include <stdlib.h>
+
+/* Allocate and assert memory. */
+ptr assert_malloc(UINT l) {
+	register ptr r = malloc(l);
+	assert(r); return r;
+}
+
+/* Allocate and assert memory. */
+ptr assert_calloc(UINT n, UINT l) {
+	register ptr r = calloc(n, l);
+	assert(r); return r;
+}
+
+/* Reallocate and assert memory. */
+ptr assert_realloc(ptr p, UINT l) {
+	register ptr r = realloc(p, l);
+	assert(r); return r;
+}
diff --git a/src/util/util.h b/src/util/util.h
new file mode 100644
index 0000000..1a8dbb2
--- /dev/null
+++ b/src/util/util.h
@@ -0,0 +1,63 @@
+// util/util.h
+// Common utility header file for ESH
+// Copyright (C) 2021, Jakob Wakeling
+// All rights reserved.
+
+/*
+OMKOV Permissive Licence, version 1.0
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal with
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimers.
+* Redistributions in binary form must reproduce the above copyright notice, this
+  list of conditions and the following disclaimers in the documentation and/or
+  other materials provided with the distribution.
+* Neither the names of the copyright holders, nor the names of its contributors
+  may be used to endorse or promote products derived from this Software without
+  specific prior written permission.
+
+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 COPYRIGHT
+HOLDERS OR CONTRIBUTORS 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 WITH THE SOFTWARE.
+*/
+
+#ifndef ESH_UTIL_UTIL_H_KP8NS9DC
+#define ESH_UTIL_UTIL_H_KP8NS9DC
+
+#include <stdbool.h>
+#include <stdint.h>
+
+typedef void *ptr;
+
+typedef uint8_t   u8;
+typedef uint16_t  u16;
+typedef uint32_t  u32;
+typedef uint64_t  u64;
+typedef uintptr_t uint_;
+
+#define UINT uint_
+
+typedef int8_t   s8;
+typedef int16_t  s16;
+typedef int32_t  s32;
+typedef int64_t  s64;
+typedef intptr_t sint;
+
+typedef float       f32;
+typedef double      f64;
+typedef long double f128;
+
+extern ptr assert_malloc(UINT l);
+extern ptr assert_calloc(UINT n, UINT l);
+extern ptr assert_realloc(ptr p, UINT l);
+
+#endif // ESH_UTIL_UTIL_H_KP8NS9DC