[OpenBIOS] [PATCH 2/6] Introduce ofmem_posix_memalign() function for arbitrary alignment.

Mark Cave-Ayland mark.cave-ayland at siriusit.co.uk
Wed Dec 29 12:07:58 CET 2010


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 at siriusit.co.uk>
---
 openbios-devel/include/libopenbios/ofmem.h |    1 +
 openbios-devel/libopenbios/ofmem_common.c  |   32 +++++++++++++++++++++------
 2 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/openbios-devel/include/libopenbios/ofmem.h b/openbios-devel/include/libopenbios/ofmem.h
index 7b49e48..3d8ddbf 100644
--- a/openbios-devel/include/libopenbios/ofmem.h
+++ b/openbios-devel/include/libopenbios/ofmem.h
@@ -80,6 +80,7 @@ extern inline phys_addr_t va2pa(unsigned long va);
 extern inline unsigned long pa2va(phys_addr_t pa);
 				      
 /* malloc interface */
+extern int ofmem_posix_memalign( void **memptr, int 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 );
diff --git a/openbios-devel/libopenbios/ofmem_common.c b/openbios-devel/libopenbios/ofmem_common.c
index bd0833f..8fb9e5b 100644
--- a/openbios-devel/libopenbios/ofmem_common.c
+++ b/openbios-devel/libopenbios/ofmem_common.c
@@ -86,7 +86,7 @@ print_trans( void )
 /* OF private allocations                                               */
 /************************************************************************/
 
-void* ofmem_malloc( size_t size )
+int ofmem_posix_memalign( void **memptr, int alignment, size_t size )
 {
 	ofmem_t *ofmem = ofmem_arch_get_private();
 	alloc_desc_t *d, **pp;
@@ -94,12 +94,12 @@ void* ofmem_malloc( size_t size )
 	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 @@ void* ofmem_malloc( size_t size )
 		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 @@ void* ofmem_malloc( size_t size )
 
 	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 )
-- 
1.7.2.3




More information about the OpenBIOS mailing list