Fix libpayload alloc() size and gcc pointer optimization problems.
authorMarc Jones <marc.jones@se-eng.com>
Tue, 20 Mar 2012 22:53:44 +0000 (16:53 -0600)
committerPeter Stuge <peter@stuge.se>
Wed, 21 Mar 2012 20:03:24 +0000 (21:03 +0100)
The previous commit was incomplete and missed setting the entire
alloc area.

There are also additional problems with gcc optimizations of the
pointer math. The "auto" casting by gcc wouldn't return warnings,
but it was causing the optimization to be incorrect. We are now
very explicit in the casting in the pointer math.

Change-Id: I020808c8d1dda544fe862b9efb0e5345eeab5aab
Signed-off-by: Marc Jones <marc.jones@se-eng.com>
Reviewed-on: http://review.coreboot.org/804
Tested-by: build bot (Jenkins)
Reviewed-by: Peter Stuge <peter@stuge.se>
payloads/libpayload/libc/malloc.c

index 9412cab189d455cc026158769957f3dd7b0bfe0a..3c5a3fd2a2c7b4b9a29fa4b2051a8926ca10f43b 100644 (file)
@@ -96,7 +96,7 @@ static void *alloc(int len)
 
        /* Make sure the region is setup correctly. */
        if (!HAS_MAGIC(*ptr))
 
        /* Make sure the region is setup correctly. */
        if (!HAS_MAGIC(*ptr))
-               setup(ptr, len);
+               setup(ptr, (int)((&_eheap - &_heap) - HDRSIZE));
 
        /* Find some free space. */
        do {
 
        /* Find some free space. */
        do {
@@ -112,7 +112,7 @@ static void *alloc(int len)
 
                if (header & FLAG_FREE) {
                        if (len <= size) {
 
                if (header & FLAG_FREE) {
                        if (len <= size) {
-                               hdrtype_t volatile *nptr = ptr + (HDRSIZE + len);
+                               hdrtype_t volatile *nptr = (hdrtype_t volatile *)((int)ptr + HDRSIZE + len);
                                int nsize = size - (HDRSIZE + len);
 
                                /* If there is still room in this block,
                                int nsize = size - (HDRSIZE + len);
 
                                /* If there is still room in this block,
@@ -131,11 +131,11 @@ static void *alloc(int len)
                                        *ptr = USED_BLOCK(size);
                                }
 
                                        *ptr = USED_BLOCK(size);
                                }
 
-                               return (void *)(ptr + HDRSIZE);
+                               return (void *)((int)ptr + HDRSIZE);
                        }
                }
 
                        }
                }
 
-               ptr += HDRSIZE + size;
+               ptr = (hdrtype_t volatile *)((int)ptr + HDRSIZE + size);
 
        } while (ptr < (hdrtype_t *) hend);
 
 
        } while (ptr < (hdrtype_t *) hend);
 
@@ -422,7 +422,7 @@ void print_malloc_map(void)
 
        if (free_memory && (minimal_free > free_memory))
                minimal_free = free_memory;
 
        if (free_memory && (minimal_free > free_memory))
                minimal_free = free_memory;
-       printf("Maximum memory consumption: %d bytes",
+       printf("Maximum memory consumption: %d bytes\n",
                (unsigned int)(&_eheap - &_heap) - HDRSIZE - minimal_free);
 }
 #endif
                (unsigned int)(&_eheap - &_heap) - HDRSIZE - minimal_free);
 }
 #endif