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