2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 * Memory allocation helpers.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sound/driver.h>
25 #include <asm/uaccess.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/time.h>
29 #include <linux/pci.h>
30 #include <sound/core.h>
31 #include <sound/info.h>
34 * memory allocation helpers and debug routines
37 #ifdef CONFIG_SND_DEBUG_MEMORY
39 struct snd_alloc_track {
43 struct list_head list;
47 #define snd_alloc_track_entry(obj) (struct snd_alloc_track *)((char*)obj - (unsigned long)((struct snd_alloc_track *)0)->data)
49 static long snd_alloc_kmalloc;
50 static LIST_HEAD(snd_alloc_kmalloc_list);
51 static DEFINE_SPINLOCK(snd_alloc_kmalloc_lock);
52 #define KMALLOC_MAGIC 0x87654321
53 static snd_info_entry_t *snd_memory_info_entry;
55 void __init snd_memory_init(void)
57 snd_alloc_kmalloc = 0;
60 void snd_memory_done(void)
62 struct list_head *head;
63 struct snd_alloc_track *t;
65 if (snd_alloc_kmalloc > 0)
66 snd_printk(KERN_ERR "Not freed snd_alloc_kmalloc = %li\n", snd_alloc_kmalloc);
67 list_for_each_prev(head, &snd_alloc_kmalloc_list) {
68 t = list_entry(head, struct snd_alloc_track, list);
69 if (t->magic != KMALLOC_MAGIC) {
70 snd_printk(KERN_ERR "Corrupted kmalloc\n");
73 snd_printk(KERN_ERR "kmalloc(%ld) from %p not freed\n", (long) t->size, t->caller);
77 static void *__snd_kmalloc(size_t size, gfp_t flags, void *caller)
79 unsigned long cpu_flags;
80 struct snd_alloc_track *t;
83 ptr = snd_wrapper_kmalloc(size + sizeof(struct snd_alloc_track), flags);
85 t = (struct snd_alloc_track *)ptr;
86 t->magic = KMALLOC_MAGIC;
88 spin_lock_irqsave(&snd_alloc_kmalloc_lock, cpu_flags);
89 list_add_tail(&t->list, &snd_alloc_kmalloc_list);
90 spin_unlock_irqrestore(&snd_alloc_kmalloc_lock, cpu_flags);
92 snd_alloc_kmalloc += size;
98 #define _snd_kmalloc(size, flags) __snd_kmalloc((size), (flags), __builtin_return_address(0));
99 void *snd_hidden_kmalloc(size_t size, gfp_t flags)
101 return _snd_kmalloc(size, flags);
104 void *snd_hidden_kzalloc(size_t size, gfp_t flags)
106 void *ret = _snd_kmalloc(size, flags);
108 memset(ret, 0, size);
111 EXPORT_SYMBOL(snd_hidden_kzalloc);
113 void *snd_hidden_kcalloc(size_t n, size_t size, gfp_t flags)
116 if (n != 0 && size > INT_MAX / n)
118 return snd_hidden_kzalloc(n * size, flags);
121 void snd_hidden_kfree(const void *obj)
124 struct snd_alloc_track *t;
127 t = snd_alloc_track_entry(obj);
128 if (t->magic != KMALLOC_MAGIC) {
129 snd_printk(KERN_WARNING "bad kfree (called from %p)\n", __builtin_return_address(0));
132 spin_lock_irqsave(&snd_alloc_kmalloc_lock, flags);
134 spin_unlock_irqrestore(&snd_alloc_kmalloc_lock, flags);
136 snd_alloc_kmalloc -= t->size;
138 snd_wrapper_kfree(obj);
141 char *snd_hidden_kstrdup(const char *s, gfp_t flags)
149 buf = _snd_kmalloc(len, flags);
155 static void snd_memory_info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
157 snd_iprintf(buffer, "kmalloc: %li bytes\n", snd_alloc_kmalloc);
160 int __init snd_memory_info_init(void)
162 snd_info_entry_t *entry;
164 entry = snd_info_create_module_entry(THIS_MODULE, "meminfo", NULL);
166 entry->c.text.read_size = 256;
167 entry->c.text.read = snd_memory_info_read;
168 if (snd_info_register(entry) < 0) {
169 snd_info_free_entry(entry);
173 snd_memory_info_entry = entry;
177 int __exit snd_memory_info_done(void)
179 if (snd_memory_info_entry)
180 snd_info_unregister(snd_memory_info_entry);
184 #endif /* CONFIG_SND_DEBUG_MEMORY */
187 * copy_to_user_fromio - copy data from mmio-space to user-space
188 * @dst: the destination pointer on user-space
189 * @src: the source pointer on mmio
190 * @count: the data size to copy in bytes
192 * Copies the data from mmio-space to user-space.
194 * Returns zero if successful, or non-zero on failure.
196 int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size_t count)
198 #if defined(__i386__) || defined(CONFIG_SPARC32)
199 return copy_to_user(dst, (const void __force*)src, count) ? -EFAULT : 0;
206 memcpy_fromio(buf, (void __iomem *)src, c);
207 if (copy_to_user(dst, buf, c))
218 * copy_from_user_toio - copy data from user-space to mmio-space
219 * @dst: the destination pointer on mmio-space
220 * @src: the source pointer on user-space
221 * @count: the data size to copy in bytes
223 * Copies the data from user-space to mmio-space.
225 * Returns zero if successful, or non-zero on failure.
227 int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size_t count)
229 #if defined(__i386__) || defined(CONFIG_SPARC32)
230 return copy_from_user((void __force *)dst, src, count) ? -EFAULT : 0;
237 if (copy_from_user(buf, src, c))
239 memcpy_toio(dst, buf, c);