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