smalloc: cleanups and allow sfree(NULL)
authorJens Axboe <jens.axboe@oracle.com>
Wed, 16 Apr 2008 17:51:46 +0000 (19:51 +0200)
committerJens Axboe <jens.axboe@oracle.com>
Wed, 16 Apr 2008 17:51:46 +0000 (19:51 +0200)
Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
smalloc.c

index 8d1193ab9f57e32aa5bb29431a760162116b2ca8..7e6c4f8f1d9a930725ddc3f6282b5ab2dd74f352 100644 (file)
--- a/smalloc.c
+++ b/smalloc.c
@@ -336,6 +336,9 @@ void sfree(void *ptr)
        struct pool *pool = NULL;
        unsigned int i;
 
        struct pool *pool = NULL;
        unsigned int i;
 
+       if (!ptr)
+               return;
+
        global_read_lock();
 
        for (i = 0; i < nr_pools; i++) {
        global_read_lock();
 
        for (i = 0; i < nr_pools; i++) {
@@ -357,14 +360,14 @@ static void *smalloc_pool(struct pool *pool, unsigned int size)
        int did_restart = 0;
        void *ret;
 
        int did_restart = 0;
        void *ret;
 
-       /*
-        * slight chance of race with sfree() here, but acceptable
-        */
-       if (!size || size > pool->room + sizeof(*hdr) ||
-           ((size > pool->largest_block) && pool->largest_block))
+       if (!size)
                return NULL;
 
        pool_lock(pool);
                return NULL;
 
        pool_lock(pool);
+       if (size > pool->room + sizeof(*hdr))
+               goto fail;
+       if ((size > pool->largest_block) && pool->largest_block)
+               goto fail;
 restart:
        hdr = pool->last;
        prv = NULL;
 restart:
        hdr = pool->last;
        prv = NULL;