Skip to content

Commit deb6596

Browse files
committed
ALSA: Refactor slot assignment code
There are two loops that are almost identical but only with different checks. Refactor them with a simple helper, and give a bit more comments what's doing there. Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent 4c3773e commit deb6596

1 file changed

Lines changed: 30 additions & 27 deletions

File tree

‎sound/core/init.c‎

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,31 @@ static inline int init_info_for_card(struct snd_card *card)
131131
#define init_info_for_card(card)
132132
#endif
133133

134+
static int check_empty_slot(struct module *module, int slot)
135+
{
136+
return !slots[slot] || !*slots[slot];
137+
}
138+
139+
/* return an empty slot number (>= 0) found in the given bitmask @mask.
140+
* @mask == -1 == 0xffffffff means: take any free slot up to 32
141+
* when no slot is available, return the original @mask as is.
142+
*/
143+
static int get_slot_from_bitmask(int mask, int (*check)(struct module *, int),
144+
struct module *module)
145+
{
146+
int slot;
147+
148+
for (slot = 0; slot < SNDRV_CARDS; slot++) {
149+
if (slot < 32 && !(mask & (1U << slot)))
150+
continue;
151+
if (!test_bit(slot, snd_cards_lock)) {
152+
if (check(module, slot))
153+
return slot; /* found */
154+
}
155+
}
156+
return mask; /* unchanged */
157+
}
158+
134159
/**
135160
* snd_card_create - create and initialize a soundcard structure
136161
* @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
@@ -152,7 +177,7 @@ int snd_card_create(int idx, const char *xid,
152177
struct snd_card **card_ret)
153178
{
154179
struct snd_card *card;
155-
int err, idx2;
180+
int err;
156181

157182
if (snd_BUG_ON(!card_ret))
158183
return -EINVAL;
@@ -167,32 +192,10 @@ int snd_card_create(int idx, const char *xid,
167192
strlcpy(card->id, xid, sizeof(card->id));
168193
err = 0;
169194
mutex_lock(&snd_card_mutex);
170-
if (idx < 0) {
171-
for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++) {
172-
/* idx == -1 == 0xffff means: take any free slot */
173-
if (idx2 < 32 && !(idx & (1U << idx2)))
174-
continue;
175-
if (!test_bit(idx2, snd_cards_lock)) {
176-
if (module_slot_match(module, idx2)) {
177-
idx = idx2;
178-
break;
179-
}
180-
}
181-
}
182-
}
183-
if (idx < 0) {
184-
for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++) {
185-
/* idx == -1 == 0xffff means: take any free slot */
186-
if (idx2 < 32 && !(idx & (1U << idx2)))
187-
continue;
188-
if (!test_bit(idx2, snd_cards_lock)) {
189-
if (!slots[idx2] || !*slots[idx2]) {
190-
idx = idx2;
191-
break;
192-
}
193-
}
194-
}
195-
}
195+
if (idx < 0) /* first check the matching module-name slot */
196+
idx = get_slot_from_bitmask(idx, module_slot_match, module);
197+
if (idx < 0) /* if not matched, assign an empty slot */
198+
idx = get_slot_from_bitmask(idx, check_empty_slot, module);
196199
if (idx < 0)
197200
err = -ENODEV;
198201
else if (idx < snd_ecards_limit) {

0 commit comments

Comments
 (0)