pxa2xx_spi: fix build breakage
[linux/fpc-iii.git] / sound / synth / util_mem.c
blobdeabe5f899c494780eb29c188f9fe23edf8c92e8
1 /*
2 * Copyright (C) 2000 Takashi Iwai <tiwai@suse.de>
4 * Generic memory management routines for soundcard memory allocation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/mutex.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <sound/core.h>
25 #include <sound/util_mem.h>
27 MODULE_AUTHOR("Takashi Iwai");
28 MODULE_DESCRIPTION("Generic memory management routines for soundcard memory allocation");
29 MODULE_LICENSE("GPL");
31 #define get_memblk(p) list_entry(p, struct snd_util_memblk, list)
34 * create a new memory manager
36 struct snd_util_memhdr *
37 snd_util_memhdr_new(int memsize)
39 struct snd_util_memhdr *hdr;
41 hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
42 if (hdr == NULL)
43 return NULL;
44 hdr->size = memsize;
45 mutex_init(&hdr->block_mutex);
46 INIT_LIST_HEAD(&hdr->block);
48 return hdr;
52 * free a memory manager
54 void snd_util_memhdr_free(struct snd_util_memhdr *hdr)
56 struct list_head *p;
58 snd_assert(hdr != NULL, return);
59 /* release all blocks */
60 while ((p = hdr->block.next) != &hdr->block) {
61 list_del(p);
62 kfree(get_memblk(p));
64 kfree(hdr);
68 * allocate a memory block (without mutex)
70 struct snd_util_memblk *
71 __snd_util_mem_alloc(struct snd_util_memhdr *hdr, int size)
73 struct snd_util_memblk *blk;
74 unsigned int units, prev_offset;
75 struct list_head *p;
77 snd_assert(hdr != NULL, return NULL);
78 snd_assert(size > 0, return NULL);
80 /* word alignment */
81 units = size;
82 if (units & 1)
83 units++;
84 if (units > hdr->size)
85 return NULL;
87 /* look for empty block */
88 prev_offset = 0;
89 list_for_each(p, &hdr->block) {
90 blk = get_memblk(p);
91 if (blk->offset - prev_offset >= units)
92 goto __found;
93 prev_offset = blk->offset + blk->size;
95 if (hdr->size - prev_offset < units)
96 return NULL;
98 __found:
99 return __snd_util_memblk_new(hdr, units, p->prev);
104 * create a new memory block with the given size
105 * the block is linked next to prev
107 struct snd_util_memblk *
108 __snd_util_memblk_new(struct snd_util_memhdr *hdr, unsigned int units,
109 struct list_head *prev)
111 struct snd_util_memblk *blk;
113 blk = kmalloc(sizeof(struct snd_util_memblk) + hdr->block_extra_size,
114 GFP_KERNEL);
115 if (blk == NULL)
116 return NULL;
118 if (prev == &hdr->block)
119 blk->offset = 0;
120 else {
121 struct snd_util_memblk *p = get_memblk(prev);
122 blk->offset = p->offset + p->size;
124 blk->size = units;
125 list_add(&blk->list, prev);
126 hdr->nblocks++;
127 hdr->used += units;
128 return blk;
133 * allocate a memory block (with mutex)
135 struct snd_util_memblk *
136 snd_util_mem_alloc(struct snd_util_memhdr *hdr, int size)
138 struct snd_util_memblk *blk;
139 mutex_lock(&hdr->block_mutex);
140 blk = __snd_util_mem_alloc(hdr, size);
141 mutex_unlock(&hdr->block_mutex);
142 return blk;
147 * remove the block from linked-list and free resource
148 * (without mutex)
150 void
151 __snd_util_mem_free(struct snd_util_memhdr *hdr, struct snd_util_memblk *blk)
153 list_del(&blk->list);
154 hdr->nblocks--;
155 hdr->used -= blk->size;
156 kfree(blk);
160 * free a memory block (with mutex)
162 int snd_util_mem_free(struct snd_util_memhdr *hdr, struct snd_util_memblk *blk)
164 snd_assert(hdr && blk, return -EINVAL);
166 mutex_lock(&hdr->block_mutex);
167 __snd_util_mem_free(hdr, blk);
168 mutex_unlock(&hdr->block_mutex);
169 return 0;
173 * return available memory size
175 int snd_util_mem_avail(struct snd_util_memhdr *hdr)
177 unsigned int size;
178 mutex_lock(&hdr->block_mutex);
179 size = hdr->size - hdr->used;
180 mutex_unlock(&hdr->block_mutex);
181 return size;
185 EXPORT_SYMBOL(snd_util_memhdr_new);
186 EXPORT_SYMBOL(snd_util_memhdr_free);
187 EXPORT_SYMBOL(snd_util_mem_alloc);
188 EXPORT_SYMBOL(snd_util_mem_free);
189 EXPORT_SYMBOL(snd_util_mem_avail);
190 EXPORT_SYMBOL(__snd_util_mem_alloc);
191 EXPORT_SYMBOL(__snd_util_mem_free);
192 EXPORT_SYMBOL(__snd_util_memblk_new);
195 * INIT part
198 static int __init alsa_util_mem_init(void)
200 return 0;
203 static void __exit alsa_util_mem_exit(void)
207 module_init(alsa_util_mem_init)
208 module_exit(alsa_util_mem_exit)