patch-2_6_7-vs1_9_1_12
[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         for (head = snd_alloc_kmalloc_list.prev;
75              head != &snd_alloc_kmalloc_list; head = head->prev) {
76                 t = list_entry(head, struct snd_alloc_track, list);
77                 if (t->magic != KMALLOC_MAGIC) {
78                         snd_printk(KERN_ERR "Corrupted kmalloc\n");
79                         break;
80                 }
81                 snd_printk(KERN_ERR "kmalloc(%ld) from %p not freed\n", (long) t->size, t->caller);
82         }
83         for (head = snd_alloc_vmalloc_list.prev;
84              head != &snd_alloc_vmalloc_list; head = head->prev) {
85                 t = list_entry(head, struct snd_alloc_track, list);
86                 if (t->magic != VMALLOC_MAGIC) {
87                         snd_printk(KERN_ERR "Corrupted vmalloc\n");
88                         break;
89                 }
90                 snd_printk(KERN_ERR "vmalloc(%ld) from %p not freed\n", (long) t->size, t->caller);
91         }
92 }
93
94 void *__snd_kmalloc(size_t size, int flags, void *caller)
95 {
96         unsigned long cpu_flags;
97         struct snd_alloc_track *t;
98         void *ptr;
99         
100         ptr = snd_wrapper_kmalloc(size + sizeof(struct snd_alloc_track), flags);
101         if (ptr != NULL) {
102                 t = (struct snd_alloc_track *)ptr;
103                 t->magic = KMALLOC_MAGIC;
104                 t->caller = caller;
105                 spin_lock_irqsave(&snd_alloc_kmalloc_lock, cpu_flags);
106                 list_add_tail(&t->list, &snd_alloc_kmalloc_list);
107                 spin_unlock_irqrestore(&snd_alloc_kmalloc_lock, cpu_flags);
108                 t->size = size;
109                 snd_alloc_kmalloc += size;
110                 ptr = t->data;
111         }
112         return ptr;
113 }
114
115 #define _snd_kmalloc(size, flags) __snd_kmalloc((size), (flags), __builtin_return_address(0));
116 void *snd_hidden_kmalloc(size_t size, int flags)
117 {
118         return _snd_kmalloc(size, flags);
119 }
120
121 void snd_hidden_kfree(const void *obj)
122 {
123         unsigned long flags;
124         struct snd_alloc_track *t;
125         if (obj == NULL) {
126                 snd_printk(KERN_WARNING "null kfree (called from %p)\n", __builtin_return_address(0));
127                 return;
128         }
129         t = snd_alloc_track_entry(obj);
130         if (t->magic != KMALLOC_MAGIC) {
131                 snd_printk(KERN_WARNING "bad kfree (called from %p)\n", __builtin_return_address(0));
132                 return;
133         }
134         spin_lock_irqsave(&snd_alloc_kmalloc_lock, flags);
135         list_del(&t->list);
136         spin_unlock_irqrestore(&snd_alloc_kmalloc_lock, flags);
137         t->magic = 0;
138         snd_alloc_kmalloc -= t->size;
139         obj = t;
140         snd_wrapper_kfree(obj);
141 }
142
143 void *_snd_magic_kcalloc(unsigned long magic, size_t size, int flags)
144 {
145         unsigned long *ptr;
146         ptr = _snd_kmalloc(size + sizeof(unsigned long), flags);
147         if (ptr) {
148                 *ptr++ = magic;
149                 memset(ptr, 0, size);
150         }
151         return ptr;
152 }
153
154 void *_snd_magic_kmalloc(unsigned long magic, size_t size, int flags)
155 {
156         unsigned long *ptr;
157         ptr = _snd_kmalloc(size + sizeof(unsigned long), flags);
158         if (ptr)
159                 *ptr++ = magic;
160         return ptr;
161 }
162
163 void snd_magic_kfree(void *_ptr)
164 {
165         unsigned long *ptr = _ptr;
166         if (ptr == NULL) {
167                 snd_printk(KERN_WARNING "null snd_magic_kfree (called from %p)\n", __builtin_return_address(0));
168                 return;
169         }
170         *--ptr = 0;
171         {
172                 struct snd_alloc_track *t;
173                 t = snd_alloc_track_entry(ptr);
174                 if (t->magic != KMALLOC_MAGIC) {
175                         snd_printk(KERN_ERR "bad snd_magic_kfree (called from %p)\n", __builtin_return_address(0));
176                         return;
177                 }
178         }
179         snd_hidden_kfree(ptr);
180         return;
181 }
182
183 void *snd_hidden_vmalloc(unsigned long size)
184 {
185         void *ptr;
186         ptr = snd_wrapper_vmalloc(size + sizeof(struct snd_alloc_track));
187         if (ptr) {
188                 struct snd_alloc_track *t = (struct snd_alloc_track *)ptr;
189                 t->magic = VMALLOC_MAGIC;
190                 t->caller = __builtin_return_address(0);
191                 spin_lock(&snd_alloc_vmalloc_lock);
192                 list_add_tail(&t->list, &snd_alloc_vmalloc_list);
193                 spin_unlock(&snd_alloc_vmalloc_lock);
194                 t->size = size;
195                 snd_alloc_vmalloc += size;
196                 ptr = t->data;
197         }
198         return ptr;
199 }
200
201 void snd_hidden_vfree(void *obj)
202 {
203         struct snd_alloc_track *t;
204         if (obj == NULL) {
205                 snd_printk(KERN_WARNING "null vfree (called from %p)\n", __builtin_return_address(0));
206                 return;
207         }
208         t = snd_alloc_track_entry(obj);
209         if (t->magic != VMALLOC_MAGIC) {
210                 snd_printk(KERN_ERR "bad vfree (called from %p)\n", __builtin_return_address(0));
211                 return;
212         }
213         spin_lock(&snd_alloc_vmalloc_lock);
214         list_del(&t->list);
215         spin_unlock(&snd_alloc_vmalloc_lock);
216         t->magic = 0;
217         snd_alloc_vmalloc -= t->size;
218         obj = t;
219         snd_wrapper_vfree(obj);
220 }
221
222 static void snd_memory_info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
223 {
224         snd_iprintf(buffer, "kmalloc: %li bytes\n", snd_alloc_kmalloc);
225         snd_iprintf(buffer, "vmalloc: %li bytes\n", snd_alloc_vmalloc);
226 }
227
228 int __init snd_memory_info_init(void)
229 {
230         snd_info_entry_t *entry;
231
232         entry = snd_info_create_module_entry(THIS_MODULE, "meminfo", NULL);
233         if (entry) {
234                 entry->c.text.read_size = 256;
235                 entry->c.text.read = snd_memory_info_read;
236                 if (snd_info_register(entry) < 0) {
237                         snd_info_free_entry(entry);
238                         entry = NULL;
239                 }
240         }
241         snd_memory_info_entry = entry;
242         return 0;
243 }
244
245 int __exit snd_memory_info_done(void)
246 {
247         if (snd_memory_info_entry)
248                 snd_info_unregister(snd_memory_info_entry);
249         return 0;
250 }
251
252 #else
253
254 #define _snd_kmalloc kmalloc
255
256 #endif /* CONFIG_SND_DEBUG_MEMORY */
257
258 /**
259  * snd_kcalloc - memory allocation and zero-clear
260  * @size: the size to allocate in bytes
261  * @flags: allocation conditions, GFP_XXX
262  *
263  * Allocates a memory chunk via kmalloc() and initializes it to zero.
264  *
265  * Returns the pointer, or NULL if no enoguh memory.
266  */
267 void *snd_kcalloc(size_t size, int flags)
268 {
269         void *ptr;
270         
271         ptr = _snd_kmalloc(size, flags);
272         if (ptr)
273                 memset(ptr, 0, size);
274         return ptr;
275 }
276
277 /**
278  * snd_kmalloc_strdup - copy the string
279  * @string: the original string
280  * @flags: allocation conditions, GFP_XXX
281  *
282  * Allocates a memory chunk via kmalloc() and copies the string to it.
283  *
284  * Returns the pointer, or NULL if no enoguh memory.
285  */
286 char *snd_kmalloc_strdup(const char *string, int flags)
287 {
288         size_t len;
289         char *ptr;
290
291         if (!string)
292                 return NULL;
293         len = strlen(string) + 1;
294         ptr = _snd_kmalloc(len, flags);
295         if (ptr)
296                 memcpy(ptr, string, len);
297         return ptr;
298 }
299
300 /**
301  * copy_to_user_fromio - copy data from mmio-space to user-space
302  * @dst: the destination pointer on user-space
303  * @src: the source pointer on mmio
304  * @count: the data size to copy in bytes
305  *
306  * Copies the data from mmio-space to user-space.
307  *
308  * Returns zero if successful, or non-zero on failure.
309  */
310 int copy_to_user_fromio(void __user *dst, unsigned long src, size_t count)
311 {
312 #if defined(__i386__) || defined(CONFIG_SPARC32)
313         return copy_to_user(dst, (const void*)src, count) ? -EFAULT : 0;
314 #else
315         char buf[256];
316         while (count) {
317                 size_t c = count;
318                 if (c > sizeof(buf))
319                         c = sizeof(buf);
320                 memcpy_fromio(buf, (void*)src, c);
321                 if (copy_to_user(dst, buf, c))
322                         return -EFAULT;
323                 count -= c;
324                 dst += c;
325                 src += c;
326         }
327         return 0;
328 #endif
329 }
330
331 /**
332  * copy_from_user_toio - copy data from user-space to mmio-space
333  * @dst: the destination pointer on mmio-space
334  * @src: the source pointer on user-space
335  * @count: the data size to copy in bytes
336  *
337  * Copies the data from user-space to mmio-space.
338  *
339  * Returns zero if successful, or non-zero on failure.
340  */
341 int copy_from_user_toio(unsigned long dst, const void __user *src, size_t count)
342 {
343 #if defined(__i386__) || defined(CONFIG_SPARC32)
344         return copy_from_user((void*)dst, src, count) ? -EFAULT : 0;
345 #else
346         char buf[256];
347         while (count) {
348                 size_t c = count;
349                 if (c > sizeof(buf))
350                         c = sizeof(buf);
351                 if (copy_from_user(buf, src, c))
352                         return -EFAULT;
353                 memcpy_toio((void*)dst, buf, c);
354                 count -= c;
355                 dst += c;
356                 src += c;
357         }
358         return 0;
359 #endif
360 }