[SeaBIOS] [PATCH 3/3] Use linked list operations in pmm.c and stack.c

Alexey Korolev alexey.korolev at endace.com
Fri Mar 9 07:56:21 CET 2012


Now let simplify a bit a cumbersome code in pmm.c and stack.c


Signed-off-by: Alexey Korolev <alexey.korolev at endace.com>
---
 src/pmm.c    |   29 +++++++++--------------------
 src/stacks.c |    8 ++------
 2 files changed, 11 insertions(+), 26 deletions(-)

diff --git a/src/pmm.c b/src/pmm.c
index c649fd8..996981c 100644
--- a/src/pmm.c
+++ b/src/pmm.c
@@ -44,24 +44,20 @@ static void *
 allocSpace(struct zone_s *zone, u32 size, u32 align, struct allocinfo_s *fill)
 {
     struct allocinfo_s *info;
-    for (info = zone->info; info; info = info->next) {
+    list_foreach_entry(zone->info, info) {
         void *dataend = info->dataend;
         void *allocend = info->allocend;
         void *newallocend = (void*)ALIGN_DOWN((u32)allocend - size, align);
         if (newallocend >= dataend && newallocend <= allocend) {
             // Found space - now reserve it.
-            struct allocinfo_s **pprev = info->pprev;
             if (!fill)
                 fill = newallocend;
-            fill->next = info;
-            fill->pprev = pprev;
+            list_add_before(info, fill);
             fill->data = newallocend;
             fill->dataend = newallocend + size;
             fill->allocend = allocend;
 
             info->allocend = newallocend;
-            info->pprev = &fill->next;
-            *pprev = fill;
             return newallocend;
         }
     }
@@ -73,13 +69,9 @@ static void
 freeSpace(struct allocinfo_s *info)
 {
     struct allocinfo_s *next = info->next;
-    struct allocinfo_s **pprev = info->pprev;
-    *pprev = next;
-    if (next) {
-        if (next->allocend == info->data)
+    if (next && (next->allocend == info->data))
             next->allocend = info->allocend;
-        next->pprev = pprev;
-    }
+    list_del(info);
 }
 
 // Add new memory to a zone
@@ -97,13 +89,12 @@ addSpace(struct zone_s *zone, void *start, void *end)
 
     // Add space using temporary allocation info.
     struct allocdetail_s tempdetail;
-    tempdetail.datainfo.next = info;
-    tempdetail.datainfo.pprev = pprev;
     tempdetail.datainfo.data = tempdetail.datainfo.dataend = start;
     tempdetail.datainfo.allocend = end;
-    *pprev = &tempdetail.datainfo;
-    if (info)
-        info->pprev = &tempdetail.datainfo.next;
+
+    tempdetail.datainfo.data = tempdetail.datainfo.dataend = start;
+    tempdetail.datainfo.allocend = end;
+    list_add_head(pprev, &tempdetail.datainfo);
 
     // Allocate final allocation info.
     struct allocdetail_s *detail = allocSpace(
@@ -112,9 +103,7 @@ addSpace(struct zone_s *zone, void *start, void *end)
         detail = allocSpace(&ZoneTmpLow, sizeof(*detail)
                             , MALLOC_MIN_ALIGN, NULL);
         if (!detail) {
-            *tempdetail.datainfo.pprev = tempdetail.datainfo.next;
-            if (tempdetail.datainfo.next)
-                tempdetail.datainfo.next->pprev = tempdetail.datainfo.pprev;
+            list_del(&tempdetail.datainfo);
             warn_noalloc();
             return;
         }
diff --git a/src/stacks.c b/src/stacks.c
index 17f1a4a..2b10188 100644
--- a/src/stacks.c
+++ b/src/stacks.c
@@ -240,8 +240,7 @@ yield(void)
 static void
 __end_thread(struct thread_info *old)
 {
-    old->next->pprev = old->pprev;
-    *old->pprev = old->next;
+    list_del(old);
     free(old);
     dprintf(DEBUG_thread, "\\%08x/ End thread\n", (u32)old);
     if (MainThread.next == &MainThread)
@@ -262,10 +261,7 @@ run_thread(void (*func)(void*), void *data)
 
     thread->stackpos = (void*)thread + THREADSTACKSIZE;
     struct thread_info *cur = getCurThread();
-    thread->next = cur;
-    thread->pprev = cur->pprev;
-    cur->pprev = &thread->next;
-    *thread->pprev = thread;
+    list_add_before(cur, thread);
 
     dprintf(DEBUG_thread, "/%08x\\ Start thread\n", (u32)thread);
     asm volatile(
-- 
1.7.5.4






More information about the SeaBIOS mailing list