From fa9b44a5be11d39c0eaaf768e540baa70dabb689 Mon Sep 17 00:00:00 2001 From: asterix Date: Wed, 4 Aug 2010 16:47:12 +0000 Subject: [PATCH] Write or read buffer in block size chunk. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@4143 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/io/kfile_block.c | 40 ++++++++++++++++++++++++++-------------- bertos/io/kfile_block.h | 4 ++-- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/bertos/io/kfile_block.c b/bertos/io/kfile_block.c index 2b8e839b..57c6f8d3 100644 --- a/bertos/io/kfile_block.c +++ b/bertos/io/kfile_block.c @@ -55,56 +55,68 @@ INLINE KFileBlock * KFILEBLOCK_CAST(KFile *fd) #define KFILEBLOCK(dir, fd, buf, size) \ ({ \ KFileBlock *fb = KFILEBLOCK_CAST(fd); \ - block_idx_t id = (fd)->seek_pos / fb->b->blk_size; \ - size_t offset = (fd)->seek_pos % fb->b->blk_size; \ - size_t len = kblock_##dir(fb->b, id, buf, offset, size); \ - (fd)->seek_pos += len; \ + size_t len = 0; \ + while (size) \ + { \ + block_idx_t id = (fd)->seek_pos / fb->blk->blk_size; \ + size_t offset = (fd)->seek_pos % fb->blk->blk_size; \ + size_t count = MIN(size, (size_t)(fb->blk->blk_size - offset)); \ + size_t ret_len = kblock_##dir(fb->blk, id, buf, offset, count); \ + size -= ret_len; \ + (fd)->seek_pos += ret_len; \ + buf = buf + ret_len; \ + len += ret_len; \ + if (ret_len != count) \ + break; \ + } \ len; \ }) -static size_t kfileblock_read(struct KFile *fd, void *buf, size_t size) +static size_t kfileblock_read(struct KFile *fd, void *_buf, size_t size) { + uint8_t *buf = (uint8_t *)_buf; return KFILEBLOCK(read, fd, buf, size); } -static size_t kfileblock_write(struct KFile *fd, const void *buf, size_t size) +static size_t kfileblock_write(struct KFile *fd, const void *_buf, size_t size) { + const uint8_t *buf = (const uint8_t *)_buf; return KFILEBLOCK(write, fd, buf, size); } static int kfileblock_flush(struct KFile *fd) { KFileBlock *fb = KFILEBLOCK_CAST(fd); - return kblock_flush(fb->b); + return kblock_flush(fb->blk); } static int kfileblock_error(struct KFile *fd) { KFileBlock *fb = KFILEBLOCK_CAST(fd); - return kblock_error(fb->b); + return kblock_error(fb->blk); } static void kfileblock_clearerr(struct KFile *fd) { KFileBlock *fb = KFILEBLOCK_CAST(fd); - return kblock_clearerr(fb->b); + return kblock_clearerr(fb->blk); } static int kfileblock_close(struct KFile *fd) { KFileBlock *fb = KFILEBLOCK_CAST(fd); - return kblock_close(fb->b); + return kblock_close(fb->blk); } -void kfileblock_init(KFileBlock *fb, KBlock *b) +void kfileblock_init(KFileBlock *fb, KBlock *blk) { ASSERT(fb); - ASSERT(b); + ASSERT(blk); memset(fb, 0, sizeof(*fb)); kfile_init(&fb->fd); DB(fb->fd._type = KFT_KFILEBLOCK); - fb->b = b; - fb->fd.size = b->blk_cnt * b->blk_size; + fb->blk = blk; + fb->fd.size = blk->blk_cnt * blk->blk_size; fb->fd.read = kfileblock_read; fb->fd.write = kfileblock_write; fb->fd.flush = kfileblock_flush; diff --git a/bertos/io/kfile_block.h b/bertos/io/kfile_block.h index f7b9f9bc..8a1b36bb 100644 --- a/bertos/io/kfile_block.h +++ b/bertos/io/kfile_block.h @@ -50,7 +50,7 @@ typedef struct KFileBlock { KFile fd; ///< KFile context - KBlock *b; ///< KBlock device + KBlock *blk; ///< KBlock device } KFileBlock; /** @@ -63,6 +63,6 @@ typedef struct KFileBlock * \param fb KFileBlock context. * \param b block device to be accessed with a KFile interface. */ -void kfileblock_init(KFileBlock *fb, KBlock *b); +void kfileblock_init(KFileBlock *fb, KBlock *blk); #endif /* IO_KFILE_KBLOCK_H */ -- 2.25.1