Add unbuffered methods for accessing a KBlock.
[bertos.git] / bertos / io / kblock.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \author Francesco Sacchi <batt@develer.com>
34  *
35  * \brief KBlock interface
36  */
37
38
39 #include "kblock.h"
40
41 #define LOG_LEVEL LOG_LVL_ERR
42 #define LOG_FORMAT LOG_FMT_VERBOSE
43
44 #include <cfg/log.h>
45 #include <string.h>
46
47 INLINE size_t kblock_readDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size)
48 {
49         KB_ASSERT_METHOD(b, readDirect);
50         ASSERT(index < b->blk_cnt);
51         return b->priv.vt->readDirect(b, b->priv.blk_start + index, buf, offset, size);
52 }
53
54 INLINE size_t kblock_readBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
55 {
56         KB_ASSERT_METHOD(b, readBuf);
57         ASSERT(offset + size <= b->blk_size);
58
59         return b->priv.vt->readBuf(b, buf, offset, size);
60 }
61
62 INLINE size_t kblock_writeBuf(struct KBlock *b, const void *buf, size_t offset, size_t size)
63 {
64         KB_ASSERT_METHOD(b, writeBuf);
65         ASSERT(offset + size <= b->blk_size);
66         return b->priv.vt->writeBuf(b, buf, offset, size);
67 }
68
69 INLINE int kblock_load(struct KBlock *b, block_idx_t index)
70 {
71         KB_ASSERT_METHOD(b, load);
72         ASSERT(index < b->blk_cnt);
73
74         LOG_INFO("index %d\n", index);
75         return b->priv.vt->load(b, b->priv.blk_start + index);
76 }
77
78 INLINE int kblock_store(struct KBlock *b, block_idx_t index)
79 {
80         KB_ASSERT_METHOD(b, store);
81         ASSERT(index < b->blk_cnt);
82
83         LOG_INFO("index %d\n", index);
84         return b->priv.vt->store(b, b->priv.blk_start + index);
85 }
86
87
88
89 size_t kblock_read(struct KBlock *b, block_idx_t idx, void *buf, size_t offset, size_t size)
90 {
91         ASSERT(b);
92         ASSERT(buf);
93         ASSERT(offset + size <= b->blk_size);
94         LOG_INFO("blk_idx %d, offset %d, size %d\n", idx, offset, size);
95
96         if (idx == b->priv.curr_blk)
97                 return kblock_readBuf(b, buf, offset, size);
98         else
99                 return kblock_readDirect(b, idx, buf, offset, size);
100 }
101
102
103 int kblock_flush(struct KBlock *b)
104 {
105         ASSERT(b);
106
107         if (b->priv.cache_dirty)
108         {
109                 LOG_INFO("flushing block %d\n", b->priv.curr_blk);
110                 if (kblock_store(b, b->priv.curr_blk) == 0)
111                         b->priv.cache_dirty = false;
112                 else
113                         return EOF;
114         }
115         return 0;
116 }
117
118
119 static bool kblock_loadPage(struct KBlock *b, block_idx_t idx)
120 {
121         ASSERT(b);
122
123         if (idx != b->priv.curr_blk)
124         {
125                 LOG_INFO("loading block %d\n", idx);
126                 if (kblock_flush(b) != 0 || kblock_load(b, idx) != 0)
127                                 return false;
128
129                 b->priv.curr_blk = idx;
130         }
131         return true;
132 }
133
134
135 size_t kblock_write(struct KBlock *b, block_idx_t idx, const void *buf, size_t offset, size_t size)
136 {
137         ASSERT(b);
138         ASSERT(buf);
139         ASSERT(idx < b->blk_cnt);
140         ASSERT(offset + size <= b->blk_size);
141
142         LOG_INFO("blk_idx %d, offset %d, size %d\n", idx, offset, size);
143
144         if (!kblock_loadPage(b, idx))
145                 return 0;
146
147         b->priv.cache_dirty = true;
148         return kblock_writeBuf(b, buf, offset, size);
149 }
150
151 int kblock_copy(struct KBlock *b, block_idx_t idx1, block_idx_t idx2)
152 {
153         ASSERT(b);
154         ASSERT(idx1 < b->blk_cnt);
155         ASSERT(idx2 < b->blk_cnt);
156
157         if (!kblock_loadPage(b, idx1))
158                 return EOF;
159
160         b->priv.curr_blk = idx2;
161         b->priv.cache_dirty = true;
162         return 0;
163 }
164
165 int kblock_swWriteBlock(struct KBlock *b, block_idx_t index, const void *buf)
166 {
167         return (kblock_write(b, index, buf, 0, b->blk_size) == b->blk_size) ? 0 : EOF;
168 }
169
170 int kblock_swReadBlock(struct KBlock *b, block_idx_t index, void *buf)
171 {
172         return (kblock_read(b, index, buf, 0, b->blk_size) == b->blk_size) ? 0 : EOF;
173 }
174
175 int kblock_swLoad(struct KBlock *b, block_idx_t index)
176 {
177         return kblock_readBlock(b, index, b->priv.buf);
178 }
179
180 int kblock_swStore(struct KBlock *b, block_idx_t index)
181 {
182         return kblock_writeBlock(b, index, b->priv.buf);
183 }
184
185 size_t kblock_swReadBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
186 {
187         ASSERT(buf);
188         ASSERT(offset + size <= b->blk_size);
189         
190         memcpy(buf, (uint8_t *)b->priv.buf + offset, size);
191         return size;
192 }
193
194 size_t kblock_swWriteBuf(struct KBlock *b, const void *buf, size_t offset, size_t size)
195 {
196         ASSERT(buf);
197         ASSERT(offset + size <= b->blk_size);
198         memcpy((uint8_t *)b->priv.buf + offset, buf, size);
199         return size;
200 }
201
202 size_t kblock_swReadDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size)
203 {
204         ASSERT(buf);
205         ASSERT(index < b->blk_cnt);
206         
207         if (!kblock_loadPage(b, index))
208                 return 0;
209                 
210         return kblock_swReadBuf(b, buf, offset, size);
211 }