engines/libblkio: Add option libblkio_force_enable_completion_eventfd
[fio.git] / smalloc.c
index 125e07bf1d21a6d4c49ba0c5cbb79a44efe20df0..23243054ec7ab401ee7d89d73db81d3d809dd612 100644 (file)
--- a/smalloc.c
+++ b/smalloc.c
@@ -54,7 +54,7 @@ struct block_hdr {
  */
 static const bool enable_smalloc_debug = false;
 
-static struct pool mp[MAX_POOLS];
+static struct pool *mp;
 static unsigned int nr_pools;
 static unsigned int last_pool;
 
@@ -173,7 +173,7 @@ static bool add_pool(struct pool *pool, unsigned int alloc_size)
        pool->mmap_size = alloc_size;
 
        pool->nr_blocks = bitmap_blocks;
-       pool->free_blocks = bitmap_blocks * SMALLOC_BPB;
+       pool->free_blocks = bitmap_blocks * SMALLOC_BPI;
 
        mmap_flags = OS_MAP_ANON;
 #ifdef CONFIG_ESX
@@ -208,6 +208,20 @@ void sinit(void)
        bool ret;
        int i;
 
+       /*
+        * sinit() can be called more than once if alloc-size is
+        * set. But we want to allocate space for the struct pool
+        * instances only once.
+        */
+       if (!mp) {
+               mp = (struct pool *) mmap(NULL,
+                       MAX_POOLS * sizeof(struct pool),
+                       PROT_READ | PROT_WRITE,
+                       OS_MAP_ANON | MAP_SHARED, -1, 0);
+
+               assert(mp != MAP_FAILED);
+       }
+
        for (i = 0; i < INITIAL_POOLS; i++) {
                ret = add_pool(&mp[nr_pools], smalloc_pool_size);
                if (!ret)
@@ -239,6 +253,8 @@ void scleanup(void)
 
        for (i = 0; i < nr_pools; i++)
                cleanup_pool(&mp[i]);
+
+       munmap(mp, MAX_POOLS * sizeof(struct pool));
 }
 
 #ifdef SMALLOC_REDZONE
@@ -267,13 +283,13 @@ static void sfree_check_redzone(struct block_hdr *hdr)
        if (hdr->prered != SMALLOC_PRE_RED) {
                log_err("smalloc pre redzone destroyed!\n"
                        " ptr=%p, prered=%x, expected %x\n",
-                               hdr, hdr->prered, SMALLOC_PRE_RED);
+                               hdr+1, hdr->prered, SMALLOC_PRE_RED);
                assert(0);
        }
        if (*postred != SMALLOC_POST_RED) {
                log_err("smalloc post redzone destroyed!\n"
                        "  ptr=%p, postred=%x, expected %x\n",
-                               hdr, *postred, SMALLOC_POST_RED);
+                               hdr+1, *postred, SMALLOC_POST_RED);
                assert(0);
        }
 }