[coreboot] r3559 - in trunk/payloads/libpayload: include libc

svn at coreboot.org svn at coreboot.org
Tue Sep 2 17:49:32 CEST 2008


Author: oxygene
Date: 2008-09-02 17:49:32 +0200 (Tue, 02 Sep 2008)
New Revision: 3559

Modified:
   trunk/payloads/libpayload/include/libpayload.h
   trunk/payloads/libpayload/libc/malloc.c
Log:
Add memalign(align, size).

Signed-off-by: Patrick Georgi <patrick.georgi at coresystems.de>
Acked-by: Jordan Crouse <jordan.crouse at amd.com>


Modified: trunk/payloads/libpayload/include/libpayload.h
===================================================================
--- trunk/payloads/libpayload/include/libpayload.h	2008-09-02 09:35:43 UTC (rev 3558)
+++ trunk/payloads/libpayload/include/libpayload.h	2008-09-02 15:49:32 UTC (rev 3559)
@@ -221,6 +221,7 @@
 void *malloc(size_t size);
 void *calloc(size_t nmemb, size_t size);
 void *realloc(void *ptr, size_t size);
+void *memalign(size_t align, size_t size);
 /** @} */
 
 /**

Modified: trunk/payloads/libpayload/libc/malloc.c
===================================================================
--- trunk/payloads/libpayload/libc/malloc.c	2008-09-02 09:35:43 UTC (rev 3558)
+++ trunk/payloads/libpayload/libc/malloc.c	2008-09-02 15:49:32 UTC (rev 3559)
@@ -72,7 +72,7 @@
 	*((hdrtype_t *) hstart) = FREE_BLOCK(size);
 }
 
-static void *alloc(int len)
+static void *alloc(int len, int align)
 {
 	hdrtype_t header;
 	void *ptr = hstart;
@@ -92,13 +92,20 @@
 		header = *((hdrtype_t *) ptr);
 		int size = SIZE(header);
 
-		if (!HAS_MAGIC(header) || size == 0) {
+		if (!HAS_MAGIC(header)) {
 			printf("memory allocator panic.\n");
 			halt();
 		}
 
 		if (header & FLAG_FREE) {
-			if (len <= size) {
+			int realaddr = (int)(ptr + HDRSIZE);
+			int overhead = ((realaddr+align-1) & ~(align-1)) - realaddr;
+			if (len + overhead <= size) {
+				if (overhead != 0) {
+					*((hdrtype_t *) ptr) = FREE_BLOCK(overhead - HDRSIZE);
+					ptr += overhead;
+					size -= overhead;
+				}
 				void *nptr = ptr + (HDRSIZE + len);
 				int nsize = size - (HDRSIZE + len);
 
@@ -186,13 +193,13 @@
 
 void *malloc(size_t size)
 {
-	return alloc(size);
+	return alloc(size, 1);
 }
 
 void *calloc(size_t nmemb, size_t size)
 {
 	size_t total = nmemb * size;
-	void *ptr = alloc(total);
+	void *ptr = alloc(total, 1);
 
 	if (ptr)
 		memset(ptr, 0, total);
@@ -206,7 +213,7 @@
 	unsigned int osize;
 
 	if (ptr == NULL)
-		return alloc(size);
+		return alloc(size, 1);
 
 	pptr = ptr - HDRSIZE;
 
@@ -222,7 +229,7 @@
 	 * reallocated the new space.
 	 */
 	free(ptr);
-	ret = alloc(size);
+	ret = alloc(size, 1);
 
 	/*
 	 * if ret == NULL, then doh - failure.
@@ -237,11 +244,23 @@
 	return ret;
 }
 
+/**
+ * Allocate an aligned chunk of memory
+ *
+ * @param align alignment, must be power of two
+ * @param size size of chunk in bytes
+ * @return Return the address of such a memory region or NULL
+ */
+void *memalign(size_t align, size_t size)
+{
+	return alloc(size, align);
+}
+
 /* This is for debugging purposes. */
 #ifdef TEST
 void print_malloc_map(void)
 {
-	void *ptr = hstart;
+void *ptr = hstart;
 
 	while (ptr < hend) {
 		hdrtype_t hdr = *((hdrtype_t *) ptr);





More information about the coreboot mailing list