libutil

C Utility Library
git clone http://git.omkov.net/libutil
Log | Tree | Refs | README | LICENCE | Download

AuthorJamozed <[email protected]>
Date2021-12-08 01:00:19
Commitf1897e7f9865ddd994bf88c9a71f6f01134f7f3c
Parent7d31e3ad7ba3afe8e6cc7c42bbed4943b2081c21

alloc: Add extended allocation wrappers

Diffstat

A src/alloc.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
A src/alloc.h | 42 ++++++++++++++++++++++++++++++++++++++++++
M src/test/test_fnv.c | 2 --

3 files changed, 90 insertions, 2 deletions

diff --git a/src/alloc.c b/src/alloc.c
new file mode 100644
index 0000000..be1ec97
--- /dev/null
+++ b/src/alloc.c
@@ -0,0 +1,48 @@
+// util/alloc.h, version 1.0.0
+// Memory allocation source file from libutil
+// 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 "alloc.h"
+#include "error.h"
+
+#include <stdlib.h>
+
+void *xmalloc(UINT l) {
+	void *q = malloc(l); if (!q) { error(1, "%s", SERR); } return q;
+}
+
+void *xcalloc(UINT n, UINT l) {
+	void *q = calloc(n, l); if (!q) { error(1, "%s", SERR); } return q;
+}
+
+void *xrealloc(void *p, UINT l) {
+	void *q = realloc(p, l); if (!q) { error(1, "%s", SERR); } return q;
+}
diff --git a/src/alloc.h b/src/alloc.h
new file mode 100644
index 0000000..f58e3ab
--- /dev/null
+++ b/src/alloc.h
@@ -0,0 +1,42 @@
+// util/alloc.h, version 1.0.0
+// Memory allocation header file from libutil
+// 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 UTIL_ALLOC_H_M0UWQ8LT
+#define UTIL_ALLOC_H_M0UWQ8LT
+
+#include "util.h"
+
+extern void *xmalloc(UINT l);
+extern void *xcalloc(UINT n, UINT l);
+extern void *xrealloc(void *p, UINT l);
+
+#endif // UTIL_ALLOC_H_M0UWQ8LT
diff --git a/src/test/test_fnv.c b/src/test/test_fnv.c
index 28385a0..9eadf21 100644
--- a/src/test/test_fnv.c
+++ b/src/test/test_fnv.c
@@ -21,9 +21,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
 #include "../util.h"
 #include "unit.h"
 
-#include <stdint.h>
 #include <stdio.h>
-#include <string.h>
 
 int main(void) {
 	{