aboutsummaryrefslogtreecommitdiffstats
path: root/sound/core/memalloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound/core/memalloc.c')
-rw-r--r--sound/core/memalloc.c52
1 files changed, 20 insertions, 32 deletions
diff --git a/sound/core/memalloc.c b/sound/core/memalloc.c
index 3733351a27f2..1b3534d67686 100644
--- a/sound/core/memalloc.c
+++ b/sound/core/memalloc.c
@@ -43,14 +43,6 @@ MODULE_LICENSE("GPL");
43/* 43/*
44 */ 44 */
45 45
46void *snd_malloc_sgbuf_pages(struct device *device,
47 size_t size, struct snd_dma_buffer *dmab,
48 size_t *res_size);
49int snd_free_sgbuf_pages(struct snd_dma_buffer *dmab);
50
51/*
52 */
53
54static DEFINE_MUTEX(list_mutex); 46static DEFINE_MUTEX(list_mutex);
55static LIST_HEAD(mem_list_head); 47static LIST_HEAD(mem_list_head);
56 48
@@ -64,18 +56,6 @@ struct snd_mem_list {
64/* id for pre-allocated buffers */ 56/* id for pre-allocated buffers */
65#define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1 57#define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
66 58
67#ifdef CONFIG_SND_DEBUG
68#define __ASTRING__(x) #x
69#define snd_assert(expr, args...) do {\
70 if (!(expr)) {\
71 printk(KERN_ERR "snd-malloc: BUG? (%s) (called from %p)\n", __ASTRING__(expr), __builtin_return_address(0));\
72 args;\
73 }\
74} while (0)
75#else
76#define snd_assert(expr, args...) /**/
77#endif
78
79/* 59/*
80 * 60 *
81 * Generic memory allocators 61 * Generic memory allocators
@@ -108,8 +88,10 @@ void *snd_malloc_pages(size_t size, gfp_t gfp_flags)
108 int pg; 88 int pg;
109 void *res; 89 void *res;
110 90
111 snd_assert(size > 0, return NULL); 91 if (WARN_ON(!size))
112 snd_assert(gfp_flags != 0, return NULL); 92 return NULL;
93 if (WARN_ON(!gfp_flags))
94 return NULL;
113 gfp_flags |= __GFP_COMP; /* compound page lets parts be mapped */ 95 gfp_flags |= __GFP_COMP; /* compound page lets parts be mapped */
114 pg = get_order(size); 96 pg = get_order(size);
115 if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL) 97 if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL)
@@ -149,8 +131,8 @@ static void *snd_malloc_dev_pages(struct device *dev, size_t size, dma_addr_t *d
149 void *res; 131 void *res;
150 gfp_t gfp_flags; 132 gfp_t gfp_flags;
151 133
152 snd_assert(size > 0, return NULL); 134 if (WARN_ON(!dma))
153 snd_assert(dma != NULL, return NULL); 135 return NULL;
154 pg = get_order(size); 136 pg = get_order(size);
155 gfp_flags = GFP_KERNEL 137 gfp_flags = GFP_KERNEL
156 | __GFP_COMP /* compound page lets parts be mapped */ 138 | __GFP_COMP /* compound page lets parts be mapped */
@@ -200,8 +182,10 @@ static void snd_free_dev_pages(struct device *dev, size_t size, void *ptr,
200int snd_dma_alloc_pages(int type, struct device *device, size_t size, 182int snd_dma_alloc_pages(int type, struct device *device, size_t size,
201 struct snd_dma_buffer *dmab) 183 struct snd_dma_buffer *dmab)
202{ 184{
203 snd_assert(size > 0, return -ENXIO); 185 if (WARN_ON(!size))
204 snd_assert(dmab != NULL, return -ENXIO); 186 return -ENXIO;
187 if (WARN_ON(!dmab))
188 return -ENXIO;
205 189
206 dmab->dev.type = type; 190 dmab->dev.type = type;
207 dmab->dev.dev = device; 191 dmab->dev.dev = device;
@@ -251,15 +235,17 @@ int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
251{ 235{
252 int err; 236 int err;
253 237
254 snd_assert(size > 0, return -ENXIO);
255 snd_assert(dmab != NULL, return -ENXIO);
256
257 while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) { 238 while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
239 size_t aligned_size;
258 if (err != -ENOMEM) 240 if (err != -ENOMEM)
259 return err; 241 return err;
260 size >>= 1;
261 if (size <= PAGE_SIZE) 242 if (size <= PAGE_SIZE)
262 return -ENOMEM; 243 return -ENOMEM;
244 aligned_size = PAGE_SIZE << get_order(size);
245 if (size != aligned_size)
246 size = aligned_size;
247 else
248 size >>= 1;
263 } 249 }
264 if (! dmab->area) 250 if (! dmab->area)
265 return -ENOMEM; 251 return -ENOMEM;
@@ -307,7 +293,8 @@ size_t snd_dma_get_reserved_buf(struct snd_dma_buffer *dmab, unsigned int id)
307{ 293{
308 struct snd_mem_list *mem; 294 struct snd_mem_list *mem;
309 295
310 snd_assert(dmab, return 0); 296 if (WARN_ON(!dmab))
297 return 0;
311 298
312 mutex_lock(&list_mutex); 299 mutex_lock(&list_mutex);
313 list_for_each_entry(mem, &mem_list_head, list) { 300 list_for_each_entry(mem, &mem_list_head, list) {
@@ -341,7 +328,8 @@ int snd_dma_reserve_buf(struct snd_dma_buffer *dmab, unsigned int id)
341{ 328{
342 struct snd_mem_list *mem; 329 struct snd_mem_list *mem;
343 330
344 snd_assert(dmab, return -EINVAL); 331 if (WARN_ON(!dmab))
332 return -EINVAL;
345 mem = kmalloc(sizeof(*mem), GFP_KERNEL); 333 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
346 if (! mem) 334 if (! mem)
347 return -ENOMEM; 335 return -ENOMEM;