Mercurial > libdvdnav.hg
annotate read_cache.c @ 393:bba82ef5d75a src
cosmetics: Remove all trailing whitespace.
author | diego |
---|---|
date | Thu, 09 Oct 2008 22:20:01 +0000 |
parents | 0a5a6f03b029 |
children | 9c5aef10d165 |
rev | line source |
---|---|
105 | 1 /* |
0 | 2 * Copyright (C) 2000 Rich Wareham <richwareham@users.sourceforge.net> |
242
f794e1c17947
porting AMD64 patches from xine (provided by Goetz Waschk and Gwenole Beauchesne
mroi
parents:
230
diff
changeset
|
3 * 2001-2004 the dvdnav project |
105 | 4 * |
0 | 5 * This file is part of libdvdnav, a DVD navigation library. |
105 | 6 * |
0 | 7 * libdvdnav is free software; you can redistribute it and/or modify |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
105 | 11 * |
0 | 12 * libdvdnav is distributed in the hope that it will be useful, |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU General Public License for more details. | |
105 | 16 * |
389
d3c273ced49c
Use consistent license headers everywhere: Fix wrong FSF address.
diego
parents:
388
diff
changeset
|
17 * You should have received a copy of the GNU General Public License along |
d3c273ced49c
Use consistent license headers everywhere: Fix wrong FSF address.
diego
parents:
388
diff
changeset
|
18 * with libdvdnav; if not, write to the Free Software Foundation, Inc., |
d3c273ced49c
Use consistent license headers everywhere: Fix wrong FSF address.
diego
parents:
388
diff
changeset
|
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
0 | 20 */ |
225 | 21 /* |
22 * There was a multithreaded read ahead cache in here for some time, but | |
23 * it had only been used for a short time. If you want to have a look at it, | |
24 * search the CVS attic. | |
25 */ | |
0 | 26 |
27 #ifdef HAVE_CONFIG_H | |
28 #include "config.h" | |
29 #endif | |
30 | |
278 | 31 #include <inttypes.h> |
288
ce4230602517
moved away from dvdnav_internal.h inclusion of various system headers
nicodvb
parents:
285
diff
changeset
|
32 #include <stdlib.h> |
294
2146ff691bcd
include limits.h; it was included in the previous dvdnav_internal.h and without it players segfault
nicodvb
parents:
290
diff
changeset
|
33 #include <limits.h> |
290 | 34 #include <sys/time.h> |
35 #include <time.h> | |
285
52877d182e96
moved all header inclusions from .h to .c files; my word, I've never seen such a horrible entanglement as in this mess
nicodvb
parents:
278
diff
changeset
|
36 #include "dvd_types.h" |
386 | 37 #include <dvdread/nav_types.h> |
38 #include <dvdread/ifo_types.h> | |
285
52877d182e96
moved all header inclusions from .h to .c files; my word, I've never seen such a horrible entanglement as in this mess
nicodvb
parents:
278
diff
changeset
|
39 #include "remap.h" |
52877d182e96
moved all header inclusions from .h to .c files; my word, I've never seen such a horrible entanglement as in this mess
nicodvb
parents:
278
diff
changeset
|
40 #include "vm/decoder.h" |
52877d182e96
moved all header inclusions from .h to .c files; my word, I've never seen such a horrible entanglement as in this mess
nicodvb
parents:
278
diff
changeset
|
41 #include "vm/vm.h" |
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
|
42 #include "dvdnav.h" |
230 | 43 #include "dvdnav_internal.h" |
0 | 44 #include "read_cache.h" |
46 | 45 |
60 | 46 #define READ_CACHE_CHUNKS 10 |
47 | |
74
bf89c194f781
align read cache chunks in memory to allow use of raw devices
mroi
parents:
65
diff
changeset
|
48 /* 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
|
49 #define ALIGNMENT 2048 |
bf89c194f781
align read cache chunks in memory to allow use of raw devices
mroi
parents:
65
diff
changeset
|
50 |
105 | 51 #define READ_AHEAD_SIZE_MIN 4 |
52 #define READ_AHEAD_SIZE_MAX 512 | |
53 | |
60 | 54 typedef struct read_cache_chunk_s { |
55 uint8_t *cache_buffer; | |
74
bf89c194f781
align read cache chunks in memory to allow use of raw devices
mroi
parents:
65
diff
changeset
|
56 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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 int cache_valid; |
60 | 62 int usage_count; /* counts how many buffers where issued from this chunk */ |
63 } read_cache_chunk_t; | |
64 | |
65 struct read_cache_s { | |
66 read_cache_chunk_t chunk[READ_CACHE_CHUNKS]; | |
67 int current; | |
68 int freeing; /* is set to one when we are about to dispose the cache */ | |
166 | 69 uint32_t read_ahead_size; |
105 | 70 int read_ahead_incr; |
71 int last_sector; | |
65
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
72 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
|
73 |
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
|
74 /* Bit of strange cross-linking going on here :) -- Gotta love C :) */ |
60 | 75 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
|
76 }; |
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
|
77 |
105 | 78 /* |
76 | 79 #define READ_CACHE_TRACE 0 |
105 | 80 */ |
107
b2801805c433
include some fixes done in xine's copy to avoid merging conflicts
mroi
parents:
105
diff
changeset
|
81 |
b2801805c433
include some fixes done in xine's copy to avoid merging conflicts
mroi
parents:
105
diff
changeset
|
82 #ifdef __GNUC__ |
b2801805c433
include some fixes done in xine's copy to avoid merging conflicts
mroi
parents:
105
diff
changeset
|
83 # if READ_CACHE_TRACE |
114 | 84 # 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
|
85 # else |
b2801805c433
include some fixes done in xine's copy to avoid merging conflicts
mroi
parents:
105
diff
changeset
|
86 # define dprintf(fmt, args...) /* Nowt */ |
b2801805c433
include some fixes done in xine's copy to avoid merging conflicts
mroi
parents:
105
diff
changeset
|
87 # 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
|
88 #else |
107
b2801805c433
include some fixes done in xine's copy to avoid merging conflicts
mroi
parents:
105
diff
changeset
|
89 # if READ_CACHE_TRACE |
114 | 90 # 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
|
91 # else |
176 | 92 #ifdef _MSC_VER |
93 # define dprintf(fmt, str) /* Nowt */ | |
94 #else | |
107
b2801805c433
include some fixes done in xine's copy to avoid merging conflicts
mroi
parents:
105
diff
changeset
|
95 # define dprintf(fmt, ...) /* Nowt */ |
176 | 96 #endif /* _MSC_VER */ |
107
b2801805c433
include some fixes done in xine's copy to avoid merging conflicts
mroi
parents:
105
diff
changeset
|
97 # 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
|
98 #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
|
99 |
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
|
100 |
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
|
101 read_cache_t *dvdnav_read_cache_new(dvdnav_t* dvd_self) { |
60 | 102 read_cache_t *self; |
103 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
|
104 |
60 | 105 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
|
106 |
60 | 107 if(self) { |
108 self->current = 0; | |
109 self->freeing = 0; | |
110 self->dvd_self = dvd_self; | |
105 | 111 self->last_sector = 0; |
112 self->read_ahead_size = READ_AHEAD_SIZE_MIN; | |
113 self->read_ahead_incr = 0; | |
65
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
114 pthread_mutex_init(&self->lock, NULL); |
60 | 115 dvdnav_read_cache_clear(self); |
116 for (i = 0; i < READ_CACHE_CHUNKS; i++) { | |
117 self->chunk[i].cache_buffer = NULL; | |
118 self->chunk[i].usage_count = 0; | |
119 } | |
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
|
120 } |
105 | 121 |
60 | 122 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
|
123 } |
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
|
124 |
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
|
125 void dvdnav_read_cache_free(read_cache_t* self) { |
60 | 126 dvdnav_t *tmp; |
127 int i; | |
105 | 128 |
65
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
129 pthread_mutex_lock(&self->lock); |
60 | 130 self->freeing = 1; |
131 for (i = 0; i < READ_CACHE_CHUNKS; i++) | |
132 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
|
133 free(self->chunk[i].cache_buffer_base); |
60 | 134 self->chunk[i].cache_buffer = NULL; |
135 } | |
65
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
136 pthread_mutex_unlock(&self->lock); |
105 | 137 |
60 | 138 for (i = 0; i < READ_CACHE_CHUNKS; i++) |
139 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
|
140 |
60 | 141 /* all buffers returned, free everything */ |
142 tmp = self->dvd_self; | |
65
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
143 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
|
144 free(self); |
60 | 145 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
|
146 } |
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
|
147 |
0 | 148 /* 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
|
149 void dvdnav_read_cache_clear(read_cache_t *self) { |
60 | 150 int i; |
151 | |
0 | 152 if(!self) |
153 return; | |
105 | 154 |
65
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
155 pthread_mutex_lock(&self->lock); |
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
156 for (i = 0; i < READ_CACHE_CHUNKS; i++) |
60 | 157 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
|
158 pthread_mutex_unlock(&self->lock); |
0 | 159 } |
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
|
160 |
0 | 161 /* 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
|
162 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
|
163 int i, use; |
105 | 164 |
0 | 165 if(!self) |
60 | 166 return; |
105 | 167 |
60 | 168 if(!self->dvd_self->use_read_ahead) |
0 | 169 return; |
170 | |
65
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
171 pthread_mutex_lock(&self->lock); |
105 | 172 |
60 | 173 /* find a free cache chunk that best fits the required size */ |
174 use = -1; | |
175 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
|
176 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
|
177 self->chunk[i].cache_malloc_size >= block_count && |
60 | 178 (use == -1 || self->chunk[use].cache_malloc_size > self->chunk[i].cache_malloc_size)) |
179 use = i; | |
105 | 180 |
60 | 181 if (use == -1) { |
182 /* we haven't found a cache chunk, so we try to reallocate an existing one */ | |
183 for (i = 0; i < READ_CACHE_CHUNKS; i++) | |
184 if (self->chunk[i].usage_count == 0 && self->chunk[i].cache_buffer && | |
185 (use == -1 || self->chunk[use].cache_malloc_size < self->chunk[i].cache_malloc_size)) | |
186 use = i; | |
187 if (use >= 0) { | |
74
bf89c194f781
align read cache chunks in memory to allow use of raw devices
mroi
parents:
65
diff
changeset
|
188 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
|
189 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
|
190 self->chunk[use].cache_buffer = |
242
f794e1c17947
porting AMD64 patches from xine (provided by Goetz Waschk and Gwenole Beauchesne
mroi
parents:
230
diff
changeset
|
191 (uint8_t *)(((uintptr_t)self->chunk[use].cache_buffer_base & ~((uintptr_t)(ALIGNMENT - 1))) + ALIGNMENT); |
60 | 192 dprintf("pre_cache DVD read realloc happened\n"); |
193 self->chunk[use].cache_malloc_size = block_count; | |
194 } else { | |
195 /* we still haven't found a cache chunk, let's allocate a new one */ | |
196 for (i = 0; i < READ_CACHE_CHUNKS; i++) | |
197 if (!self->chunk[i].cache_buffer) { | |
198 use = i; | |
199 break; | |
200 } | |
201 if (use >= 0) { | |
202 /* We start with a sensible figure for the first malloc of 500 blocks. | |
203 * Some DVDs I have seen venture to 450 blocks. | |
204 * This is so that fewer realloc's happen if at all. | |
105 | 205 */ |
74
bf89c194f781
align read cache chunks in memory to allow use of raw devices
mroi
parents:
65
diff
changeset
|
206 self->chunk[i].cache_buffer_base = |
bf89c194f781
align read cache chunks in memory to allow use of raw devices
mroi
parents:
65
diff
changeset
|
207 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
|
208 self->chunk[i].cache_buffer = |
242
f794e1c17947
porting AMD64 patches from xine (provided by Goetz Waschk and Gwenole Beauchesne
mroi
parents:
230
diff
changeset
|
209 (uint8_t *)(((uintptr_t)self->chunk[i].cache_buffer_base & ~((uintptr_t)(ALIGNMENT - 1))) + ALIGNMENT); |
60 | 210 self->chunk[i].cache_malloc_size = block_count > 500 ? block_count : 500; |
211 dprintf("pre_cache DVD read malloc %d blocks\n", | |
105 | 212 (block_count > 500 ? block_count : 500 )); |
0 | 213 } |
214 } | |
48 | 215 } |
105 | 216 |
60 | 217 if (use >= 0) { |
218 self->chunk[use].cache_start_sector = sector; | |
219 self->chunk[use].cache_block_count = block_count; | |
103
8905d8de7e91
changes to read cache behaviour inspired by Thibaut Mattern:
mroi
parents:
76
diff
changeset
|
220 self->chunk[use].cache_read_count = 0; |
8905d8de7e91
changes to read cache behaviour inspired by Thibaut Mattern:
mroi
parents:
76
diff
changeset
|
221 self->chunk[use].cache_valid = 1; |
60 | 222 self->current = use; |
105 | 223 } else { |
60 | 224 dprintf("pre_caching was impossible, no cache chunk available\n"); |
105 | 225 } |
65
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
226 pthread_mutex_unlock(&self->lock); |
60 | 227 } |
228 | |
229 int dvdnav_read_cache_block(read_cache_t *self, int sector, size_t block_count, uint8_t **buf) { | |
230 int i, use; | |
105 | 231 int start; |
232 int size; | |
233 int incr; | |
234 uint8_t *read_ahead_buf; | |
235 int32_t res; | |
236 | |
60 | 237 if(!self) |
238 return 0; | |
105 | 239 |
60 | 240 use = -1; |
105 | 241 |
60 | 242 if(self->dvd_self->use_read_ahead) { |
243 /* first check, if sector is in current chunk */ | |
244 read_cache_chunk_t cur = self->chunk[self->current]; | |
245 if (cur.cache_valid && sector >= cur.cache_start_sector && | |
105 | 246 sector <= (cur.cache_start_sector + cur.cache_read_count) && |
60 | 247 sector + block_count <= cur.cache_start_sector + cur.cache_block_count) |
248 use = self->current; | |
249 else | |
250 for (i = 0; i < READ_CACHE_CHUNKS; i++) | |
105 | 251 if (self->chunk[i].cache_valid && |
252 sector >= self->chunk[i].cache_start_sector && | |
253 sector <= (self->chunk[i].cache_start_sector + self->chunk[i].cache_read_count) && | |
254 sector + block_count <= self->chunk[i].cache_start_sector + self->chunk[i].cache_block_count) | |
255 use = i; | |
60 | 256 } |
105 | 257 |
60 | 258 if (use >= 0) { |
108 | 259 read_cache_chunk_t *chunk; |
388 | 260 |
105 | 261 /* Increment read-ahead size if sector follows the last sector */ |
262 if (sector == (self->last_sector + 1)) { | |
225 | 263 if (self->read_ahead_incr < READ_AHEAD_SIZE_MAX) |
264 self->read_ahead_incr++; | |
105 | 265 } else { |
266 self->read_ahead_size = READ_AHEAD_SIZE_MIN; | |
267 self->read_ahead_incr = 0; | |
268 } | |
269 self->last_sector = sector; | |
270 | |
271 /* The following resources need to be protected by a mutex : | |
272 * self->chunk[*].cache_buffer | |
273 * self->chunk[*].cache_malloc_size | |
274 * self->chunk[*].usage_count | |
275 */ | |
276 pthread_mutex_lock(&self->lock); | |
108 | 277 chunk = &self->chunk[use]; |
105 | 278 read_ahead_buf = chunk->cache_buffer + chunk->cache_read_count * DVD_VIDEO_LB_LEN; |
279 *buf = chunk->cache_buffer + (sector - chunk->cache_start_sector) * DVD_VIDEO_LB_LEN; | |
280 chunk->usage_count++; | |
281 pthread_mutex_unlock(&self->lock); | |
282 | |
225 | 283 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 | 284 |
225 | 285 /* read_ahead_size */ |
286 incr = self->read_ahead_incr >> 1; | |
287 if ((self->read_ahead_size + incr) > READ_AHEAD_SIZE_MAX) { | |
288 self->read_ahead_size = READ_AHEAD_SIZE_MAX; | |
289 } else { | |
290 self->read_ahead_size += incr; | |
291 } | |
105 | 292 |
225 | 293 /* real read size */ |
294 start = chunk->cache_start_sector + chunk->cache_read_count; | |
295 if (chunk->cache_read_count + self->read_ahead_size > chunk->cache_block_count) { | |
296 size = chunk->cache_block_count - chunk->cache_read_count; | |
297 } else { | |
298 size = self->read_ahead_size; | |
299 /* ensure that the sector we want will be read */ | |
300 if (sector >= chunk->cache_start_sector + chunk->cache_read_count + size) | |
301 size = sector - chunk->cache_start_sector - chunk->cache_read_count; | |
302 } | |
303 dprintf("libdvdnav: read_ahead_size=%d, size=%d\n", self->read_ahead_size, size); | |
105 | 304 |
225 | 305 if (size) |
105 | 306 chunk->cache_read_count += DVDReadBlocks(self->dvd_self->file, |
307 start, | |
308 size, | |
309 read_ahead_buf); | |
310 | |
311 res = DVD_VIDEO_LB_LEN * block_count; | |
312 | |
60 | 313 } else { |
105 | 314 |
60 | 315 if (self->dvd_self->use_read_ahead) |
316 dprintf("cache miss on sector %d\n", sector); | |
105 | 317 |
318 res = DVDReadBlocks(self->dvd_self->file, | |
319 sector, | |
320 block_count, | |
321 *buf) * DVD_VIDEO_LB_LEN; | |
60 | 322 } |
105 | 323 |
324 return res; | |
325 | |
60 | 326 } |
327 | |
328 dvdnav_status_t dvdnav_free_cache_block(dvdnav_t *self, unsigned char *buf) { | |
329 read_cache_t *cache; | |
330 int i; | |
105 | 331 |
60 | 332 if (!self) |
333 return DVDNAV_STATUS_ERR; | |
105 | 334 |
60 | 335 cache = self->cache; |
336 if (!cache) | |
337 return DVDNAV_STATUS_ERR; | |
105 | 338 |
65
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
339 pthread_mutex_lock(&cache->lock); |
105 | 340 for (i = 0; i < READ_CACHE_CHUNKS; i++) { |
60 | 341 if (cache->chunk[i].cache_buffer && buf >= cache->chunk[i].cache_buffer && |
105 | 342 buf < cache->chunk[i].cache_buffer + cache->chunk[i].cache_malloc_size * DVD_VIDEO_LB_LEN) { |
60 | 343 cache->chunk[i].usage_count--; |
105 | 344 } |
345 } | |
65
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
346 pthread_mutex_unlock(&cache->lock); |
dcde6d9cea7a
ensure serialized access to the cache to avoid concurrent access on the
mroi
parents:
62
diff
changeset
|
347 |
60 | 348 if (cache->freeing) |
349 /* when we want to dispose the cache, try freeing it now */ | |
350 dvdnav_read_cache_free(cache); | |
105 | 351 |
60 | 352 return DVDNAV_STATUS_OK; |
0 | 353 } |