CORE: Add two new file, disk.c, disk.h
authorLiu Aleaxander <Aleaxander@gmail.com>
Wed, 3 Jun 2009 20:50:06 +0000 (04:50 +0800)
committerLiu Aleaxander <Aleaxander@gmail.com>
Wed, 3 Jun 2009 20:50:06 +0000 (04:50 +0800)
extract the sector read functions to disk.c, and add SECTOR_SHIFT, SECTOR_SIZE
stuff in the disk.h as asked by hpa.

core/cache.c
core/disk.c [new file with mode: 0644]
core/ext2.c
core/include/disk.h [new file with mode: 0644]

index 0ddf944..debd5b5 100644 (file)
@@ -1,11 +1,11 @@
 #include "core.h"
 #include "cache.h"
-
+#include "disk.h"
 #include <stdio.h>
 #include <string.h>
 
 
-/*
+/**
  * Each CachePtr contains:
  * - Block pointer
  * - LRU previous pointer
@@ -15,7 +15,6 @@
  * The cache buffer are pointed to by a cache_head structure.
  */
 
-
 static struct cache_struct cache_head, cache[MAX_CACHE_ENTRIES];
 static int cache_block_size;
 static int cache_entries;
@@ -55,31 +54,6 @@ void cache_init(com32sys_t * regs)
 }
 
 
-void read_sectors(char *buf, int sector_num, int sectors)
-{
-    com32sys_t regs;
-        
-    memset(&regs, 0, sizeof(regs) );
-    regs.eax.l = sector_num;
-    regs.ebp.l = sectors;
-    regs.es = SEG(core_xfer_buf);
-    regs.ebx.w[0] = OFFS(core_xfer_buf);
-
-    call16(getlinsec, &regs, NULL);
-
-    memcpy(buf, core_xfer_buf, sectors << 9);
-}
-
-
-void getoneblk(char *buf, uint32_t block, int block_size)
-{
-    int sec_per_block = block_size >> 9; /* 512==sector size */
-        
-    read_sectors(buf, block * sec_per_block, sec_per_block);
-}
-
-
-
 /**
  * get_cache_block:
  *
@@ -132,24 +106,19 @@ void get_cache_block(com32sys_t * regs)
             cs = cs->prev;
     }
     
-    if ( i == cache_entries ) {
-        /* missed, so we need to load it */
-        
+    /* missed, so we need to load it */
+    if ( i == cache_entries ) {        
         /* store it at the head of real cache */
-        cs = cache_head.next;
-        
+        cs = cache_head.next;        
         cs->block = block;
         getoneblk(cs->data, block, cache_block_size);
 
         missed ++;
     } 
     
-
-    
     /* remove cs from current position in list */
     cs->prev->next = cs->next;
-    cs->next->prev = cs->prev;
-    
+    cs->next->prev = cs->prev;    
     
     /* add to just before head node */
     last = cache_head.prev;
@@ -158,13 +127,11 @@ void get_cache_block(com32sys_t * regs)
     last->next = cs;
     cs->prev = last;
     head->prev = cs;
-    cs->next = head;
+    cs->next = head;    
     
  out:
-
-        total_read ++;
-
 #if 0 /* testing how efficiency the cache is */
+    total_read ++;
     if ( total_read % 5 == 0 ) 
         printf("total_read %d\tmissed %d\n", total_read, missed);
 #endif
diff --git a/core/disk.c b/core/disk.c
new file mode 100644 (file)
index 0000000..1a25d87
--- /dev/null
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <string.h>
+#include "core.h"
+#include "disk.h"
+
+void read_sectors(char *buf, int sector_num, int sectors)
+{
+    com32sys_t regs;
+        
+    memset(&regs, 0, sizeof regs);
+    regs.eax.l = sector_num;
+    regs.ebp.l = sectors;
+    regs.es = SEG(core_xfer_buf);
+    regs.ebx.w[0] = OFFS(core_xfer_buf);
+    call16(getlinsec, &regs, NULL);
+
+    memcpy(buf, core_xfer_buf, sectors << SECTOR_SHIFT);
+}
+
+
+void getoneblk(char *buf, uint32_t block, int block_size)
+{
+    int sec_per_block = block_size >> SECTOR_SHIFT;
+        
+    read_sectors(buf, block * sec_per_block, sec_per_block);
+}
+
+
index 5491f81..3cd2782 100644 (file)
@@ -1,7 +1,7 @@
 #include <stdio.h>
 #include <string.h>
-
 #include "core.h"
+#include "disk.h"
 #include "ext2_fs.h"
 
 
  */
 void init_fs(com32sys_t *regs)
 {
-
     extern uint16_t ClustByteShift,  ClustShift;
     extern uint32_t SecPerClust, ClustSize, ClustMask;
     extern uint32_t PtrsPerBlock1, PtrsPerBlock2;
     extern char SuperBlock[1024];
-
-    struct ext2_super_block *sb;
-    
+    struct ext2_super_block *sb;    
     
     /* read the super block */
     read_sectors(SuperBlock, 2, 2);
@@ -26,17 +23,17 @@ void init_fs(com32sys_t *regs)
     
     ClustByteShift = sb->s_log_block_size + 10;
     ClustSize = 1 << ClustByteShift;
-    ClustShift = ClustByteShift - 9;
+    ClustShift = ClustByteShift - SECTOR_SHIFT;
     
     //DescPerBlock  = ClustSize >> ext2_group_desc_lg2size;
     //InodePerBlock = ClustSize / sb->s_inode_size;
         
-    SecPerClust = ClustSize >> 9;
+    SecPerClust = ClustSize >> SECTOR_SHIFT;
     ClustMask = SecPerClust - 1;
     
     PtrsPerBlock1 = 1 << ( ClustByteShift - 2 );
     PtrsPerBlock2 = 1 << ( (ClustByteShift - 2) * 2);
     //PtrsPerBlock3 = 1 << ( (ClustByteShift - 2) * 3);
     
-    regs->eax.l = 9;
+    regs->eax.l = SECTOR_SHIFT;
 }
diff --git a/core/include/disk.h b/core/include/disk.h
new file mode 100644 (file)
index 0000000..e99e3cb
--- /dev/null
@@ -0,0 +1,11 @@
+#ifndef DISK_H
+#define DISK_H
+
+#define SECTOR_SHIFT     9
+#define SECTOR_SIZE      (1 << SECTOR_SHIFT)
+
+
+extern void read_sectors(char *, int, int);
+extern void get_cache_block(com32sys_t *);
+
+#endif /* DISK_H */