Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * generic arrays
0004  */
0005 
0006 #include <linux/slab.h>
0007 #include <sound/core.h>
0008 #include <sound/hdaudio.h>
0009 
0010 /**
0011  * snd_array_new - get a new element from the given array
0012  * @array: the array object
0013  *
0014  * Get a new element from the given array.  If it exceeds the
0015  * pre-allocated array size, re-allocate the array.
0016  *
0017  * Returns NULL if allocation failed.
0018  */
0019 void *snd_array_new(struct snd_array *array)
0020 {
0021     if (snd_BUG_ON(!array->elem_size))
0022         return NULL;
0023     if (array->used >= array->alloced) {
0024         int num = array->alloced + array->alloc_align;
0025         int oldsize = array->alloced * array->elem_size;
0026         int size = (num + 1) * array->elem_size;
0027         void *nlist;
0028         if (snd_BUG_ON(num >= 4096))
0029             return NULL;
0030         nlist = krealloc(array->list, size, GFP_KERNEL);
0031         if (!nlist)
0032             return NULL;
0033         memset(nlist + oldsize, 0, size - oldsize);
0034         array->list = nlist;
0035         array->alloced = num;
0036     }
0037     return snd_array_elem(array, array->used++);
0038 }
0039 EXPORT_SYMBOL_GPL(snd_array_new);
0040 
0041 /**
0042  * snd_array_free - free the given array elements
0043  * @array: the array object
0044  */
0045 void snd_array_free(struct snd_array *array)
0046 {
0047     kfree(array->list);
0048     array->used = 0;
0049     array->alloced = 0;
0050     array->list = NULL;
0051 }
0052 EXPORT_SYMBOL_GPL(snd_array_free);