[OpenBIOS] [PATCH 10/16] Switch SPARC32 over to use OFMEM's malloc()/free() internally rather than using it's own implementation.
Mark Cave-Ayland
mark.cave-ayland at siriusit.co.uk
Thu Dec 30 19:07:13 CET 2010
Also juggle the memory to take into account that the allocations are coming from OFMEM, and remove
posix_memalign2() since it is no longer required.
Signed-off-by: Mark Cave-Ayland <mark.cave-ayland at siriusit.co.uk>
---
openbios-devel/arch/sparc32/ldscript | 3 +-
openbios-devel/arch/sparc32/lib.c | 124 +++------------------------
openbios-devel/arch/sparc32/ofmem_sparc32.c | 2 +-
3 files changed, 13 insertions(+), 116 deletions(-)
diff --git a/openbios-devel/arch/sparc32/ldscript b/openbios-devel/arch/sparc32/ldscript
index da94a56..32c9445 100644
--- a/openbios-devel/arch/sparc32/ldscript
+++ b/openbios-devel/arch/sparc32/ldscript
@@ -12,8 +12,7 @@ BASE_ADDR = 0xffd00000;
/* 16KB stack */
STACK_SIZE = 16384;
-/* 256k general alloc + 256k Forth dictionary + 16k Forth memory */
-VMEM_SIZE = (256 + 256 + 16) * 1024;
+VMEM_SIZE = 128 * 1024;
IOMEM_SIZE = 256 * 1024 + 768 * 1024;
SECTIONS
diff --git a/openbios-devel/arch/sparc32/lib.c b/openbios-devel/arch/sparc32/lib.c
index a5ad8ca..010b7b6 100644
--- a/openbios-devel/arch/sparc32/lib.c
+++ b/openbios-devel/arch/sparc32/lib.c
@@ -75,18 +75,6 @@ struct linux_mlist_v0 *ptphys;
struct linux_mlist_v0 *ptmap;
struct linux_mlist_v0 *ptavail;
-static struct {
- char *next_malloc;
- int left;
- alloc_desc_t *mfree; /* list of free malloc blocks */
-
- range_t *phys_range;
- range_t *virt_range;
-
- translation_t *trans; /* this is really a translation_t */
-} ofmem;
-#define ALLOC_BLOCK (64 * 1024)
-
/* Private functions for mapping between physical/virtual addresses */
phys_addr_t
va2pa(unsigned long va)
@@ -108,112 +96,22 @@ pa2va(phys_addr_t pa)
return pa;
}
-// XXX should be posix_memalign
-static int
-posix_memalign2(void **memptr, size_t alignment, size_t size)
-{
- alloc_desc_t *d, **pp;
- char *ret;
-
- if( !size )
- return -1;
-
- size = (size + (alignment - 1)) & ~(alignment - 1);
- size += sizeof(alloc_desc_t);
-
- /* look in the freelist */
- for( pp=&ofmem.mfree; *pp && (**pp).size < size; pp = &(**pp).next )
- ;
-
- /* waste at most 4K by taking an entry from the freelist */
- if( *pp && (**pp).size < size + 0x1000 ) {
- ret = (char*)*pp + sizeof(alloc_desc_t);
- memset( ret, 0, (**pp).size - sizeof(alloc_desc_t) );
- *pp = (**pp).next;
- *memptr = ret;
- return 0;
- }
-
- if( !ofmem.next_malloc || ofmem.left < size) {
- unsigned long alloc_size = ALLOC_BLOCK;
- if (size > ALLOC_BLOCK)
- alloc_size = size;
- // Recover possible leftover
- if ((size_t)ofmem.left > sizeof(alloc_desc_t) + 4) {
- alloc_desc_t *d_leftover;
-
- d_leftover = (alloc_desc_t*)ofmem.next_malloc;
- d_leftover->size = ofmem.left - sizeof(alloc_desc_t);
- free((void *)((unsigned long)d_leftover +
- sizeof(alloc_desc_t)));
- }
-
- ofmem.next_malloc = mem_alloc(&cmem, alloc_size, 8);
- ofmem.left = alloc_size;
- }
-
- if( ofmem.left < size) {
- printk("out of malloc memory (%x)!\n", size );
- return -1;
- }
- d = (alloc_desc_t*) ofmem.next_malloc;
- ofmem.next_malloc += size;
- ofmem.left -= size;
-
- d->next = NULL;
- d->size = size;
-
- ret = (char*)d + sizeof(alloc_desc_t);
- memset( ret, 0, size - sizeof(alloc_desc_t) );
- *memptr = ret;
- return 0;
-}
-
-void *malloc(int size)
-{
- int ret;
- void *mem;
-
- ret = posix_memalign2(&mem, 8, size);
- if (ret != 0)
- return NULL;
- return mem;
-}
-
-void free(void *ptr)
+void *
+malloc(int size)
{
- alloc_desc_t **pp, *d;
-
- /* it is legal to free NULL pointers (size zero allocations) */
- if( !ptr )
- return;
-
- d = (alloc_desc_t*)((unsigned long)ptr - sizeof(alloc_desc_t));
- d->next = ofmem.mfree;
-
- /* insert in the (sorted) freelist */
- for( pp=&ofmem.mfree; *pp && (**pp).size < d->size ; pp = &(**pp).next )
- ;
- d->next = *pp;
- *pp = d;
+ return ofmem_malloc(size);
}
-
+
void *
realloc( void *ptr, size_t size )
{
- alloc_desc_t *d = (alloc_desc_t*)((unsigned long)ptr - sizeof(alloc_desc_t));
- char *p;
-
- if( !ptr )
- return malloc( size );
- if( !size ) {
- free( ptr );
- return NULL;
- }
- p = malloc( size );
- memcpy( p, ptr, MIN(d->size - sizeof(alloc_desc_t),size) );
- free( ptr );
- return p;
+ return ofmem_realloc(ptr, size);
+}
+
+void
+free(void *ptr)
+{
+ ofmem_free(ptr);
}
/*
diff --git a/openbios-devel/arch/sparc32/ofmem_sparc32.c b/openbios-devel/arch/sparc32/ofmem_sparc32.c
index 6484be9..8c21f9b 100644
--- a/openbios-devel/arch/sparc32/ofmem_sparc32.c
+++ b/openbios-devel/arch/sparc32/ofmem_sparc32.c
@@ -21,7 +21,7 @@
#define OF_MALLOC_BASE ((char*)OFMEM + ALIGN_SIZE(sizeof(ofmem_t), 8))
-#define MEMSIZE (256 * 1024)
+#define MEMSIZE (512 * 1024)
static union {
char memory[MEMSIZE];
ofmem_t ofmem;
--
1.7.2.3
More information about the OpenBIOS
mailing list