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 2009 Develer S.r.l. (http://www.develer.com/)
33 * \author Francesco Sacchi <batt@develer.com>
35 * \brief KBlock interface
42 INLINE size_t kblock_readDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size)
44 KB_ASSERT_METHOD(b, readDirect);
45 return b->priv.vt->readDirect(b, index, buf, offset, size);
48 INLINE size_t kblock_readBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
50 KB_ASSERT_METHOD(b, readBuf);
51 ASSERT(offset + size <= b->blk_size);
53 return b->priv.vt->readBuf(b, buf, offset, size);
56 INLINE size_t kblock_writeBuf(struct KBlock *b, const void *buf, size_t offset, size_t size)
58 KB_ASSERT_METHOD(b, writeBuf);
59 ASSERT(offset + size <= b->blk_size);
60 return b->priv.vt->writeBuf(b, buf, offset, size);
63 INLINE int kblock_load(struct KBlock *b, block_idx_t index)
65 KB_ASSERT_METHOD(b, load);
66 ASSERT(index < b->blk_cnt);
68 return b->priv.vt->load(b, b->priv.blk_start + index);
71 INLINE int kblock_store(struct KBlock *b, block_idx_t index)
73 KB_ASSERT_METHOD(b, store);
74 ASSERT(index < b->blk_cnt);
76 return b->priv.vt->store(b, b->priv.blk_start + index);
81 size_t kblock_read(struct KBlock *b, block_idx_t idx, void *_buf, size_t offset, size_t size)
84 uint8_t *buf = (uint8_t *)_buf;
91 size_t len = MIN(size, b->blk_size - offset);
94 if (idx == b->priv.curr_blk)
95 rlen = kblock_readBuf(b, buf, offset, len);
97 rlen = kblock_readDirect(b, idx, buf, offset, len);
113 int kblock_flush(struct KBlock *b)
117 if (b->priv.cache_dirty)
119 if (kblock_store(b, b->priv.curr_blk) == 0)
120 b->priv.cache_dirty = false;
128 static bool kblock_loadPage(struct KBlock *b, block_idx_t idx)
132 if (idx != b->priv.curr_blk)
134 if (kblock_flush(b) != 0 || kblock_load(b, idx) != 0)
137 b->priv.curr_blk = idx;
143 size_t kblock_write(struct KBlock *b, block_idx_t idx, const void *_buf, size_t offset, size_t size)
146 const uint8_t *buf = (const uint8_t *)_buf;
153 size_t len = MIN(size, b->blk_size - offset);
156 if (!kblock_loadPage(b, idx))
159 wlen = kblock_writeBuf(b, buf, offset, len);
160 b->priv.cache_dirty = true;
175 int kblock_copy(struct KBlock *b, block_idx_t idx1, block_idx_t idx2)
179 if (!kblock_loadPage(b, idx1))
182 b->priv.curr_blk = idx2;
183 b->priv.cache_dirty = true;