staging: rt2860: use new hex_to_bin() method
[safe/jmp/linux-2.6] / lib / genalloc.c
index 75ae68c..736c3b0 100644 (file)
@@ -10,7 +10,9 @@
  * Version 2.  See the file COPYING for more details.
  */
 
+#include <linux/slab.h>
 #include <linux/module.h>
+#include <linux/bitmap.h>
 #include <linux/genalloc.h>
 
 
@@ -54,11 +56,10 @@ int gen_pool_add(struct gen_pool *pool, unsigned long addr, size_t size,
        int nbytes = sizeof(struct gen_pool_chunk) +
                                (nbits + BITS_PER_BYTE - 1) / BITS_PER_BYTE;
 
-       chunk = kmalloc_node(nbytes, GFP_KERNEL, nid);
+       chunk = kmalloc_node(nbytes, GFP_KERNEL | __GFP_ZERO, nid);
        if (unlikely(chunk == NULL))
                return -1;
 
-       memset(chunk, 0, nbytes);
        spin_lock_init(&chunk->lock);
        chunk->start_addr = addr;
        chunk->end_addr = addr + size;
@@ -86,7 +87,6 @@ void gen_pool_destroy(struct gen_pool *pool)
        int bit, end_bit;
 
 
-       write_lock(&pool->lock);
        list_for_each_safe(_chunk, _next_chunk, &pool->chunks) {
                chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
                list_del(&chunk->next_chunk);
@@ -116,7 +116,7 @@ unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size)
        struct gen_pool_chunk *chunk;
        unsigned long addr, flags;
        int order = pool->min_alloc_order;
-       int nbits, bit, start_bit, end_bit;
+       int nbits, start_bit, end_bit;
 
        if (size == 0)
                return 0;
@@ -131,29 +131,19 @@ unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size)
                end_bit -= nbits + 1;
 
                spin_lock_irqsave(&chunk->lock, flags);
-               bit = -1;
-               while (bit + 1 < end_bit) {
-                       bit = find_next_zero_bit(chunk->bits, end_bit, bit + 1);
-                       if (bit >= end_bit)
-                               break;
-
-                       start_bit = bit;
-                       if (nbits > 1) {
-                               bit = find_next_bit(chunk->bits, bit + nbits,
-                                                       bit + 1);
-                               if (bit - start_bit < nbits)
-                                       continue;
-                       }
-
-                       addr = chunk->start_addr +
-                                           ((unsigned long)start_bit << order);
-                       while (nbits--)
-                               __set_bit(start_bit++, &chunk->bits);
+               start_bit = bitmap_find_next_zero_area(chunk->bits, end_bit, 0,
+                                               nbits, 0);
+               if (start_bit >= end_bit) {
                        spin_unlock_irqrestore(&chunk->lock, flags);
-                       read_unlock(&pool->lock);
-                       return addr;
+                       continue;
                }
+
+               addr = chunk->start_addr + ((unsigned long)start_bit << order);
+
+               bitmap_set(chunk->bits, start_bit, nbits);
                spin_unlock_irqrestore(&chunk->lock, flags);
+               read_unlock(&pool->lock);
+               return addr;
        }
        read_unlock(&pool->lock);
        return 0;
@@ -187,7 +177,7 @@ void gen_pool_free(struct gen_pool *pool, unsigned long addr, size_t size)
                        spin_lock_irqsave(&chunk->lock, flags);
                        bit = (addr - chunk->start_addr) >> order;
                        while (nbits--)
-                               __clear_bit(bit++, &chunk->bits);
+                               __clear_bit(bit++, chunk->bits);
                        spin_unlock_irqrestore(&chunk->lock, flags);
                        break;
                }