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 long snd_alloc_vmalloc
;
51 static LIST_HEAD(snd_alloc_kmalloc_list
);
52 static LIST_HEAD(snd_alloc_vmalloc_list
);
53 static DEFINE_SPINLOCK(snd_alloc_kmalloc_lock
);
54 static DEFINE_SPINLOCK(snd_alloc_vmalloc_lock
);
55 #define KMALLOC_MAGIC 0x87654321
56 #define VMALLOC_MAGIC 0x87654320
57 static snd_info_entry_t
*snd_memory_info_entry
;
59 void __init
snd_memory_init(void)
61 snd_alloc_kmalloc
= 0;
62 snd_alloc_vmalloc
= 0;
65 void snd_memory_done(void)
67 struct list_head
*head
;
68 struct snd_alloc_track
*t
;
70 if (snd_alloc_kmalloc
> 0)
71 snd_printk(KERN_ERR
"Not freed snd_alloc_kmalloc = %li\n", snd_alloc_kmalloc
);
72 if (snd_alloc_vmalloc
> 0)
73 snd_printk(KERN_ERR
"Not freed snd_alloc_vmalloc = %li\n", snd_alloc_vmalloc
);
74 list_for_each_prev(head
, &snd_alloc_kmalloc_list
) {
75 t
= list_entry(head
, struct snd_alloc_track
, list
);
76 if (t
->magic
!= KMALLOC_MAGIC
) {
77 snd_printk(KERN_ERR
"Corrupted kmalloc\n");
80 snd_printk(KERN_ERR
"kmalloc(%ld) from %p not freed\n", (long) t
->size
, t
->caller
);
82 list_for_each_prev(head
, &snd_alloc_vmalloc_list
) {
83 t
= list_entry(head
, struct snd_alloc_track
, list
);
84 if (t
->magic
!= VMALLOC_MAGIC
) {
85 snd_printk(KERN_ERR
"Corrupted vmalloc\n");
88 snd_printk(KERN_ERR
"vmalloc(%ld) from %p not freed\n", (long) t
->size
, t
->caller
);
92 static void *__snd_kmalloc(size_t size
, unsigned int __nocast flags
, void *caller
)
94 unsigned long cpu_flags
;
95 struct snd_alloc_track
*t
;
98 ptr
= snd_wrapper_kmalloc(size
+ sizeof(struct snd_alloc_track
), flags
);
100 t
= (struct snd_alloc_track
*)ptr
;
101 t
->magic
= KMALLOC_MAGIC
;
103 spin_lock_irqsave(&snd_alloc_kmalloc_lock
, cpu_flags
);
104 list_add_tail(&t
->list
, &snd_alloc_kmalloc_list
);
105 spin_unlock_irqrestore(&snd_alloc_kmalloc_lock
, cpu_flags
);
107 snd_alloc_kmalloc
+= size
;
113 #define _snd_kmalloc(size, flags) __snd_kmalloc((size), (flags), __builtin_return_address(0));
114 void *snd_hidden_kmalloc(size_t size
, unsigned int __nocast flags
)
116 return _snd_kmalloc(size
, flags
);
119 void *snd_hidden_kzalloc(size_t size
, unsigned int __nocast flags
)
121 void *ret
= _snd_kmalloc(size
, flags
);
123 memset(ret
, 0, size
);
126 EXPORT_SYMBOL(snd_hidden_kzalloc
);
128 void *snd_hidden_kcalloc(size_t n
, size_t size
, unsigned int __nocast flags
)
131 if (n
!= 0 && size
> INT_MAX
/ n
)
133 return snd_hidden_kzalloc(n
* size
, flags
);
136 void snd_hidden_kfree(const void *obj
)
139 struct snd_alloc_track
*t
;
142 t
= snd_alloc_track_entry(obj
);
143 if (t
->magic
!= KMALLOC_MAGIC
) {
144 snd_printk(KERN_WARNING
"bad kfree (called from %p)\n", __builtin_return_address(0));
147 spin_lock_irqsave(&snd_alloc_kmalloc_lock
, flags
);
149 spin_unlock_irqrestore(&snd_alloc_kmalloc_lock
, flags
);
151 snd_alloc_kmalloc
-= t
->size
;
153 snd_wrapper_kfree(obj
);
156 void *snd_hidden_vmalloc(unsigned long size
)
159 ptr
= snd_wrapper_vmalloc(size
+ sizeof(struct snd_alloc_track
));
161 struct snd_alloc_track
*t
= (struct snd_alloc_track
*)ptr
;
162 t
->magic
= VMALLOC_MAGIC
;
163 t
->caller
= __builtin_return_address(0);
164 spin_lock(&snd_alloc_vmalloc_lock
);
165 list_add_tail(&t
->list
, &snd_alloc_vmalloc_list
);
166 spin_unlock(&snd_alloc_vmalloc_lock
);
168 snd_alloc_vmalloc
+= size
;
174 void snd_hidden_vfree(void *obj
)
176 struct snd_alloc_track
*t
;
179 t
= snd_alloc_track_entry(obj
);
180 if (t
->magic
!= VMALLOC_MAGIC
) {
181 snd_printk(KERN_ERR
"bad vfree (called from %p)\n", __builtin_return_address(0));
184 spin_lock(&snd_alloc_vmalloc_lock
);
186 spin_unlock(&snd_alloc_vmalloc_lock
);
188 snd_alloc_vmalloc
-= t
->size
;
190 snd_wrapper_vfree(obj
);
193 char *snd_hidden_kstrdup(const char *s
, unsigned int __nocast flags
)
201 buf
= _snd_kmalloc(len
, flags
);
207 static void snd_memory_info_read(snd_info_entry_t
*entry
, snd_info_buffer_t
* buffer
)
209 snd_iprintf(buffer
, "kmalloc: %li bytes\n", snd_alloc_kmalloc
);
210 snd_iprintf(buffer
, "vmalloc: %li bytes\n", snd_alloc_vmalloc
);
213 int __init
snd_memory_info_init(void)
215 snd_info_entry_t
*entry
;
217 entry
= snd_info_create_module_entry(THIS_MODULE
, "meminfo", NULL
);
219 entry
->c
.text
.read_size
= 256;
220 entry
->c
.text
.read
= snd_memory_info_read
;
221 if (snd_info_register(entry
) < 0) {
222 snd_info_free_entry(entry
);
226 snd_memory_info_entry
= entry
;
230 int __exit
snd_memory_info_done(void)
232 if (snd_memory_info_entry
)
233 snd_info_unregister(snd_memory_info_entry
);
237 #endif /* CONFIG_SND_DEBUG_MEMORY */
240 * copy_to_user_fromio - copy data from mmio-space to user-space
241 * @dst: the destination pointer on user-space
242 * @src: the source pointer on mmio
243 * @count: the data size to copy in bytes
245 * Copies the data from mmio-space to user-space.
247 * Returns zero if successful, or non-zero on failure.
249 int copy_to_user_fromio(void __user
*dst
, const volatile void __iomem
*src
, size_t count
)
251 #if defined(__i386__) || defined(CONFIG_SPARC32)
252 return copy_to_user(dst
, (const void*)src
, count
) ? -EFAULT
: 0;
259 memcpy_fromio(buf
, (void __iomem
*)src
, c
);
260 if (copy_to_user(dst
, buf
, c
))
271 * copy_from_user_toio - copy data from user-space to mmio-space
272 * @dst: the destination pointer on mmio-space
273 * @src: the source pointer on user-space
274 * @count: the data size to copy in bytes
276 * Copies the data from user-space to mmio-space.
278 * Returns zero if successful, or non-zero on failure.
280 int copy_from_user_toio(volatile void __iomem
*dst
, const void __user
*src
, size_t count
)
282 #if defined(__i386__) || defined(CONFIG_SPARC32)
283 return copy_from_user((void*)dst
, src
, count
) ? -EFAULT
: 0;
290 if (copy_from_user(buf
, src
, c
))
292 memcpy_toio(dst
, buf
, c
);