blob: 217e8e552a429bd3ac6dce249b50c366cbb95dfa [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
21#include <sound/driver.h>
22#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;
45 init_MUTEX(&hdr->block_mutex);
46 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
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));
63 }
64 kfree(hdr);
65}
66
67/*
68 * allocate a memory block (without mutex)
69 */
Takashi Iwai03da3122005-11-17 14:24:47 +010070struct snd_util_memblk *
71__snd_util_mem_alloc(struct snd_util_memhdr *hdr, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Takashi Iwai03da3122005-11-17 14:24:47 +010073 struct snd_util_memblk *blk;
74 unsigned int units, prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 struct list_head *p;
76
77 snd_assert(hdr != NULL, return NULL);
78 snd_assert(size > 0, return NULL);
79
80 /* word alignment */
81 units = size;
82 if (units & 1)
83 units++;
84 if (units > hdr->size)
85 return NULL;
86
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;
94 }
95 if (hdr->size - prev_offset < units)
96 return NULL;
97
98__found:
99 return __snd_util_memblk_new(hdr, units, p->prev);
100}
101
102
103/*
104 * create a new memory block with the given size
105 * the block is linked next to prev
106 */
Takashi Iwai03da3122005-11-17 14:24:47 +0100107struct snd_util_memblk *
108__snd_util_memblk_new(struct snd_util_memhdr *hdr, unsigned int units,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 struct list_head *prev)
110{
Takashi Iwai03da3122005-11-17 14:24:47 +0100111 struct snd_util_memblk *blk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Takashi Iwai03da3122005-11-17 14:24:47 +0100113 blk = kmalloc(sizeof(struct snd_util_memblk) + hdr->block_extra_size,
114 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (blk == NULL)
116 return NULL;
117
118 if (! prev || prev == &hdr->block)
119 blk->offset = 0;
120 else {
Takashi Iwai03da3122005-11-17 14:24:47 +0100121 struct snd_util_memblk *p = get_memblk(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 blk->offset = p->offset + p->size;
123 }
124 blk->size = units;
125 list_add(&blk->list, prev);
126 hdr->nblocks++;
127 hdr->used += units;
128 return blk;
129}
130
131
132/*
133 * allocate a memory block (with mutex)
134 */
Takashi Iwai03da3122005-11-17 14:24:47 +0100135struct snd_util_memblk *
136snd_util_mem_alloc(struct snd_util_memhdr *hdr, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Takashi Iwai03da3122005-11-17 14:24:47 +0100138 struct snd_util_memblk *blk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 down(&hdr->block_mutex);
140 blk = __snd_util_mem_alloc(hdr, size);
141 up(&hdr->block_mutex);
142 return blk;
143}
144
145
146/*
147 * remove the block from linked-list and free resource
148 * (without mutex)
149 */
150void
Takashi Iwai03da3122005-11-17 14:24:47 +0100151__snd_util_mem_free(struct snd_util_memhdr *hdr, struct snd_util_memblk *blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 list_del(&blk->list);
154 hdr->nblocks--;
155 hdr->used -= blk->size;
156 kfree(blk);
157}
158
159/*
160 * free a memory block (with mutex)
161 */
Takashi Iwai03da3122005-11-17 14:24:47 +0100162int snd_util_mem_free(struct snd_util_memhdr *hdr, struct snd_util_memblk *blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 snd_assert(hdr && blk, return -EINVAL);
165
166 down(&hdr->block_mutex);
167 __snd_util_mem_free(hdr, blk);
168 up(&hdr->block_mutex);
169 return 0;
170}
171
172/*
173 * return available memory size
174 */
Takashi Iwai03da3122005-11-17 14:24:47 +0100175int snd_util_mem_avail(struct snd_util_memhdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 unsigned int size;
178 down(&hdr->block_mutex);
179 size = hdr->size - hdr->used;
180 up(&hdr->block_mutex);
181 return size;
182}
183
184
185EXPORT_SYMBOL(snd_util_memhdr_new);
186EXPORT_SYMBOL(snd_util_memhdr_free);
187EXPORT_SYMBOL(snd_util_mem_alloc);
188EXPORT_SYMBOL(snd_util_mem_free);
189EXPORT_SYMBOL(snd_util_mem_avail);
190EXPORT_SYMBOL(__snd_util_mem_alloc);
191EXPORT_SYMBOL(__snd_util_mem_free);
192EXPORT_SYMBOL(__snd_util_memblk_new);
193
194/*
195 * INIT part
196 */
197
198static int __init alsa_util_mem_init(void)
199{
200 return 0;
201}
202
203static void __exit alsa_util_mem_exit(void)
204{
205}
206
207module_init(alsa_util_mem_init)
208module_exit(alsa_util_mem_exit)