commit d23b0e3d8d3e079bd215673626de1a646991b7c5
parent 95ca975e234aecd24b8069d451eed2cfa34ab0e9
Author: William Casarin <jb55@jb55.com>
Date: Mon, 7 Aug 2023 15:34:54 -0700
util: add util helpers
Diffstat:
A | util.h | | | 33 | +++++++++++++++++++++++++++++++++ |
1 file changed, 33 insertions(+), 0 deletions(-)
diff --git a/util.h b/util.h
@@ -0,0 +1,33 @@
+
+#ifndef NDB_UTIL_H
+#define NDB_UTIL_H
+
+static inline int min(int a, int b) {
+ return a < b ? a : b;
+}
+
+static inline int max(int a, int b) {
+ return a > b ? a : b;
+}
+
+static inline void* memdup(const void* src, size_t size) {
+ void* dest = malloc(size);
+ if (dest == NULL) {
+ return NULL; // Memory allocation failed
+ }
+ memcpy(dest, src, size);
+ return dest;
+}
+
+static inline char *strdupn(const char *src, size_t size) {
+ char* dest = malloc(size+1);
+ if (dest == NULL) {
+ return NULL; // Memory allocation failed
+ }
+ memcpy(dest, src, size);
+ dest[size] = '\0';
+ return dest;
+}
+
+#endif // NDB_UTIL_H
+