ia64/linux-2.6.18-xen.hg

diff lib/genalloc.c @ 0:831230e53067

Import 2.6.18 from kernel.org tarball.
author Ian Campbell <ian.campbell@xensource.com>
date Wed Apr 11 14:15:44 2007 +0100 (2007-04-11)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lib/genalloc.c	Wed Apr 11 14:15:44 2007 +0100
     1.3 @@ -0,0 +1,165 @@
     1.4 +/*
     1.5 + * Basic general purpose allocator for managing special purpose memory
     1.6 + * not managed by the regular kmalloc/kfree interface.
     1.7 + * Uses for this includes on-device special memory, uncached memory
     1.8 + * etc.
     1.9 + *
    1.10 + * Copyright 2005 (C) Jes Sorensen <jes@trained-monkey.org>
    1.11 + *
    1.12 + * This source code is licensed under the GNU General Public License,
    1.13 + * Version 2.  See the file COPYING for more details.
    1.14 + */
    1.15 +
    1.16 +#include <linux/module.h>
    1.17 +#include <linux/genalloc.h>
    1.18 +
    1.19 +
    1.20 +/*
    1.21 + * Create a new special memory pool.
    1.22 + *
    1.23 + * @min_alloc_order: log base 2 of number of bytes each bitmap bit represents
    1.24 + * @nid: node id of the node the pool structure should be allocated on, or -1
    1.25 + */
    1.26 +struct gen_pool *gen_pool_create(int min_alloc_order, int nid)
    1.27 +{
    1.28 +	struct gen_pool *pool;
    1.29 +
    1.30 +	pool = kmalloc_node(sizeof(struct gen_pool), GFP_KERNEL, nid);
    1.31 +	if (pool != NULL) {
    1.32 +		rwlock_init(&pool->lock);
    1.33 +		INIT_LIST_HEAD(&pool->chunks);
    1.34 +		pool->min_alloc_order = min_alloc_order;
    1.35 +	}
    1.36 +	return pool;
    1.37 +}
    1.38 +EXPORT_SYMBOL(gen_pool_create);
    1.39 +
    1.40 +
    1.41 +/*
    1.42 + * Add a new chunk of memory to the specified pool.
    1.43 + *
    1.44 + * @pool: pool to add new memory chunk to
    1.45 + * @addr: starting address of memory chunk to add to pool
    1.46 + * @size: size in bytes of the memory chunk to add to pool
    1.47 + * @nid: node id of the node the chunk structure and bitmap should be
    1.48 + *       allocated on, or -1
    1.49 + */
    1.50 +int gen_pool_add(struct gen_pool *pool, unsigned long addr, size_t size,
    1.51 +		 int nid)
    1.52 +{
    1.53 +	struct gen_pool_chunk *chunk;
    1.54 +	int nbits = size >> pool->min_alloc_order;
    1.55 +	int nbytes = sizeof(struct gen_pool_chunk) +
    1.56 +				(nbits + BITS_PER_BYTE - 1) / BITS_PER_BYTE;
    1.57 +
    1.58 +	chunk = kmalloc_node(nbytes, GFP_KERNEL, nid);
    1.59 +	if (unlikely(chunk == NULL))
    1.60 +		return -1;
    1.61 +
    1.62 +	memset(chunk, 0, nbytes);
    1.63 +	spin_lock_init(&chunk->lock);
    1.64 +	chunk->start_addr = addr;
    1.65 +	chunk->end_addr = addr + size;
    1.66 +
    1.67 +	write_lock(&pool->lock);
    1.68 +	list_add(&chunk->next_chunk, &pool->chunks);
    1.69 +	write_unlock(&pool->lock);
    1.70 +
    1.71 +	return 0;
    1.72 +}
    1.73 +EXPORT_SYMBOL(gen_pool_add);
    1.74 +
    1.75 +
    1.76 +/*
    1.77 + * Allocate the requested number of bytes from the specified pool.
    1.78 + * Uses a first-fit algorithm.
    1.79 + *
    1.80 + * @pool: pool to allocate from
    1.81 + * @size: number of bytes to allocate from the pool
    1.82 + */
    1.83 +unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size)
    1.84 +{
    1.85 +	struct list_head *_chunk;
    1.86 +	struct gen_pool_chunk *chunk;
    1.87 +	unsigned long addr, flags;
    1.88 +	int order = pool->min_alloc_order;
    1.89 +	int nbits, bit, start_bit, end_bit;
    1.90 +
    1.91 +	if (size == 0)
    1.92 +		return 0;
    1.93 +
    1.94 +	nbits = (size + (1UL << order) - 1) >> order;
    1.95 +
    1.96 +	read_lock(&pool->lock);
    1.97 +	list_for_each(_chunk, &pool->chunks) {
    1.98 +		chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
    1.99 +
   1.100 +		end_bit = (chunk->end_addr - chunk->start_addr) >> order;
   1.101 +		end_bit -= nbits + 1;
   1.102 +
   1.103 +		spin_lock_irqsave(&chunk->lock, flags);
   1.104 +		bit = -1;
   1.105 +		while (bit + 1 < end_bit) {
   1.106 +			bit = find_next_zero_bit(chunk->bits, end_bit, bit + 1);
   1.107 +			if (bit >= end_bit)
   1.108 +				break;
   1.109 +
   1.110 +			start_bit = bit;
   1.111 +			if (nbits > 1) {
   1.112 +				bit = find_next_bit(chunk->bits, bit + nbits,
   1.113 +							bit + 1);
   1.114 +				if (bit - start_bit < nbits)
   1.115 +					continue;
   1.116 +			}
   1.117 +
   1.118 +			addr = chunk->start_addr +
   1.119 +					    ((unsigned long)start_bit << order);
   1.120 +			while (nbits--)
   1.121 +				__set_bit(start_bit++, &chunk->bits);
   1.122 +			spin_unlock_irqrestore(&chunk->lock, flags);
   1.123 +			read_unlock(&pool->lock);
   1.124 +			return addr;
   1.125 +		}
   1.126 +		spin_unlock_irqrestore(&chunk->lock, flags);
   1.127 +	}
   1.128 +	read_unlock(&pool->lock);
   1.129 +	return 0;
   1.130 +}
   1.131 +EXPORT_SYMBOL(gen_pool_alloc);
   1.132 +
   1.133 +
   1.134 +/*
   1.135 + * Free the specified memory back to the specified pool.
   1.136 + *
   1.137 + * @pool: pool to free to
   1.138 + * @addr: starting address of memory to free back to pool
   1.139 + * @size: size in bytes of memory to free
   1.140 + */
   1.141 +void gen_pool_free(struct gen_pool *pool, unsigned long addr, size_t size)
   1.142 +{
   1.143 +	struct list_head *_chunk;
   1.144 +	struct gen_pool_chunk *chunk;
   1.145 +	unsigned long flags;
   1.146 +	int order = pool->min_alloc_order;
   1.147 +	int bit, nbits;
   1.148 +
   1.149 +	nbits = (size + (1UL << order) - 1) >> order;
   1.150 +
   1.151 +	read_lock(&pool->lock);
   1.152 +	list_for_each(_chunk, &pool->chunks) {
   1.153 +		chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
   1.154 +
   1.155 +		if (addr >= chunk->start_addr && addr < chunk->end_addr) {
   1.156 +			BUG_ON(addr + size > chunk->end_addr);
   1.157 +			spin_lock_irqsave(&chunk->lock, flags);
   1.158 +			bit = (addr - chunk->start_addr) >> order;
   1.159 +			while (nbits--)
   1.160 +				__clear_bit(bit++, &chunk->bits);
   1.161 +			spin_unlock_irqrestore(&chunk->lock, flags);
   1.162 +			break;
   1.163 +		}
   1.164 +	}
   1.165 +	BUG_ON(nbits > 0);
   1.166 +	read_unlock(&pool->lock);
   1.167 +}
   1.168 +EXPORT_SYMBOL(gen_pool_free);