aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLiu Aleaxander <Aleaxander@gmail.com>2009-05-25 06:43:48 +0800
committerLiu Aleaxander <Aleaxander@gmail.com>2009-05-25 06:43:48 +0800
commitb23af34e29bf848e2e15d5b7c35da71166e28248 (patch)
tree70d00934168242f736224383951d104c0dd6bc0c
parent0bf5aa38d3b2574e5963c7e5f3843d09390b155b (diff)
downloaddevel-b23af34e29bf848e2e15d5b7c35da71166e28248.tar.gz
devel-b23af34e29bf848e2e15d5b7c35da71166e28248.tar.xz
devel-b23af34e29bf848e2e15d5b7c35da71166e28248.zip
Fix the cache part. rmove the malloc() and free() functions.
and make the getoneblk function simpler
-rw-r--r--cache.c62
-rw-r--r--cache.h4
-rw-r--r--disklab.c34
-rw-r--r--disklab.h3
-rw-r--r--extlinux.c6
-rw-r--r--ldlinux.c8
-rw-r--r--main.c9
7 files changed, 66 insertions, 60 deletions
diff --git a/cache.c b/cache.c
index 9245d35..2a9d547 100644
--- a/cache.c
+++ b/cache.c
@@ -3,7 +3,8 @@
#include <stdio.h>
#include <fcntl.h>
-#include <malloc.h>
+
+
/*
@@ -14,9 +15,12 @@
* - Block data buffer address
* The first entry is the head node of the list
*/
-struct cache_struct cache[CACHE_ENTRIES + 1] = {0,};
-extern int blk_size;
+
+static struct cache_struct cache_head, cache[MAX_CACHE_ENTRIES];
+static __u8 cache_data[65536];
+static int cache_block_size;
+static int cache_entries;
/**
* cache_init:
@@ -24,22 +28,29 @@ extern int blk_size;
* Initialize the cache data structres.
*
*/
-void cache_init(void)
+void cache_init(int block_size)
{
struct cache_struct *prev, *cur;
+ __u8 *data = cache_data;
int i;
-
- cur = cache;
- prev = &cache[CACHE_ENTRIES];
- for ( i = 0; i < CACHE_ENTRIES + 1; i++ ) {
+ cache_block_size = block_size;
+ cache_entries = sizeof cache_data / block_size;
+ if (cache_entries > MAX_CACHE_ENTRIES)
+ cache_entries = MAX_CACHE_ENTRIES;
+
+ cache_head.prev = &cache[cache_entries-1];
+ cache_head.prev->next = &cache_head;
+ prev = &cache_head;
+
+ for (i = 0; i < cache_entries; i++) {
+ cur = &cache[i];
cur->block = 0;
cur->prev = prev;
prev->next = cur;
- cur->data = NULL;
-
- prev = cur;
- cur = &cache[i+1];
+ cur->data = data;
+ data += block_size;
+ prev = cur++;
}
}
@@ -64,25 +75,26 @@ struct cache_struct * get_cache_block(__u32 block)
struct cache_struct *head, *last;
int i;
- for ( i = 0; i < CACHE_ENTRIES; i ++ ) {
+ if ( !block ) {
+ printf("ERROR: we got a ZERO block number that's not we want!\n");
+ return NULL;
+ }
+
+ for ( i = 0; i < cache_entries; i ++ ) {
if ( cs->block == block )
break;
else
cs = &cache[i + 1];
}
- if ( i == CACHE_ENTRIES ) {
+ if ( i == cache_entries ) {
/* missed, so we need to load it */
- /* first free it if nessary */
- if ( cache[0].next->data )
- free(cache[0].next->data);
-
/* store it at the head of real cache */
- cs = cache[0].next;
+ cs = cache_head.next;
cs->block = block;
- cs->data = (void*)getoneblk(block);
+ getoneblk(cs->data, block, cache_block_size);
}
/* remove cs from current position in list */
@@ -91,8 +103,8 @@ struct cache_struct * get_cache_block(__u32 block)
/* add to just before head node */
- last = cache[0].prev;
- head = cache;
+ last = cache_head.prev;
+ head = &cache_head;
last->next = cs;
cs->prev = last;
@@ -114,10 +126,10 @@ struct cache_struct * get_cache_block(__u32 block)
void print_cache(void)
{
int i = 0;
- struct cache_struct *cs = cache;
- for (; i < CACHE_ENTRIES; i++) {
+ struct cache_struct *cs = &cache_head;
+ for (; i < cache_entries; i++) {
cs = cs->next;
- printf("%d ", cs->block);
+ printf("%d ", cs->block);
}
printf("\n");
diff --git a/cache.h b/cache.h
index 4b3abc6..24403d2 100644
--- a/cache.h
+++ b/cache.h
@@ -4,7 +4,7 @@
#include "types.h"
-#define CACHE_ENTRIES 0x064 /* just a test */
+#define MAX_CACHE_ENTRIES 0x064 /* I'm not sure it's the max */
@@ -19,7 +19,7 @@ struct cache_struct {
/* functions defined in cache.c */
-void cache_init(void);
+void cache_init(int block_size);
struct cache_struct *get_cache_block(__u32 block);
diff --git a/disklab.c b/disklab.c
index a30998f..c9f1302 100644
--- a/disklab.c
+++ b/disklab.c
@@ -1,32 +1,9 @@
#include "disklab.h"
#include <stdio.h>
-#include <malloc.h>
-
extern int fd;
-extern int blk_size;
-
-void* getoneblk(__u32 block)
-{
- int bytes_read;
- char *buf;
-
- if ( lseek(fd, block * blk_size, SEEK_SET) < 0) {/* ... */
- printf("seek file ext2.img error....\n");
- return NULL;
- }
-
- buf = malloc(blk_size);
-
- if ( (bytes_read = read(fd, buf, blk_size)) < blk_size )
- printf("%s:%s: read %d bytes less than %dB..\n",
- __FILE__, __FUNCTION__, bytes_read, blk_size);
-
- return buf;
-}
-
void getlinsec(char *buf, int sector, int sector_cnt)
{
@@ -42,3 +19,14 @@ void getlinsec(char *buf, int sector, int sector_cnt)
__FILE__, __FUNCTION__, bytes_read, 512 * sector_cnt);
}
+
+
+void getoneblk(char *buf, __u32 block, int block_size)
+{
+ int sec_per_block = block_size / 512; /* 512==sector size */
+
+ getlinsec(buf, block*sec_per_block, sec_per_block);
+}
+
+
+
diff --git a/disklab.h b/disklab.h
index 2b22849..142b3ab 100644
--- a/disklab.h
+++ b/disklab.h
@@ -6,6 +6,7 @@
/* disk lab functions */
-void* getoneblk(__u32);
+void getoneblk(char *, __u32, int);
+
#endif /* disklab.h */
diff --git a/extlinux.c b/extlinux.c
index d411794..f3f60bc 100644
--- a/extlinux.c
+++ b/extlinux.c
@@ -851,9 +851,9 @@ int read_file(struct open_file_t *file, char *buf, int size, int *have_more)
}
/**
- * init. the fs meta data
+ * init. the fs meta data, return the block size
*/
-void init_fs()
+int init_fs()
{
/* read the super block */
@@ -873,5 +873,7 @@ void init_fs()
PtrsPerBlock1 = 1 << (ClustBytesShift - 2 );
PtrsPerBlock2 = 1 << ( (ClustBytesShift - 2) * 2);
PtrsPerBlock3 = 1 << ( (ClustBytesShift - 2) * 3);
+
+ return blk_size;
}
diff --git a/ldlinux.c b/ldlinux.c
index db0403d..12a3aad 100644
--- a/ldlinux.c
+++ b/ldlinux.c
@@ -875,15 +875,15 @@ void bsr(__u8 *res, int num)
*res = i;
}
-/* init. the fs meta data */
-void init_fs()
+/* init. the fs meta data, return the block size */
+int init_fs()
{
int sectors_per_fat;
__u32 clust_num;
int RootDirSize;
/* get the fat bpb information */
- getlinsec(&fat, 0, 1);
+ getlinsec((char *)&fat, 0, 1);
TotalSectors = fat.bxSectors ? : fat.bsHugeSectors;
FAT = fat.bxResSectors;
@@ -907,5 +907,7 @@ void init_fs()
FATType = FAT16;
else
FATType = FAT32;
+
+ return blk_size;
}
diff --git a/main.c b/main.c
index d5c0943..130476d 100644
--- a/main.c
+++ b/main.c
@@ -67,6 +67,7 @@ int main(int argc, char *argv[])
int bytes_read = 0;
int total_bytes = 0;
+ int block_size = 0;
int have_more;
char *fs = argv[1];
char *filename = argv[2];
@@ -80,9 +81,7 @@ int main(int argc, char *argv[])
return 0;
}
- /* init. the cache */
- cache_init();
-
+
fd = open(fs, O_RDONLY);
if ( fd < 0 ) {
@@ -90,7 +89,9 @@ int main(int argc, char *argv[])
return 0;
}
- init_fs();
+ block_size = init_fs();
+
+ cache_init(block_size);
file = (struct open_file_t *)open_file(filename);
if ( ! file ) {