Hi there,

after the discussion on the list about memalign, I wrote version that's
better integrated with the other memory management stuff, and doesn't
come with its own pool allocator.


Regards,
Patrick Georgi
Signed-off-by: Patrick Georgi <[EMAIL PROTECTED]>

Index: include/libpayload.h
===================================================================
--- include/libpayload.h        (revision 3536)
+++ include/libpayload.h        (working copy)
@@ -164,6 +164,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);
 
 /* libc/exec.c */
 int exec(long addr, int argc, char **argv);
Index: libc/malloc.c
===================================================================
--- libc/malloc.c       (revision 3536)
+++ libc/malloc.c       (working copy)
@@ -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,6 +244,11 @@
        return ret;
 }
 
+void *memalign(size_t align, size_t size)
+{
+       return alloc(size, align);
+}
+
 /* This is for debugging purposes. */
 #ifdef TEST
 void print_malloc_map(void)
--
coreboot mailing list
[email protected]
http://www.coreboot.org/mailman/listinfo/coreboot

Reply via email to