* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*
- * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
+ * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
*
* -->
*
#include "kblock.h"
-#define LOG_LEVEL LOG_LVL_ERR
-#define LOG_FORMAT LOG_FMT_VERBOSE
+#define LOG_LEVEL LOG_LVL_ERR
+#define LOG_FORMAT LOG_FMT_VERBOSE
#include <cfg/log.h>
+#include <string.h>
INLINE size_t kblock_readDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size)
{
KB_ASSERT_METHOD(b, readDirect);
+ ASSERT(index < b->blk_cnt);
return b->priv.vt->readDirect(b, b->priv.blk_start + index, buf, offset, size);
}
+INLINE size_t kblock_writeDirect(struct KBlock *b, block_idx_t index, const void *buf, size_t offset, size_t size)
+{
+ KB_ASSERT_METHOD(b, writeDirect);
+ ASSERT(index < b->blk_cnt);
+ return b->priv.vt->writeDirect(b, b->priv.blk_start + index, buf, offset, size);
+}
+
INLINE size_t kblock_readBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
{
KB_ASSERT_METHOD(b, readBuf);
KB_ASSERT_METHOD(b, load);
ASSERT(index < b->blk_cnt);
- LOG_INFO("index %d\n", index);
+ LOG_INFO("index %ld\n", index);
return b->priv.vt->load(b, b->priv.blk_start + index);
}
KB_ASSERT_METHOD(b, store);
ASSERT(index < b->blk_cnt);
- LOG_INFO("index %d\n", index);
+ LOG_INFO("index %ld\n", index);
return b->priv.vt->store(b, b->priv.blk_start + index);
}
+INLINE void kblock_setDirty(struct KBlock *b, bool dirty)
+{
+ if (dirty)
+ b->priv.flags |= KB_CACHE_DIRTY;
+ else
+ b->priv.flags &= ~KB_CACHE_DIRTY;
+}
-size_t kblock_read(struct KBlock *b, block_idx_t idx, void *_buf, size_t offset, size_t size)
+size_t kblock_read(struct KBlock *b, block_idx_t idx, void *buf, size_t offset, size_t size)
{
- size_t tot_rd = 0;
- uint8_t *buf = (uint8_t *)_buf;
-
ASSERT(b);
ASSERT(buf);
- LOG_INFO("blk_idx %d, offset %d, size %d\n", idx, offset, size);
-
- while (size)
- {
- size_t len = MIN(size, b->blk_size - offset);
- size_t rlen;
-
- if (idx == b->priv.curr_blk)
- rlen = kblock_readBuf(b, buf, offset, len);
- else
- rlen = kblock_readDirect(b, idx, buf, offset, len);
-
- tot_rd += rlen;
- if (rlen != len)
- break;
-
- idx++;
- size -= rlen;
- offset = 0;
- buf += rlen;
- }
+ ASSERT(offset + size <= b->blk_size);
+ LOG_INFO("blk_idx %ld, offset %u, size %u\n", idx, offset, size);
- return tot_rd;
+ if (kblock_buffered(b) && idx == b->priv.curr_blk)
+ return kblock_readBuf(b, buf, offset, size);
+ else
+ return kblock_readDirect(b, idx, buf, offset, size);
}
{
ASSERT(b);
- if (b->priv.cache_dirty)
+ if (!kblock_buffered(b))
+ return 0;
+
+ if (kblock_cacheDirty(b))
{
- LOG_INFO("flushing block %d\n", b->priv.curr_blk);
+ LOG_INFO("flushing block %ld\n", b->priv.curr_blk);
if (kblock_store(b, b->priv.curr_blk) == 0)
- b->priv.cache_dirty = false;
+ kblock_setDirty(b, false);
else
return EOF;
}
if (idx != b->priv.curr_blk)
{
- LOG_INFO("loading block %d\n", idx);
+ LOG_INFO("loading block %ld\n", idx);
if (kblock_flush(b) != 0 || kblock_load(b, idx) != 0)
return false;
}
-size_t kblock_write(struct KBlock *b, block_idx_t idx, const void *_buf, size_t offset, size_t size)
+int kblock_trim(struct KBlock *b, block_idx_t start, block_idx_t count)
{
- size_t tot_wr = 0;
- const uint8_t *buf = (const uint8_t *)_buf;
+ ASSERT(start + count <= b->blk_cnt);
+
+ if (kblock_buffered(b))
+ {
+ if (!kblock_loadPage(b, start))
+ return EOF;
+ }
+ b->priv.blk_start += start;
+ b->priv.curr_blk = 0; // adjust logical address
+ b->blk_cnt = count;
+ return 0;
+}
+
+
+size_t kblock_write(struct KBlock *b, block_idx_t idx, const void *buf, size_t offset, size_t size)
+{
ASSERT(b);
ASSERT(buf);
+ ASSERT(idx < b->blk_cnt);
+ ASSERT(offset + size <= b->blk_size);
- LOG_INFO("blk_idx %d, offset %d, size %d\n", idx, offset, size);
- while (size)
- {
- size_t len = MIN(size, b->blk_size - offset);
- size_t wlen;
+ LOG_INFO("blk_idx %ld, offset %u, size %u\n", idx, offset, size);
+ if (kblock_buffered(b))
+ {
if (!kblock_loadPage(b, idx))
- break;
+ return 0;
+
+ kblock_setDirty(b, true);
+ return kblock_writeBuf(b, buf, offset, size);
+ }
+ else
+ {
+ #ifdef _DEBUG
+ if (offset != 0 || size != b->blk_size)
+ ASSERT(kblock_partialWrite(b));
+ #endif
+ return kblock_writeDirect(b, idx, buf, offset, size);
+ }
+}
- wlen = kblock_writeBuf(b, buf, offset, len);
- b->priv.cache_dirty = true;
+int kblock_copy(struct KBlock *b, block_idx_t src, block_idx_t dest)
+{
+ ASSERT(b);
+ ASSERT(src < b->blk_cnt);
+ ASSERT(dest < b->blk_cnt);
- tot_wr += wlen;
- if (wlen != len)
- break;
+ if (kblock_buffered(b))
+ {
+ if (!kblock_loadPage(b, src))
+ return EOF;
- idx++;
- size -= wlen;
- offset = 0;
- buf += wlen;
+ b->priv.curr_blk = dest;
+ kblock_setDirty(b, true);
+ return 0;
}
+ else if (kblock_partialWrite(b))
+ {
+ uint8_t buf[16];
+ size_t blk_size = b->blk_size;
+ size_t offset = 0;
+
+ while (blk_size)
+ {
+ size_t size = MIN(sizeof(buf), blk_size);
+ if (kblock_readDirect(b, src, buf, offset, size) != size)
+ return EOF;
+ if (kblock_writeDirect(b, dest, buf, offset, size) != size)
+ return EOF;
+
+ blk_size -= size;
+ offset += size;
+ }
+ return 0;
+ }
+ else
+ {
+ ASSERT(0);
+ return EOF;
+ }
+}
+
+int kblock_swLoad(struct KBlock *b, block_idx_t index)
+{
+ /*
+ * Since this is a low level API, the index here is a fisical index.
+ * If we call another low level API, logical to fisical translation
+ * would be applied twice.
+ * In order to avoid this we subtract the start block index.
+ */
+ ASSERT(index >= b->priv.blk_start);
+ return (kblock_readDirect(b, index - b->priv.blk_start, b->priv.buf, 0, b->blk_size) == b->blk_size) ? 0 : EOF;
+}
- return tot_wr;
+int kblock_swStore(struct KBlock *b, block_idx_t index)
+{
+ /*
+ * Since this is a low level API, the index here is a fisical index.
+ * If we call another low level API, logical to fisical translation
+ * would be applied twice.
+ * In order to avoid this we subtract the start block index.
+ */
+ ASSERT(index >= b->priv.blk_start);
+ return (kblock_writeDirect(b, index - b->priv.blk_start, b->priv.buf, 0, b->blk_size) == b->blk_size) ? 0 : EOF;
}
-int kblock_copy(struct KBlock *b, block_idx_t idx1, block_idx_t idx2)
+size_t kblock_swReadBuf(struct KBlock *b, void *buf, size_t offset, size_t size)
{
- ASSERT(b);
+ ASSERT(buf);
+ ASSERT(offset + size <= b->blk_size);
- if (!kblock_loadPage(b, idx1))
- return EOF;
+ memcpy(buf, (uint8_t *)b->priv.buf + offset, size);
+ return size;
+}
- b->priv.curr_blk = idx2;
- b->priv.cache_dirty = true;
- return 0;
+size_t kblock_swWriteBuf(struct KBlock *b, const void *buf, size_t offset, size_t size)
+{
+ ASSERT(buf);
+ ASSERT(offset + size <= b->blk_size);
+ memcpy((uint8_t *)b->priv.buf + offset, buf, size);
+ return size;
}
+int kblock_swClose(UNUSED_ARG(struct KBlock, *b))
+{
+ return 0;
+}