smalloc: fix off-by-one in ptr_valid()
[fio.git] / smalloc.c
index 3b512bcf63cdee4f57b6377684d366546fb0e55c..e97ba011319bd83e33549b51880d63fa2031806a 100644 (file)
--- a/smalloc.c
+++ b/smalloc.c
@@ -90,11 +90,16 @@ static inline void global_write_unlock(void)
 
 static inline int ptr_valid(struct pool *pool, void *ptr)
 {
-       unsigned int pool_size = pool->nr_blocks * SMALLOC_BPL;
+       unsigned int pool_size = (pool->nr_blocks + 1) * SMALLOC_BPL;
 
        return (ptr >= pool->map) && (ptr < pool->map + pool_size);
 }
 
+static inline unsigned int size_to_blocks(unsigned int size)
+{
+       return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
+}
+
 static int blocks_iter(unsigned int *map, unsigned int idx,
                       unsigned int nr_blocks,
                       int (*func)(unsigned int *map, unsigned int mask))
@@ -367,11 +372,6 @@ void sfree(void *ptr)
        sfree_pool(pool, ptr);
 }
 
-static inline unsigned int size_to_blocks(unsigned int size)
-{
-       return (size + SMALLOC_BPB - 1) / SMALLOC_BPB;
-}
-
 static void *__smalloc_pool(struct pool *pool, unsigned int size)
 {
        unsigned int nr_blocks;
@@ -439,10 +439,8 @@ static void *smalloc_pool(struct pool *pool, unsigned int size)
 #endif
 
        ptr = __smalloc_pool(pool, alloc_size);
-       if (!ptr) {
-               printf("failed allocating %u\n", alloc_size);
+       if (!ptr)
                return NULL;
-       }
 
        hdr = ptr;
        hdr->size = alloc_size;