Compare commits

...

2 Commits

Author SHA1 Message Date
Drew Galbraith 02e6b49d90 [glacier] Add a vector class 2023-06-26 15:46:03 -07:00
Drew Galbraith 64d355b20d [glacier] Move LinkedList to glacier. 2023-06-26 15:01:55 -07:00
18 changed files with 150 additions and 72 deletions

View File

@ -2,7 +2,7 @@
#include <stdint.h> #include <stdint.h>
#include "debug/debug.h" namespace glcr {
template <typename T> template <typename T>
class LinkedList { class LinkedList {
@ -32,10 +32,6 @@ class LinkedList {
} }
T PopFront() { T PopFront() {
if (size_ == 0 || front_ == nullptr) {
panic("Popping from empty list");
}
size_--; size_--;
ListItem* old_front = front_; ListItem* old_front = front_;
@ -45,33 +41,6 @@ class LinkedList {
return ret; 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; } T PeekFront() const { return front_->item; }
struct ListItem { struct ListItem {
@ -105,3 +74,5 @@ class LinkedList {
ListItem* front_ = nullptr; ListItem* front_ = nullptr;
}; };
} // namespace glcr

View File

@ -0,0 +1,110 @@
#pragma once
#include <glacier/memory/move.h>
#include <stdint.h>
namespace glcr {
template <typename T>
class Vector {
public:
Vector() : data_(nullptr), size_(0), capacity_(0) {}
Vector(const Vector&) = delete;
// TODO: Implement Move
Vector(Vector&&) = delete;
~Vector() { delete[] data_; }
// FIXME: Handle downsizing.
void Resize(uint64_t capacity);
// Setters.
void PushBack(const T& item);
void PushBack(T&& item);
template <typename... Args>
void EmplaceBack(Args... args);
// Accessors.
T& operator[](uint64_t index);
const T& operator[](uint64_t index) const;
T& at(uint64_t index);
const T& at(uint64_t index) const;
uint64_t size() const { return size_; }
uint64_t capacity() const { return capacity_; }
private:
T* data_;
uint64_t size_;
uint64_t capacity_;
void Expand();
};
template <typename T>
void Vector<T>::Resize(uint64_t capacity) {
T* new_data = reinterpret_cast<T*>(new uint8_t[capacity * sizeof(T)]);
for (uint64_t i = 0; i < size_; i++) {
new_data[i] = glcr::Move(data_[i]);
}
delete[] data_;
data_ = new_data;
capacity_ = capacity;
}
template <typename T>
void Vector<T>::PushBack(const T& item) {
if (size_ >= capacity_) {
Expand();
}
data_[size_++] = item;
}
template <typename T>
void Vector<T>::PushBack(T&& item) {
if (size_ >= capacity_) {
Expand();
}
data_[size_++] = glcr::Move(item);
}
template <typename T>
template <typename... Args>
void Vector<T>::EmplaceBack(Args... args) {
if (size_ >= capacity_) {
Expand();
}
data_[size_++] = T(args...);
}
template <typename T>
T& Vector<T>::operator[](uint64_t index) {
return data_[index];
}
template <typename T>
const T& Vector<T>::operator[](uint64_t index) const {
return data_[index];
}
template <typename T>
T& Vector<T>::at(uint64_t index) {
return data_[index];
}
template <typename T>
const T& Vector<T>::at(uint64_t index) const {
return data_[index];
}
template <typename T>
void Vector<T>::Expand() {
uint64_t new_capacity = capacity_ == 0 ? 1 : capacity_ * 2;
Resize(new_capacity);
}
} // namespace glcr

View File

@ -1,5 +1,7 @@
#include "capability/capability_table.h" #include "capability/capability_table.h"
#include "debug/debug.h"
CapabilityTable::CapabilityTable() {} CapabilityTable::CapabilityTable() {}
uint64_t CapabilityTable::AddExistingCapability( uint64_t CapabilityTable::AddExistingCapability(

View File

@ -1,9 +1,9 @@
#pragma once #pragma once
#include <glacier/container/linked_list.h>
#include <glacier/memory/ref_ptr.h> #include <glacier/memory/ref_ptr.h>
#include "capability/capability.h" #include "capability/capability.h"
#include "lib/linked_list.h"
#include "lib/mutex.h" #include "lib/mutex.h"
class CapabilityTable { class CapabilityTable {
@ -30,7 +30,7 @@ class CapabilityTable {
uint64_t id; uint64_t id;
glcr::RefPtr<Capability> cap; glcr::RefPtr<Capability> cap;
}; };
LinkedList<CapEntry> capabilities_; glcr::LinkedList<CapEntry> capabilities_;
}; };
template <typename T> template <typename T>

View File

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

View File

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

View File

@ -68,3 +68,4 @@ void* operator new(uint64_t size) { return GetKernelHeap().Allocate(size); }
void* operator new[](uint64_t size) { return GetKernelHeap().Allocate(size); } void* operator new[](uint64_t size) { return GetKernelHeap().Allocate(size); }
void operator delete(void*, uint64_t) {} void operator delete(void*, uint64_t) {}
void operator delete[](void*) {}

View File

@ -1,5 +1,6 @@
#include "object/address_space.h" #include "object/address_space.h"
#include "debug/debug.h"
#include "memory/kernel_stack_manager.h" #include "memory/kernel_stack_manager.h"
#include "memory/paging_util.h" #include "memory/paging_util.h"
#include "memory/physical_memory.h" #include "memory/physical_memory.h"

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <glacier/container/linked_list.h>
#include <glacier/memory/ref_ptr.h> #include <glacier/memory/ref_ptr.h>
#include <stdint.h> #include <stdint.h>
@ -86,7 +87,7 @@ class AddressSpace : public KernelObject {
uint64_t vaddr; uint64_t vaddr;
glcr::RefPtr<MemoryObject> mem_obj; glcr::RefPtr<MemoryObject> mem_obj;
}; };
LinkedList<MemoryMapping> memory_mappings_; glcr::LinkedList<MemoryMapping> memory_mappings_;
MemoryMapping* GetMemoryMappingForAddr(uint64_t vaddr); MemoryMapping* GetMemoryMappingForAddr(uint64_t vaddr);
}; };

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "lib/linked_list.h" #include <glacier/container/linked_list.h>
#include "object/kernel_object.h" #include "object/kernel_object.h"
class MemoryObject; class MemoryObject;
@ -37,7 +38,7 @@ class MemoryObject : public KernelObject {
virtual uint64_t PageNumberToPhysAddr(uint64_t page_num); virtual uint64_t PageNumberToPhysAddr(uint64_t page_num);
LinkedList<uint64_t> phys_page_list_; glcr::LinkedList<uint64_t> phys_page_list_;
}; };
class FixedMemoryObject : public MemoryObject { class FixedMemoryObject : public MemoryObject {

View File

@ -39,25 +39,19 @@ glcr::RefPtr<Thread> Process::CreateThread() {
glcr::RefPtr<Thread> Process::GetThread(uint64_t tid) { glcr::RefPtr<Thread> Process::GetThread(uint64_t tid) {
MutexHolder lock(mutex_); MutexHolder lock(mutex_);
auto iter = threads_.begin(); if (tid >= threads_.size()) {
while (iter != threads_.end()) { panic("Bad thread access %u on process %u with %u threads.", tid, id_,
if ((*iter)->tid() == tid) { threads_.size());
return *iter;
} }
++iter; return threads_[tid];
}
panic("Bad thread access.");
return nullptr;
} }
void Process::CheckState() { void Process::CheckState() {
MutexHolder lock(mutex_); MutexHolder lock(mutex_);
auto iter = threads_.begin(); for (uint64_t i = 0; i < threads_.size(); i++) {
while (iter != threads_.end()) { if (threads_[i]->GetState() != Thread::FINISHED) {
if ((*iter)->GetState() != Thread::FINISHED) {
return; return;
} }
++iter;
} }
state_ = FINISHED; state_ = FINISHED;
} }

View File

@ -1,11 +1,11 @@
#pragma once #pragma once
#include <glacier/container/vector.h>
#include <glacier/memory/ref_ptr.h> #include <glacier/memory/ref_ptr.h>
#include <stdint.h> #include <stdint.h>
#include "capability/capability.h" #include "capability/capability.h"
#include "capability/capability_table.h" #include "capability/capability_table.h"
#include "lib/linked_list.h"
#include "lib/mutex.h" #include "lib/mutex.h"
#include "object/address_space.h" #include "object/address_space.h"
#include "object/channel.h" #include "object/channel.h"
@ -64,8 +64,7 @@ class Process : public KernelObject {
State state_; State state_;
uint64_t next_thread_id_ = 0; uint64_t next_thread_id_ = 0;
uint64_t next_cap_id_ = 0x100;
LinkedList<glcr::RefPtr<Thread>> threads_; glcr::Vector<glcr::RefPtr<Thread>> threads_;
CapabilityTable caps_; CapabilityTable caps_;
}; };

View File

@ -1,5 +1,7 @@
#include "scheduler/process_manager.h" #include "scheduler/process_manager.h"
#include "debug/debug.h"
ProcessManager* gProcMan = nullptr; ProcessManager* gProcMan = nullptr;
void ProcessManager::Init() { void ProcessManager::Init() {
@ -12,23 +14,15 @@ void ProcessManager::InsertProcess(const glcr::RefPtr<Process>& proc) {
} }
Process& ProcessManager::FromId(uint64_t pid) { Process& ProcessManager::FromId(uint64_t pid) {
auto iter = proc_list_.begin(); if (pid >= proc_list_.size()) {
while (iter != proc_list_.end()) { panic("Bad proc access %u, have %u processes", pid, proc_list_.size());
if ((*iter)->id() == pid) {
return **iter;
} }
++iter; return *proc_list_[pid];
}
panic("Searching for invalid process id");
return *((Process*)0);
} }
void ProcessManager::DumpProcessStates() { void ProcessManager::DumpProcessStates() {
dbgln("Process States: %u", proc_list_.size()); dbgln("Process States: %u", proc_list_.size());
auto iter = proc_list_.begin(); for (uint64_t i = 0; i < proc_list_.size(); i++) {
while (iter != proc_list_.end()) { dbgln("%u: %u", proc_list_[i]->id(), proc_list_[i]->GetState());
dbgln("%u: %u", (*iter)->id(), (*iter)->GetState());
++iter;
} }
} }

View File

@ -1,8 +1,8 @@
#pragma once #pragma once
#include <glacier/container/vector.h>
#include <glacier/memory/ref_ptr.h> #include <glacier/memory/ref_ptr.h>
#include "lib/linked_list.h"
#include "object/process.h" #include "object/process.h"
class ProcessManager { class ProcessManager {
@ -18,7 +18,7 @@ class ProcessManager {
private: private:
// TODO: This should be a hashmap. // TODO: This should be a hashmap.
LinkedList<glcr::RefPtr<Process>> proc_list_; glcr::Vector<glcr::RefPtr<Process>> proc_list_;
}; };
extern ProcessManager* gProcMan; extern ProcessManager* gProcMan;

View File

@ -2,7 +2,6 @@
#include "common/gdt.h" #include "common/gdt.h"
#include "debug/debug.h" #include "debug/debug.h"
#include "lib/linked_list.h"
#include "scheduler/process_manager.h" #include "scheduler/process_manager.h"
namespace { namespace {

View File

@ -1,6 +1,7 @@
#include "syscall/process.h" #include "syscall/process.h"
#include "capability/capability.h" #include "capability/capability.h"
#include "debug/debug.h"
#include "scheduler/process_manager.h" #include "scheduler/process_manager.h"
#include "scheduler/scheduler.h" #include "scheduler/scheduler.h"

View File

@ -3,6 +3,7 @@
#include <stdint.h> #include <stdint.h>
#include "common/msr.h" #include "common/msr.h"
#include "debug/debug.h"
#include "scheduler/scheduler.h" #include "scheduler/scheduler.h"
#include "syscall/address_space.h" #include "syscall/address_space.h"
#include "syscall/capability.h" #include "syscall/capability.h"

View File

@ -1,6 +1,7 @@
#include "syscall/thread.h" #include "syscall/thread.h"
#include "capability/capability.h" #include "capability/capability.h"
#include "debug/debug.h"
#include "scheduler/scheduler.h" #include "scheduler/scheduler.h"
glcr::ErrorCode ThreadCreate(ZThreadCreateReq* req) { glcr::ErrorCode ThreadCreate(ZThreadCreateReq* req) {
@ -29,6 +30,7 @@ glcr::ErrorCode ThreadExit(ZThreadExitReq*) {
auto curr_thread = gScheduler->CurrentThread(); auto curr_thread = gScheduler->CurrentThread();
curr_thread->Exit(); curr_thread->Exit();
panic("Returned from thread exit"); panic("Returned from thread exit");
UNREACHABLE
} }
glcr::ErrorCode ThreadWait(ZThreadWaitReq* req) { glcr::ErrorCode ThreadWait(ZThreadWaitReq* req) {