[gPXE-devel] [PATCH 11/33] [linux] Add umalloc

Piotr Jaroszyński p.jaroszynski at gmail.com
Sun Aug 15 18:59:16 EDT 2010


Add umalloc API.

Signed-off-by: Piotr Jaroszyński <p.jaroszynski at gmail.com>
---
 src/config/defaults/linux.h            |    1 +
 src/include/gpxe/linux/linux_umalloc.h |   18 +++++
 src/include/gpxe/umalloc.h             |    1 +
 src/interface/linux/linux_umalloc.c    |  126 ++++++++++++++++++++++++++++++++
 4 files changed, 146 insertions(+), 0 deletions(-)
 create mode 100644 src/include/gpxe/linux/linux_umalloc.h
 create mode 100644 src/interface/linux/linux_umalloc.c

diff --git a/src/config/defaults/linux.h b/src/config/defaults/linux.h
index 7f180b5..108cc7a 100644
--- a/src/config/defaults/linux.h
+++ b/src/config/defaults/linux.h
@@ -10,6 +10,7 @@
 #define CONSOLE_LINUX
 #define TIMER_LINUX
 #define UACCESS_LINUX
+#define UMALLOC_LINUX
 
 #define IMAGE_SCRIPT
 
diff --git a/src/include/gpxe/linux/linux_umalloc.h b/src/include/gpxe/linux/linux_umalloc.h
new file mode 100644
index 0000000..474d9cd
--- /dev/null
+++ b/src/include/gpxe/linux/linux_umalloc.h
@@ -0,0 +1,18 @@
+#ifndef _GPXE_LINUX_UMALLOC_H
+#define _GPXE_LINUX_UMALLOC_H
+
+FILE_LICENCE(GPL2_OR_LATER);
+
+/** @file
+ *
+ * gPXE user memory allocation API for linux
+ *
+ */
+
+#ifdef UMALLOC_LINUX
+#define UMALLOC_PREFIX_linux
+#else
+#define UMALLOC_PREFIX_linux __linux_
+#endif
+
+#endif /* _GPXE_LINUX_UMALLOC_H */
diff --git a/src/include/gpxe/umalloc.h b/src/include/gpxe/umalloc.h
index b0e5564..6fa73d9 100644
--- a/src/include/gpxe/umalloc.h
+++ b/src/include/gpxe/umalloc.h
@@ -26,6 +26,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
 
 /* Include all architecture-independent I/O API headers */
 #include <gpxe/efi/efi_umalloc.h>
+#include <gpxe/linux/linux_umalloc.h>
 
 /* Include all architecture-dependent I/O API headers */
 #include <bits/umalloc.h>
diff --git a/src/interface/linux/linux_umalloc.c b/src/interface/linux/linux_umalloc.c
new file mode 100644
index 0000000..d665c00
--- /dev/null
+++ b/src/interface/linux/linux_umalloc.c
@@ -0,0 +1,126 @@
+/*
+ * Copyright (C) 2010 Piotr Jaroszyński <p.jaroszynski at gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+FILE_LICENCE(GPL2_OR_LATER);
+
+/** @file
+ *
+ * gPXE user memory allocation API for linux
+ *
+ */
+
+#include <assert.h>
+#include <gpxe/umalloc.h>
+
+#include <linux_api.h>
+
+/** Special address returned for empty allocations */
+#define NOWHERE ((void *)-1)
+
+/** Poison to make the metadata more unique */
+#define POISON 0xa5a5a5a5
+#define min(a,b) (((a)<(b))?(a):(b))
+
+/** Metadata stored at the beginning of all allocations */
+struct metadata
+{
+	unsigned poison;
+	size_t size;
+};
+
+#define SIZE_MD (sizeof(struct metadata))
+
+/** Simple realloc which passes most of the work to mmap(), mremap() and munmap() */
+static void * linux_realloc(void *ptr, size_t size)
+{
+	struct metadata md = {0, 0};
+	struct metadata * mdptr = NULL;
+
+	DBG2("linux_realloc(%p, %zd)\n", ptr, size);
+
+	/* Check whether we have a valid pointer */
+	if (ptr != NULL && ptr != NOWHERE) {
+		mdptr = ptr - SIZE_MD;
+		md = *mdptr;
+
+		/* Check for poison in the metadata */
+		if (md.poison != POISON) {
+			DBG("linux_realloc bad poison: 0x%x (expected 0x%x)\n", md.poison, POISON);
+			return NULL;
+		}
+	} else {
+		/* Handle NOWHERE as NULL */
+		ptr = NULL;
+	}
+
+	/*
+	 * At this point, ptr is either NULL or pointing to a region allocated by us.
+	 * In the latter case mdptr is pointing to a valid metadata, otherwise it is NULL.
+	 */
+
+	/* Handle deallocation or allocation of size 0 */
+	if (size == 0) {
+		if (mdptr) {
+			if (linux_munmap(mdptr, md.size))
+				DBG("linux_realloc munmap failed: %s\n", linux_strerror(linux_errno));
+		}
+		return NOWHERE;
+	}
+
+	if (ptr) {
+		/* ptr is pointing to an already allocated memory, mremap() it with new size */
+		mdptr = linux_mremap(mdptr, md.size + SIZE_MD, size + SIZE_MD, MREMAP_MAYMOVE);
+		if (mdptr == MAP_FAILED) {
+			DBG("linux_realloc mremap failed: %s\n", linux_strerror(linux_errno));
+			return NULL;
+		}
+
+		ptr = ((void *)mdptr) + SIZE_MD;
+	} else {
+		/* allocate new memory with mmap() */
+		mdptr = linux_mmap(NULL, size + SIZE_MD, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+		if (mdptr == MAP_FAILED) {
+			DBG("linux_realloc mmap failed: %s\n", linux_strerror(linux_errno));
+			return NULL;
+		}
+		ptr = ((void *)mdptr) + SIZE_MD;
+	}
+
+	/* Update the metadata */
+	mdptr->poison = POISON;
+	mdptr->size = size;
+
+	return ptr;
+}
+
+/**
+ * Reallocate external memory
+ *
+ * @v old_ptr		Memory previously allocated by umalloc(), or UNULL
+ * @v new_size		Requested size
+ * @ret new_ptr		Allocated memory, or UNULL
+ *
+ * Calling realloc() with a new size of zero is a valid way to free a
+ * memory block.
+ */
+static userptr_t linux_urealloc(userptr_t old_ptr, size_t new_size)
+{
+	return (userptr_t)linux_realloc((void *)old_ptr, new_size);
+}
+
+PROVIDE_UMALLOC(linux, urealloc, linux_urealloc);
-- 
1.7.1



More information about the gPXE-devel mailing list