[glacier] Move LinkedList to glacier.

This commit is contained in:
Drew Galbraith 2023-06-26 15:01:55 -07:00
parent 08abe776a4
commit 64d355b20d
15 changed files with 28 additions and 46 deletions

View file

@ -1,107 +0,0 @@
#pragma once
#include <stdint.h>
#include "debug/debug.h"
template <typename T>
class LinkedList {
public:
LinkedList() {}
LinkedList(const LinkedList&) = delete;
bool empty() const { return size_ == 0; }
uint64_t size() const { return size_; }
void PushBack(const T& item) {
size_++;
ListItem* new_item = new ListItem{
.item = item,
.next = nullptr,
};
if (front_ == nullptr) {
front_ = new_item;
return;
}
ListItem* litem = front_;
while (litem->next != nullptr) {
litem = litem->next;
}
litem->next = new_item;
}
T PopFront() {
if (size_ == 0 || front_ == nullptr) {
panic("Popping from empty list");
}
size_--;
ListItem* old_front = front_;
front_ = front_->next;
T ret = old_front->item;
delete old_front;
return ret;
}
/*
* Returns the front item in the list and pushes the passed item to the back.
*
* Done in one function to avoid a memory alloc/dealloc during scheduling.
**/
T CycleFront(const T& new_item) {
if (size_ == 0 || front_ == nullptr) {
panic("Cycling empty list");
}
T ret = front_->item;
front_->item = new_item;
if (size_ == 1) {
return ret;
}
ListItem* old_front = front_;
ListItem* iter = front_;
front_ = front_->next;
while (iter->next != nullptr) {
iter = iter->next;
}
iter->next = old_front;
old_front->next = nullptr;
return ret;
}
T PeekFront() const { return front_->item; }
struct ListItem {
T item;
ListItem* next;
};
class Iterator {
public:
Iterator(ListItem* item) : item_(item) {}
Iterator next() { return {item_->next}; }
Iterator& operator++() {
item_ = item_->next;
return *this;
}
T& operator*() { return item_->item; }
T* operator->() { return &item_->item; }
bool operator==(const Iterator& other) { return item_ == other.item_; }
bool operator!=(const Iterator& other) { return item_ != other.item_; }
private:
ListItem* item_;
};
Iterator begin() { return {front_}; }
Iterator end() { return {nullptr}; }
private:
uint64_t size_ = 0;
ListItem* front_ = nullptr;
};

View file

@ -1,5 +1,6 @@
#include "lib/message_queue.h"
#include "debug/debug.h"
#include "scheduler/scheduler.h"
z_err_t UnboundedMessageQueue::PushBack(uint64_t num_bytes, const void* bytes,

View file

@ -1,13 +1,13 @@
#pragma once
#include <glacier/container/intrusive_list.h>
#include <glacier/container/linked_list.h>
#include <glacier/memory/ref_ptr.h>
#include <glacier/memory/shared_ptr.h>
#include <glacier/status/error.h>
#include "capability/capability.h"
#include "include/ztypes.h"
#include "lib/linked_list.h"
#include "lib/mutex.h"
class MessageQueue {
@ -51,10 +51,10 @@ class UnboundedMessageQueue : public MessageQueue {
uint64_t num_bytes;
uint8_t* bytes;
LinkedList<glcr::RefPtr<Capability>> caps;
glcr::LinkedList<glcr::RefPtr<Capability>> caps;
};
LinkedList<glcr::SharedPtr<Message>> pending_messages_;
glcr::LinkedList<glcr::SharedPtr<Message>> pending_messages_;
};
class SingleMessageQueue : public MessageQueue {
@ -79,5 +79,5 @@ class SingleMessageQueue : public MessageQueue {
bool has_read_ = false;
uint64_t num_bytes_;
uint8_t* bytes_;
LinkedList<glcr::RefPtr<Capability>> caps_;
glcr::LinkedList<glcr::RefPtr<Capability>> caps_;
};