瀏覽代碼

AK: Make Bitmap use size_t for its size

Also rework its API's to return Optional<size_t> instead of int with -1
as the error value.
Andreas Kling 5 年之前
父節點
當前提交
0763f67043
共有 4 個文件被更改,包括 34 次插入32 次删除
  1. 26 24
      AK/Bitmap.h
  2. 6 6
      Kernel/FileSystem/Ext2FileSystem.cpp
  3. 1 1
      Kernel/VM/InodeVMObject.cpp
  4. 1 1
      Kernel/VM/Region.cpp

+ 26 - 24
AK/Bitmap.h

@@ -28,6 +28,7 @@
 
 
 #include <AK/Assertions.h>
 #include <AK/Assertions.h>
 #include <AK/Noncopyable.h>
 #include <AK/Noncopyable.h>
+#include <AK/Optional.h>
 #include <AK/StdLibExtras.h>
 #include <AK/StdLibExtras.h>
 #include <AK/Types.h>
 #include <AK/Types.h>
 #include <AK/kmalloc.h>
 #include <AK/kmalloc.h>
@@ -79,13 +80,13 @@ public:
         m_data = nullptr;
         m_data = nullptr;
     }
     }
 
 
-    int size() const { return m_size; }
-    bool get(int index) const
+    size_t size() const { return m_size; }
+    bool get(size_t index) const
     {
     {
         ASSERT(index < m_size);
         ASSERT(index < m_size);
         return 0 != (m_data[index / 8] & (1u << (index % 8)));
         return 0 != (m_data[index / 8] & (1u << (index % 8)));
     }
     }
