blob: 304a8f1740c3852f990227dfebd2a478b1822c42 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Copyright (C) 2000 Takashi Iwai <tiwai@suse.de>
4 *
5 * Generic memory management routines for soundcard memory allocation
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Ingo Molnaref9f0a42006-01-16 16:31:42 +01008#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/init.h>
10#include <linux/slab.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040011#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <sound/core.h>
13#include <sound/util_mem.h>
14
15MODULE_AUTHOR("Takashi Iwai");
16MODULE_DESCRIPTION("Generic memory management routines for soundcard memory allocation");
17MODULE_LICENSE("GPL");
18
Takashi Iwai03da3122005-11-17 14:24:47 +010019#define get_memblk(p) list_entry(p, struct snd_util_memblk, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21/*
22 * create a new memory manager
23 */
Takashi Iwai03da3122005-11-17 14:24:47 +010024struct snd_util_memhdr *
Linus Torvalds1da177e2005-04-16 15:20:36 -070025snd_util_memhdr_new(int memsize)
26{
Takashi Iwai03da3122005-11-17 14:24:47 +010027 struct snd_util_memhdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Takashi Iwai561b2202005-09-09 14:22:34 +020029 hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 if (hdr == NULL)
31 return NULL;
32 hdr->size = memsize;
Ingo Molnaref9f0a42006-01-16 16:31:42 +010033 mutex_init(&hdr->block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 INIT_LIST_HEAD(&hdr->block);
35
36 return hdr;
37}
38
39/*
40 * free a memory manager
41 */
Takashi Iwai03da3122005-11-17 14:24:47 +010042void snd_util_memhdr_free(struct snd_util_memhdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 struct list_head *p;
45
Takashi Iwai5e246b82008-08-08 17:12:47 +020046 if (!hdr)
47 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 /* release all blocks */
49 while ((p = hdr->block.next) != &hdr->block) {
50 list_del(p);
51 kfree(get_memblk(p));
52 }
53 kfree(hdr);
54}
55
56/*
57 * allocate a memory block (without mutex)
58 */
Takashi Iwai03da3122005-11-17 14:24:47 +010059struct snd_util_memblk *
60__snd_util_mem_alloc(struct snd_util_memhdr *hdr, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Takashi Iwai03da3122005-11-17 14:24:47 +010062 struct snd_util_memblk *blk;
63 unsigned int units, prev_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 struct list_head *p;
65
Takashi Iwai5e246b82008-08-08 17:12:47 +020066 if (snd_BUG_ON(!hdr || size <= 0))
67 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 /* word alignment */
70 units = size;
71 if (units & 1)
72 units++;
73 if (units > hdr->size)
74 return NULL;
75
76 /* look for empty block */
77 prev_offset = 0;
78 list_for_each(p, &hdr->block) {
79 blk = get_memblk(p);
80 if (blk->offset - prev_offset >= units)
81 goto __found;
82 prev_offset = blk->offset + blk->size;
83 }
84 if (hdr->size - prev_offset < units)
85 return NULL;
86
87__found:
88 return __snd_util_memblk_new(hdr, units, p->prev);
89}
90
91
92/*
93 * create a new memory block with the given size
94 * the block is linked next to prev
95 */
Takashi Iwai03da3122005-11-17 14:24:47 +010096struct snd_util_memblk *
97__snd_util_memblk_new(struct snd_util_memhdr *hdr, unsigned int units,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 struct list_head *prev)
99{
Takashi Iwai03da3122005-11-17 14:24:47 +0100100 struct snd_util_memblk *blk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Takashi Iwai03da3122005-11-17 14:24:47 +0100102 blk = kmalloc(sizeof(struct snd_util_memblk) + hdr->block_extra_size,
103 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (blk == NULL)
105 return NULL;
106
Adrian Bunk8e6c9622007-07-30 15:40:43 +0200107 if (prev == &hdr->block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 blk->offset = 0;
109 else {
Takashi Iwai03da3122005-11-17 14:24:47 +0100110 struct snd_util_memblk *p = get_memblk(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 blk->offset = p->offset + p->size;
112 }
113 blk->size = units;
114 list_add(&blk->list, prev);
115 hdr->nblocks++;
116 hdr->used += units;
117 return blk;
118}
119
120
121/*
122 * allocate a memory block (with mutex)
123 */
Takashi Iwai03da3122005-11-17 14:24:47 +0100124struct snd_util_memblk *
125snd_util_mem_alloc(struct snd_util_memhdr *hdr, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Takashi Iwai03da3122005-11-17 14:24:47 +0100127 struct snd_util_memblk *blk;
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100128 mutex_lock(&hdr->block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 blk = __snd_util_mem_alloc(hdr, size);
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100130 mutex_unlock(&hdr->block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return blk;
132}
133
134
135/*
136 * remove the block from linked-list and free resource
137 * (without mutex)
138 */
139void
Takashi Iwai03da3122005-11-17 14:24:47 +0100140__snd_util_mem_free(struct snd_util_memhdr *hdr, struct snd_util_memblk *blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 list_del(&blk->list);
143 hdr->nblocks--;
144 hdr->used -= blk->size;
145 kfree(blk);
146}
147
148/*
149 * free a memory block (with mutex)
150 */
Takashi Iwai03da3122005-11-17 14:24:47 +0100151int snd_util_mem_free(struct snd_util_memhdr *hdr, struct snd_util_memblk *blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Takashi Iwai5e246b82008-08-08 17:12:47 +0200153 if (snd_BUG_ON(!hdr || !blk))
154 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100156 mutex_lock(&hdr->block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 __snd_util_mem_free(hdr, blk);
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100158 mutex_unlock(&hdr->block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return 0;
160}
161
162/*
163 * return available memory size
164 */
Takashi Iwai03da3122005-11-17 14:24:47 +0100165int snd_util_mem_avail(struct snd_util_memhdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 unsigned int size;
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100168 mutex_lock(&hdr->block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 size = hdr->size - hdr->used;
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100170 mutex_unlock(&hdr->block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return size;
172}
173
174
175EXPORT_SYMBOL(snd_util_memhdr_new);
176EXPORT_SYMBOL(snd_util_memhdr_free);
177EXPORT_SYMBOL(snd_util_mem_alloc);
178EXPORT_SYMBOL(snd_util_mem_free);
179EXPORT_SYMBOL(snd_util_mem_avail);
180EXPORT_SYMBOL(__snd_util_mem_alloc);
181EXPORT_SYMBOL(__snd_util_mem_free);
182EXPORT_SYMBOL(__snd_util_memblk_new);