From 2c6855a96996394a7f3b2ff5674cd50e0da025d3 Mon Sep 17 00:00:00 2001 From: Danila Shtan Date: Wed, 12 Feb 2014 15:37:44 +0500 Subject: [PATCH] fix bitmap allocation --- core/cache.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/core/cache.c b/core/cache.c index 287d5666..bda69c7e 100644 --- a/core/cache.c +++ b/core/cache.c @@ -104,19 +104,27 @@ static uint64_t cache_mark_blocks(struct uwsgi_cache *uc, uint64_t index, uint64 uint64_t first_byte = index/8; uint8_t first_byte_bit = index % 8; - uint64_t last_byte = (index+needed_blocks)/8; - uint8_t last_byte_bit = (index+needed_blocks) % 8; + // offset starts with 0, so actial last byte is index + needed_blocks - 1 + uint64_t last_byte = (index + needed_blocks - 1) / 8; + uint8_t last_byte_bit = (index + needed_blocks - 1) % 8; + + uint64_t needed_bytes = (last_byte - first_byte) + 1; //uwsgi_log("%llu %u %llu %u\n", first_byte, first_byte_bit, last_byte, last_byte_bit); uint8_t mask = 0xff >> first_byte_bit; + + if (needed_bytes == 1) { + // kinda hacky, but it does the job + mask >>= (8 - last_byte_bit); + mask <<= (8 - last_byte_bit); + } + uc->blocks_bitmap[first_byte] |= mask; - uint64_t needed_bytes = (last_byte - first_byte)+1; - if (needed_bytes > 1) { mask = 0xff << (8 - last_byte_bit); - uc->blocks_bitmap[last_byte-1] |= mask; + uc->blocks_bitmap[last_byte] |= mask; } if (needed_bytes > 2) {