diff read_cache.c @ 34:1f29402ef2ef src

'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
author richwareham
date Thu, 30 May 2002 09:52:29 +0000
parents 328eadb3f37e
children 832ca4921e04
line wrap: on
line diff
--- a/read_cache.c	Thu May 09 11:57:24 2002 +0000
+++ b/read_cache.c	Thu May 30 09:52:29 2002 +0000
@@ -25,24 +25,74 @@
 #include "config.h"
 #endif
 
+#include "dvdnav.h"
 #include "read_cache.h"
 
+/* Read-ahead cache structure. */
+#if _MULTITHREAD_
+struct read_cache_s {
+  /* Bit of strange cross-linking going on here :) -- Gotta love C :) */
+  dvdnav_t    *dvd_self;
+};
+#else
+struct read_cache_s {
+  /* Read-ahead cache. */
+  uint8_t      *cache_buffer;
+  int32_t      cache_start_sector; /* -1 means cache invalid */
+  size_t       cache_block_count;
+  size_t       cache_malloc_size;
+  int          cache_valid;
+
+  /* Bit of strange cross-linking going on here :) -- Gotta love C :) */
+  dvdnav_t    *dvd_self;
+};
+#endif
+
+read_cache_t *dvdnav_read_cache_new(dvdnav_t* dvd_self) {
+  read_cache_t *me;
+
+  me = (read_cache_t*)malloc(sizeof(struct read_cache_s));
+
+  if(me) {
+    me->dvd_self = dvd_self;
+
+    dvdnav_read_cache_clear(me);
+    me->cache_buffer = NULL;
+  }
+  
+  /* this->cache_start_sector = -1;
+  this->cache_block_count = 0;
+  this->cache_valid = 0; */
+
+  return me;
+}
+
+void dvdnav_read_cache_free(read_cache_t* self) {
+  if(self->cache_buffer) {
+    free(self->cache_buffer);
+    self->cache_buffer = NULL;
+  }
+
+  free(self);
+}
+
 /* This function MUST be called whenever self->file changes. */
-void dvdnav_read_cache_clear(dvdnav_t *self) {
+void dvdnav_read_cache_clear(read_cache_t *self) {
   if(!self)
    return;
   
   self->cache_start_sector = -1;
   self->cache_valid = 0;
 }
+
 /* This function is called just after reading the NAV packet. */
-void dvdnav_pre_cache_blocks(dvdnav_t *self, int sector, size_t block_count) {
+void dvdnav_pre_cache_blocks(read_cache_t *self, int sector, size_t block_count) {
   int result;
  
   if(!self)
    return;
   
-  if(!self->use_read_ahead) {
+  if(!self->dvd_self->use_read_ahead) {
     self->cache_valid = 0;
     self->cache_start_sector = -1;
     return;
@@ -59,18 +109,18 @@
   }
   self->cache_start_sector = sector;
   self->cache_block_count = block_count;
-  result = DVDReadBlocks( self->file, sector, block_count, self->cache_buffer);
+  result = DVDReadBlocks( self->dvd_self->file, sector, block_count, self->cache_buffer);
   self->cache_valid = 1;
 }
 
 /* This function will do the cache read once implemented */
-int dvdnav_read_cache_block( dvdnav_t *self, int sector, size_t block_count, uint8_t *buf) {
+int dvdnav_read_cache_block( read_cache_t *self, int sector, size_t block_count, uint8_t *buf) {
   int result;
  
   if(!self)
    return 0;
 
-  if(self->cache_valid && self->use_read_ahead) {
+  if(self->cache_valid && self->dvd_self->use_read_ahead) {
     if (self->cache_start_sector != -1 ) {
       if ((sector >= self->cache_start_sector) && 
 	  (sector < self->cache_start_sector + self->cache_block_count)) {
@@ -79,12 +129,12 @@
       }
     }
   } else {
-    result = DVDReadBlocks( self->file, sector, block_count, buf);
+    result = DVDReadBlocks( self->dvd_self->file, sector, block_count, buf);
     return result;
   }
   
   fprintf(stderr,"DVD read cache miss! sector=%d, start=%d\n", sector, self->cache_start_sector); 
-  result = DVDReadBlocks( self->file, sector, block_count, buf);
+  result = DVDReadBlocks( self->dvd_self->file, sector, block_count, buf);
   return result;
 }