Skip to content

Commit 71fc4c7

Browse files
committed
ALSA: hda - Move generic array helpers to core lib
This will be used by the regmap support. Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent e311782 commit 71fc4c7

5 files changed

Lines changed: 84 additions & 77 deletions

File tree

‎include/sound/hdaudio.h‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ struct hdac_widget_tree;
2121
*/
2222
extern struct bus_type snd_hda_bus_type;
2323

24+
/*
25+
* generic arrays
26+
*/
27+
struct snd_array {
28+
unsigned int used;
29+
unsigned int alloced;
30+
unsigned int elem_size;
31+
unsigned int alloc_align;
32+
void *list;
33+
};
34+
2435
/*
2536
* HD-audio codec base device
2637
*/
@@ -178,4 +189,26 @@ static inline void snd_hdac_codec_link_down(struct hdac_device *codec)
178189
clear_bit(codec->addr, &codec->bus->codec_powered);
179190
}
180191

192+
/*
193+
* generic array helpers
194+
*/
195+
void *snd_array_new(struct snd_array *array);
196+
void snd_array_free(struct snd_array *array);
197+
static inline void snd_array_init(struct snd_array *array, unsigned int size,
198+
unsigned int align)
199+
{
200+
array->elem_size = size;
201+
array->alloc_align = align;
202+
}
203+
204+
static inline void *snd_array_elem(struct snd_array *array, unsigned int idx)
205+
{
206+
return array->list + idx * array->elem_size;
207+
}
208+
209+
static inline unsigned int snd_array_index(struct snd_array *array, void *ptr)
210+
{
211+
return (unsigned long)(ptr - array->list) / array->elem_size;
212+
}
213+
181214
#endif /* __SOUND_HDAUDIO_H */

‎sound/hda/Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
snd-hda-core-objs := hda_bus_type.o hdac_bus.o hdac_device.o hdac_sysfs.o
1+
snd-hda-core-objs := hda_bus_type.o hdac_bus.o hdac_device.o hdac_sysfs.o \
2+
array.o
23

34
snd-hda-core-objs += trace.o
45
CFLAGS_trace.o := -I$(src)

‎sound/hda/array.c‎

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

‎sound/pci/hda/hda_codec.c‎

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5012,52 +5012,6 @@ void snd_hda_bus_reset(struct hda_bus *bus)
50125012
}
50135013
EXPORT_SYMBOL_GPL(snd_hda_bus_reset);
50145014

5015-
/*
5016-
* generic arrays
5017-
*/
5018-
5019-
/**
5020-
* snd_array_new - get a new element from the given array
5021-
* @array: the array object
5022-
*
5023-
* Get a new element from the given array. If it exceeds the
5024-
* pre-allocated array size, re-allocate the array.
5025-
*
5026-
* Returns NULL if allocation failed.
5027-
*/
5028-
void *snd_array_new(struct snd_array *array)
5029-
{
5030-
if (snd_BUG_ON(!array->elem_size))
5031-
return NULL;
5032-
if (array->used >= array->alloced) {
5033-
int num = array->alloced + array->alloc_align;
5034-
int size = (num + 1) * array->elem_size;
5035-
void *nlist;
5036-
if (snd_BUG_ON(num >= 4096))
5037-
return NULL;
5038-
nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
5039-
if (!nlist)
5040-
return NULL;
5041-
array->list = nlist;
5042-
array->alloced = num;
5043-
}
5044-
return snd_array_elem(array, array->used++);
5045-
}
5046-
EXPORT_SYMBOL_GPL(snd_array_new);
5047-
5048-
/**
5049-
* snd_array_free - free the given array elements
5050-
* @array: the array object
5051-
*/
5052-
void snd_array_free(struct snd_array *array)
5053-
{
5054-
kfree(array->list);
5055-
array->used = 0;
5056-
array->alloced = 0;
5057-
array->list = NULL;
5058-
}
5059-
EXPORT_SYMBOL_GPL(snd_array_free);
5060-
50615015
/**
50625016
* snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
50635017
* @pcm: PCM caps bits

‎sound/pci/hda/hda_codec.h‎

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,6 @@
2929
#include <sound/hdaudio.h>
3030
#include <sound/hda_verbs.h>
3131

32-
/*
33-
* generic arrays
34-
*/
35-
struct snd_array {
36-
unsigned int used;
37-
unsigned int alloced;
38-
unsigned int elem_size;
39-
unsigned int alloc_align;
40-
void *list;
41-
};
42-
43-
void *snd_array_new(struct snd_array *array);
44-
void snd_array_free(struct snd_array *array);
45-
static inline void snd_array_init(struct snd_array *array, unsigned int size,
46-
unsigned int align)
47-
{
48-
array->elem_size = size;
49-
array->alloc_align = align;
50-
}
51-
52-
static inline void *snd_array_elem(struct snd_array *array, unsigned int idx)
53-
{
54-
return array->list + idx * array->elem_size;
55-
}
56-
57-
static inline unsigned int snd_array_index(struct snd_array *array, void *ptr)
58-
{
59-
return (unsigned long)(ptr - array->list) / array->elem_size;
60-
}
61-
6232
/*
6333
* Structures
6434
*/

0 commit comments

Comments
 (0)