From 7592f9afd5bf1b794c125db14762707f969c0635 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 20 Feb 2020 13:18:42 +0100 Subject: [PATCH] AK: Use size_t for CircularQueue and CircularDeque --- AK/CircularDeque.h | 2 +- AK/CircularQueue.h | 20 ++++++++++---------- AK/Forward.h | 6 ++++-- Applications/SystemMonitor/GraphWidget.cpp | 2 +- Kernel/TTY/TTY.cpp | 4 +++- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/AK/CircularDeque.h b/AK/CircularDeque.h index 319106e249d..445abe46aaa 100644 --- a/AK/CircularDeque.h +++ b/AK/CircularDeque.h @@ -32,7 +32,7 @@ namespace AK { -template +template class CircularDeque : public CircularQueue { public: T dequeue_end() diff --git a/AK/CircularQueue.h b/AK/CircularQueue.h index 2941958bdec..69f641aa28f 100644 --- a/AK/CircularQueue.h +++ b/AK/CircularQueue.h @@ -32,7 +32,7 @@ namespace AK { -template +template class CircularQueue { public: CircularQueue() @@ -46,7 +46,7 @@ public: void clear() { - for (int i = 0; i < m_size; ++i) + for (size_t i = 0; i < m_size; ++i) elements()[(m_head + i) % Capacity].~T(); m_head = 0; @@ -54,9 +54,9 @@ public: } bool is_empty() const { return !m_size; } - int size() const { return m_size; } + size_t size() const { return m_size; } - int capacity() const { return Capacity; } + size_t capacity() const { return Capacity; } void enqueue(T&& value) { @@ -87,7 +87,7 @@ public: return value; } - const T& at(int index) const { return elements()[(m_head + index) % Capacity]; } + const T& at(size_t index) const { return elements()[(m_head + index) % Capacity]; } const T& first() const { return at(0); } const T& last() const { return at(size() - 1); } @@ -107,19 +107,19 @@ public: private: friend class CircularQueue; - ConstIterator(const CircularQueue& queue, const int index) + ConstIterator(const CircularQueue& queue, const size_t index) : m_queue(queue) , m_index(index) { } const CircularQueue& m_queue; - int m_index { 0 }; + size_t m_index { 0 }; }; ConstIterator begin() const { return ConstIterator(*this, m_head); } ConstIterator end() const { return ConstIterator(*this, size()); } - int head_index() const { return m_head; } + size_t head_index() const { return m_head; } protected: T* elements() { return reinterpret_cast(m_storage); } @@ -127,8 +127,8 @@ protected: friend class ConstIterator; alignas(T) u8 m_storage[sizeof(T) * Capacity]; - int m_size { 0 }; - int m_head { 0 }; + size_t m_size { 0 }; + size_t m_head { 0 }; }; } diff --git a/AK/Forward.h b/AK/Forward.h index ae6758b41b3..f69794d860a 100644 --- a/AK/Forward.h +++ b/AK/Forward.h @@ -26,6 +26,8 @@ #pragma once +#include + namespace AK { class Bitmap; @@ -57,7 +59,7 @@ class DoublyLinkedList; template class InlineLinkedList; -template +template class CircularQueue; template @@ -116,8 +118,8 @@ using AK::FixedArray; using AK::Function; using AK::HashMap; using AK::HashTable; -using AK::IPv4Address; using AK::InlineLinkedList; +using AK::IPv4Address; using AK::JsonArray; using AK::JsonObject; using AK::JsonValue; diff --git a/Applications/SystemMonitor/GraphWidget.cpp b/Applications/SystemMonitor/GraphWidget.cpp index 9023b55768a..70d6175b7ea 100644 --- a/Applications/SystemMonitor/GraphWidget.cpp +++ b/Applications/SystemMonitor/GraphWidget.cpp @@ -58,7 +58,7 @@ void GraphWidget::paint_event(GUI::PaintEvent& event) float scale = (float)inner_rect.height() / (float)m_max; Gfx::Point prev_point; - for (int i = 0; i < m_values.size(); ++i) { + for (size_t i = 0; i < m_values.size(); ++i) { int x = inner_rect.right() - (i * 2) + 1; if (x < 0) break; diff --git a/Kernel/TTY/TTY.cpp b/Kernel/TTY/TTY.cpp index 8a092579712..debf0f6a6e4 100644 --- a/Kernel/TTY/TTY.cpp +++ b/Kernel/TTY/TTY.cpp @@ -54,7 +54,9 @@ void TTY::set_default_termios() ssize_t TTY::read(FileDescription&, u8* buffer, ssize_t size) { - if (m_input_buffer.size() < size) + ASSERT(size >= 0); + + if (m_input_buffer.size() < static_cast(size)) size = m_input_buffer.size(); if (in_canonical_mode()) {