Clean up callback_list, remove sentinel/active/tail, callbacks return bool for removal, register before send
This commit is contained in:
@@ -1,87 +1,80 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <new>
|
||||
#include <utility>
|
||||
|
||||
template <typename T, int N>
|
||||
struct callback_list {
|
||||
struct node {
|
||||
alignas(T) uint8_t storage[sizeof(T)];
|
||||
T value;
|
||||
node* prev = nullptr;
|
||||
node* next = nullptr;
|
||||
bool active = false;
|
||||
|
||||
T& value() { return *reinterpret_cast<T*>(storage); }
|
||||
const T& value() const { return *reinterpret_cast<const T*>(storage); }
|
||||
};
|
||||
|
||||
node nodes[N];
|
||||
node* free_head = &nodes[0];
|
||||
node sentinel;
|
||||
node* head = nullptr;
|
||||
|
||||
callback_list() {
|
||||
for (int i = 0; i < N - 1; i++) nodes[i].next = &nodes[i + 1];
|
||||
nodes[N - 1].next = nullptr;
|
||||
sentinel.prev = &sentinel;
|
||||
sentinel.next = &sentinel;
|
||||
}
|
||||
|
||||
bool empty() const { return sentinel.next == &sentinel; }
|
||||
bool empty() const { return head == nullptr; }
|
||||
|
||||
node* insert(T value) {
|
||||
if (!free_head) return nullptr;
|
||||
node* n = alloc(std::move(value));
|
||||
link_before(&sentinel, n);
|
||||
node* n = free_head;
|
||||
free_head = n->next;
|
||||
n->value = std::move(value);
|
||||
n->prev = nullptr;
|
||||
n->next = head;
|
||||
if (head) head->prev = n;
|
||||
head = n;
|
||||
return n;
|
||||
}
|
||||
|
||||
template <typename Less>
|
||||
node* insert_sorted(T value, Less&& less) {
|
||||
if (!free_head) return nullptr;
|
||||
node* n = alloc(std::move(value));
|
||||
node* pos = sentinel.next;
|
||||
while (pos != &sentinel && !less(n->value(), pos->value()))
|
||||
pos = pos->next;
|
||||
link_before(pos, n);
|
||||
node* n = free_head;
|
||||
free_head = n->next;
|
||||
n->value = std::move(value);
|
||||
if (!head || less(n->value, head->value)) {
|
||||
n->prev = nullptr;
|
||||
n->next = head;
|
||||
if (head) head->prev = n;
|
||||
head = n;
|
||||
return n;
|
||||
}
|
||||
node* cur = head;
|
||||
while (cur->next && !less(n->value, cur->next->value))
|
||||
cur = cur->next;
|
||||
n->prev = cur;
|
||||
n->next = cur->next;
|
||||
if (cur->next) cur->next->prev = n;
|
||||
cur->next = n;
|
||||
return n;
|
||||
}
|
||||
|
||||
void remove(node* n) {
|
||||
if (!n || !n->active) return;
|
||||
n->prev->next = n->next;
|
||||
n->next->prev = n->prev;
|
||||
n->active = false;
|
||||
n->value().~T();
|
||||
if (!n) return;
|
||||
if (n->prev) n->prev->next = n->next;
|
||||
else head = n->next;
|
||||
if (n->next) n->next->prev = n->prev;
|
||||
n->value = T{};
|
||||
n->next = free_head;
|
||||
n->prev = nullptr;
|
||||
free_head = n;
|
||||
}
|
||||
|
||||
node* front() { return sentinel.next != &sentinel ? sentinel.next : nullptr; }
|
||||
node* front() { return head; }
|
||||
|
||||
template <typename Fn>
|
||||
void for_each(Fn&& fn) {
|
||||
node* cur = sentinel.next;
|
||||
while (cur != &sentinel) {
|
||||
node* cur = head;
|
||||
while (cur) {
|
||||
node* next = cur->next;
|
||||
fn(cur);
|
||||
cur = next;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
node* alloc(T value) {
|
||||
node* n = free_head;
|
||||
free_head = n->next;
|
||||
new (n->storage) T(std::move(value));
|
||||
n->active = true;
|
||||
return n;
|
||||
}
|
||||
|
||||
void link_before(node* pos, node* n) {
|
||||
n->prev = pos->prev;
|
||||
n->next = pos;
|
||||
pos->prev->next = n;
|
||||
pos->prev = n;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -18,11 +18,7 @@ using net_handler = std::function<void(std::span<const uint8_t> payload,
|
||||
|
||||
using net_frame_callback = std::function<bool(std::span<const uint8_t> frame)>;
|
||||
|
||||
struct frame_callback_entry {
|
||||
net_frame_callback fn;
|
||||
};
|
||||
|
||||
using frame_cb_list = callback_list<frame_callback_entry, 16>;
|
||||
using frame_cb_list = callback_list<net_frame_callback, 16>;
|
||||
using frame_cb_handle = frame_cb_list::node*;
|
||||
|
||||
bool net_init();
|
||||
|
||||
@@ -29,7 +29,7 @@ struct timer_queue {
|
||||
}
|
||||
|
||||
bool cancel(timer_handle h) {
|
||||
if (!h || !h->active) return false;
|
||||
if (!h) return false;
|
||||
list.remove(h);
|
||||
arm();
|
||||
return true;
|
||||
@@ -39,8 +39,8 @@ struct timer_queue {
|
||||
if (!irq_pending) return;
|
||||
irq_pending = false;
|
||||
while (auto* n = list.front()) {
|
||||
if (absolute_time_diff_us(get_absolute_time(), n->value().when) > 0) break;
|
||||
auto fn = std::move(n->value().fn);
|
||||
if (absolute_time_diff_us(get_absolute_time(), n->value.when) > 0) break;
|
||||
auto fn = std::move(n->value.fn);
|
||||
list.remove(n);
|
||||
fn();
|
||||
}
|
||||
@@ -59,6 +59,6 @@ private:
|
||||
if (alarm >= 0) cancel_alarm(alarm);
|
||||
alarm = -1;
|
||||
if (auto* n = list.front())
|
||||
alarm = add_alarm_at(n->value().when, alarm_cb, this, false);
|
||||
alarm = add_alarm_at(n->value.when, alarm_cb, this, false);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user