X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fio%2Fkblock.c;h=65f427903a11ba018e92acbd1d2a2600bf57e8e1;hb=b708b0cbae2dc58ef30943ade3d56c464b49a59c;hp=c36fd0db454bb0319724cce85ff5bdee301dc902;hpb=61622130cdca050e7a84ef6547748de18a5b8f2b;p=bertos.git diff --git a/bertos/io/kblock.c b/bertos/io/kblock.c index c36fd0db..65f42790 100644 --- a/bertos/io/kblock.c +++ b/bertos/io/kblock.c @@ -84,36 +84,17 @@ INLINE int kblock_store(struct KBlock *b, block_idx_t index) -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); + 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 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; - } - - return tot_rd; + if (idx == b->priv.curr_blk) + return kblock_readBuf(b, buf, offset, size); + else + return kblock_readDirect(b, idx, buf, offset, size); } @@ -149,37 +130,19 @@ static bool kblock_loadPage(struct KBlock *b, block_idx_t idx) } -size_t kblock_write(struct KBlock *b, block_idx_t idx, const void *_buf, size_t offset, size_t size) +size_t kblock_write(struct KBlock *b, block_idx_t idx, const void *buf, size_t offset, size_t size) { - size_t tot_wr = 0; - const uint8_t *buf = (const uint8_t *)_buf; - ASSERT(b); ASSERT(buf); + 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; - - if (!kblock_loadPage(b, idx)) - break; - wlen = kblock_writeBuf(b, buf, offset, len); - b->priv.cache_dirty = true; + if (!kblock_loadPage(b, idx)) + return 0; - tot_wr += wlen; - if (wlen != len) - break; - - idx++; - size -= wlen; - offset = 0; - buf += wlen; - } - - return tot_wr; + b->priv.cache_dirty = true; + return kblock_writeBuf(b, buf, offset, size); } int kblock_copy(struct KBlock *b, block_idx_t idx1, block_idx_t idx2)