Mercurial > libdvdnav.hg
annotate read_cache.c @ 230:065a2835ba90 src
Patch to compile with mingw32 from: -
Gildas Bazin <gbazin AT altern.org>
| author | jcdutton |
|---|---|
| date | Sat, 31 Jan 2004 13:27:40 +0000 |
| parents | 9b1b740e3fc9 |
| children | f794e1c17947 |
| rev | line source |
|---|---|
| 105 | 1 /* |
| 0 | 2 * Copyright (C) 2000 Rich Wareham <richwareham@users.sourceforge.net> |
| 105 | 3 * |
| 0 | 4 * This file is part of libdvdnav, a DVD navigation library. |
| 105 | 5 * |
| 0 | 6 * libdvdnav is free software; you can redistribute it and/or modify |
| 7 * it under the terms of the GNU General Public License as published by | |
| 8 * the Free Software Foundation; either version 2 of the License, or | |
| 9 * (at your option) any later version. | |
| 105 | 10 * |
| 0 | 11 * libdvdnav is distributed in the hope that it will be useful, |
| 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 * GNU General Public License for more details. | |
| 105 | 15 * |
| 0 | 16 * You should have received a copy of the GNU General Public License |
| 17 * along with this program; if not, write to the Free Software | |
| 18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | |
| 19 * | |
| 20 * $Id$ | |
| 21 * | |
| 22 */ | |
| 225 | 23 /* |
| 24 * There was a multithreaded read ahead cache in here for some time, but | |
| 25 * it had only been used for a short time. If you want to have a look at it, | |
| 26 * search the CVS attic. | |
| 27 */ | |
| 0 | 28 |
| 29 #ifdef HAVE_CONFIG_H | |
| 30 #include "config.h" | |
| 31 #endif | |
| 32 | |
|
34
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
33 #include "dvdnav.h" |
| 230 | 34 #include "dvdnav_internal.h" |
| 0 | 35 #include "read_cache.h" |
| 46 | 36 #include <sys/time.h> |
| 37 #include <time.h> | |
| 38 | |
| 60 | 39 #define READ_CACHE_CHUNKS 10 |
| 40 | |
|
74
bf89c194f781
align read cache chunks in memory to allow use of raw devices
mroi
parents:
65
diff
changeset
|
41 /* all cache chunks must be memory aligned to allow use of raw devices */ |
|
bf89c194f781
align read cache chunks in memory to allow use of raw devices
mroi
parents:
65
diff
changeset
|
42 #define ALIGNMENT 2048 |
|
bf89c194f781
align read cache chunks in memory to allow use of raw devices
mroi
parents:
65
diff
changeset
|
43 |
| 105 | 44 #define READ_AHEAD_SIZE_MIN 4 |
| 45 #define READ_AHEAD_SIZE_MAX 512 | |
| 46 | |
| 60 | 47 typedef struct read_cache_chunk_s { |
| 48 uint8_t *cache_buffer; | |
|
74
bf89c194f781
align read cache chunks in memory to allow use of raw devices
mroi
parents:
65
diff
changeset
|
49 uint8_t *cache_buffer_base; /* used in malloc and free for alignment */ |
|
34
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
50 int32_t cache_start_sector; /* -1 means cache invalid */ |
|
103
8905d8de7e91
changes to read cache behaviour inspired by Thibaut Mattern:
mroi
parents:
76
diff
changeset
|
51 int32_t cache_read_count; /* this many sectors are already read */ |
|
8905d8de7e91
changes to read cache behaviour inspired by Thibaut Mattern:
mroi
parents:
76
diff
changeset
|
52 size_t cache_block_count; /* this many sectors will go in this chunk */ |
|
34
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
53 size_t cache_malloc_size; |
|
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
54 int cache_valid; |
| 60 | 55 int usage_count; /* counts how many buffers where issued from this chunk */ |
| 56 } read_cache_chunk_t; | |
| 57 | |
| 58 struct read_cache_s { | |
| 59 read_cache_chunk_t chunk[READ_CACHE_CHUNKS]; | |
| 60 int current; | |
| 61 int freeing; /* is set to one when we are about to dispose the cache */ | |
| 166 | 62 uint32_t read_ahead_size; |
| 105 | 63 int read_ahead_incr; |
| 64 int last_sector; | |
|
65
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
65 pthread_mutex_t lock; |
|
34
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
66 |
|
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
67 /* Bit of strange cross-linking going on here :) -- Gotta love C :) */ |
| 60 | 68 dvdnav_t *dvd_self; |
|
34
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
69 }; |
|
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
70 |
| 105 | 71 /* |
| 76 | 72 #define READ_CACHE_TRACE 0 |
| 105 | 73 */ |
|
107
b2801805c433
include some fixes done in xine's copy to avoid merging conflicts
mroi
parents:
105
diff
changeset
|
74 |
|
b2801805c433
include some fixes done in xine's copy to avoid merging conflicts
mroi
parents:
105
diff
changeset
|
75 #ifdef __GNUC__ |
|
b2801805c433
include some fixes done in xine's copy to avoid merging conflicts
mroi
parents:
105
diff
changeset
|
76 # if READ_CACHE_TRACE |
| 114 | 77 # define dprintf(fmt, args...) fprintf(MSG_OUT, "libdvdnav: %s: "fmt, __func__ , ## args) |
|
107
b2801805c433
include some fixes done in xine's copy to avoid merging conflicts
mroi
parents:
105
diff
changeset
|
78 # else |
|
b2801805c433
include some fixes done in xine's copy to avoid merging conflicts
mroi
parents:
105
diff
changeset
|
79 # define dprintf(fmt, args...) /* Nowt */ |
|
b2801805c433
include some fixes done in xine's copy to avoid merging conflicts
mroi
parents:
105
diff
changeset
|
80 # endif |
|
41
50e0855a2017
Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents:
40
diff
changeset
|
81 #else |
|
107
b2801805c433
include some fixes done in xine's copy to avoid merging conflicts
mroi
parents:
105
diff
changeset
|
82 # if READ_CACHE_TRACE |
| 114 | 83 # define dprintf(fmt, ...) fprintf(MSG_OUT, "libdvdnav: %s: "fmt, __func__ , __VA_ARGS__) |
|
107
b2801805c433
include some fixes done in xine's copy to avoid merging conflicts
mroi
parents:
105
diff
changeset
|
84 # else |
| 176 | 85 #ifdef _MSC_VER |
| 86 # define dprintf(fmt, str) /* Nowt */ | |
| 87 #else | |
|
107
b2801805c433
include some fixes done in xine's copy to avoid merging conflicts
mroi
parents:
105
diff
changeset
|
88 # define dprintf(fmt, ...) /* Nowt */ |
| 176 | 89 #endif /* _MSC_VER */ |
|
107
b2801805c433
include some fixes done in xine's copy to avoid merging conflicts
mroi
parents:
105
diff
changeset
|
90 # endif |
|
41
50e0855a2017
Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents:
40
diff
changeset
|
91 #endif |
|
50e0855a2017
Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents:
40
diff
changeset
|
92 |
|
40
a049c3753f32
Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents:
37
diff
changeset
|
93 |
|
34
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
94 read_cache_t *dvdnav_read_cache_new(dvdnav_t* dvd_self) { |
| 60 | 95 read_cache_t *self; |
| 96 int i; | |
|
34
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
97 |
| 60 | 98 self = (read_cache_t *)malloc(sizeof(read_cache_t)); |
|
34
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
99 |
| 60 | 100 if(self) { |
| 101 self->current = 0; | |
| 102 self->freeing = 0; | |
| 103 self->dvd_self = dvd_self; | |
| 105 | 104 self->last_sector = 0; |
| 105 self->read_ahead_size = READ_AHEAD_SIZE_MIN; | |
| 106 self->read_ahead_incr = 0; | |
|
65
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
107 pthread_mutex_init(&self->lock, NULL); |
| 60 | 108 dvdnav_read_cache_clear(self); |
| 109 for (i = 0; i < READ_CACHE_CHUNKS; i++) { | |
| 110 self->chunk[i].cache_buffer = NULL; | |
| 111 self->chunk[i].usage_count = 0; | |
| 112 } | |
|
34
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
113 } |
| 105 | 114 |
| 60 | 115 return self; |
|
34
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
116 } |
|
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
117 |
|
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
118 void dvdnav_read_cache_free(read_cache_t* self) { |
| 60 | 119 dvdnav_t *tmp; |
| 120 int i; | |
| 105 | 121 |
|
65
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
122 pthread_mutex_lock(&self->lock); |
| 60 | 123 self->freeing = 1; |
| 124 for (i = 0; i < READ_CACHE_CHUNKS; i++) | |
| 125 if (self->chunk[i].cache_buffer && self->chunk[i].usage_count == 0) { | |
|
74
bf89c194f781
align read cache chunks in memory to allow use of raw devices
mroi
parents:
65
diff
changeset
|
126 free(self->chunk[i].cache_buffer_base); |
| 60 | 127 self->chunk[i].cache_buffer = NULL; |
| 128 } | |
|
65
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
129 pthread_mutex_unlock(&self->lock); |
| 105 | 130 |
| 60 | 131 for (i = 0; i < READ_CACHE_CHUNKS; i++) |
| 132 if (self->chunk[i].cache_buffer) return; | |
|
34
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
133 |
| 60 | 134 /* all buffers returned, free everything */ |
| 135 tmp = self->dvd_self; | |
|
65
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
136 pthread_mutex_destroy(&self->lock); |
|
34
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
137 free(self); |
| 60 | 138 free(tmp); |
|
34
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
139 } |
|
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
140 |
| 0 | 141 /* This function MUST be called whenever self->file changes. */ |
|
34
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
142 void dvdnav_read_cache_clear(read_cache_t *self) { |
| 60 | 143 int i; |
| 144 | |
| 0 | 145 if(!self) |
| 146 return; | |
| 105 | 147 |
|
65
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
148 pthread_mutex_lock(&self->lock); |
|
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
149 for (i = 0; i < READ_CACHE_CHUNKS; i++) |
| 60 | 150 self->chunk[i].cache_valid = 0; |
|
65
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
151 pthread_mutex_unlock(&self->lock); |
| 0 | 152 } |
|
34
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
153 |
| 0 | 154 /* This function is called just after reading the NAV packet. */ |
|
34
1f29402ef2ef
'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents:
3
diff
changeset
|
155 void dvdnav_pre_cache_blocks(read_cache_t *self, int sector, size_t block_count) { |
|
103
8905d8de7e91
changes to read cache behaviour inspired by Thibaut Mattern:
mroi
parents:
76
diff
changeset
|
156 int i, use; |
| 105 | 157 |
| 0 | 158 if(!self) |
| 60 | 159 return; |
| 105 | 160 |
| 60 | 161 if(!self->dvd_self->use_read_ahead) |
| 0 | 162 return; |
| 163 | |
|
65
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
164 pthread_mutex_lock(&self->lock); |
| 105 | 165 |
| 60 | 166 /* find a free cache chunk that best fits the required size */ |
| 167 use = -1; | |
| 168 for (i = 0; i < READ_CACHE_CHUNKS; i++) | |
|
61
6b7520caf9a1
fix stupid bug: test if the buffer is there before using it
mroi
parents:
60
diff
changeset
|
169 if (self->chunk[i].usage_count == 0 && self->chunk[i].cache_buffer && |
|
6b7520caf9a1
fix stupid bug: test if the buffer is there before using it
mroi
parents:
60
diff
changeset
|
170 self->chunk[i].cache_malloc_size >= block_count && |
| 60 | 171 (use == -1 || self->chunk[use].cache_malloc_size > self->chunk[i].cache_malloc_size)) |
| 172 use = i; | |
| 105 | 173 |
| 60 | 174 if (use == -1) { |
| 175 /* we haven't found a cache chunk, so we try to reallocate an existing one */ | |
| 176 for (i = 0; i < READ_CACHE_CHUNKS; i++) | |
| 177 if (self->chunk[i].usage_count == 0 && self->chunk[i].cache_buffer && | |
| 178 (use == -1 || self->chunk[use].cache_malloc_size < self->chunk[i].cache_malloc_size)) | |
| 179 use = i; | |
| 180 if (use >= 0) { | |
|
74
bf89c194f781
align read cache chunks in memory to allow use of raw devices
mroi
parents:
65
diff
changeset
|
181 self->chunk[use].cache_buffer_base = realloc(self->chunk[use].cache_buffer_base, |
|
bf89c194f781
align read cache chunks in memory to allow use of raw devices
mroi
parents:
65
diff
changeset
|
182 block_count * DVD_VIDEO_LB_LEN + ALIGNMENT); |
|
bf89c194f781
align read cache chunks in memory to allow use of raw devices
mroi
parents:
65
diff
changeset
|
183 self->chunk[use].cache_buffer = |
|
211
7eda95524e83
use uintptr_t for pointer arithmetics for AMD64 compatibility
mroi
parents:
176
diff
changeset
|
184 (uint8_t *)(((uintptr_t)self->chunk[use].cache_buffer_base & ~(ALIGNMENT - 1)) + ALIGNMENT); |
| 60 | 185 dprintf("pre_cache DVD read realloc happened\n"); |
| 186 self->chunk[use].cache_malloc_size = block_count; | |
| 187 } else { | |
| 188 /* we still haven't found a cache chunk, let's allocate a new one */ | |
| 189 for (i = 0; i < READ_CACHE_CHUNKS; i++) | |
| 190 if (!self->chunk[i].cache_buffer) { | |
| 191 use = i; | |
| 192 break; | |
| 193 } | |
| 194 if (use >= 0) { | |
| 195 /* We start with a sensible figure for the first malloc of 500 blocks. | |
| 196 * Some DVDs I have seen venture to 450 blocks. | |
| 197 * This is so that fewer realloc's happen if at all. | |
| 105 | 198 */ |
|
74
bf89c194f781
align read cache chunks in memory to allow use of raw devices
mroi
parents:
65
diff
changeset
|
199 self->chunk[i].cache_buffer_base = |
|
bf89c194f781
align read cache chunks in memory to allow use of raw devices
mroi
parents:
65
diff
changeset
|
200 malloc((block_count > 500 ? block_count : 500) * DVD_VIDEO_LB_LEN + ALIGNMENT); |
|
bf89c194f781
align read cache chunks in memory to allow use of raw devices
mroi
parents:
65
diff
changeset
|
201 self->chunk[i].cache_buffer = |
|
211
7eda95524e83
use uintptr_t for pointer arithmetics for AMD64 compatibility
mroi
parents:
176
diff
changeset
|
202 (uint8_t *)(((uintptr_t)self->chunk[i].cache_buffer_base & ~(ALIGNMENT - 1)) + ALIGNMENT); |
| 60 | 203 self->chunk[i].cache_malloc_size = block_count > 500 ? block_count : 500; |
| 204 dprintf("pre_cache DVD read malloc %d blocks\n", | |
| 105 | 205 (block_count > 500 ? block_count : 500 )); |
| 0 | 206 } |
| 207 } | |
| 48 | 208 } |
| 105 | 209 |
| 60 | 210 if (use >= 0) { |
| 211 self->chunk[use].cache_start_sector = sector; | |
| 212 self->chunk[use].cache_block_count = block_count; | |
|
103
8905d8de7e91
changes to read cache behaviour inspired by Thibaut Mattern:
mroi
parents:
76
diff
changeset
|
213 self->chunk[use].cache_read_count = 0; |
|
8905d8de7e91
changes to read cache behaviour inspired by Thibaut Mattern:
mroi
parents:
76
diff
changeset
|
214 self->chunk[use].cache_valid = 1; |
| 60 | 215 self->current = use; |
| 105 | 216 } else { |
| 60 | 217 dprintf("pre_caching was impossible, no cache chunk available\n"); |
| 105 | 218 } |
|
65
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
219 pthread_mutex_unlock(&self->lock); |
| 60 | 220 } |
| 221 | |
| 222 int dvdnav_read_cache_block(read_cache_t *self, int sector, size_t block_count, uint8_t **buf) { | |
| 223 int i, use; | |
| 105 | 224 int start; |
| 225 int size; | |
| 226 int incr; | |
| 227 uint8_t *read_ahead_buf; | |
| 228 int32_t res; | |
| 229 | |
| 60 | 230 if(!self) |
| 231 return 0; | |
| 105 | 232 |
| 60 | 233 use = -1; |
| 105 | 234 |
| 60 | 235 if(self->dvd_self->use_read_ahead) { |
| 236 /* first check, if sector is in current chunk */ | |
| 237 read_cache_chunk_t cur = self->chunk[self->current]; | |
| 238 if (cur.cache_valid && sector >= cur.cache_start_sector && | |
| 105 | 239 sector <= (cur.cache_start_sector + cur.cache_read_count) && |
| 60 | 240 sector + block_count <= cur.cache_start_sector + cur.cache_block_count) |
| 241 use = self->current; | |
| 242 else | |
| 243 for (i = 0; i < READ_CACHE_CHUNKS; i++) | |
| 105 | 244 if (self->chunk[i].cache_valid && |
| 245 sector >= self->chunk[i].cache_start_sector && | |
| 246 sector <= (self->chunk[i].cache_start_sector + self->chunk[i].cache_read_count) && | |
| 247 sector + block_count <= self->chunk[i].cache_start_sector + self->chunk[i].cache_block_count) | |
| 248 use = i; | |
| 60 | 249 } |
| 105 | 250 |
| 60 | 251 if (use >= 0) { |
| 108 | 252 read_cache_chunk_t *chunk; |
| 253 | |
| 105 | 254 /* Increment read-ahead size if sector follows the last sector */ |
| 255 if (sector == (self->last_sector + 1)) { | |
| 225 | 256 if (self->read_ahead_incr < READ_AHEAD_SIZE_MAX) |
| 257 self->read_ahead_incr++; | |
| 105 | 258 } else { |
| 259 self->read_ahead_size = READ_AHEAD_SIZE_MIN; | |
| 260 self->read_ahead_incr = 0; | |
| 261 } | |
| 262 self->last_sector = sector; | |
| 263 | |
| 264 /* The following resources need to be protected by a mutex : | |
| 265 * self->chunk[*].cache_buffer | |
| 266 * self->chunk[*].cache_malloc_size | |
| 267 * self->chunk[*].usage_count | |
| 268 */ | |
| 269 pthread_mutex_lock(&self->lock); | |
| 108 | 270 chunk = &self->chunk[use]; |
| 105 | 271 read_ahead_buf = chunk->cache_buffer + chunk->cache_read_count * DVD_VIDEO_LB_LEN; |
| 272 *buf = chunk->cache_buffer + (sector - chunk->cache_start_sector) * DVD_VIDEO_LB_LEN; | |
| 273 chunk->usage_count++; | |
| 274 pthread_mutex_unlock(&self->lock); | |
| 275 | |
| 225 | 276 dprintf("libdvdnav: sector=%d, start_sector=%d, last_sector=%d\n", sector, chunk->cache_start_sector, chunk->cache_start_sector + chunk->cache_block_count); |
| 105 | 277 |
| 225 | 278 /* read_ahead_size */ |
| 279 incr = self->read_ahead_incr >> 1; | |
| 280 if ((self->read_ahead_size + incr) > READ_AHEAD_SIZE_MAX) { | |
| 281 self->read_ahead_size = READ_AHEAD_SIZE_MAX; | |
| 282 } else { | |
| 283 self->read_ahead_size += incr; | |
| 284 } | |
| 105 | 285 |
| 225 | 286 /* real read size */ |
| 287 start = chunk->cache_start_sector + chunk->cache_read_count; | |
| 288 if (chunk->cache_read_count + self->read_ahead_size > chunk->cache_block_count) { | |
| 289 size = chunk->cache_block_count - chunk->cache_read_count; | |
| 290 } else { | |
| 291 size = self->read_ahead_size; | |
| 292 /* ensure that the sector we want will be read */ | |
| 293 if (sector >= chunk->cache_start_sector + chunk->cache_read_count + size) | |
| 294 size = sector - chunk->cache_start_sector - chunk->cache_read_count; | |
| 295 } | |
| 296 dprintf("libdvdnav: read_ahead_size=%d, size=%d\n", self->read_ahead_size, size); | |
| 105 | 297 |
| 225 | 298 if (size) |
| 105 | 299 chunk->cache_read_count += DVDReadBlocks(self->dvd_self->file, |
| 300 start, | |
| 301 size, | |
| 302 read_ahead_buf); | |
| 303 | |
| 304 res = DVD_VIDEO_LB_LEN * block_count; | |
| 305 | |
| 60 | 306 } else { |
| 105 | 307 |
| 60 | 308 if (self->dvd_self->use_read_ahead) |
| 309 dprintf("cache miss on sector %d\n", sector); | |
| 105 | 310 |
| 311 res = DVDReadBlocks(self->dvd_self->file, | |
| 312 sector, | |
| 313 block_count, | |
| 314 *buf) * DVD_VIDEO_LB_LEN; | |
| 60 | 315 } |
| 105 | 316 |
| 317 return res; | |
| 318 | |
| 60 | 319 } |
| 320 | |
| 321 dvdnav_status_t dvdnav_free_cache_block(dvdnav_t *self, unsigned char *buf) { | |
| 322 read_cache_t *cache; | |
| 323 int i; | |
| 105 | 324 |
| 60 | 325 if (!self) |
| 326 return DVDNAV_STATUS_ERR; | |
| 105 | 327 |
| 60 | 328 cache = self->cache; |
| 329 if (!cache) | |
| 330 return DVDNAV_STATUS_ERR; | |
| 105 | 331 |
|
65
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
332 pthread_mutex_lock(&cache->lock); |
| 105 | 333 for (i = 0; i < READ_CACHE_CHUNKS; i++) { |
| 60 | 334 if (cache->chunk[i].cache_buffer && buf >= cache->chunk[i].cache_buffer && |
| 105 | 335 buf < cache->chunk[i].cache_buffer + cache->chunk[i].cache_malloc_size * DVD_VIDEO_LB_LEN) { |
| 60 | 336 cache->chunk[i].usage_count--; |
| 105 | 337 } |
| 338 } | |
|
65
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
339 pthread_mutex_unlock(&cache->lock); |
|
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
340 |
| 60 | 341 if (cache->freeing) |
| 342 /* when we want to dispose the cache, try freeing it now */ | |
| 343 dvdnav_read_cache_free(cache); | |
| 105 | 344 |
| 60 | 345 return DVDNAV_STATUS_OK; |
| 0 | 346 } |
