From 06598a5e283451d544f77b10f8eea4f912d5253a Mon Sep 17 00:00:00 2001 From: batt Date: Mon, 5 Jul 2010 22:19:25 +0000 Subject: [PATCH] Remove readBlock method in order to simplify low level API. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@3997 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/io/kblock.c | 53 +++++++++++++++++++++------------------------- bertos/io/kblock.h | 39 +++++++--------------------------- 2 files changed, 32 insertions(+), 60 deletions(-) diff --git a/bertos/io/kblock.c b/bertos/io/kblock.c index 8f1913f3..def111cb 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); @@ -148,12 +155,21 @@ size_t kblock_write(struct KBlock *b, block_idx_t idx, const void *buf, size_t o ASSERT(offset + size <= b->blk_size); LOG_INFO("blk_idx %d, offset %d, size %d\n", idx, offset, size); + + if (kblock_buffered(b)) + { + if (!kblock_loadPage(b, idx)) + return 0; - 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) @@ -161,6 +177,7 @@ int kblock_copy(struct KBlock *b, block_idx_t idx1, block_idx_t idx2) ASSERT(b); ASSERT(idx1 < b->blk_cnt); ASSERT(idx2 < b->blk_cnt); + ASSERT(kblock_buffered(b)); if (!kblock_loadPage(b, idx1)) return EOF; @@ -170,19 +187,9 @@ int kblock_copy(struct KBlock *b, block_idx_t idx1, block_idx_t idx2) 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) @@ -206,15 +213,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); -} - diff --git a/bertos/io/kblock.h b/bertos/io/kblock.h index 14a5b88e..c0d23e03 100644 --- a/bertos/io/kblock.h +++ b/bertos/io/kblock.h @@ -57,14 +57,13 @@ struct KBlock; * \{ */ typedef size_t (* kblock_read_direct_t) (struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size); +typedef int (* kblock_write_block_t) (struct KBlock *b, block_idx_t index, const void *buf); + typedef size_t (* kblock_read_t) (struct KBlock *b, void *buf, size_t offset, size_t size); typedef size_t (* kblock_write_t) (struct KBlock *b, const void *buf, size_t offset, size_t size); typedef int (* kblock_load_t) (struct KBlock *b, block_idx_t index); typedef int (* kblock_store_t) (struct KBlock *b, block_idx_t index); -typedef int (* kblock_write_block_t) (struct KBlock *b, block_idx_t index, const void *buf); -typedef int (* kblock_read_block_t) (struct KBlock *b, block_idx_t index, void *buf); - typedef int (* kblock_error_t) (struct KBlock *b); typedef int (* kblock_clearerr_t) (struct KBlock *b); typedef int (* kblock_close_t) (struct KBlock *b); @@ -76,15 +75,13 @@ typedef int (* kblock_close_t) (struct KBlock *b); typedef struct KBlockVTable { kblock_read_direct_t readDirect; - + kblock_write_block_t writeBlock; + kblock_read_t readBuf; kblock_write_t writeBuf; kblock_load_t load; kblock_store_t store; - - kblock_read_block_t readBlock; - kblock_write_block_t writeBlock; - + kblock_error_t error; ///< \sa kblock_error() kblock_clearerr_t clearerr; ///< \sa kblock_clearerr() @@ -128,7 +125,7 @@ typedef struct KBlock { KBlockPriv priv; ///< Interface private data, do not use directly. - /* Public access members/methods */ + /* Public access members */ size_t blk_size; ///< Block size. block_idx_t blk_cnt; ///< Number of blocks available in the device. } KBlock; @@ -226,20 +223,6 @@ INLINE int kblock_close(struct KBlock *b) return b->priv.vt->close(b); } -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 int kblock_readBlock(struct KBlock *b, block_idx_t index, void *buf) -{ - KB_ASSERT_METHOD(b, readDirect); - ASSERT(index < b->blk_cnt); - return b->priv.vt->readBlock(b, b->priv.blk_start + index, buf); -} - INLINE bool kblock_cacheDirty(struct KBlock *b) { ASSERT(b); @@ -257,20 +240,14 @@ INLINE bool kblock_buffered(struct KBlock *b) return (b->priv.flags & KB_BUFFERED); } - size_t kblock_read(struct KBlock *b, block_idx_t idx, void *buf, size_t offset, size_t size); -int kblock_flush(struct KBlock *b); - size_t kblock_write(struct KBlock *b, block_idx_t idx, const void *buf, size_t offset, size_t size); -int kblock_copy(struct KBlock *b, block_idx_t idx1, block_idx_t idx2); - +int kblock_flush(struct KBlock *b); -int kblock_swWriteBlock(struct KBlock *b, block_idx_t index, const void *buf); -int kblock_swReadBlock(struct KBlock *b, block_idx_t index, void *buf); +int kblock_copy(struct KBlock *b, block_idx_t idx1, block_idx_t idx2); -size_t kblock_swReadDirect(struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size); int kblock_swLoad(struct KBlock *b, block_idx_t index); int kblock_swStore(struct KBlock *b, block_idx_t index); size_t kblock_swReadBuf(struct KBlock *b, void *buf, size_t offset, size_t size); -- 2.25.1