[freenos] [freenos commit] r168 - Added BitMap template class.

  • From: codesite-noreply@xxxxxxxxxx
  • To: freenos@xxxxxxxxxxxxx
  • Date: Mon, 29 Jun 2009 13:26:05 +0000

Author: nieklinnenbank
Date: Mon Jun 29 06:24:26 2009
New Revision: 168

Added:
   trunk/include/BitMap.h
   trunk/srv/filesystem/ext2/Ext2Create.cpp
Removed:
   trunk/srv/filesystem/ext2/Main.cpp
Modified:
   trunk/srv/filesystem/ext2/Ext2FileSystem.cpp
   trunk/srv/filesystem/ext2/Ext2Inode.h
   trunk/srv/filesystem/ext2/SConscript

Log:
Added BitMap template class.
The BitMap class may be used to mark bits (un)used in
it's internal map. The implementation is currently untested.
Also added an empty Ext2Create.cpp which can be used to create
Extended 2 FileSystems and may use BitMap.


Added: trunk/include/BitMap.h
==============================================================================
--- (empty file)
+++ trunk/include/BitMap.h      Mon Jun 29 06:24:26 2009
@@ -0,0 +1,140 @@
+/*
+ * Copyright (C) 2009 Niek Linnenbank
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __BITMAP_H
+#define __BITMAP_H
+
+#include "Macros.h"
+#include "Assert.h"
+
+/**
+ * Bit map template class.
+ */
+template <class T> class BitMap
+{
+    public:
+
+       /**
+        * Class constructor.
+        * @param cnt Number of bits.
+        */
+       BitMap(Size cnt) : count(cnt), free(cnt)
+       {
+           map = new u8[(cnt / 8) + 1];
+       }
+
+       /**
+        * Mark a given bit used.
+        * @param bit Bit number to mark used.
+        */
+       void mark(Size bit)
+       {
+           assert(bit < count);
+           assertRead(map);
+           assertWrite(map);
+       
+           /* Only mark if the bit is free. */
+           if (!isMarked(bit))
+           {
+               map[bit / 8] |= 1 << (bit % 8);
+               free--;
+           }
+        }
+       
+       /**
+        * Marks the next free bit used.
+        * @return Bit number on success and -1 otherwise.
+        */
+       Error markNext()
+       {
+           Address *ptr;
+           Size num = count / sizeof(Address);
+
+           /* At least one, and include partially used bytes. */
+           if (!num || sizeof(Address) % count)
+           {
+               num++;
+           }
+           /* Scan bitmap as fast as possible. */
+           for (Size i = 0; i < num; i++)
+           {
+               /* Point to the correct offset. */
+               ptr = (Address *) (&map) + i;
+
+               /* Any blocks free? */
+               if (*ptr != (Address) ~ZERO)
+               {
+                   /* Find the first free bit. */
+                   for (Size bit = 0; bit < sizeof(Address) * 8; bit++)
+                   {
+                       if (!(*ptr & 1 << bit))
+                       {
+                           *ptr |= (1 << bit);
+                           free--;
+                           return bit + (sizeof(Address) * 8 * i);
+                       }
+                   }
+               }
+            }
+           /* No free bits left! */
+           return -1;
+        }
+       
+       /**
+        * Unmarks the given bit.
+        * @param bit Bit number to unmark.
+        */
+       void unmark(Size bit)
+       {
+           assert(bit < count);
+           assertRead(map);
+           assertWrite(map);
+       
+           /* Only unmark if the bit is marked now. */
+           if (isMarked(bit))
+           {
+               map[bit / 8] &= ~(1 << (bit % 8));
+               free++;
+           }
+       }
+
+       /**
+        * Verify if a given bit is marked.
+        * @param bit Bit number to check.
+        * @return True if marked, false otherwise.
+        */
+       bool isMarked(Size bit)
+       {
+           assert(bit < count);
+           assertRead(map);
+       
+           return map[bit / 8] & (bit % 8);
+       }
+
+    private:
+
+       /** Total number of bits in the map. */
+       Size count;
+
+       /** Unmarked bits remaining. */
+       Size free;
+
+       /** Bitmap which represents free and used blocks. */
+       u8 *map;
+};
+
+#endif /* __BITMAP_H */

