summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2010-11-17 10:04:54 +0100
committerTobias Klauser <tklauser@distanz.ch>2010-11-17 10:04:54 +0100
commitf7af7bfcbf18011826beede7cd9e626af5f60765 (patch)
tree0a2d7ddb31fe76b0800b424bf507ea2a5e267362
parent7b796fda06442a469f9109828ceb57847920887f (diff)
Add utils
-rw-r--r--Makefile2
-rw-r--r--util.c31
-rw-r--r--util.h65
3 files changed, 97 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 40a6460..049d114 100644
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@ P = nios2sim-ng
VERSION = 0.1
OBJS = main.o image.o elf.o srec.o nios2.o memory.o instruction.o simulator.o \
- device.o uart.o jtag_uart.o
+ device.o uart.o jtag_uart.o util.o
#OBJS += load_image.o nor_flash.o simulator.o simulator.o niosii.o i_type_handler.o j_type_handler.o \
# r_type_handler.o custom_instr.o timer.o
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..0ec10b9
--- /dev/null
+++ b/util.c
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
+ *
+ * This file is part of nios2sim-ng.
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License. See the file "COPYING" in the main directory of this archive
+ * for more details.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "nios2sim-ng.h"
+#include "util.h"
+
+/**
+ * Allocate memory which is set to zero.
+ *
+ * @param size requested size of memory in bytes
+ * @return pointer to the allocated and zeroed memory on success, NULL on
+ * error
+ */
+void *zalloc(const size_t size)
+{
+ void *ret = malloc(size);
+
+ if (likely(ret != NULL))
+ memset(ret, 0x00, size);
+ return ret;
+}
diff --git a/util.h b/util.h
new file mode 100644
index 0000000..92b9621
--- /dev/null
+++ b/util.h
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
+ *
+ * This file is part of nios2sim-ng.
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License. See the file "COPYING" in the main directory of this archive
+ * for more details.
+ */
+
+#ifndef _UTIL_H_
+#define _UTIL_H_
+
+#include <string.h>
+
+/*
+ * Memory allocation
+ */
+
+extern void *zalloc(const size_t size);
+
+/*
+ * String handling and manipulation
+ */
+
+/**
+ * Check whether two strings match exactly.
+ *
+ * @param ctx the reference string (usually a defined constant or string under
+ * the programs control)
+ * @param arg the string to match against the reference string (usually the
+ * user-supplied string)
+ * @return true if the strings match
+ */
+static inline bool xstrmatch(const char *ctx, const char *arg)
+{
+ size_t len = strlen(ctx);
+ return (strlen(arg) == len && strncmp(arg, ctx, len) == 0);
+}
+
+/**
+ * Check whether a string is empty (either NULL or empty string).
+ *
+ * @param str string to check for emptiness
+ * @return true if the string is empty
+ */
+static inline bool xstrisempty(const char *str)
+{
+ return ((str == NULL) || (xstrmatch("", str)));
+}
+
+
+/*
+ * Stringification macro (taken from Linux Kernel source code)
+ */
+
+/* Indirect stringification. Doing two levels allows the parameter to be a
+ * macro itself. For example, compile with -DFOO=bar, __stringify(FOO)
+ * converts to "bar".
+ */
+
+#define __stringify_1(x...) #x
+#define __stringify(x...) __stringify_1(x)
+
+#endif /* _UTIL_H_ */