Mercurial > libdvdnav.hg
comparison 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 |
comparison
equal
deleted
inserted
replaced
33:ef2136c4e7b2 | 34:1f29402ef2ef |
---|---|
23 | 23 |
24 #ifdef HAVE_CONFIG_H | 24 #ifdef HAVE_CONFIG_H |
25 #include "config.h" | 25 #include "config.h" |
26 #endif | 26 #endif |
27 | 27 |
28 #include "dvdnav.h" | |
28 #include "read_cache.h" | 29 #include "read_cache.h" |
29 | 30 |
31 /* Read-ahead cache structure. */ | |
32 #if _MULTITHREAD_ | |
33 struct read_cache_s { | |
34 /* Bit of strange cross-linking going on here :) -- Gotta love C :) */ | |
35 dvdnav_t *dvd_self; | |
36 }; | |
37 #else | |
38 struct read_cache_s { | |
39 /* Read-ahead cache. */ | |
40 uint8_t *cache_buffer; | |
41 int32_t cache_start_sector; /* -1 means cache invalid */ | |
42 size_t cache_block_count; | |
43 size_t cache_malloc_size; | |
44 int cache_valid; | |
45 | |
46 /* Bit of strange cross-linking going on here :) -- Gotta love C :) */ | |
47 dvdnav_t *dvd_self; | |
48 }; | |
49 #endif | |
50 | |
51 read_cache_t *dvdnav_read_cache_new(dvdnav_t* dvd_self) { | |
52 read_cache_t *me; | |
53 | |
54 me = (read_cache_t*)malloc(sizeof(struct read_cache_s)); | |
55 | |
56 if(me) { | |
57 me->dvd_self = dvd_self; | |
58 | |
59 dvdnav_read_cache_clear(me); | |
60 me->cache_buffer = NULL; | |
61 } | |
62 | |
63 /* this->cache_start_sector = -1; | |
64 this->cache_block_count = 0; | |
65 this->cache_valid = 0; */ | |
66 | |
67 return me; | |
68 } | |
69 | |
70 void dvdnav_read_cache_free(read_cache_t* self) { | |
71 if(self->cache_buffer) { | |
72 free(self->cache_buffer); | |
73 self->cache_buffer = NULL; | |
74 } | |
75 | |
76 free(self); | |
77 } | |
78 | |
30 /* This function MUST be called whenever self->file changes. */ | 79 /* This function MUST be called whenever self->file changes. */ |
31 void dvdnav_read_cache_clear(dvdnav_t *self) { | 80 void dvdnav_read_cache_clear(read_cache_t *self) { |
32 if(!self) | 81 if(!self) |
33 return; | 82 return; |
34 | 83 |
35 self->cache_start_sector = -1; | 84 self->cache_start_sector = -1; |
36 self->cache_valid = 0; | 85 self->cache_valid = 0; |
37 } | 86 } |
87 | |
38 /* This function is called just after reading the NAV packet. */ | 88 /* This function is called just after reading the NAV packet. */ |
39 void dvdnav_pre_cache_blocks(dvdnav_t *self, int sector, size_t block_count) { | 89 void dvdnav_pre_cache_blocks(read_cache_t *self, int sector, size_t block_count) { |
40 int result; | 90 int result; |
41 | 91 |
42 if(!self) | 92 if(!self) |
43 return; | 93 return; |
44 | 94 |
45 if(!self->use_read_ahead) { | 95 if(!self->dvd_self->use_read_ahead) { |
46 self->cache_valid = 0; | 96 self->cache_valid = 0; |
47 self->cache_start_sector = -1; | 97 self->cache_start_sector = -1; |
48 return; | 98 return; |
49 } | 99 } |
50 | 100 |
57 self->cache_buffer = malloc(block_count * DVD_VIDEO_LB_LEN); | 107 self->cache_buffer = malloc(block_count * DVD_VIDEO_LB_LEN); |
58 self->cache_malloc_size = block_count; | 108 self->cache_malloc_size = block_count; |
59 } | 109 } |
60 self->cache_start_sector = sector; | 110 self->cache_start_sector = sector; |
61 self->cache_block_count = block_count; | 111 self->cache_block_count = block_count; |
62 result = DVDReadBlocks( self->file, sector, block_count, self->cache_buffer); | 112 result = DVDReadBlocks( self->dvd_self->file, sector, block_count, self->cache_buffer); |
63 self->cache_valid = 1; | 113 self->cache_valid = 1; |
64 } | 114 } |
65 | 115 |
66 /* This function will do the cache read once implemented */ | 116 /* This function will do the cache read once implemented */ |
67 int dvdnav_read_cache_block( dvdnav_t *self, int sector, size_t block_count, uint8_t *buf) { | 117 int dvdnav_read_cache_block( read_cache_t *self, int sector, size_t block_count, uint8_t *buf) { |
68 int result; | 118 int result; |
69 | 119 |
70 if(!self) | 120 if(!self) |
71 return 0; | 121 return 0; |
72 | 122 |
73 if(self->cache_valid && self->use_read_ahead) { | 123 if(self->cache_valid && self->dvd_self->use_read_ahead) { |
74 if (self->cache_start_sector != -1 ) { | 124 if (self->cache_start_sector != -1 ) { |
75 if ((sector >= self->cache_start_sector) && | 125 if ((sector >= self->cache_start_sector) && |
76 (sector < self->cache_start_sector + self->cache_block_count)) { | 126 (sector < self->cache_start_sector + self->cache_block_count)) { |
77 memcpy(buf, self->cache_buffer + ((off_t)((off_t)sector - (off_t)self->cache_start_sector) * DVD_VIDEO_LB_LEN), DVD_VIDEO_LB_LEN); | 127 memcpy(buf, self->cache_buffer + ((off_t)((off_t)sector - (off_t)self->cache_start_sector) * DVD_VIDEO_LB_LEN), DVD_VIDEO_LB_LEN); |
78 return DVD_VIDEO_LB_LEN; | 128 return DVD_VIDEO_LB_LEN; |
79 } | 129 } |
80 } | 130 } |
81 } else { | 131 } else { |
82 result = DVDReadBlocks( self->file, sector, block_count, buf); | 132 result = DVDReadBlocks( self->dvd_self->file, sector, block_count, buf); |
83 return result; | 133 return result; |
84 } | 134 } |
85 | 135 |
86 fprintf(stderr,"DVD read cache miss! sector=%d, start=%d\n", sector, self->cache_start_sector); | 136 fprintf(stderr,"DVD read cache miss! sector=%d, start=%d\n", sector, self->cache_start_sector); |
87 result = DVDReadBlocks( self->file, sector, block_count, buf); | 137 result = DVDReadBlocks( self->dvd_self->file, sector, block_count, buf); |
88 return result; | 138 return result; |
89 } | 139 } |
90 | 140 |
91 | 141 |