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 patrick.georgi@coresystems.de
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)
On 01/09/08 15:28 +0200, Patrick Georgi wrote:
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 patrick.georgi@coresystems.de
Acked-by: Jordan Crouse jordan.crouse@amd.com
We need to get this in there. But I promise that that damn malloc() engine is going to get gutted soon, because it constantly makes my eyes hurt when see it.
Patrick - if you get a chance, can you submit a short doxygen comment for memalign? I know the rest of the file isn't documented yet, but we need to start getting into the habit of documenting external functions.
Jordan
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 coreboot@coreboot.org http://www.coreboot.org/mailman/listinfo/coreboot
Jordan Crouse schrieb:
Signed-off-by: Patrick Georgi patrick.georgi@coresystems.de
Acked-by: Jordan Crouse jordan.crouse@amd.com
Thanks, r3559