annotate read_cache.c @ 34:1f29402ef2ef src

'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
author richwareham
date Thu, 30 May 2002 09:52:29 +0000
parents 328eadb3f37e
children 832ca4921e04
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
1 /*
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
2 * Copyright (C) 2000 Rich Wareham <richwareham@users.sourceforge.net>
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
3 *
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
4 * This file is part of libdvdnav, a DVD navigation library.
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
5 *
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
6 * libdvdnav is free software; you can redistribute it and/or modify
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
8 * the Free Software Foundation; either version 2 of the License, or
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
9 * (at your option) any later version.
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
10 *
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
11 * libdvdnav is distributed in the hope that it will be useful,
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
14 * GNU General Public License for more details.
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
15 *
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
16 * You should have received a copy of the GNU General Public License
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
17 * along with this program; if not, write to the Free Software
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
19 *
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
20 * $Id$
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
21 *
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
22 */
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
23
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
25 #include "config.h"
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
26 #endif
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
27
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
28 #include "dvdnav.h"
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
29 #include "read_cache.h"
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
30
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
31 /* Read-ahead cache structure. */
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
32 #if _MULTITHREAD_
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 struct read_cache_s {
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
34 /* Bit of strange cross-linking going on here :) -- Gotta love C :) */
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 dvdnav_t *dvd_self;
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
36 };
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
37 #else
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
38 struct read_cache_s {
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
39 /* Read-ahead cache. */
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
40 uint8_t *cache_buffer;
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
41 int32_t cache_start_sector; /* -1 means cache invalid */
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 size_t cache_block_count;
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
43 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
44 int cache_valid;
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
45
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 /* Bit of strange cross-linking going on here :) -- Gotta love C :) */
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
47 dvdnav_t *dvd_self;
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
48 };
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
49 #endif
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
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
51 read_cache_t *dvdnav_read_cache_new(dvdnav_t* dvd_self) {
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 read_cache_t *me;
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
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 me = (read_cache_t*)malloc(sizeof(struct read_cache_s));
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
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 if(me) {
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 me->dvd_self = dvd_self;
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
58
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
59 dvdnav_read_cache_clear(me);
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 me->cache_buffer = NULL;
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 }
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
62
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
63 /* this->cache_start_sector = -1;
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 this->cache_block_count = 0;
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 this->cache_valid = 0; */
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 return me;
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
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 void dvdnav_read_cache_free(read_cache_t* self) {
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 if(self->cache_buffer) {
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 free(self->cache_buffer);
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 self->cache_buffer = NULL;
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 }
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
75
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 free(self);
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
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
79 /* 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
80 void dvdnav_read_cache_clear(read_cache_t *self) {
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
81 if(!self)
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
82 return;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
83
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
84 self->cache_start_sector = -1;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
85 self->cache_valid = 0;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
86 }
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
87
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
88 /* 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
89 void dvdnav_pre_cache_blocks(read_cache_t *self, int sector, size_t block_count) {
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
90 int result;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
91
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
92 if(!self)
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
93 return;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
94
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
95 if(!self->dvd_self->use_read_ahead) {
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
96 self->cache_valid = 0;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
97 self->cache_start_sector = -1;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
98 return;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
99 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
100
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
101 if (self->cache_buffer) {
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
102 if( block_count > self->cache_malloc_size) {
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
103 self->cache_buffer = realloc(self->cache_buffer, block_count * DVD_VIDEO_LB_LEN);
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
104 self->cache_malloc_size = block_count;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
105 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
106 } else {
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
107 self->cache_buffer = malloc(block_count * DVD_VIDEO_LB_LEN);
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
108 self->cache_malloc_size = block_count;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
109 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
110 self->cache_start_sector = sector;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
111 self->cache_block_count = block_count;
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
112 result = DVDReadBlocks( self->dvd_self->file, sector, block_count, self->cache_buffer);
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
113 self->cache_valid = 1;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
114 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
115
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
116 /* This function will do the cache read once implemented */
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
117 int dvdnav_read_cache_block( read_cache_t *self, int sector, size_t block_count, uint8_t *buf) {
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
118 int result;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
119
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
120 if(!self)
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
121 return 0;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
122
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 if(self->cache_valid && self->dvd_self->use_read_ahead) {
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
124 if (self->cache_start_sector != -1 ) {
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
125 if ((sector >= self->cache_start_sector) &&
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
126 (sector < self->cache_start_sector + self->cache_block_count)) {
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
127 memcpy(buf, self->cache_buffer + ((off_t)((off_t)sector - (off_t)self->cache_start_sector) * DVD_VIDEO_LB_LEN), DVD_VIDEO_LB_LEN);
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
128 return DVD_VIDEO_LB_LEN;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
129 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
130 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
131 } else {
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
132 result = DVDReadBlocks( self->dvd_self->file, sector, block_count, buf);
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
133 return result;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
134 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
135
3
328eadb3f37e Added initial example programs directory and make sure all debug/error output goes to stderr.
richwareham
parents: 0
diff changeset
136 fprintf(stderr,"DVD read cache miss! sector=%d, start=%d\n", sector, self->cache_start_sector);
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 result = DVDReadBlocks( self->dvd_self->file, sector, block_count, buf);
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
138 return result;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
139 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
140
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
141