[glacier] Add a vector class

This commit is contained in:
Drew Galbraith 2023-06-26 15:46:03 -07:00
parent 64d355b20d
commit 02e6b49d90
6 changed files with 126 additions and 30 deletions

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

@ -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

@ -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;
} }
panic("Bad thread access."); return threads_[tid];
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,6 +1,6 @@
#pragma once #pragma once
#include <glacier/container/linked_list.h> #include <glacier/container/vector.h>
#include <glacier/memory/ref_ptr.h> #include <glacier/memory/ref_ptr.h>
#include <stdint.h> #include <stdint.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;
glcr::LinkedList<glcr::RefPtr<Thread>> threads_; glcr::Vector<glcr::RefPtr<Thread>> threads_;
CapabilityTable caps_; CapabilityTable caps_;
}; };

View File

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