smalloc: remember to account for sizeof block header
[fio.git] / smalloc.c
index fd0e9bd05fe81e1a91750933ca4f405937dd3a8a..8d1193ab9f57e32aa5bb29431a760162116b2ca8 100644 (file)
--- a/smalloc.c
+++ b/smalloc.c
 #define INITIAL_SIZE   1048576 /* new pool size */
 #define MAX_POOLS      32      /* maximum number of pools to setup */
 
+unsigned int smalloc_pool_size = INITIAL_SIZE;
+
 #ifdef ENABLE_RESIZE
-#define MAX_SIZE       8 * INITIAL_SIZE
+#define MAX_SIZE       8 * smalloc_pool_size
 static unsigned int resize_error;
 #endif
 
@@ -218,7 +220,7 @@ fail:
 #endif
 }
 
-static int add_pool(struct pool *pool)
+static int add_pool(struct pool *pool, unsigned int alloc_size)
 {
        struct mem_hdr *hdr;
        void *ptr;
@@ -229,7 +231,12 @@ static int add_pool(struct pool *pool)
        if (fd < 0)
                goto out_close;
 
-       pool->size = INITIAL_SIZE;
+       alloc_size += sizeof(*hdr);
+       if (alloc_size > smalloc_pool_size)
+               pool->size = alloc_size;
+       else
+               pool->size = smalloc_pool_size;
+
        if (ftruncate(fd, pool->size) < 0)
                goto out_unlink;
 
@@ -273,7 +280,7 @@ void sinit(void)
 #ifdef MP_SAFE
        lock = fio_mutex_rw_init();
 #endif
-       ret = add_pool(&mp[0]);
+       ret = add_pool(&mp[0], INITIAL_SIZE);
        assert(!ret);
 }
 
@@ -456,7 +463,7 @@ void *smalloc(unsigned int size)
                else {
                        i = nr_pools;
                        global_read_unlock();
-                       if (add_pool(&mp[nr_pools]))
+                       if (add_pool(&mp[nr_pools], size))
                                goto out;
                        global_read_lock();
                }