vserver 1.9.3
[linux-2.6.git] / sound / core / memory.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3  * 
4  *  Memory allocation helpers.
5  *
6  *
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.
11  *
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.
16  *
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
20  *
21  */
22
23 #include <sound/driver.h>
24 #include <asm/io.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>
32
33 /*
34  *  memory allocation helpers and debug routines
35  */
36
37 #ifdef CONFIG_SND_DEBUG_MEMORY
38
39 struct snd_alloc_track {
40         unsigned long magic;
41         void *caller;
42         size_t size;
43         struct list_head list;
44         long data[0];
45 };
46
47 #define snd_alloc_track_entry(obj) (struct snd_alloc_track *)((char*)obj - (unsigned long)((struct snd_alloc_track *)0)->data)
48
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 spinlock_t snd_alloc_kmalloc_lock = SPIN_LOCK_UNLOCKED;
54 static spinlock_t snd_alloc_vmalloc_lock = SPIN_LOCK_UNLOCKED;
55 #define KMALLOC_MAGIC 0x87654321
56 #define VMALLOC_MAGIC 0x87654320
57 static snd_info_entry_t *snd_memory_info_entry;
58
59 void snd_memory_init(void)
60 {
61         snd_alloc_kmalloc = 0;
62         snd_alloc_vmalloc = 0;
63 }
64
65 void snd_memory_done(void)
66 {
67         struct list_head *head;
68         struct snd_alloc_track *t;
69
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");
78                         break;
79                 }
80                 snd_printk(KERN_ERR "kmalloc(%ld) from %p not freed\n", (long) t->size, t->caller);
81         }
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");
86                         break;
87                 }
88                 snd_printk(KERN_ERR "vmalloc(%ld) from %p not freed\n", (long) t->size, t->caller);
89         }
90 }
91
92 void *__snd_kmalloc(size_t size, int flags, void *caller)
93 {
94         unsigned long cpu_flags;
95         struct snd_alloc_track *t;
96         void *ptr;
97         
98         ptr = snd_wrapper_kmalloc(size + sizeof(struct snd_alloc_track), flags);
99         if (ptr != NULL) {
100                 t = (struct snd_alloc_track *)ptr;
101                 t->magic = KMALLOC_MAGIC;
102                 t->caller = caller;
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);
106                 t->size = size;
107                 snd_alloc_kmalloc += size;
108                 ptr = t->data;
109         }
110         return ptr;
111 }
112
113 #define _snd_kmalloc(size, flags) __snd_kmalloc((size), (flags), __builtin_return_address(0));
114 void *snd_hidden_kmalloc(size_t size, int flags)
115 {
116         return _snd_kmalloc(size, flags);
117 }
118
119 void *snd_hidden_kcalloc(size_t n, size_t size, int flags)
120 {
121         void *ret = NULL;
122         if (n != 0 && size > INT_MAX / n)
123                 return ret;
124         ret = _snd_kmalloc(n * size, flags);
125         if (ret)
126                 memset(ret, 0, n * size);
127         return ret;
128 }
129
130 void snd_hidden_kfree(const void *obj)
131 {
132         unsigned long flags;
133         struct snd_alloc_track *t;
134         if (obj == NULL) {
135                 snd_printk(KERN_WARNING "null kfree (called from %p)\n", __builtin_return_address(0));
136                 return;
137         }
138         t = snd_alloc_track_entry(obj);
139         if (t->magic != KMALLOC_MAGIC) {
140                 snd_printk(KERN_WARNING "bad kfree (called from %p)\n", __builtin_return_address(0));
141                 return;
142         }
143         spin_lock_irqsave(&snd_alloc_kmalloc_lock, flags);
144         list_del(&t->list);
145         spin_unlock_irqrestore(&snd_alloc_kmalloc_lock, flags);
146         t->magic = 0;
147         snd_alloc_kmalloc -= t->size;
148         obj = t;
149         snd_wrapper_kfree(obj);
150 }
151
152 void *snd_hidden_vmalloc(unsigned long size)
153 {
154         void *ptr;
155         ptr = snd_wrapper_vmalloc(size + sizeof(struct snd_alloc_track));
156         if (ptr) {
157                 struct snd_alloc_track *t = (struct snd_alloc_track *)ptr;
158                 t->magic = VMALLOC_MAGIC;
159                 t->caller = __builtin_return_address(0);
160                 spin_lock(&snd_alloc_vmalloc_lock);
161                 list_add_tail(&t->list, &snd_alloc_vmalloc_list);
162                 spin_unlock(&snd_alloc_vmalloc_lock);
163                 t->size = size;
164                 snd_alloc_vmalloc += size;
165                 ptr = t->data;
166         }
167         return ptr;
168 }
169
170 void snd_hidden_vfree(void *obj)
171 {
172         struct snd_alloc_track *t;
173         if (obj == NULL) {
174                 snd_printk(KERN_WARNING "null vfree (called from %p)\n", __builtin_return_address(0));
175                 return;
176         }
177         t = snd_alloc_track_entry(obj);
178         if (t->magic != VMALLOC_MAGIC) {
179                 snd_printk(KERN_ERR "bad vfree (called from %p)\n", __builtin_return_address(0));
180                 return;
181         }
182         spin_lock(&snd_alloc_vmalloc_lock);
183         list_del(&t->list);
184         spin_unlock(&snd_alloc_vmalloc_lock);
185         t->magic = 0;
186         snd_alloc_vmalloc -= t->size;
187         obj = t;
188         snd_wrapper_vfree(obj);
189 }
190
191 static void snd_memory_info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
192 {
193         snd_iprintf(buffer, "kmalloc: %li bytes\n", snd_alloc_kmalloc);
194         snd_iprintf(buffer, "vmalloc: %li bytes\n", snd_alloc_vmalloc);
195 }
196
197 int __init snd_memory_info_init(void)
198 {
199         snd_info_entry_t *entry;
200
201         entry = snd_info_create_module_entry(THIS_MODULE, "meminfo", NULL);
202         if (entry) {
203                 entry->c.text.read_size = 256;
204                 entry->c.text.read = snd_memory_info_read;
205                 if (snd_info_register(entry) < 0) {
206                         snd_info_free_entry(entry);
207                         entry = NULL;
208                 }
209         }
210         snd_memory_info_entry = entry;
211         return 0;
212 }
213
214 int __exit snd_memory_info_done(void)
215 {
216         if (snd_memory_info_entry)
217                 snd_info_unregister(snd_memory_info_entry);
218         return 0;
219 }
220
221 #else
222
223 #define _snd_kmalloc kmalloc
224
225 #endif /* CONFIG_SND_DEBUG_MEMORY */
226
227 /**
228  * snd_kmalloc_strdup - copy the string
229  * @string: the original string
230  * @flags: allocation conditions, GFP_XXX
231  *
232  * Allocates a memory chunk via kmalloc() and copies the string to it.
233  *
234  * Returns the pointer, or NULL if no enoguh memory.
235  */
236 char *snd_kmalloc_strdup(const char *string, int flags)
237 {
238         size_t len;
239         char *ptr;
240
241         if (!string)
242                 return NULL;
243         len = strlen(string) + 1;
244         ptr = _snd_kmalloc(len, flags);
245         if (ptr)
246                 memcpy(ptr, string, len);
247         return ptr;
248 }
249
250 /**
251  * copy_to_user_fromio - copy data from mmio-space to user-space
252  * @dst: the destination pointer on user-space
253  * @src: the source pointer on mmio
254  * @count: the data size to copy in bytes
255  *
256  * Copies the data from mmio-space to user-space.
257  *
258  * Returns zero if successful, or non-zero on failure.
259  */
260 int copy_to_user_fromio(void __user *dst, unsigned long src, size_t count)
261 {
262 #if defined(__i386__) || defined(CONFIG_SPARC32)
263         return copy_to_user(dst, (const void*)src, count) ? -EFAULT : 0;
264 #else
265         char buf[256];
266         while (count) {
267                 size_t c = count;
268                 if (c > sizeof(buf))
269                         c = sizeof(buf);
270                 memcpy_fromio(buf, (void*)src, c);
271                 if (copy_to_user(dst, buf, c))
272                         return -EFAULT;
273                 count -= c;
274                 dst += c;
275                 src += c;
276         }
277         return 0;
278 #endif
279 }
280
281 /**
282  * copy_from_user_toio - copy data from user-space to mmio-space
283  * @dst: the destination pointer on mmio-space
284  * @src: the source pointer on user-space
285  * @count: the data size to copy in bytes
286  *
287  * Copies the data from user-space to mmio-space.
288  *
289  * Returns zero if successful, or non-zero on failure.
290  */
291 int copy_from_user_toio(unsigned long dst, const void __user *src, size_t count)
292 {
293 #if defined(__i386__) || defined(CONFIG_SPARC32)
294         return copy_from_user((void*)dst, src, count) ? -EFAULT : 0;
295 #else
296         char buf[256];
297         while (count) {
298                 size_t c = count;
299                 if (c > sizeof(buf))
300                         c = sizeof(buf);
301                 if (copy_from_user(buf, src, c))
302                         return -EFAULT;
303                 memcpy_toio((void*)dst, buf, c);
304                 count -= c;
305                 dst += c;
306                 src += c;
307         }
308         return 0;
309 #endif
310 }