3 //==========================================================================
7 // Block cache and access library
9 //==========================================================================
10 //####ECOSGPLCOPYRIGHTBEGIN####
11 // -------------------------------------------
12 // This file is part of eCos, the Embedded Configurable Operating System.
13 // Copyright (C) 2003 Savin Zlobec
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 version.
19 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
20 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 // You should have received a copy of the GNU General Public License along
25 // with eCos; if not, write to the Free Software Foundation, Inc.,
26 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
28 // As a special exception, if other files instantiate templates or use macros
29 // or inline functions from this file, or you compile this file and link it
30 // with other works to produce a work based on this file, this file does not
31 // by itself cause the resulting work to be covered by the GNU General Public
32 // License. However the source code for this file must still be made available
33 // in accordance with section (3) of the GNU General Public License.
35 // This exception does not invalidate any other reasons why a work based on
36 // this file might be covered by the GNU General Public License.
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
47 //####DESCRIPTIONEND####
49 //==========================================================================
51 #include <cyg/infra/cyg_type.h>
52 #include <cyg/io/io.h>
54 #include <linux/rbtree.h>
55 #include <linux/list.h>
57 // --------------------------------------------------------------------
59 typedef int (*cyg_blib_bread_fn) (
60 void*, // private data
61 void*, // block buffer
62 cyg_uint32*, // number of blocks to read
63 cyg_uint32 // starting block number
66 typedef int (*cyg_blib_bwrite_fn) (
67 void*, // private data
68 const void*, // block buffer
69 cyg_uint32*, // number of blocks to write
70 cyg_uint32 // starting block number
74 cyg_uint32 n_gets; // number of block gets
75 cyg_uint32 n_reads; // number of block reads
76 cyg_uint32 n_writes; // number of block writes
80 void *priv; // private data
81 struct list_head list_head; // head of block list
82 struct rb_root rb_root; // block red-black tree root
83 cyg_uint32 block_size; // block size
84 cyg_uint32 block_size_log2; // block size log2
85 cyg_uint8 *mem_base; // memory base
86 cyg_uint32 mem_size; // memory size
87 struct list_head free_list_head; // list of free blocks
88 cyg_blib_bread_fn bread_fn; // block read function
89 cyg_blib_bwrite_fn bwrite_fn; // block write function
90 #ifdef CYGIMP_BLOCK_LIB_STATISTICS
91 cyg_blib_stat_t stat; // statistics
95 // --------------------------------------------------------------------
97 // --------------------------------------------------------------------
98 // Creates a block lib instance
100 // priv_data - private data to pass to bread_fn and bwrite_fn
101 // mem_base - block cache memory base
102 // mem_size - block cache memory size
103 // block_size - block size
104 // bread_fn - function which reads blocks
105 // bwrite_fn - function which writes blocks
106 // bl - block lib instance space holder
108 // returns ENOERR if create succeded
111 int cyg_blib_create(void *priv_data,
114 cyg_uint32 block_size,
115 cyg_blib_bread_fn bread_fn,
116 cyg_blib_bwrite_fn bwrite_fn,
119 // --------------------------------------------------------------------
120 // Creates a block lib instance on top of IO system
121 // (cyg_io_bread and cyg_io_bwrite)
123 // handle - cyg_io_handle_t
124 // mem_base - block cache memory base
125 // mem_size - block cache memory size
126 // block_size - block size
127 // bl - block lib instance space holder
129 // returns ENOERR if create succeded
132 int cyg_blib_io_create(cyg_io_handle_t handle,
135 cyg_uint32 block_size,
138 // --------------------------------------------------------------------
139 // Deletes a block lib instance
141 // bl - block lib instance
143 // The block cache is synced before
145 // returns ENOERR if delete succeded
148 int cyg_blib_delete(cyg_blib_t *bl);
150 // --------------------------------------------------------------------
151 // Reads a number of blocks
153 // bl - block lib instance
154 // buf - block buffer ptr
155 // len - number of blocks to read
156 // pos - starting block number
158 // returns ENOERR if read succeded
161 int cyg_blib_bread(cyg_blib_t *bl,
166 // --------------------------------------------------------------------
167 // Writes a number of blocks
169 // bl - block lib instance
170 // buf - block buffer ptr
171 // len - number of blocks to write
172 // pos - starting block number
174 // returns ENOERR if write succeded
177 int cyg_blib_bwrite(cyg_blib_t *bl,
182 // --------------------------------------------------------------------
185 // bl - block lib instance
186 // buf - data buffer ptr
187 // len - number of bytes to read
188 // bnum - starting block number
189 // pos - starting position inside starting block
191 // returns ENOERR if read succeded
193 // The block number is automatically adjusted if
194 // position is greater than block size
197 int cyg_blib_read(cyg_blib_t *bl,
203 // --------------------------------------------------------------------
206 // bl - block lib instance
207 // buf - data buffer ptr
208 // len - number of bytes to write
209 // bnum - starting block number
210 // pos - starting position inside starting block
212 // returns ENOERR if write succeded
214 // The block number is automatically adjusted if
215 // position is greater than block size
218 int cyg_blib_write(cyg_blib_t *bl,
224 // --------------------------------------------------------------------
225 // Syncs block cache - ie write modified blocks
227 // bl - block lib instance
229 // returns ENOERR if sync succeded
232 int cyg_blib_sync(cyg_blib_t *bl);
234 // --------------------------------------------------------------------
235 // Syncs block - ie write if modified
237 // bl - block lib instance
238 // num - block number to sync
240 // returns ENOERR if sync succeded
243 int cyg_blib_sync_block(cyg_blib_t *bl, cyg_uint32 num);
245 // --------------------------------------------------------------------
246 // Flushes block cache
248 // bl - block lib instance
250 // returns ENOERR if flush succeded
252 // The block cache is synced before
255 int cyg_blib_flush(cyg_blib_t *bl);
257 // --------------------------------------------------------------------
260 // bl - block lib instance
261 // block_size - new block size
263 // returns ENOERR if set succeded
265 // The block cache is synced before
268 int cyg_blib_set_block_size(cyg_blib_t *bl, cyg_uint32 block_size);
270 // --------------------------------------------------------------------
273 // bl - block lib instance
275 // returns the current block size
277 static inline cyg_uint32 cyg_blib_get_block_size(cyg_blib_t *bl)
279 return bl->block_size;
282 // --------------------------------------------------------------------
283 // Gets log2 of block size
285 // bl - block lib instance
287 // returns log2 of the current block size
289 static inline cyg_uint32 cyg_blib_get_block_size_log2(cyg_blib_t *bl)
291 return bl->block_size_log2;
294 // --------------------------------------------------------------------
295 // Gets block cache statistics
297 // bl - block lib instance
299 // returns ENOERR if get succeded
302 int cyg_blib_get_stat(cyg_blib_t *bl, cyg_blib_stat_t *stat);
304 #endif // CYGONCE_BLIB_H
306 // --------------------------------------------------------------------