From 4d5ed8157ca95e461d13f78fcb9b9677186dcb90 Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Sat, 1 Jan 2022 19:06:24 -0800 Subject: [PATCH] [kutil] Remove unused kutil headers Part two of rearranging kutil code. (See 042f061) Removing unused kutil headers: I can imagine that avl_tree or slab_allocated may want to be returned to at some point, but for now they're just clutter. --- src/libraries/kutil/include/kutil/avl_tree.h | 251 ------------------ src/libraries/kutil/include/kutil/coord.h | 14 - src/libraries/kutil/include/kutil/guid.h | 38 --- .../kutil/include/kutil/slab_allocated.h | 47 ---- .../kutil/include/kutil/type_macros.h | 3 - 5 files changed, 353 deletions(-) delete mode 100644 src/libraries/kutil/include/kutil/avl_tree.h delete mode 100644 src/libraries/kutil/include/kutil/coord.h delete mode 100644 src/libraries/kutil/include/kutil/guid.h delete mode 100644 src/libraries/kutil/include/kutil/slab_allocated.h delete mode 100644 src/libraries/kutil/include/kutil/type_macros.h diff --git a/src/libraries/kutil/include/kutil/avl_tree.h b/src/libraries/kutil/include/kutil/avl_tree.h deleted file mode 100644 index 17da833..0000000 --- a/src/libraries/kutil/include/kutil/avl_tree.h +++ /dev/null @@ -1,251 +0,0 @@ -#pragma once -/// \file avl_tree.h -/// Templated container class for an AVL tree - -#include -#include "kutil/assert.h" -#include "kutil/slab_allocated.h" - -namespace kutil { - -template class avl_tree; - - -/// A node in a `avl_tree` -template -class avl_node : - public T, - public slab_allocated> -{ -public: - using item_type = T; - using node_type = avl_node; - - /// Dereference operator. Helper to cast this node to the contained type. - /// \returns A pointer to the node, cast to T*. - inline item_type & operator*() { return *this; } - - /// Dereference operator. Helper to cast this node to the contained type. - /// \returns A pointer to the node, cast to T*. - inline const item_type & operator*() const { return *this; } - - /// Cast operator. Helper to cast this node to the contained type. - /// \returns A reference to the node, cast to T&. - inline operator item_type& () { return *this; } - - /// Cast operator. Helper to cast this node to the contained type. - /// \returns A reference to the node, cast to const T&. - inline operator const item_type& () { return *this; } - - /// Helper to cast this node to the contained type. - /// \returns A reference to the node, cast to const T&. - inline const item_type& item() const { return *this; } - - /// Helper to cast this node to the contained type. - /// \returns A reference to the node, cast to T&. - inline item_type& item() { return *this; } - - /// Accessor for the left child. - /// \returns A pointer to the left child, or nullptr. - inline node_type * left() { return m_left; } - - /// Accessor for the left child. - /// \returns A pointer to the left child, or nullptr. - inline const node_type * left() const { return m_left; } - - /// Accessor for the right child. - /// \returns A pointer to the right child, or nullptr. - inline node_type * right() { return m_right; } - - /// Accessor for the right child. - /// \returns A pointer to the right child, or nullptr. - inline const node_type * right() const { return m_right; } - -private: - friend class avl_tree; - - inline static int height(node_type *l) { return (l ? l->m_height : 0); } - - // Update this node's height and return its new balance factor - inline int update_height() - { - int left = height(m_left); - int right = height(m_right); - m_height = left > right ? left : right; - return left - right; - } - - int bias(node_type *addend) - { - const item_type &this_item = *this; - const item_type &that_item = *addend; - if (that_item < this_item) - return -1; - else if (that_item > this_item) - return 1; - - kassert(false, "Equal items not allowed in AVL tree"); - return 0; - } - - static node_type * rotate_right(node_type *existing) - { - node_type *root = existing->m_left; - node_type *left = root->m_right; - - root->m_right = existing; - existing->m_left = left; - - existing->update_height(); - root->update_height(); - - return root; - } - - static node_type * rotate_left(node_type *existing) - { - node_type *root = existing->m_right; - node_type *right = root->m_left; - - root->m_left = existing; - existing->m_right = right; - - existing->update_height(); - root->update_height(); - - return root; - } - - static node_type * insert(node_type *existing, node_type *addend) - { - if (existing == nullptr) - return addend; - - if (existing->compare(addend) < 0) - existing->m_left = insert(existing->m_left, addend); - else - existing->m_right = insert(existing->m_right, addend); - - int balance = existing->update_height(); - if (balance > 1) { - // Left-heavy - if (existing->m_left->compare(addend) < 0) { - // Left Left - return rotate_right(existing); - } else { - // Left Right - existing->m_left = rotate_left(existing->m_left); - return rotate_right(existing); - } - } else if (balance < -1) { - // Right-heavy - if (existing->m_right->compare(addend) > 0) { - // Right Right - return rotate_left(existing); - } else { - // Right Left - existing->m_right = rotate_right(existing->m_right); - return rotate_left(existing); - } - } - - return existing; - } - - static node_type * remove(node_type *existing, node_type *subtrahend) - { - if (existing == nullptr) - return existing; - - if (existing == subtrahend) { - if (!existing->m_left || !existing->m_right) { - // At least one child is null - node_type *temp = existing->m_left ? - existing->m_left : existing->m_right; - - if (temp == nullptr) { - // Both were null - temp = existing; - existing = nullptr; - } else { - *existing = *temp; - } - - delete temp; - } else { - // Both children exist, find next node - node_type *temp = existing->m_right; - while (temp->m_left) - temp = temp->m_left; - - *existing = *temp; - existing->m_right = remove(existing->m_right, temp); - } - } else if (existing->compare(subtrahend) < 0) { - existing->m_left = remove(existing->m_left, subtrahend); - } else { - existing->m_right = remove(existing->m_right, subtrahend); - } - - if (!existing) - return nullptr; - - int balance = existing->update_height(); - if (balance > 1) { - int left_balance = existing->m_left->update_height(); - - if (left_balance < 0) - existing->m_left = rotate_left(existing->m_left); - - return rotate_right(existing); - } else if (balance < -1) { - int right_balance = existing->m_right->update_height(); - - if (right_balance > 0) - existing->m_right = rotate_right(existing->m_right); - - return rotate_left(existing); - } - - return existing; - } - - int m_height; - node_type *m_left; - node_type *m_right; -}; - -template -class avl_tree -{ -public: - using item_type = T; - using node_type = avl_node; - - avl_tree() = default; - avl_tree(avl_tree &&other) : - m_count(other.m_count), m_root(other.m_root) - { - other.m_root = nullptr; - other.m_count = 0; - } - - inline node_type * root() { return m_root; } - inline unsigned count() const { return m_count; } - - inline void remove(node_type *subtrahend) { - m_root = node_type::remove(m_root, subtrahend); - m_count--; - } - - inline void insert(node_type *addend) { - m_root = node_type::insert(m_root, addend); - m_count++; - } - -private: - unsigned m_count {0}; - node_type *m_root {nullptr}; -}; - -} // namespace kutil diff --git a/src/libraries/kutil/include/kutil/coord.h b/src/libraries/kutil/include/kutil/coord.h deleted file mode 100644 index f7f36e5..0000000 --- a/src/libraries/kutil/include/kutil/coord.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -namespace kutil { - -template -struct coord -{ - T x, y; - coord() : x(T{}), y(T{}) {} - coord(T x, T y) : x(x), y(y) {} - T size() const { return x * y; } -}; - -} // namespace kutil diff --git a/src/libraries/kutil/include/kutil/guid.h b/src/libraries/kutil/include/kutil/guid.h deleted file mode 100644 index 68003c4..0000000 --- a/src/libraries/kutil/include/kutil/guid.h +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once -/// \file guid.h -/// Definition of the guid type and related functions -#include -#include "kutil/misc.h" - -namespace kutil { - - -/// A GUID -struct guid -{ - uint64_t a, b; -}; - -/// Make a GUID by writing it naturally-ordered in code: -/// AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE becomes: -/// make_guid(0xAAAAAAAA, 0xBBBB, 0xCCCC, 0xDDDD, 0xEEEEEEEEEEEE); -/// \returns The guid object -inline constexpr guid make_guid(uint32_t a, uint16_t b, uint16_t c, uint16_t d, uint64_t e) -{ - const uint64_t h = - static_cast(c) << 48 | - static_cast(b) << 32 | - a; - - const uint64_t l = - static_cast(byteswap(e & 0xffffffff)) << 32 | - (byteswap(e >> 32) & 0xffff0000) | - ((d << 8) & 0xff00) | ((d >> 8) & 0xff); - - return {h, l}; -} - -} // namespace kutil - - -inline bool operator==(const kutil::guid &a, const kutil::guid &b) { return a.a == b.a && a.b == b.b; } diff --git a/src/libraries/kutil/include/kutil/slab_allocated.h b/src/libraries/kutil/include/kutil/slab_allocated.h deleted file mode 100644 index d4b7f80..0000000 --- a/src/libraries/kutil/include/kutil/slab_allocated.h +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once -/// \file slab_allocated.h -/// A parent template class for slab-allocated objects - -#include "kernel_memory.h" -#include "kutil/memory.h" -#include "kutil/vector.h" - -namespace kutil { - -template -class slab_allocated -{ -public: - void * operator new(size_t size) - { - kassert(size == sizeof(T), "Slab allocator got wrong size allocation"); - if (s_free.count() == 0) - allocate_chunk(); - - T *item = s_free.pop(); - kutil::memset(item, 0, sizeof(T)); - return item; - } - - void operator delete(void *p) { s_free.append(reinterpret_cast(p)); } - -private: - static void allocate_chunk() - { - size_t size = N * ::memory::frame_size; - s_free.ensure_capacity(size / sizeof(T)); - - void *memory = kalloc(size); - T *current = reinterpret_cast(memory); - T *end = offset_pointer(current, size); - while (current < end) - s_free.append(current++); - } - - static vector s_free; -}; - -#define DEFINE_SLAB_ALLOCATOR(type, N) \ - template<> ::kutil::vector kutil::slab_allocated::s_free {}; - -} // namespace kutil diff --git a/src/libraries/kutil/include/kutil/type_macros.h b/src/libraries/kutil/include/kutil/type_macros.h deleted file mode 100644 index bce08ae..0000000 --- a/src/libraries/kutil/include/kutil/type_macros.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -#define __weak __attribute__ ((weak))