瀏覽代碼

system: system/BReactor_badvpn: port to SAvl

ambrop7 13 年之前
父節點
當前提交
2a6d9a9c25
共有 3 個文件被更改,包括 31 次插入46 次删除
  1. 19 22
      system/BReactor_badvpn.c
  2. 5 11
      system/BReactor_badvpn.h
  3. 7 13
      system/BReactor_badvpn_timerstree.h

+ 19 - 22
system/BReactor_badvpn.c

@@ -65,16 +65,15 @@ static int compare_timers (BTimer *t1, BTimer *t2)
 }
 }
 
 
 #include "BReactor_badvpn_timerstree.h"
 #include "BReactor_badvpn_timerstree.h"
-#include <structure/CAvl_impl.h>
+#include <structure/SAvl_impl.h>
 
 
 static int move_expired_timers (BReactor *bsys, btime_t now)
 static int move_expired_timers (BReactor *bsys, btime_t now)
 {
 {
     int moved = 0;
     int moved = 0;
     
     
     // move timed out timers to the expired list
     // move timed out timers to the expired list
-    BReactor__TimersTreeRef ref;
-    while (BReactor__TimersTreeIsValidRef(ref = BReactor__TimersTree_GetFirst(&bsys->timers_tree, 0))) {
-        BTimer *timer = ref.ptr;
+    BTimer *timer;
+    while (timer = BReactor__TimersTree_GetFirst(&bsys->timers_tree, 0)) {
         ASSERT(timer->active)
         ASSERT(timer->active)
         
         
         // if it's in the future, stop
         // if it's in the future, stop
@@ -83,8 +82,8 @@ static int move_expired_timers (BReactor *bsys, btime_t now)
         }
         }
         moved = 1;
         moved = 1;
         
         
-        // remove from running timers heap
-        BReactor__TimersTree_Remove(&bsys->timers_tree, 0, BReactor__TimersTreeDeref(0, timer));
+        // remove from running timers tree
+        BReactor__TimersTree_Remove(&bsys->timers_tree, 0, timer);
         
         
         // add to expired timers list
         // add to expired timers list
         LinkedList1_Append(&bsys->timers_expired_list, &timer->list_node);
         LinkedList1_Append(&bsys->timers_expired_list, &timer->list_node);
@@ -99,14 +98,13 @@ static int move_expired_timers (BReactor *bsys, btime_t now)
 static void move_first_timers (BReactor *bsys)
 static void move_first_timers (BReactor *bsys)
 {
 {
     // get the time of the first timer
     // get the time of the first timer
-    BReactor__TimersTreeRef ref = BReactor__TimersTree_GetFirst(&bsys->timers_tree, 0);
-    ASSERT(BReactor__TimersTreeIsValidRef(ref))
-    BTimer *first_timer = ref.ptr;
+    BTimer *first_timer = BReactor__TimersTree_GetFirst(&bsys->timers_tree, 0);
+    ASSERT(first_timer)
     ASSERT(first_timer->active)
     ASSERT(first_timer->active)
     btime_t first_time = first_timer->absTime;
     btime_t first_time = first_timer->absTime;
     
     
-    // remove from running timers heap
-    BReactor__TimersTree_Remove(&bsys->timers_tree, 0, BReactor__TimersTreeDeref(0, first_timer));
+    // remove from running timers tree
+    BReactor__TimersTree_Remove(&bsys->timers_tree, 0, first_timer);
     
     
     // add to expired timers list
     // add to expired timers list
     LinkedList1_Append(&bsys->timers_expired_list, &first_timer->list_node);
     LinkedList1_Append(&bsys->timers_expired_list, &first_timer->list_node);
@@ -115,8 +113,8 @@ static void move_first_timers (BReactor *bsys)
     first_timer->expired = 1;
     first_timer->expired = 1;
     
     
     // also move other timers with the same timeout
     // also move other timers with the same timeout
-    while (BReactor__TimersTreeIsValidRef(ref = BReactor__TimersTree_GetFirst(&bsys->timers_tree, 0))) {
-        BTimer *timer = ref.ptr;
+    BTimer *timer;
+    while (timer = BReactor__TimersTree_GetFirst(&bsys->timers_tree, 0)) {
         ASSERT(timer->active)
         ASSERT(timer->active)
         ASSERT(timer->absTime >= first_time)
         ASSERT(timer->absTime >= first_time)
         
         
@@ -125,8 +123,8 @@ static void move_first_timers (BReactor *bsys)
             break;
             break;
         }
         }
         
         
-        // remove from running timers heap
-        BReactor__TimersTree_Remove(&bsys->timers_tree, 0, BReactor__TimersTreeDeref(0, timer));
+        // remove from running timers tree
+        BReactor__TimersTree_Remove(&bsys->timers_tree, 0, timer);
         
         
         // add to expired timers list
         // add to expired timers list
         LinkedList1_Append(&bsys->timers_expired_list, &timer->list_node);
         LinkedList1_Append(&bsys->timers_expired_list, &timer->list_node);
@@ -300,8 +298,8 @@ static void wait_for_events (BReactor *bsys)
     btime_t now;
     btime_t now;
     
     
     // compute timeout
     // compute timeout
-    BReactor__TimersTreeRef ref = BReactor__TimersTree_GetFirst(&bsys->timers_tree, 0);
-    if (BReactor__TimersTreeIsValidRef(ref)) {
+    BTimer *first_timer = BReactor__TimersTree_GetFirst(&bsys->timers_tree, 0);
+    if (first_timer) {
         // get current time
         // get current time
         now = btime_gettime();
         now = btime_gettime();
         
         
@@ -312,7 +310,6 @@ static void wait_for_events (BReactor *bsys)
         }
         }
         
         
         // timeout is first timer, remember absolute time
         // timeout is first timer, remember absolute time
-        BTimer *first_timer = ref.ptr;
         have_timeout = 1;
         have_timeout = 1;
         timeout_abs = first_timer->absTime;
         timeout_abs = first_timer->absTime;
     }
     }
@@ -981,8 +978,8 @@ void BReactor_SetTimerAbsolute (BReactor *bsys, BTimer *bt, btime_t time)
     bt->expired = 0;
     bt->expired = 0;
     bt->absTime = time;
     bt->absTime = time;
 
 
-    // insert to running timers heap
-    int res = BReactor__TimersTree_Insert(&bsys->timers_tree, 0, BReactor__TimersTreeDeref(0, bt), NULL);
+    // insert to running timers tree
+    int res = BReactor__TimersTree_Insert(&bsys->timers_tree, 0, bt, NULL);
     ASSERT(res)
     ASSERT(res)
 }
 }
 
 
