From 91164be29025acef50289e3b3ee26544a95e91c4 Mon Sep 17 00:00:00 2001 From: Tomohiro Kusumi Date: Wed, 4 Feb 2015 05:31:46 +0900 Subject: [PATCH 1/2] sbin/hammer: add get_ondisk() - Cleanup get_buffer_data() and get_node() using a new inline function get_ondisk(). These two look different but actually do the same thing except for the way they release an existing buffer (--ref and eventually free it). Using get_ondisk() makes the code clear. - get_ondisk_data() frees an existing non-NULL buffer when isnew or the offset is out of 16KB range of the cached data. get_node() frees an existing buffer for btree node whenever the buffer is non-NULL. They both do the same thing after this. --- sbin/hammer/ondisk.c | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/sbin/hammer/ondisk.c b/sbin/hammer/ondisk.c index 936db7e..dd50eef 100644 --- a/sbin/hammer/ondisk.c +++ b/sbin/hammer/ondisk.c @@ -48,6 +48,8 @@ static void *alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp, struct buffer_info **bufferp); static hammer_off_t alloc_bigblock(struct volume_info *volume, int zone); static void get_buffer_readahead(struct buffer_info *base); +static __inline void *get_ondisk(hammer_off_t buf_offset, + struct buffer_info **bufferp, int isnew); #if 0 static void init_fifo_head(hammer_fifo_head_t head, u_int16_t hdr_type); static hammer_off_t hammer_alloc_fifo(int32_t base_bytes, int32_t ext_bytes, @@ -356,6 +358,11 @@ rel_buffer(struct buffer_info *buffer) } } +/* + * Retrieve a pointer to a buffer data given a buffer offset. The underlying + * bufferp is freed if isnew or the offset is beyond cached ondisk data. + * If bufferp is freed a referenced buffer is loaded into it. + */ void * get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp, int isnew) @@ -369,11 +376,7 @@ get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp, buffer = *bufferp = NULL; } } - if (buffer == NULL) - buffer = *bufferp = get_buffer(buf_offset, isnew); - if (buffer == NULL) - return (NULL); - return((char *)buffer->ondisk + ((int32_t)buf_offset & HAMMER_BUFMASK)); + return get_ondisk(buf_offset, bufferp, isnew); } /* @@ -383,17 +386,33 @@ get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp, hammer_node_ondisk_t get_node(hammer_off_t node_offset, struct buffer_info **bufp) { - struct buffer_info *buf; - - if (*bufp) + if (*bufp) { rel_buffer(*bufp); - *bufp = buf = get_buffer(node_offset, 0); - if (buf) { - return((void *)((char *)buf->ondisk + - (int32_t)(node_offset & HAMMER_BUFMASK))); - } else { - return(NULL); + *bufp = NULL; } + return get_ondisk(node_offset, bufp, 0); +} + +/* + * Return a pointer to a buffer data given a buffer offset. + * If *bufferp is NULL acquire the buffer otherwise use that buffer. + */ +static __inline +void * +get_ondisk(hammer_off_t buf_offset, struct buffer_info **bufferp, + int isnew) +{ + struct buffer_info *buffer; + + buffer = *bufferp; + if (buffer == NULL) { + buffer = *bufferp = get_buffer(buf_offset, isnew); + if (buffer == NULL) + return(NULL); + } + + return (char *)buffer->ondisk + + ((int32_t)buf_offset & HAMMER_BUFMASK); } /* -- 2.1.2