array.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * generic arrays
  3. */
  4. #include <linux/slab.h>
  5. #include <sound/core.h>
  6. #include <sound/hdaudio.h>
  7. /**
  8. * snd_array_new - get a new element from the given array
  9. * @array: the array object
  10. *
  11. * Get a new element from the given array. If it exceeds the
  12. * pre-allocated array size, re-allocate the array.
  13. *
  14. * Returns NULL if allocation failed.
  15. */
  16. void *snd_array_new(struct snd_array *array)
  17. {
  18. if (snd_BUG_ON(!array->elem_size))
  19. return NULL;
  20. if (array->used >= array->alloced) {
  21. int num = array->alloced + array->alloc_align;
  22. int size = (num + 1) * array->elem_size;
  23. void *nlist;
  24. if (snd_BUG_ON(num >= 4096))
  25. return NULL;
  26. nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
  27. if (!nlist)
  28. return NULL;
  29. array->list = nlist;
  30. array->alloced = num;
  31. }
  32. return snd_array_elem(array, array->used++);
  33. }
  34. EXPORT_SYMBOL_GPL(snd_array_new);
  35. /**
  36. * snd_array_free - free the given array elements
  37. * @array: the array object
  38. */
  39. void snd_array_free(struct snd_array *array)
  40. {
  41. kfree(array->list);
  42. array->used = 0;
  43. array->alloced = 0;
  44. array->list = NULL;
  45. }
  46. EXPORT_SYMBOL_GPL(snd_array_free);