]> xenbits.xensource.com Git - seabios.git/commitdiff
Remove the pmm handle argument from _malloc().
authorKevin O'Connor <kevin@koconnor.net>
Sat, 14 Dec 2013 18:14:02 +0000 (13:14 -0500)
committerKevin O'Connor <kevin@koconnor.net>
Fri, 27 Dec 2013 17:40:02 +0000 (12:40 -0500)
The PMM handle argument will almost always be 0xffffffff.  Use
separate code for the few rare cases where it may not be the default
value.  Gcc produces better code if _malloc() only requires three
parameters.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
src/fw/csm.c
src/fw/romfile_loader.c
src/malloc.c
src/malloc.h
src/pmm.c

index dfb0d12f28a9d3f6fe0563945f98ab85ee3b55d0..a44ed267c4208ec5e8e3630b271d8772e3154c03 100644 (file)
@@ -257,9 +257,9 @@ handle_csm_0006(struct bregs *regs)
         size, align, region);
 
     if (region & 2)
-        chunk = _malloc(&ZoneLow, MALLOC_DEFAULT_HANDLE, size, align);
+        chunk = _malloc(&ZoneLow, size, align);
     if (!chunk && (region & 1))
-        chunk = _malloc(&ZoneFSeg, MALLOC_DEFAULT_HANDLE, size, align);
+        chunk = _malloc(&ZoneFSeg, size, align);
 
     dprintf(3, "Legacy16GetTableAddress size %x align %x region %d yields %p\n",
         size, align, region, chunk);
index 325a0fa3a04957be7a9cff2db403b5d94b25bfc0..f4b17ff90e4b20d2046c4cf0daf00e83b37d4379 100644 (file)
@@ -57,7 +57,7 @@ static void romfile_loader_allocate(struct romfile_loader_entry_s *entry,
     file->file = romfile_find(entry->alloc_file);
     if (!file->file || !file->file->size)
         return;
-    data = _malloc(zone, MALLOC_DEFAULT_HANDLE, file->file->size, alloc_align);
+    data = _malloc(zone, file->file->size, alloc_align);
     if (!data) {
         warn_noalloc();
         return;
index 6ef418a4d65119257a56c1b5638d7d1a7955d8a2..c4cb17149e8e642e95c6bc1dd13523a418af93b8 100644 (file)
@@ -225,7 +225,7 @@ zonelow_expand(u32 size, u32 align, struct allocinfo_s *fill)
 
 // Allocate memory from the given zone and track it as a PMM allocation
 void * __malloc
-_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
+_malloc(struct zone_s *zone, u32 size, u32 align)
 {
     ASSERT32FLAT();
     if (!size)
@@ -240,6 +240,7 @@ _malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
         if (!detail)
             return NULL;
     }
+    detail->handle = MALLOC_DEFAULT_HANDLE;
 
     // Find and reserve space for main allocation
     void *data = allocSpace(zone, size, align, &detail->datainfo);
@@ -250,9 +251,8 @@ _malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
         return NULL;
     }
 
-    dprintf(8, "_malloc zone=%p handle=%x size=%d align=%x ret=%p (detail=%p)\n"
-            , zone, handle, size, align, data, detail);
-    detail->handle = handle;
+    dprintf(8, "_malloc zone=%p size=%d align=%x ret=%p (detail=%p)\n"
+            , zone, size, align, data, detail);
 
     return data;
 }
@@ -296,9 +296,22 @@ malloc_getspace(struct zone_s *zone)
     return maxspace - reserve;
 }
 
+// Set a handle associated with an allocation.
+void
+malloc_sethandle(void *data, u32 handle)
+{
+    ASSERT32FLAT();
+    struct allocinfo_s *info = findAlloc(data);
+    if (!info || data == (void*)info || data == info->dataend)
+        return;
+    struct allocdetail_s *detail = container_of(
+        info, struct allocdetail_s, datainfo);
+    detail->handle = handle;
+}
+
 // Find the data block allocated with _malloc with a given handle.
 void *