-    void set(int index, bool value) const
+    void set(size_t index, bool value) const
     {
     {
         ASSERT(index < m_size);
         ASSERT(index < m_size);
         if (value)
         if (value)
@@ -97,7 +98,7 @@ public:
     u8* data() { return m_data; }
     u8* data() { return m_data; }
     const u8* data() const { return m_data; }
     const u8* data() const { return m_data; }
 
 
-    void grow(int size, bool default_value)
+    void grow(size_t size, bool default_value)
     {
     {
         ASSERT(size > m_size);
         ASSERT(size > m_size);
 
 
@@ -129,27 +130,28 @@ public:
         memset(m_data, value ? 0xff : 0x00, size_in_bytes());
         memset(m_data, value ? 0xff : 0x00, size_in_bytes());
     }
     }
 
 
-    int find_first_set() const
+    Optional<size_t> find_first_set() const
     {
     {
-        int i = 0;
+        size_t i = 0;
         while (i < m_size / 8 && m_data[i] == 0x00)
         while (i < m_size / 8 && m_data[i] == 0x00)
             i++;
             i++;
 
 
-        int j = 0;
-        for (j = i * 8; j < m_size; j++)
+        size_t j = 0;
+        for (j = i * 8; j < m_size; j++) {
             if (get(j))
             if (get(j))
                 return j;
                 return j;
+        }
 
 
-        return -1;
+        return {};
     }
     }
 
 
-    int find_first_unset() const
+    Optional<size_t> find_first_unset() const
     {
     {
-        int i = 0;
+        size_t i = 0;
         while (i < m_size / 8 && m_data[i] == 0xff)
         while (i < m_size / 8 && m_data[i] == 0xff)
             i++;
             i++;
 
 
-        int j = 0;
+        size_t j = 0;
         for (j = i * 8; j < m_size; j++)
         for (j = i * 8; j < m_size; j++)
             if (!get(j))
             if (!get(j))
                 return j;
                 return j;
@@ -157,20 +159,20 @@ public:
         return -1;
         return -1;
     }
     }
 
 
-    int find_longest_range_of_unset_bits(int max_length, int& found_range_size) const
+    Optional<size_t> find_longest_range_of_unset_bits(size_t max_length, size_t& found_range_size) const
     {
     {
-        int first_index = find_first_unset();
-        if (first_index == -1)
-            return -1;
+        auto first_index = find_first_unset();
+        if (!first_index.has_value())
+            return {};
 
 
-        int free_region_start = first_index;
-        int free_region_size = 1;
+        size_t free_region_start = first_index;
+        size_t free_region_size = 1;
 
 
-        int max_region_start = free_region_start;
-        int max_region_size = free_region_size;
+        size_t max_region_start = free_region_start;
+        size_t max_region_size = free_region_size;
 
 
         // Let's try and find the best fit possible
         // Let's try and find the best fit possible
-        for (int j = first_index + 1; j < m_size && free_region_size < max_length; j++) {
+        for (size_t j = first_index + 1; j < m_size && free_region_size < max_length; j++) {
             if (!get(j)) {
             if (!get(j)) {
                 if (free_region_size == 0)
                 if (free_region_size == 0)
                     free_region_start = j;
                     free_region_start = j;
@@ -204,7 +206,7 @@ public:
         m_data = nullptr;
         m_data = nullptr;
     }
     }
 
 
-    explicit Bitmap(int size, bool default_value)
+    explicit Bitmap(size_t size, bool default_value)
         : m_size(size)
         : m_size(size)
         , m_owned(true)
         , m_owned(true)
     {
     {
@@ -221,10 +223,10 @@ public:
     }
     }
 
 
 private:
 private:
-    int size_in_bytes() const { return ceil_div(m_size, 8); }
+    size_t size_in_bytes() const { return ceil_div(m_size, 8); }
 
 
     u8* m_data { nullptr };
     u8* m_data { nullptr };
-    int m_size { 0 };
+    size_t m_size { 0 };
     bool m_owned { false };
     bool m_owned { false };
 };
 };
 
 

+ 6 - 6
Kernel/FileSystem/Ext2FileSystem.cpp

@@ -1130,14 +1130,14 @@ Vector<Ext2FS::BlockIndex> Ext2FS::allocate_blocks(GroupIndex preferred_group_in
         auto block_bitmap = Bitmap::wrap(cached_bitmap.buffer.data(), blocks_in_group);
         auto block_bitmap = Bitmap::wrap(cached_bitmap.buffer.data(), blocks_in_group);
 
 
         BlockIndex first_block_in_group = (group_index - 1) * blocks_per_group() + first_block_index();
         BlockIndex first_block_in_group = (group_index - 1) * blocks_per_group() + first_block_index();
-        int free_region_size = 0;
-        int first_unset_bit_index = block_bitmap.find_longest_range_of_unset_bits(count - blocks.size(), free_region_size);
-        ASSERT(first_unset_bit_index != -1);
+        size_t free_region_size = 0;
+        auto first_unset_bit_index = block_bitmap.find_longest_range_of_unset_bits(count - blocks.size(), free_region_size);
+        ASSERT(first_unset_bit_index.has_value());
 #ifdef EXT2_DEBUG
 #ifdef EXT2_DEBUG
         dbg() << "Ext2FS: allocating free region of size: " << free_region_size << "[" << group_index << "]";
         dbg() << "Ext2FS: allocating free region of size: " << free_region_size << "[" << group_index << "]";
 #endif
 #endif
-        for (int i = 0; i < free_region_size; ++i) {
-            BlockIndex block_index = (unsigned)(first_unset_bit_index + i) + first_block_in_group;
+        for (size_t i = 0; i < free_region_size; ++i) {
+            BlockIndex block_index = (first_unset_bit_index.value() + i) + first_block_in_group;
             set_block_allocation_state(block_index, true);
             set_block_allocation_state(block_index, true);
             blocks.unchecked_append(block_index);
             blocks.unchecked_append(block_index);
 #ifdef EXT2_DEBUG
 #ifdef EXT2_DEBUG
@@ -1198,7 +1198,7 @@ unsigned Ext2FS::find_a_free_inode(GroupIndex preferred_group, off_t expected_si
 
 
     auto& cached_bitmap = get_bitmap_block(bgd.bg_inode_bitmap);
     auto& cached_bitmap = get_bitmap_block(bgd.bg_inode_bitmap);
     auto inode_bitmap = Bitmap::wrap(cached_bitmap.buffer.data(), inodes_in_group);
     auto inode_bitmap = Bitmap::wrap(cached_bitmap.buffer.data(), inodes_in_group);
-    for (int i = 0; i < inode_bitmap.size(); ++i) {
+    for (size_t i = 0; i < inode_bitmap.size(); ++i) {
         if (inode_bitmap.get(i))
         if (inode_bitmap.get(i))
             continue;
             continue;
         first_free_inode_in_group = first_inode_in_group + i;
         first_free_inode_in_group = first_inode_in_group + i;

+ 1 - 1
Kernel/VM/InodeVMObject.cpp

@@ -78,7 +78,7 @@ size_t InodeVMObject::amount_clean() const
 size_t InodeVMObject::amount_dirty() const
 size_t InodeVMObject::amount_dirty() const
 {
 {
     size_t count = 0;
     size_t count = 0;
-    for (int i = 0; i < m_dirty_pages.size(); ++i) {
+    for (size_t i = 0; i < m_dirty_pages.size(); ++i) {
         if (m_dirty_pages.get(i))
         if (m_dirty_pages.get(i))
             ++count;
             ++count;
     }
     }

+ 1 - 1
Kernel/VM/Region.cpp

@@ -164,7 +164,7 @@ u32 Region::cow_pages() const
     if (!m_cow_map)
     if (!m_cow_map)
         return 0;
         return 0;
     u32 count = 0;
     u32 count = 0;
-    for (int i = 0; i < m_cow_map->size(); ++i)
+    for (size_t i = 0; i < m_cow_map->size(); ++i)
         count += m_cow_map->get(i);
         count += m_cow_map->get(i);
     return count;
     return count;
 }
 }