]>
git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - sound/synth/util_mem.c
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
);
45 mutex_init(&hdr
->block_mutex
);
46 INIT_LIST_HEAD(&hdr
->block
);
52 * free a memory manager
54 void snd_util_memhdr_free(struct snd_util_memhdr
*hdr
)
60 /* release all blocks */
61 while ((p
= hdr
->block
.next
) != &hdr
->block
) {
69 * allocate a memory block (without mutex)
71 struct snd_util_memblk
*
72 __snd_util_mem_alloc(struct snd_util_memhdr
*hdr
, int size
)
74 struct snd_util_memblk
*blk
;
75 unsigned int units
, prev_offset
;
78 if (snd_BUG_ON(!hdr
|| size
<= 0))
85 if (units
> hdr
->size
)
88 /* look for empty block */
90 list_for_each(p
, &hdr
->block
) {
92 if (blk
->offset
- prev_offset
>= units
)
94 prev_offset
= blk
->offset
+ blk
->size
;
96 if (hdr
->size
- prev_offset
< units
)
100 return __snd_util_memblk_new(hdr
, units
, p
->prev
);
105 * create a new memory block with the given size
106 * the block is linked next to prev
108 struct snd_util_memblk
*
109 __snd_util_memblk_new(struct snd_util_memhdr
*hdr
, unsigned int units
,
110 struct list_head
*prev
)
112 struct snd_util_memblk
*blk
;
114 blk
= kmalloc(sizeof(struct snd_util_memblk
) + hdr
->block_extra_size
,
119 if (prev
== &hdr
->block
)
122 struct snd_util_memblk
*p
= get_memblk(prev
);
123 blk
->offset
= p
->offset
+ p
->size
;
126 list_add(&blk
->list
, prev
);
134 * allocate a memory block (with mutex)
136 struct snd_util_memblk
*
137 snd_util_mem_alloc(struct snd_util_memhdr
*hdr
, int size
)
139 struct snd_util_memblk
*blk
;
140 mutex_lock(&hdr
->block_mutex
);
141 blk
= __snd_util_mem_alloc(hdr
, size
);
142 mutex_unlock(&hdr
->block_mutex
);
148 * remove the block from linked-list and free resource
152 __snd_util_mem_free(struct snd_util_memhdr
*hdr
, struct snd_util_memblk
*blk
)
154 list_del(&blk
->list
);
156 hdr
->used
-= blk
->size
;
161 * free a memory block (with mutex)
163 int snd_util_mem_free(struct snd_util_memhdr
*hdr
, struct snd_util_memblk
*blk
)
165 if (snd_BUG_ON(!hdr
|| !blk
))
168 mutex_lock(&hdr
->block_mutex
);
169 __snd_util_mem_free(hdr
, blk
);
170 mutex_unlock(&hdr
->block_mutex
);
175 * return available memory size
177 int snd_util_mem_avail(struct snd_util_memhdr
*hdr
)
180 mutex_lock(&hdr
->block_mutex
);
181 size
= hdr
->size
- hdr
->used
;
182 mutex_unlock(&hdr
->block_mutex
);
187 EXPORT_SYMBOL(snd_util_memhdr_new
);
188 EXPORT_SYMBOL(snd_util_memhdr_free
);
189 EXPORT_SYMBOL(snd_util_mem_alloc
);
190 EXPORT_SYMBOL(snd_util_mem_free
);
191 EXPORT_SYMBOL(snd_util_mem_avail
);
192 EXPORT_SYMBOL(__snd_util_mem_alloc
);
193 EXPORT_SYMBOL(__snd_util_mem_free
);
194 EXPORT_SYMBOL(__snd_util_memblk_new
);
200 static int __init
alsa_util_mem_init(void)
205 static void __exit
alsa_util_mem_exit(void)
209 module_init(alsa_util_mem_init
)
210 module_exit(alsa_util_mem_exit
)