21a28ef57fac49ba50b3e1f42fab5be0e5df13ea
[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 #include <cfg/log.h>
41
42 INLINE size_t kblock_readDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size)
43 {
44         KB_ASSERT_METHOD(b, readDirect);
45         return b->priv.vt->readDirect(b, index, buf, offset, size);
46 }
47
48 INLINE size_t kblock_readBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
49 {
50         KB_ASSERT_METHOD(b, readBuf);
51         ASSERT(offset + size <= b->blk_size);
52
53         return b->priv.vt->readBuf(b, buf, offset, size);
54 }
55
56 INLINE size_t kblock_writeBuf(struct KBlock *b, const void *buf, size_t offset, size_t size)
57 {
58         KB_ASSERT_METHOD(b, writeBuf);
59         ASSERT(offset + size <= b->blk_size);
60         return b->priv.vt->writeBuf(b, buf, offset, size);
61 }
62
63 INLINE int kblock_load(struct KBlock *b, block_idx_t index)
64 {
65         KB_ASSERT_METHOD(b, load);
66         ASSERT(index < b->blk_cnt);
67
68         return b->priv.vt->load(b, b->priv.blk_start + index);
69 }
70
71 INLINE int kblock_store(struct KBlock *b, block_idx_t index)
72 {
73         KB_ASSERT_METHOD(b, store);
74         ASSERT(index < b->blk_cnt);
75
76         return b->priv.vt->store(b, b->priv.blk_start + index);
77 }
78
79
80
81 size_t kblock_read(struct KBlock *b, block_idx_t idx, void *_buf, size_t offset, size_t size)
82 {
83         size_t tot_rd = 0;
84         uint8_t *buf = (uint8_t *)_buf;
85
86         ASSERT(b);
87         ASSERT(buf);
88
89         while (size)
90         {
91                 size_t len = MIN(size, b->blk_size - offset);
92                 size_t rlen;
93
94                 if (idx == b->priv.curr_blk)
95                         rlen = kblock_readBuf(b, buf, offset, len);
96                 else
97                         rlen = kblock_readDirect(b, idx, buf, offset, len);
98
99                 tot_rd += rlen;
100                 if (rlen != len)
101                         break;
102
103                 idx++;
104                 size -= rlen;
105                 offset = 0;
106                 buf += rlen;
107         }
108
109         return tot_rd;
110 }
111
112
113 int kblock_flush(struct KBlock *b)
114 {
115         ASSERT(b);
116
117         if (b->priv.cache_dirty)
118         {
119                 if (kblock_store(b, b->priv.curr_blk) == 0)
120                         b->priv.cache_dirty = false;
121                 else
122                         return EOF;
123         }
124         return 0;
125 }
126
127
128 static bool kblock_loadPage(struct KBlock *b, block_idx_t idx)
129 {
130         ASSERT(b);
131
132         if (idx != b->priv.curr_blk)
133         {
134                 if (kblock_flush(b) != 0 || kblock_load(b, idx) != 0)
135                                 return false;
136
137                 b->priv.curr_blk = idx;
138         }
139         return true;
140 }
141
142
143 size_t kblock_write(struct KBlock *b, block_idx_t idx, const void *_buf, size_t offset, size_t size)
144 {
145         size_t tot_wr = 0;
146         const uint8_t *buf = (const uint8_t *)_buf;
147
148         ASSERT(b);
149         ASSERT(buf);
150
151         while (size)
152         {
153                 size_t len = MIN(size, b->blk_size - offset);
154                 size_t wlen;
155
156                 if (!kblock_loadPage(b, idx))
157                         break;
158
159                 wlen = kblock_writeBuf(b, buf, offset, len);
160                 b->priv.cache_dirty = true;
161
162                 tot_wr += wlen;
163                 if (wlen != len)
164                         break;
165
166                 idx++;
167                 size -= wlen;
168                 offset = 0;
169                 buf += wlen;
170         }
171
172         return tot_wr;
173 }
174
175 int kblock_copy(struct KBlock *b, block_idx_t idx1, block_idx_t idx2)
176 {
177         ASSERT(b);
178
179         if (!kblock_loadPage(b, idx1))
180                 return EOF;
181
182         b->priv.curr_blk = idx2;
183         b->priv.cache_dirty = true;
184         return 0;
185 }
186