-malloc_find(u32 handle)
+malloc_findhandle(u32 handle)
 {
     int i;
     for (i=0; i<ARRAY_SIZE(Zones); i++) {
index af8a21daf487376c70b44cf445164c4f1c679849..2bcb5bf6dcf37d20c668ca0a773350342dd8e609 100644 (file)
@@ -15,29 +15,30 @@ void malloc_preinit(void);
 extern u32 LegacyRamSize;
 void malloc_init(void);
 void malloc_prepboot(void);
-void *_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align);
+void *_malloc(struct zone_s *zone, u32 size, u32 align);
 int _free(void *data);
 u32 malloc_getspace(struct zone_s *zone);
-void *malloc_find(u32 handle);
+void malloc_sethandle(void *data, u32 handle);
+void *malloc_findhandle(u32 handle);
 
 #define MALLOC_DEFAULT_HANDLE 0xFFFFFFFF
 // Minimum alignment of malloc'd memory
 #define MALLOC_MIN_ALIGN 16
 // Helper functions for memory allocation.
 static inline void *malloc_low(u32 size) {
-    return _malloc(&ZoneLow, MALLOC_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
+    return _malloc(&ZoneLow, size, MALLOC_MIN_ALIGN);
 }
 static inline void *malloc_high(u32 size) {
-    return _malloc(&ZoneHigh, MALLOC_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
+    return _malloc(&ZoneHigh, size, MALLOC_MIN_ALIGN);
 }
 static inline void *malloc_fseg(u32 size) {
-    return _malloc(&ZoneFSeg, MALLOC_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
+    return _malloc(&ZoneFSeg, size, MALLOC_MIN_ALIGN);
 }
 static inline void *malloc_tmplow(u32 size) {
-    return _malloc(&ZoneTmpLow, MALLOC_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
+    return _malloc(&ZoneTmpLow, size, MALLOC_MIN_ALIGN);
 }
 static inline void *malloc_tmphigh(u32 size) {
-    return _malloc(&ZoneTmpHigh, MALLOC_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
+    return _malloc(&ZoneTmpHigh, size, MALLOC_MIN_ALIGN);
 }
 static inline void *malloc_tmp(u32 size) {
     void *ret = malloc_tmphigh(size);
@@ -46,16 +47,16 @@ static inline void *malloc_tmp(u32 size) {
     return malloc_tmplow(size);
 }
 static inline void *memalign_low(u32 align, u32 size) {
-    return _malloc(&ZoneLow, MALLOC_DEFAULT_HANDLE, size, align);
+    return _malloc(&ZoneLow, size, align);
 }
 static inline void *memalign_high(u32 align, u32 size) {
-    return _malloc(&ZoneHigh, MALLOC_DEFAULT_HANDLE, size, align);
+    return _malloc(&ZoneHigh, size, align);
 }
 static inline void *memalign_tmplow(u32 align, u32 size) {
-    return _malloc(&ZoneTmpLow, MALLOC_DEFAULT_HANDLE, size, align);
+    return _malloc(&ZoneTmpLow, size, align);
 }
 static inline void *memalign_tmphigh(u32 align, u32 size) {
-    return _malloc(&ZoneTmpHigh, MALLOC_DEFAULT_HANDLE, size, align);
+    return _malloc(&ZoneTmpHigh, size, align);
 }
 static inline void *memalign_tmp(u32 align, u32 size) {
     void *ret = memalign_tmphigh(align, size);
index be03bdb42486e1afe66e1cbb5eac076860606813..304faab2c5d371905da639319d3a5cca71af36d3 100644 (file)
--- a/src/pmm.c
+++ b/src/pmm.c
@@ -65,21 +65,26 @@ handle_pmm00(u16 *args)
         if (align < MALLOC_MIN_ALIGN)
             align = MALLOC_MIN_ALIGN;
     }
+    void *data;
     switch (flags & 3) {
     default:
     case 0:
         return 0;
     case 1:
-        return (u32)_malloc(lowzone, handle, size, align);
+        data = _malloc(lowzone, size, align);
+        break;
     case 2:
-        return (u32)_malloc(highzone, handle, size, align);
+        data = _malloc(highzone, size, align);
+        break;
     case 3: {
-        void *data = _malloc(lowzone, handle, size, align);
-        if (data)
-            return (u32)data;
-        return (u32)_malloc(highzone, handle, size, align);
+        data = _malloc(lowzone, size, align);
+        if (!data)
+            data = _malloc(highzone, size, align);
     }
     }
+    if (data && handle != MALLOC_DEFAULT_HANDLE)
+        malloc_sethandle(data, handle);
+    return (u32)data;
 }
 
 // PMM - find
@@ -90,7 +95,7 @@ handle_pmm01(u16 *args)
     dprintf(3, "pmm01: handle=%x\n", handle);
     if (handle == MALLOC_DEFAULT_HANDLE)
         return 0;
-    return (u32)malloc_find(handle);
+    return (u32)malloc_findhandle(handle);
 }
 
 // PMM - deallocate