Added: trunk/srv/filesystem/ext2/Ext2Create.cpp
==============================================================================
--- (empty file)
+++ trunk/srv/filesystem/ext2/Ext2Create.cpp    Mon Jun 29 06:24:26 2009
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2009 Niek Linnenbank
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <BitMap.h>
+#include "Ext2SuperBlock.h"
+#include "Ext2Inode.h"
+#include "Ext2Group.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(int argc, char **argv)
+{
+    return EXIT_SUCCESS;
+}

Modified: trunk/srv/filesystem/ext2/Ext2FileSystem.cpp
==============================================================================
--- trunk/srv/filesystem/ext2/Ext2FileSystem.cpp        (original)
+++ trunk/srv/filesystem/ext2/Ext2FileSystem.cpp        Mon Jun 29 06:24:26 2009
@@ -16,17 +16,30 @@
  */

 #include <File.h>
+#include <BootModule.h>
 #include <Directory.h>
 #include <Device.h>
 #include <LogMessage.h>
-#include <stdlib.h>
-#include <string.h>
 #include "Ext2FileSystem.h"
 #include "Ext2SuperBlock.h"
 #include "Ext2File.h"
 #include "Ext2Directory.h"
 #include "Ext2Inode.h"
 #include "Ext2Group.h"
+#include <stdlib.h>
+#include <string.h>
+
+int main(int argc, char **argv)
+{
+    BootModule module("/boot/boot.ext2");
+    if (module.load())
+    {
+        Ext2FileSystem server("/img", &module);
+        return server.run();
+    }
+    exit(1);
+    return 1;
+}

 Ext2FileSystem::Ext2FileSystem(const char *p, Storage *s)
     : FileSystem(p), storage(s), groups(ZERO)

Modified: trunk/srv/filesystem/ext2/Ext2Inode.h
==============================================================================
--- trunk/srv/filesystem/ext2/Ext2Inode.h       (original)
+++ trunk/srv/filesystem/ext2/Ext2Inode.h       Mon Jun 29 06:24:26 2009
@@ -33,8 +33,10 @@
 #ifndef __FILESYSTEM_EXT2INODE_H
 #define __FILESYSTEM_EXT2INODE_H

+#ifndef __HOST__
 #include <FileSystemMessage.h>
 #include <sys/stat.h>
+#endif /* __HOST__ */

 /**
  * @defgroup ext2 ext2fs (Extended 2 Filesystem)

Modified: trunk/srv/filesystem/ext2/SConscript
==============================================================================
--- trunk/srv/filesystem/ext2/SConscript        (original)
+++ trunk/srv/filesystem/ext2/SConscript        Mon Jun 29 06:24:26 2009
@@ -17,8 +17,25 @@

 from build import *

-env = Prepare(target, [ 'libcrt', 'liballoc', 'libposix', 'libexec', 'libc' ],
+#
+# Target system environment.
+#
+targetEnv = Prepare(target, [ 'libcrt', 'liballoc', 'libposix', 'libexec', 'libc' ],
                      [ 'filesystem', 'process', 'log' ])

-env.Program('server', [ 'Ext2Directory.cpp', 'Ext2File.cpp', 'Ext2FileSystem.cpp', 'Main.cpp' ],
-                        LIBS = env['LIBS'], LIBPATH = env['LIBPATH'])
+#
+# Host system environment.
+#
+hostEnv   = Prepare(host, [ 'libexec' ], [ 'filesystem' ])
+
+#
+# Extended 2 FileSystem server.
+#
+targetEnv.Program('server', [ 'Ext2Directory.cpp', 'Ext2File.cpp', 'Ext2FileSystem.cpp' ],
+                  LIBS = targetEnv['LIBS'], LIBPATH = targetEnv['LIBPATH'])
+
+#
+# Extended 2 FileSystem creation utility.
+#
+hostEnv.Program('create', [ 'Ext2Create.cpp' ],
+                LIBS = hostEnv['LIBS'], LIBPATH = hostEnv['LIBPATH'])

Other related posts:

  • » [freenos] [freenos commit] r168 - Added BitMap template class. - codesite-noreply