@@ -996,8 +993,8 @@ void BReactor_RemoveTimer (BReactor *bsys, BTimer *bt)
         // remove from expired list
         // remove from expired list
         LinkedList1_Remove(&bsys->timers_expired_list, &bt->list_node);
         LinkedList1_Remove(&bsys->timers_expired_list, &bt->list_node);
     } else {
     } else {
-        // remove from running heap
-        BReactor__TimersTree_Remove(&bsys->timers_tree, 0, BReactor__TimersTreeDeref(0, bt));
+        // remove from running tree
+        BReactor__TimersTree_Remove(&bsys->timers_tree, 0, bt);
     }
     }
 
 
     // set inactive
     // set inactive

+ 5 - 11
system/BReactor_badvpn.h

@@ -63,12 +63,15 @@
 #include <misc/debugcounter.h>
 #include <misc/debugcounter.h>
 #include <base/DebugObject.h>
 #include <base/DebugObject.h>
 #include <structure/LinkedList1.h>
 #include <structure/LinkedList1.h>
-#include <structure/CAvl.h>
+#include <structure/SAvl.h>
 #include <system/BTime.h>
 #include <system/BTime.h>
 #include <base/BPending.h>
 #include <base/BPending.h>
 
 
 struct BTimer_t;
 struct BTimer_t;
 
 
+#include "BReactor_badvpn_timerstree.h"
+#include <structure/SAvl_decl.h>
+
 /**
 /**
  * Handler function invoked when the timer expires.
  * Handler function invoked when the timer expires.
  * The timer was in running state.
  * The timer was in running state.
@@ -92,11 +95,7 @@ typedef struct BTimer_t {
     uint8_t expired;
     uint8_t expired;
     btime_t absTime;
     btime_t absTime;
     union {
     union {
-        struct {
-            struct BTimer_t *tree_child[2];
-            struct BTimer_t *tree_parent;
-            int8_t tree_balance;
-        };
+        BReactor__TimersTreeNode tree_node;
         LinkedList1Node list_node;
         LinkedList1Node list_node;
     };
     };
 } BTimer;
 } BTimer;
@@ -186,11 +185,6 @@ void BFileDescriptor_Init (BFileDescriptor *bs, int fd, BFileDescriptor_handler
 #define BSYSTEM_MAX_HANDLES 64
 #define BSYSTEM_MAX_HANDLES 64
 #define BSYSTEM_MAX_POLL_FDS 4096
 #define BSYSTEM_MAX_POLL_FDS 4096
 
 
-typedef BTimer *BReactor__TimersTree_link;
-
-#include "BReactor_badvpn_timerstree.h"
-#include <structure/CAvl_decl.h>
-
 /**
 /**
  * Event loop that supports file desciptor (Linux) or HANDLE (Windows) events
  * Event loop that supports file desciptor (Linux) or HANDLE (Windows) events
  * and timers.
  * and timers.

+ 7 - 13
system/BReactor_badvpn_timerstree.h

@@ -1,13 +1,7 @@
-#define CAVL_PARAM_NAME BReactor__TimersTree
-#define CAVL_PARAM_FEATURE_COUNTS 0
-#define CAVL_PARAM_FEATURE_KEYS_ARE_INDICES 0
-#define CAVL_PARAM_FEATURE_NOKEYS 1
-#define CAVL_PARAM_TYPE_ENTRY BTimer
-#define CAVL_PARAM_TYPE_LINK BReactor__TimersTree_link
-#define CAVL_PARAM_TYPE_ARG int
-#define CAVL_PARAM_VALUE_NULL ((BReactor__TimersTree_link)NULL)
-#define CAVL_PARAM_FUN_DEREF(arg, link) (link)
-#define CAVL_PARAM_FUN_COMPARE_ENTRIES(arg, entry1, entry2) compare_timers((entry1).ptr, (entry2).ptr)
-#define CAVL_PARAM_MEMBER_CHILD tree_child
-#define CAVL_PARAM_MEMBER_BALANCE tree_balance
-#define CAVL_PARAM_MEMBER_PARENT tree_parent
+#define SAVL_PARAM_NAME BReactor__TimersTree
+#define SAVL_PARAM_FEATURE_COUNTS 0
+#define SAVL_PARAM_FEATURE_NOKEYS 1
+#define SAVL_PARAM_TYPE_ENTRY struct BTimer_t
+#define SAVL_PARAM_TYPE_ARG int
+#define SAVL_PARAM_FUN_COMPARE_ENTRIES(arg, entry1, entry2) compare_timers((entry1), (entry2))
+#define SAVL_PARAM_MEMBER_NODE tree_node