1 |
786 |
skrzyp |
#ifndef CYGONCE_BLIB_H
|
2 |
|
|
#define CYGONCE_BLIB_H
|
3 |
|
|
//==========================================================================
|
4 |
|
|
//
|
5 |
|
|
// blib.h
|
6 |
|
|
//
|
7 |
|
|
// Block cache and access library
|
8 |
|
|
//
|
9 |
|
|
//==========================================================================
|
10 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
11 |
|
|
// -------------------------------------------
|
12 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
13 |
|
|
// Copyright (C) 2003 Free Software Foundation, Inc.
|
14 |
|
|
//
|
15 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
16 |
|
|
// the terms of the GNU General Public License as published by the Free
|
17 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
18 |
|
|
// version.
|
19 |
|
|
//
|
20 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
21 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
22 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
23 |
|
|
// for more details.
|
24 |
|
|
//
|
25 |
|
|
// You should have received a copy of the GNU General Public License
|
26 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
27 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
28 |
|
|
//
|
29 |
|
|
// As a special exception, if other files instantiate templates or use
|
30 |
|
|
// macros or inline functions from this file, or you compile this file
|
31 |
|
|
// and link it with other works to produce a work based on this file,
|
32 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
33 |
|
|
// the GNU General Public License. However the source code for this file
|
34 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
35 |
|
|
// General Public License v2.
|
36 |
|
|
//
|
37 |
|
|
// This exception does not invalidate any other reasons why a work based
|
38 |
|
|
// on this file might be covered by the GNU General Public License.
|
39 |
|
|
// -------------------------------------------
|
40 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
41 |
|
|
//==========================================================================
|
42 |
|
|
//#####DESCRIPTIONBEGIN####
|
43 |
|
|
//
|
44 |
|
|
// Author(s): savin
|
45 |
|
|
// Date: 2003-08-29
|
46 |
|
|
// Description:
|
47 |
|
|
//
|
48 |
|
|
//####DESCRIPTIONEND####
|
49 |
|
|
//
|
50 |
|
|
//==========================================================================
|
51 |
|
|
|
52 |
|
|
#include <cyg/infra/cyg_type.h>
|
53 |
|
|
#include <cyg/io/io.h>
|
54 |
|
|
|
55 |
|
|
#include <linux/rbtree.h>
|
56 |
|
|
#include <linux/list.h>
|
57 |
|
|
|
58 |
|
|
// --------------------------------------------------------------------
|
59 |
|
|
|
60 |
|
|
typedef int (*cyg_blib_bread_fn) (
|
61 |
|
|
void*, // private data
|
62 |
|
|
void*, // block buffer
|
63 |
|
|
cyg_uint32*, // number of blocks to read
|
64 |
|
|
cyg_uint32 // starting block number
|
65 |
|
|
);
|
66 |
|
|
|
67 |
|
|
typedef int (*cyg_blib_bwrite_fn) (
|
68 |
|
|
void*, // private data
|
69 |
|
|
const void*, // block buffer
|
70 |
|
|
cyg_uint32*, // number of blocks to write
|
71 |
|
|
cyg_uint32 // starting block number
|
72 |
|
|
);
|
73 |
|
|
|
74 |
|
|
typedef struct {
|
75 |
|
|
cyg_uint32 n_gets; // number of block gets
|
76 |
|
|
cyg_uint32 n_reads; // number of block reads
|
77 |
|
|
cyg_uint32 n_writes; // number of block writes
|
78 |
|
|
} cyg_blib_stat_t;
|
79 |
|
|
|
80 |
|
|
typedef struct {
|
81 |
|
|
void *priv; // private data
|
82 |
|
|
struct list_head list_head; // head of block list
|
83 |
|
|
struct rb_root rb_root; // block red-black tree root
|
84 |
|
|
cyg_uint32 block_size; // block size
|
85 |
|
|
cyg_uint32 block_size_log2; // block size log2
|
86 |
|
|
cyg_uint8 *mem_base; // memory base
|
87 |
|
|
cyg_uint32 mem_size; // memory size
|
88 |
|
|
struct list_head free_list_head; // list of free blocks
|
89 |
|
|
cyg_blib_bread_fn bread_fn; // block read function
|
90 |
|
|
cyg_blib_bwrite_fn bwrite_fn; // block write function
|
91 |
|
|
#ifdef CYGIMP_BLOCK_LIB_STATISTICS
|
92 |
|
|
cyg_blib_stat_t stat; // statistics
|
93 |
|
|
#endif
|
94 |
|
|
} cyg_blib_t;
|
95 |
|
|
|
96 |
|
|
// --------------------------------------------------------------------
|
97 |
|
|
|
98 |
|
|
// --------------------------------------------------------------------
|
99 |
|
|
// Creates a block lib instance
|
100 |
|
|
//
|
101 |
|
|
// priv_data - private data to pass to bread_fn and bwrite_fn
|
102 |
|
|
// mem_base - block cache memory base
|
103 |
|
|
// mem_size - block cache memory size
|
104 |
|
|
// block_size - block size
|
105 |
|
|
// bread_fn - function which reads blocks
|
106 |
|
|
// bwrite_fn - function which writes blocks
|
107 |
|
|
// bl - block lib instance space holder
|
108 |
|
|
//
|
109 |
|
|
// returns ENOERR if create succeded
|
110 |
|
|
//
|
111 |
|
|
|
112 |
|
|
int cyg_blib_create(void *priv_data,
|
113 |
|
|
void *mem_base,
|
114 |
|
|
cyg_uint32 mem_size,
|
115 |
|
|
cyg_uint32 block_size,
|
116 |
|
|
cyg_blib_bread_fn bread_fn,
|
117 |
|
|
cyg_blib_bwrite_fn bwrite_fn,
|
118 |
|
|
cyg_blib_t *bl);
|
119 |
|
|
|
120 |
|
|
// --------------------------------------------------------------------
|
121 |
|
|
// Creates a block lib instance on top of IO system
|
122 |
|
|
// (cyg_io_bread and cyg_io_bwrite)
|
123 |
|
|
//
|
124 |
|
|
// handle - cyg_io_handle_t
|
125 |
|
|
// mem_base - block cache memory base
|
126 |
|
|
// mem_size - block cache memory size
|
127 |
|
|
// block_size - block size
|
128 |
|
|
// bl - block lib instance space holder
|
129 |
|
|
//
|
130 |
|
|
// returns ENOERR if create succeded
|
131 |
|
|
//
|
132 |
|
|
|
133 |
|
|
int cyg_blib_io_create(cyg_io_handle_t handle,
|
134 |
|
|
void *mem_base,
|
135 |
|
|
cyg_uint32 mem_size,
|
136 |
|
|
cyg_uint32 block_size,
|
137 |
|
|
cyg_blib_t *bl);
|
138 |
|
|
|
139 |
|
|
// --------------------------------------------------------------------
|
140 |
|
|
// Deletes a block lib instance
|
141 |
|
|
//
|
142 |
|
|
// bl - block lib instance
|
143 |
|
|
//
|
144 |
|
|
// The block cache is synced before
|
145 |
|
|
//
|
146 |
|
|
// returns ENOERR if delete succeded
|
147 |
|
|
//
|
148 |
|
|
|
149 |
|
|
int cyg_blib_delete(cyg_blib_t *bl);
|
150 |
|
|
|
151 |
|
|
// --------------------------------------------------------------------
|
152 |
|
|
// Reads a number of blocks
|
153 |
|
|
//
|
154 |
|
|
// bl - block lib instance
|
155 |
|
|
// buf - block buffer ptr
|
156 |
|
|
// len - number of blocks to read
|
157 |
|
|
// pos - starting block number
|
158 |
|
|
//
|
159 |
|
|
// returns ENOERR if read succeded
|
160 |
|
|
//
|
161 |
|
|
|
162 |
|
|
int cyg_blib_bread(cyg_blib_t *bl,
|
163 |
|
|
void *buf,
|
164 |
|
|
cyg_uint32 *len,
|
165 |
|
|
cyg_uint32 pos);
|
166 |
|
|
|
167 |
|
|
// --------------------------------------------------------------------
|
168 |
|
|
// Writes a number of blocks
|
169 |
|
|
//
|
170 |
|
|
// bl - block lib instance
|
171 |
|
|
// buf - block buffer ptr
|
172 |
|
|
// len - number of blocks to write
|
173 |
|
|
// pos - starting block number
|
174 |
|
|
//
|
175 |
|
|
// returns ENOERR if write succeded
|
176 |
|
|
//
|
177 |
|
|
|
178 |
|
|
int cyg_blib_bwrite(cyg_blib_t *bl,
|
179 |
|
|
const void *buf,
|
180 |
|
|
cyg_uint32 *len,
|
181 |
|
|
cyg_uint32 pos);
|
182 |
|
|
|
183 |
|
|
// --------------------------------------------------------------------
|
184 |
|
|
// Reads data
|
185 |
|
|
//
|
186 |
|
|
// bl - block lib instance
|
187 |
|
|
// buf - data buffer ptr
|
188 |
|
|
// len - number of bytes to read
|
189 |
|
|
// bnum - starting block number
|
190 |
|
|
// pos - starting position inside starting block
|
191 |
|
|
//
|
192 |
|
|
// returns ENOERR if read succeded
|
193 |
|
|
//
|
194 |
|
|
// The block number is automatically adjusted if
|
195 |
|
|
// position is greater than block size
|
196 |
|
|
//
|
197 |
|
|
|
198 |
|
|
int cyg_blib_read(cyg_blib_t *bl,
|
199 |
|
|
void *buf,
|
200 |
|
|
cyg_uint32 *len,
|
201 |
|
|
cyg_uint32 bnum,
|
202 |
|
|
cyg_uint32 pos);
|
203 |
|
|
|
204 |
|
|
// --------------------------------------------------------------------
|
205 |
|
|
// Writes data
|
206 |
|
|
//
|
207 |
|
|
// bl - block lib instance
|
208 |
|
|
// buf - data buffer ptr
|
209 |
|
|
// len - number of bytes to write
|
210 |
|
|
// bnum - starting block number
|
211 |
|
|
// pos - starting position inside starting block
|
212 |
|
|
//
|
213 |
|
|
// returns ENOERR if write succeded
|
214 |
|
|
//
|
215 |
|
|
// The block number is automatically adjusted if
|
216 |
|
|
// position is greater than block size
|
217 |
|
|
//
|
218 |
|
|
|
219 |
|
|
int cyg_blib_write(cyg_blib_t *bl,
|
220 |
|
|
const void *buf,
|
221 |
|
|
cyg_uint32 *len,
|
222 |
|
|
cyg_uint32 bnum,
|
223 |
|
|
cyg_uint32 pos);
|
224 |
|
|
|
225 |
|
|
// --------------------------------------------------------------------
|
226 |
|
|
// Syncs block cache - ie write modified blocks
|
227 |
|
|
//
|
228 |
|
|
// bl - block lib instance
|
229 |
|
|
//
|
230 |
|
|
// returns ENOERR if sync succeded
|
231 |
|
|
//
|
232 |
|
|
|
233 |
|
|
int cyg_blib_sync(cyg_blib_t *bl);
|
234 |
|
|
|
235 |
|
|
// --------------------------------------------------------------------
|
236 |
|
|
// Syncs block - ie write if modified
|
237 |
|
|
//
|
238 |
|
|
// bl - block lib instance
|
239 |
|
|
// num - block number to sync
|
240 |
|
|
//
|
241 |
|
|
// returns ENOERR if sync succeded
|
242 |
|
|
//
|
243 |
|
|
|
244 |
|
|
int cyg_blib_sync_block(cyg_blib_t *bl, cyg_uint32 num);
|
245 |
|
|
|
246 |
|
|
// --------------------------------------------------------------------
|
247 |
|
|
// Flushes block cache
|
248 |
|
|
//
|
249 |
|
|
// bl - block lib instance
|
250 |
|
|
//
|
251 |
|
|
// returns ENOERR if flush succeded
|
252 |
|
|
//
|
253 |
|
|
// The block cache is synced before
|
254 |
|
|
//
|
255 |
|
|
|
256 |
|
|
int cyg_blib_flush(cyg_blib_t *bl);
|
257 |
|
|
|
258 |
|
|
// --------------------------------------------------------------------
|
259 |
|
|
// Sets block size
|
260 |
|
|
//
|
261 |
|
|
// bl - block lib instance
|
262 |
|
|
// block_size - new block size
|
263 |
|
|
//
|
264 |
|
|
// returns ENOERR if set succeded
|
265 |
|
|
//
|
266 |
|
|
// The block cache is synced before
|
267 |
|
|
//
|
268 |
|
|
|
269 |
|
|
int cyg_blib_set_block_size(cyg_blib_t *bl, cyg_uint32 block_size);
|
270 |
|
|
|
271 |
|
|
// --------------------------------------------------------------------
|
272 |
|
|
// Gets block size
|
273 |
|
|
//
|
274 |
|
|
// bl - block lib instance
|
275 |
|
|
//
|
276 |
|
|
// returns the current block size
|
277 |
|
|
|
278 |
|
|
static inline cyg_uint32 cyg_blib_get_block_size(cyg_blib_t *bl)
|
279 |
|
|
{
|
280 |
|
|
return bl->block_size;
|
281 |
|
|
}
|
282 |
|
|
|
283 |
|
|
// --------------------------------------------------------------------
|
284 |
|
|
// Gets log2 of block size
|
285 |
|
|
//
|
286 |
|
|
// bl - block lib instance
|
287 |
|
|
//
|
288 |
|
|
// returns log2 of the current block size
|
289 |
|
|
|
290 |
|
|
static inline cyg_uint32 cyg_blib_get_block_size_log2(cyg_blib_t *bl)
|
291 |
|
|
{
|
292 |
|
|
return bl->block_size_log2;
|
293 |
|
|
}
|
294 |
|
|
|
295 |
|
|
// --------------------------------------------------------------------
|
296 |
|
|
// Gets block cache statistics
|
297 |
|
|
//
|
298 |
|
|
// bl - block lib instance
|
299 |
|
|
//
|
300 |
|
|
// returns ENOERR if get succeded
|
301 |
|
|
//
|
302 |
|
|
|
303 |
|
|
int cyg_blib_get_stat(cyg_blib_t *bl, cyg_blib_stat_t *stat);
|
304 |
|
|
|
305 |
|
|
#endif // CYGONCE_BLIB_H
|
306 |
|
|
|
307 |
|
|
// --------------------------------------------------------------------
|
308 |
|
|
// EOF blib.h
|