1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2000 Takashi Iwai <tiwai@suse.de>
5 * Generic memory management routines for soundcard memory allocation
8 #include <linux/mutex.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <sound/core.h>
13 #include <sound/util_mem.h>
15 MODULE_AUTHOR("Takashi Iwai");
16 MODULE_DESCRIPTION("Generic memory management routines for soundcard memory allocation");
17 MODULE_LICENSE("GPL");
19 #define get_memblk(p) list_entry(p, struct snd_util_memblk, list)
22 * create a new memory manager
24 struct snd_util_memhdr *
25 snd_util_memhdr_new(int memsize)
27 struct snd_util_memhdr *hdr;
29 hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
33 mutex_init(&hdr->block_mutex);
34 INIT_LIST_HEAD(&hdr->block);
40 * free a memory manager
42 void snd_util_memhdr_free(struct snd_util_memhdr *hdr)
48 /* release all blocks */
49 while ((p = hdr->block.next) != &hdr->block) {
57 * allocate a memory block (without mutex)
59 struct snd_util_memblk *
60 __snd_util_mem_alloc(struct snd_util_memhdr *hdr, int size)
62 struct snd_util_memblk *blk;
63 unsigned int units, prev_offset;
66 if (snd_BUG_ON(!hdr || size <= 0))
73 if (units > hdr->size)
76 /* look for empty block */
78 list_for_each(p, &hdr->block) {
80 if (blk->offset - prev_offset >= units)
82 prev_offset = blk->offset + blk->size;
84 if (hdr->size - prev_offset < units)
88 return __snd_util_memblk_new(hdr, units, p->prev);
93 * create a new memory block with the given size
94 * the block is linked next to prev
96 struct snd_util_memblk *
97 __snd_util_memblk_new(struct snd_util_memhdr *hdr, unsigned int units,
98 struct list_head *prev)
100 struct snd_util_memblk *blk;
102 blk = kmalloc(sizeof(struct snd_util_memblk) + hdr->block_extra_size,
107 if (prev == &hdr->block)
110 struct snd_util_memblk *p = get_memblk(prev);
111 blk->offset = p->offset + p->size;
114 list_add(&blk->list, prev);
122 * allocate a memory block (with mutex)
124 struct snd_util_memblk *
125 snd_util_mem_alloc(struct snd_util_memhdr *hdr, int size)
127 struct snd_util_memblk *blk;
128 mutex_lock(&hdr->block_mutex);
129 blk = __snd_util_mem_alloc(hdr, size);
130 mutex_unlock(&hdr->block_mutex);
136 * remove the block from linked-list and free resource
140 __snd_util_mem_free(struct snd_util_memhdr *hdr, struct snd_util_memblk *blk)
142 list_del(&blk->list);
144 hdr->used -= blk->size;
149 * free a memory block (with mutex)
151 int snd_util_mem_free(struct snd_util_memhdr *hdr, struct snd_util_memblk *blk)
153 if (snd_BUG_ON(!hdr || !blk))
156 mutex_lock(&hdr->block_mutex);
157 __snd_util_mem_free(hdr, blk);
158 mutex_unlock(&hdr->block_mutex);
163 * return available memory size
165 int snd_util_mem_avail(struct snd_util_memhdr *hdr)
168 mutex_lock(&hdr->block_mutex);
169 size = hdr->size - hdr->used;
170 mutex_unlock(&hdr->block_mutex);
175 EXPORT_SYMBOL(snd_util_memhdr_new);
176 EXPORT_SYMBOL(snd_util_memhdr_free);
177 EXPORT_SYMBOL(snd_util_mem_alloc);
178 EXPORT_SYMBOL(snd_util_mem_free);
179 EXPORT_SYMBOL(snd_util_mem_avail);
180 EXPORT_SYMBOL(__snd_util_mem_alloc);
181 EXPORT_SYMBOL(__snd_util_mem_free);
182 EXPORT_SYMBOL(__snd_util_memblk_new);