BarryServer : Git

All the code for all my projects
// BarryServer : Git / Orion / commit / d41a53cbc7d055b1c00cf0a339dbed6925f4f02c / mem / mem.c

// Related

Orion

Barry Importing existing Orion kernel d41a53c (2 years, 4 months ago)
diff --git a/mem/mem.c b/mem/mem.c
new file mode 100644
index 0000000..80a7c89
--- /dev/null
+++ b/mem/mem.c
@@ -0,0 +1,106 @@
+/*
+ * This file contains a few routines for the manipulation of memory and strings.
+ * The functions would normally be part of a C library, but this is for the
+ * Kernel.  The functions are standalone, and have no dependencies - they can be
+ * called immediately after boot.
+ */
+
+#include <stdint.h>
+#include <stddef.h>
+
+/* Fill a region of memory with the specified byte */
+void *
+memset(void *s, int c, size_t n)
+{
+	unsigned char *a = s;
+	if (n > 0) {
+		while (n-- > 0)
+			*a++ = c;
+	}
+	return s;
+}
+
+/* Copy one region of memory to another */
+void *
+memcpy(void *dest, void *src, size_t n)
+{
+	unsigned char *a = (unsigned char *) dest,
+	              *b = (unsigned char *) src;
+	while (n-- > 0)
+		*a++ = *b++;
+	return dest;
+}
+
+/* Compare two regions of memory */
+int
+memcmp(void *s1, void *s2, size_t n)
+{
+	unsigned char *a = (unsigned char *) s1,
+	              *b = (unsigned char *) s2;
+	while (n-- > 0)
+		if (*a++ != *b++)
+			return a[-1] - b[-1];
+	return 0;
+}
+
+/* Find the length of a string */
+size_t
+strlen(char *s)
+{
+	if (!s)
+		return 0;
+	size_t i;
+	for (i = 0; s[i]; i++);
+	return i;
+}
+
+/* Find the length of a string up to maximum */
+size_t
+strnlen(char *s, size_t maxlen)
+{
+	if (!s)
+		return 0;
+	size_t i;
+	for (i = 0; s[i] && i <= maxlen; i++);
+	return i;
+}
+
+/* Compare two strings */
+int
+strcmp(char *s1, char *s2)
+{
+	for (; *s1 == *s2 && *s1 && *s2; s1++, s2++);
+	return *(unsigned char *) s1 - *(unsigned char *) s2;
+}
+
+/* Compare two limited strings */
+int
+strncmp(char *s1, char *s2, size_t n)
+{
+	if (!n--) return 0;
+	for (; *s1 == *s2 && *s1 && *s2 && n; s1++, s2++, n--);
+	return *(unsigned char *) s1 - *(unsigned char *) s2;
+}
+
+/* Copy a string */
+char *
+strcpy(char *dest, const char *src)
+{
+	char *ret = dest;
+	while (*src)
+		*dest++ = *src++;
+	*dest = '\0';
+	return ret;
+}
+
+/* Copy a limited string */
+char *
+strncpy(char *dest, const char *src, size_t n)
+{
+	char *ret = dest;
+	while (*src && n--)
+		*dest++ = *src++;
+	*dest = '\0';
+	return ret;
+}
+