4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \author Francesco Sacchi <batt@develer.com>
35 * \brief KBlock interface over libc files.
42 #include "kblock_file.h"
47 static int kblockfile_load(KBlock *b, block_idx_t index)
49 KBlockFile *f = KBLOCKFILE_CAST(b);
50 fseek(f->fp, index * b->blk_size, SEEK_SET);
51 return (fread(f->pagebuf, 1, b->blk_size, f->fp) == b->blk_size) ? 0 : EOF;
54 static int kblockfile_store(struct KBlock *b, block_idx_t index)
56 KBlockFile *f = KBLOCKFILE_CAST(b);
57 fseek(f->fp, index * b->blk_size, SEEK_SET);
58 return (fwrite(f->pagebuf, 1, b->blk_size, f->fp) == b->blk_size) ? 0 : EOF;
61 static size_t kblockfile_readBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
63 KBlockFile *f = KBLOCKFILE_CAST(b);
64 memcpy(buf, f->pagebuf + offset, size);
68 static size_t kblockfile_readDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size)
70 KBlockFile *f = KBLOCKFILE_CAST(b);
71 fseek(f->fp, index * b->blk_size + offset, SEEK_SET);
72 return fread(buf, 1, size, f->fp);
75 static size_t kblockfile_writeBuf(struct KBlock *b, const void *buf, size_t offset, size_t size)
77 KBlockFile *f = KBLOCKFILE_CAST(b);
78 memcpy(f->pagebuf + offset, buf, size);
82 static int kblockfile_error(struct KBlock *b)
84 KBlockFile *f = KBLOCKFILE_CAST(b);
89 static int kblockfile_claererr(struct KBlock *b)
91 KBlockFile *f = KBLOCKFILE_CAST(b);
97 static int kblockfile_close(struct KBlock *b)
99 KBlockFile *f = KBLOCKFILE_CAST(b);
101 return fflush(f->fp) | fclose(f->fp);
105 static KBlockVTable kblockfile_vt =
107 .readDirect = kblockfile_readDirect,
108 .readBuf = kblockfile_readBuf,
109 .writeBuf = kblockfile_writeBuf,
110 .load = kblockfile_load,
111 .store = kblockfile_store,
113 .error = kblockfile_error,
114 .clearerr = kblockfile_claererr,
115 .close = kblockfile_close,
119 void kblockfile_init(KBlockFile *f, FILE *fp, void *buf, size_t block_size, block_idx_t block_count)
126 memset(f, 0, sizeof(*f));
128 DB(f->b.priv.type = KBT_KBLOCKFILE);
132 f->b.blk_size = block_size;
133 f->b.blk_cnt = block_count;
134 f->b.priv.vt = &kblockfile_vt;
135 kblockfile_load(&f->b, 0);
136 f->b.priv.curr_blk = 0;
137 f->b.priv.cache_dirty = false;