Przeglądaj źródła

Kernel: Use plain Function objects for the WorkQueue

The WorkQueue class previously had its own inline storage functionality
for function pointers. With the recent changes to the Function class
this is no longer necessary.
Gunnar Beutner 4 lat temu
rodzic
commit
e9898a6031
2 zmienionych plików z 8 dodań i 25 usunięć
  1. 1 3
      Kernel/WorkQueue.cpp
  2. 7 22
      Kernel/WorkQueue.h

+ 1 - 3
Kernel/WorkQueue.cpp

@@ -31,9 +31,7 @@ WorkQueue::WorkQueue(const char* name)
                 have_more = !m_items.is_empty();
             }
             if (item) {
-                item->function(item->data);
-                if (item->free_data)
-                    item->free_data(item->data);
+                item->function();
                 delete item;
 
                 if (have_more)

+ 7 - 22
Kernel/WorkQueue.h

@@ -25,9 +25,11 @@ public:
     void queue(void (*function)(void*), void* data = nullptr, void (*free_data)(void*) = nullptr)
     {
         auto* item = new WorkItem; // TODO: use a pool
-        item->function = function;
-        item->data = data;
-        item->free_data = free_data;
+        item->function = [function, data, free_data] {
+            function(data);
+            if (free_data)
+                free_data(data);
+        };
         do_queue(item);
     }
 
@@ -35,31 +37,14 @@ public:
     void queue(Function function)
     {
         auto* item = new WorkItem; // TODO: use a pool
-        item->function = [](void* f) {
-            (*reinterpret_cast<Function*>(f))();
-        };
-        if constexpr (sizeof(Function) <= sizeof(item->inline_data)) {
-            item->data = new (item->inline_data) Function(move(function));
-            item->free_data = [](void* f) {
-                reinterpret_cast<Function*>(f)->~Function();
-            };
-
-        } else {
-            item->data = new Function(move(function));
-            item->free_data = [](void* f) {
-                delete reinterpret_cast<Function*>(f);
-            };
-        }
+        item->function = Function(function);
         do_queue(item);
     }
 
 private:
     struct WorkItem {
         IntrusiveListNode<WorkItem> m_node;
-        void (*function)(void*);
-        void* data;
-        void (*free_data)(void*);
-        u8 inline_data[4 * sizeof(void*)];
+        Function<void()> function;
     };
 
     void do_queue(WorkItem*);