X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fio%2Fkblock.c;h=7355deeded97884c6c010decd54a54f7580d8a83;hb=ec1f9b136d8b9ec44272ba7ab8ddc4b729589af2;hp=487cfcd1ab0d5c688a8f21d4079c12c716a0f8ff;hpb=976a96d1824ea1a84bb3a12023033cf2fed81761;p=bertos.git diff --git a/bertos/io/kblock.c b/bertos/io/kblock.c index 487cfcd1..7355deed 100644 --- a/bertos/io/kblock.c +++ b/bertos/io/kblock.c @@ -51,6 +51,13 @@ INLINE size_t kblock_readDirect(struct KBlock *b, block_idx_t index, void *buf, return b->priv.vt->readDirect(b, b->priv.blk_start + index, buf, offset, size); } +INLINE int kblock_writeBlock(struct KBlock *b, block_idx_t index, const void *buf) +{ + KB_ASSERT_METHOD(b, writeBlock); + ASSERT(index < b->blk_cnt); + return b->priv.vt->writeBlock(b, b->priv.blk_start + index, buf); +} + INLINE size_t kblock_readBuf(struct KBlock *b, void *buf, size_t offset, size_t size) { KB_ASSERT_METHOD(b, readBuf); @@ -101,7 +108,7 @@ size_t kblock_read(struct KBlock *b, block_idx_t idx, void *buf, size_t offset, ASSERT(offset + size <= b->blk_size); LOG_INFO("blk_idx %d, offset %d, size %d\n", idx, offset, size); - if (idx == b->priv.curr_blk) + 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); @@ -112,6 +119,9 @@ int kblock_flush(struct KBlock *b) { ASSERT(b); + if (!kblock_buffered(b)) + return 0; + if (kblock_cacheDirty(b)) { LOG_INFO("flushing block %d\n", b->priv.curr_blk); @@ -149,40 +159,40 @@ size_t kblock_write(struct KBlock *b, block_idx_t idx, const void *buf, size_t o LOG_INFO("blk_idx %d, offset %d, size %d\n", idx, offset, size); - if (!kblock_loadPage(b, idx)) - return 0; + if (kblock_buffered(b)) + { + if (!kblock_loadPage(b, idx)) + return 0; - kblock_setDirty(b, true); - return kblock_writeBuf(b, buf, offset, size); + kblock_setDirty(b, true); + return kblock_writeBuf(b, buf, offset, size); + } + else + { + ASSERT(offset == 0); + ASSERT(size == b->blk_size); + return (kblock_writeBlock(b, idx, buf) == 0) ? size : 0; + } } -int kblock_copy(struct KBlock *b, block_idx_t idx1, block_idx_t idx2) +int kblock_copy(struct KBlock *b, block_idx_t src, block_idx_t dest) { ASSERT(b); - ASSERT(idx1 < b->blk_cnt); - ASSERT(idx2 < b->blk_cnt); + ASSERT(src < b->blk_cnt); + ASSERT(dest < b->blk_cnt); + ASSERT(kblock_buffered(b)); - if (!kblock_loadPage(b, idx1)) + if (!kblock_loadPage(b, src)) return EOF; - b->priv.curr_blk = idx2; + b->priv.curr_blk = dest; kblock_setDirty(b, true); return 0; } -int kblock_swWriteBlock(struct KBlock *b, block_idx_t index, const void *buf) -{ - return (kblock_write(b, index, buf, 0, b->blk_size) == b->blk_size) ? 0 : EOF; -} - -int kblock_swReadBlock(struct KBlock *b, block_idx_t index, void *buf) -{ - return (kblock_read(b, index, buf, 0, b->blk_size) == b->blk_size) ? 0 : EOF; -} - int kblock_swLoad(struct KBlock *b, block_idx_t index) { - return kblock_readBlock(b, index, b->priv.buf); + return (kblock_readDirect(b, index, b->priv.buf, 0, b->blk_size) == b->blk_size) ? 0 : EOF; } int kblock_swStore(struct KBlock *b, block_idx_t index) @@ -194,7 +204,7 @@ size_t kblock_swReadBuf(struct KBlock *b, void *buf, size_t offset, size_t size) { ASSERT(buf); ASSERT(offset + size <= b->blk_size); - + memcpy(buf, (uint8_t *)b->priv.buf + offset, size); return size; } @@ -206,14 +216,3 @@ size_t kblock_swWriteBuf(struct KBlock *b, const void *buf, size_t offset, size_ memcpy((uint8_t *)b->priv.buf + offset, buf, size); return size; } - -size_t kblock_swReadDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size) -{ - ASSERT(buf); - ASSERT(index < b->blk_cnt); - - if (!kblock_loadPage(b, index)) - return 0; - - return kblock_swReadBuf(b, buf, offset, size); -} \ No newline at end of file