blob: c85522e3808da75afa81283fe74c002c8a362f59 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2000 Takashi Iwai <tiwai@suse.de>
3 *
4 * Generic memory management routines for soundcard memory allocation
5 *
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.
10 *
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.
15 *
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
19 */
20
Ingo Molnaref9f0a42006-01-16 16:31:42 +010021#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/slab.h>
24#include <sound/core.h>
25#include <sound/util_mem.h>
26
27MODULE_AUTHOR("Takashi Iwai");
28MODULE_DESCRIPTION("Generic memory management routines for soundcard memory allocation");
29MODULE_LICENSE("GPL");
30
Takashi Iwai03da3122005-11-17 14:24:47 +010031#define get_memblk(p) list_entry(p, struct snd_util_memblk, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/*
34 * create a new memory manager
35 */
Takashi Iwai03da3122005-11-17 14:24:47 +010036struct snd_util_memhdr *
Linus Torvalds1da177e2005-04-16 15:20:36 -070037snd_util_memhdr_new(int memsize)
38{
Takashi Iwai03da3122005-11-17 14:24:47 +010039 struct snd_util_memhdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Takashi Iwai561b2202005-09-09 14:22:34 +020041 hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 if (hdr == NULL)
43 return NULL;
44 hdr->size = memsize;
Ingo Molnaref9f0a42006-01-16 16:31:42 +010045 mutex_init(&hdr->block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 INIT_LIST_HEAD(&hdr->block);
47
48 return hdr;
49}
50
51/*
52 * free a memory manager
53 */
Takashi Iwai03da3122005-11-17 14:24:47 +010054void snd_util_memhdr_free(struct snd_util_memhdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 struct list_head *p;
57
Takashi Iwai5e246b82008-08-08 17:12:47 +020058 if (!hdr)
59 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 /* release all blocks */
61 while ((p = hdr->block.next) != &hdr->block) {
62 list_del(p);
63 kfree(get_memblk(p));
64 }
65 kfree(hdr);
66}
67
68/*
69 * allocate a memory block (without mutex)
70 */
Takashi Iwai03da3122005-11-17 14:24:47 +010071struct snd_util_memblk *
72__snd_util_mem_alloc(struct snd_util_memhdr *hdr, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Takashi Iwai03da3122005-11-17 14:24:47 +010074 struct snd_util_memblk *blk;
75 unsigned int units, prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct list_head *p;
77
Takashi Iwai5e246b82008-08-08 17:12:47 +020078 if (snd_BUG_ON(!hdr || size <= 0))
79 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 /* word alignment */
82 units = size;
83 if (units & 1)
84 units++;
85 if (units > hdr->size)
86 return NULL;
87
88 /* look for empty block */
89 prev_offset = 0;
90 list_for_each(p, &hdr->block) {
91 blk = get_memblk(p);
92 if (blk->offset - prev_offset >= units)
93 goto __found;
94 prev_offset = blk->offset + blk->size;
95 }
96 if (hdr->size - prev_offset < units)
97 return NULL;
98
99__found:
100 return __snd_util_memblk_new(hdr, units, p->prev);
101}
102
103
104/*
105 * create a new memory block with the given size
106 * the block is linked next to prev
107 */
Takashi Iwai03da3122005-11-17 14:24:47 +0100108struct snd_util_memblk *
109__snd_util_memblk_new(struct snd_util_memhdr *hdr, unsigned int units,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 struct list_head *prev)
111{
Takashi Iwai03da3122005-11-17 14:24:47 +0100112 struct snd_util_memblk *blk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Takashi Iwai03da3122005-11-17 14:24:47 +0100114 blk = kmalloc(sizeof(struct snd_util_memblk) + hdr->block_extra_size,
115 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (blk == NULL)
117 return NULL;
118
Adrian Bunk8e6c9622007-07-30 15:40:43 +0200119 if (prev == &hdr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 blk->offset = 0;
121 else {
Takashi Iwai03da3122005-11-17 14:24:47 +0100122 struct snd_util_memblk *p = get_memblk(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 blk->offset = p->offset + p->size;
124 }
125 blk->size = units;
126 list_add(&blk->list, prev);
127 hdr->nblocks++;
128 hdr->used += units;
129 return blk;
130}
131
132
133/*
134 * allocate a memory block (with mutex)
135 */
Takashi Iwai03da3122005-11-17 14:24:47 +0100136struct snd_util_memblk *
137snd_util_mem_alloc(struct snd_util_memhdr *hdr, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Takashi Iwai03da3122005-11-17 14:24:47 +0100139 struct snd_util_memblk *blk;
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100140 mutex_lock(&hdr->block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 blk = __snd_util_mem_alloc(hdr, size);
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100142 mutex_unlock(&hdr->block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return blk;
144}
145
146
147/*
148 * remove the block from linked-list and free resource
149 * (without mutex)
150 */
151void
Takashi Iwai03da3122005-11-17 14:24:47 +0100152__snd_util_mem_free(struct snd_util_memhdr *hdr, struct snd_util_memblk *blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 list_del(&blk->list);
155 hdr->nblocks--;
156 hdr->used -= blk->size;
157 kfree(blk);
158}
159
160/*
161 * free a memory block (with mutex)
162 */
Takashi Iwai03da3122005-11-17 14:24:47 +0100163int snd_util_mem_free(struct snd_util_memhdr *hdr, struct snd_util_memblk *blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Takashi Iwai5e246b82008-08-08 17:12:47 +0200165 if (snd_BUG_ON(!hdr || !blk))
166 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100168 mutex_lock(&hdr->block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 __snd_util_mem_free(hdr, blk);
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100170 mutex_unlock(&hdr->block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return 0;
172}
173
174/*
175 * return available memory size
176 */
Takashi Iwai03da3122005-11-17 14:24:47 +0100177int snd_util_mem_avail(struct snd_util_memhdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 unsigned int size;
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100180 mutex_lock(&hdr->block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 size = hdr->size - hdr->used;
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100182 mutex_unlock(&hdr->block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return size;
184}
185
186
187EXPORT_SYMBOL(snd_util_memhdr_new);
188EXPORT_SYMBOL(snd_util_memhdr_free);
189EXPORT_SYMBOL(snd_util_mem_alloc);
190EXPORT_SYMBOL(snd_util_mem_free);
191EXPORT_SYMBOL(snd_util_mem_avail);
192EXPORT_SYMBOL(__snd_util_mem_alloc);
193EXPORT_SYMBOL(__snd_util_mem_free);
194EXPORT_SYMBOL(__snd_util_memblk_new);
195
196/*
197 * INIT part
198 */
199
200static int __init alsa_util_mem_init(void)
201{
202 return 0;
203}
204
205static void __exit alsa_util_mem_exit(void)
206{
207}
208
209module_init(alsa_util_mem_init)
210module_exit(alsa_util_mem_exit)