j
: Next unread message k
: Previous unread message j a
: Jump to all threads
j l
: Jump to MailingList overview
Author: mcayland Date: Thu Dec 30 14:56:14 2010 New Revision: 992 URL: http://tracker.coreboot.org/trac/openbios/changeset/992
Log: Introduce ofmem_posix_memalign() function for arbitrary alignment.
Rework ofmem_malloc() so that it takes a parameter specifying the alignment for the allocation and then change the API to match that of posix_memalign(). Then create ofmem_malloc() as a simple wrapper function onto ofmem_posix_memalign() using a default alignment of CONFIG_OFMEM_MALLOC_ALIGN.
Signed-off-by: Mark Cave-Ayland mark.cave-ayland@siriusit.co.uk
Modified: trunk/openbios-devel/include/libopenbios/ofmem.h trunk/openbios-devel/libopenbios/ofmem_common.c
Modified: trunk/openbios-devel/include/libopenbios/ofmem.h ============================================================================== --- trunk/openbios-devel/include/libopenbios/ofmem.h Thu Dec 30 14:56:12 2010 (r991) +++ trunk/openbios-devel/include/libopenbios/ofmem.h Thu Dec 30 14:56:14 2010 (r992) @@ -80,6 +80,7 @@ extern unsigned long pa2va(phys_addr_t pa); /* malloc interface */ +extern int ofmem_posix_memalign( void **memptr, size_t alignment, size_t size ); extern void* ofmem_malloc( size_t size ); extern void ofmem_free( void *ptr ); extern void* ofmem_realloc( void *ptr, size_t size );
Modified: trunk/openbios-devel/libopenbios/ofmem_common.c ============================================================================== --- trunk/openbios-devel/libopenbios/ofmem_common.c Thu Dec 30 14:56:12 2010 (r991) +++ trunk/openbios-devel/libopenbios/ofmem_common.c Thu Dec 30 14:56:14 2010 (r992) @@ -86,7 +86,7 @@ /* OF private allocations */ /************************************************************************/
-void* ofmem_malloc( size_t size ) +int ofmem_posix_memalign( void **memptr, size_t alignment, size_t size ) { ofmem_t *ofmem = ofmem_arch_get_private(); alloc_desc_t *d, **pp; @@ -94,12 +94,12 @@ ucell top;
if( !size ) - return NULL; + return ENOMEM;
if( !ofmem->next_malloc ) ofmem->next_malloc = (char*)ofmem_arch_get_malloc_base();
- size = align_size(size + sizeof(alloc_desc_t), CONFIG_OFMEM_MALLOC_ALIGN); + size = align_size(size + sizeof(alloc_desc_t), alignment);
/* look in the freelist */ for( pp=&ofmem->mfree; *pp && (**pp).size < size; pp = &(**pp).next ) { @@ -110,15 +110,17 @@ ret = (void *)((uintptr_t)*pp + sizeof(alloc_desc_t)); memset( ret, 0, (**pp).size - sizeof(alloc_desc_t) ); *pp = (**pp).next; - return ret; + + *memptr = ret; + return 0; }
top = ofmem_arch_get_heap_top();
- ret = align_ptr((uintptr_t)ofmem->next_malloc + sizeof(alloc_desc_t), CONFIG_OFMEM_MALLOC_ALIGN); + ret = align_ptr((uintptr_t)ofmem->next_malloc + sizeof(alloc_desc_t), alignment); if( pointer2cell(ret) + size > top ) { printk("out of malloc memory (%x)!\n", size ); - return NULL; + return ENOMEM; }
d = (alloc_desc_t*)((uintptr_t)ret - sizeof(alloc_desc_t)); @@ -129,7 +131,23 @@
memset( ret, 0, size - sizeof(alloc_desc_t) );
- return ret; + *memptr = ret; + return 0; +} + +void* ofmem_malloc( size_t size ) +{ + void *memptr; + int res; + + res = ofmem_posix_memalign( &memptr, CONFIG_OFMEM_MALLOC_ALIGN, size ); + if (!res) { + /* Success */ + return memptr; + } else { + /* Failure */ + return NULL; + } }
void ofmem_free( void *ptr )