BeRTOS
kblock.h
Go to the documentation of this file.
00001 
00080 #ifndef IO_KBLOCK_H
00081 #define IO_KBLOCK_H
00082 
00083 #include <cfg/compiler.h>
00084 #include <cfg/debug.h>
00085 #include <cfg/macros.h>
00086 
00088 typedef uint32_t block_idx_t;
00089 
00090 // Fwd Declaration
00091 struct KBlock;
00092 
00102 typedef size_t (* kblock_read_direct_t)  (struct KBlock *b, block_idx_t index, void *buf, size_t offset, size_t size);
00103 typedef size_t (* kblock_write_direct_t) (struct KBlock *b, block_idx_t index, const void *buf, size_t offset, size_t size);
00104 
00105 typedef size_t (* kblock_read_t)        (struct KBlock *b, void *buf, size_t offset, size_t size);
00106 typedef size_t (* kblock_write_t)       (struct KBlock *b, const void *buf, size_t offset, size_t size);
00107 typedef int    (* kblock_load_t)        (struct KBlock *b, block_idx_t index);
00108 typedef int    (* kblock_store_t)       (struct KBlock *b, block_idx_t index);
00109 
00110 typedef int    (* kblock_error_t)       (struct KBlock *b);
00111 typedef void   (* kblock_clearerr_t)    (struct KBlock *b);
00112 typedef int    (* kblock_close_t)       (struct KBlock *b);
00113 /* \} */
00114 
00115 /*
00116  * Table of interface functions for a KBlock device.
00117  */
00118 typedef struct KBlockVTable
00119 {
00120     kblock_read_direct_t readDirect;
00121     kblock_write_direct_t writeDirect;
00122 
00123     kblock_read_t  readBuf;
00124     kblock_write_t writeBuf;
00125     kblock_load_t  load;
00126     kblock_store_t store;
00127 
00128     kblock_error_t    error;    // \sa kblock_error()
00129     kblock_clearerr_t clearerr; // \sa kblock_clearerr()
00130 
00131     kblock_close_t  close; // \sa kblock_close()
00132 } KBlockVTable;
00133 
00134 
00135 #define KB_BUFFERED        BV(0) ///< Internal flag: true if the KBlock has a buffer
00136 #define KB_CACHE_DIRTY     BV(1) ///< Internal flag: true if the cache is dirty
00137 #define KB_PARTIAL_WRITE   BV(2) ///< Internal flag: true if the device allows partial block write
00138 
00139 
00140 /*
00141  * KBlock private members.
00142  * These are the private members of the KBlock interface, please do not
00143  * access these directly, use the KBlock API.
00144  */
00145 typedef struct KBlockPriv
00146 {
00147     DB(id_t type);         // Used to keep track, at runtime, of the class type.
00148     int flags;             // Status and error flags.
00149     void *buf;             // Pointer to the page buffer for RAM-cached KBlocks.
00150     block_idx_t blk_start; // Start block number when the device is trimmed. \sa kblock_trim().
00151     block_idx_t curr_blk;  // Current cached block number in cached KBlocks.
00152 
00153     const struct KBlockVTable *vt; // Virtual table of interface functions.
00154 } KBlockPriv;
00155 
00160 typedef struct KBlock
00161 {
00162     KBlockPriv priv;         
00163 
00164     /* Public access members */
00165     size_t blk_size;         
00166     block_idx_t blk_cnt;     
00167 } KBlock;
00168 
00169 
00200 int kblock_trim(struct KBlock *b, block_idx_t start, block_idx_t count);
00201 
00202 
00203 #define KB_ASSERT_METHOD(b, method) \
00204     do \
00205     { \
00206         ASSERT(b); \
00207         ASSERT((b)->priv.vt); \
00208         ASSERT((b)->priv.vt->method); \
00209     } \
00210     while (0)
00211 
00212 
00224 INLINE int kblock_error(struct KBlock *b)
00225 {
00226     KB_ASSERT_METHOD(b, error);
00227     return b->priv.vt->error(b);
00228 }
00229 
00238 INLINE void kblock_clearerr(struct KBlock *b)
00239 {
00240     KB_ASSERT_METHOD(b, clearerr);
00241     b->priv.vt->clearerr(b);
00242 }
00243 
00244 
00254 int kblock_flush(struct KBlock *b);
00255 
00263 INLINE int kblock_close(struct KBlock *b)
00264 {
00265     KB_ASSERT_METHOD(b, close);
00266     return kblock_flush(b) | b->priv.vt->close(b);
00267 }
00268 
00274 INLINE bool kblock_buffered(struct KBlock *b)
00275 {
00276     ASSERT(b);
00277     return (b->priv.flags & KB_BUFFERED);
00278 }
00279 
00280 
00287 INLINE block_idx_t kblock_cachedBlock(struct KBlock *b)
00288 {
00289     ASSERT(kblock_buffered(b));
00290     return b->priv.curr_blk;
00291 }
00292 
00293 
00303 INLINE bool kblock_cacheDirty(struct KBlock *b)
00304 {
00305     ASSERT(kblock_buffered(b));
00306     return kblock_buffered(b) && (b->priv.flags & KB_CACHE_DIRTY);
00307 }
00308 
00316 INLINE bool kblock_partialWrite(struct KBlock *b)
00317 {
00318     ASSERT(b);
00319     return (b->priv.flags & KB_PARTIAL_WRITE);
00320 }
00321 
00345 size_t kblock_read(struct KBlock *b, block_idx_t idx, void *buf, size_t offset, size_t size);
00346 
00347 
00373 size_t kblock_write(struct KBlock *b, block_idx_t idx, const void *buf, size_t offset, size_t size);
00374 
00389 int kblock_copy(struct KBlock *b, block_idx_t src, block_idx_t dest);
00390 
00391 int kblock_swLoad(struct KBlock *b, block_idx_t index);
00392 int kblock_swStore(struct KBlock *b, block_idx_t index);
00393 size_t kblock_swReadBuf(struct KBlock *b, void *buf, size_t offset, size_t size);
00394 size_t kblock_swWriteBuf(struct KBlock *b, const void *buf, size_t offset, size_t size);
00395 int kblock_swClose(struct KBlock *b);
00396  //defgroup io_kblock
00398 
00399 
00400 #endif /* IO_KBLOCK_H */