From 7a31847f7d116857bbd377898a1389bd05ca4291 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Mon, 8 Sep 2014 15:07:47 -0700 Subject: Use a vector instead of a manual dynamic array --- Alc/backends/qsa.c | 240 ++++++++++++++++++++--------------------------------- 1 file changed, 90 insertions(+), 150 deletions(-) (limited to 'Alc') diff --git a/Alc/backends/qsa.c b/Alc/backends/qsa.c index 32ae06a0..fac07cef 100644 --- a/Alc/backends/qsa.c +++ b/Alc/backends/qsa.c @@ -53,12 +53,12 @@ typedef struct { int card; int dev; } DevMap; +TYPEDEF_VECTOR(DevMap, vector_DevMap) + +static vector_DevMap DeviceNameMap; +static vector_DevMap CaptureNameMap; static const ALCchar qsaDevice[] = "QSA Default"; -static DevMap* allDevNameMap; -static ALuint numDevNames; -static DevMap* allCaptureDevNameMap; -static ALuint numCaptureDevNames; static const struct { int32_t format; @@ -104,69 +104,58 @@ static const struct { {0}, }; -static DevMap *deviceList(int type, ALuint *count) +static void deviceList(int type, vector_DevMap *devmap) { snd_ctl_t* handle; snd_pcm_info_t pcminfo; - int max_cards, card, err, dev, num_devices, idx; - DevMap* dev_list; + int max_cards, card, err, dev; + DevMap entry; char name[1024]; struct snd_ctl_hw_info info; void* temp; - idx=0; - num_devices=0; - max_cards=snd_cards(); + max_cards = snd_cards(); + if(max_cards < 0) + return; - if (max_cards<=0) - { - return 0; - } + VECTOR_RESERVE(*devmap, max_cards+1); + VECTOR_RESIZE(*devmap, 0); - dev_list=malloc(sizeof(DevMap)*1); - dev_list[0].name=strdup(qsaDevice); - num_devices=1; + entry.name = strdup(qsaDevice); + entry.card = 0; + entry.dev = 0; + VECTOR_PUSH_BACK(*devmap, entry); - for (card=0; cardpcmHandle, &card, &dev, SND_PCM_OPEN_PLAYBACK); - } + if(strcmp(deviceName, qsaDevice) == 0) + status = snd_pcm_open_preferred(&data->pcmHandle, &card, &dev, SND_PCM_OPEN_PLAYBACK); else { - size_t idx; + const DevMap *iter; - if (!allDevNameMap) - { - allDevNameMap=deviceList(SND_PCM_CHANNEL_PLAYBACK, &numDevNames); - } + if(VECTOR_SIZE(DeviceNameMap) == 0) + deviceList(SND_PCM_CHANNEL_PLAYBACK, &DeviceNameMap); - for (idx=0; idx0) - { - break; - } - } - } - if (idx==numDevNames) +#define MATCH_DEVNAME(iter) ((iter)->name && strcmp(deviceName, (iter)->name)==0) + VECTOR_FIND_IF(iter, const DevMap, DeviceNameMap, MATCH_DEVNAME); +#undef MATCH_DEVNAME + if(iter == VECTOR_ITER_END(DeviceNameMap)) { free(data); return ALC_INVALID_DEVICE; } - status=snd_pcm_open(&data->pcmHandle, allDevNameMap[idx].card, allDevNameMap[idx].dev, SND_PCM_OPEN_PLAYBACK); + status = snd_pcm_open(&data->pcmHandle, iter->card, iter->dev, SND_PCM_OPEN_PLAYBACK); } - if (status<0) + if(status < 0) { free(data); return ALC_INVALID_DEVICE; } - data->audio_fd=snd_pcm_file_descriptor(data->pcmHandle, SND_PCM_CHANNEL_PLAYBACK); - if (data->audio_fd<0) + data->audio_fd = snd_pcm_file_descriptor(data->pcmHandle, SND_PCM_CHANNEL_PLAYBACK); + if(data->audio_fd < 0) { + snd_pcm_close(data->pcmHandle); free(data); return ALC_INVALID_DEVICE; } @@ -648,56 +620,38 @@ static ALCenum qsa_open_capture(ALCdevice* device, const ALCchar* deviceName) return ALC_OUT_OF_MEMORY; } - if (!deviceName) - { + if(!deviceName) deviceName=driver; - } - if (strcmp(deviceName, qsaDevice)==0) - { - if (!deviceName) - { - deviceName=qsaDevice; - } - - status=snd_pcm_open_preferred(&data->pcmHandle, &card, &dev, SND_PCM_OPEN_CAPTURE); - } + if(strcmp(deviceName, qsaDevice) == 0) + status = snd_pcm_open_preferred(&data->pcmHandle, &card, &dev, SND_PCM_OPEN_CAPTURE); else { - size_t idx; + const DevMap *iter; - if (!allCaptureDevNameMap) - { - allCaptureDevNameMap=deviceList(SND_PCM_CHANNEL_CAPTURE, &numDevNames); - } + if(VECTOR_SIZE(CaptureNameMap) == 0) + deviceList(SND_PCM_CHANNEL_CAPTURE, &CaptureNameMap); - for (idx=0; idx0) - { - break; - } - } - } - if (idx==numDevNames) +#define MATCH_DEVNAME(iter) ((iter)->name && strcmp(deviceName, (iter)->name)==0) + VECTOR_FIND_IF(iter, const DevMap, CaptureNameMap, MATCH_DEVNAME); +#undef MATCH_DEVNAME + if(iter == VECTOR_ITER_END(CaptureNameMap)) { free(data); return ALC_INVALID_DEVICE; } - status=snd_pcm_open(&data->pcmHandle, allCaptureDevNameMap[idx].card, allCaptureDevNameMap[idx].dev, SND_PCM_OPEN_CAPTURE); + status = snd_pcm_open(&data->pcmHandle, iter->card, iter->dev, SND_PCM_OPEN_CAPTURE); } - if (status<0) + if(status < 0) { free(data); return ALC_INVALID_DEVICE; } - data->audio_fd=snd_pcm_file_descriptor(data->pcmHandle, SND_PCM_CHANNEL_CAPTURE); - if (data->audio_fd<0) + data->audio_fd = snd_pcm_file_descriptor(data->pcmHandle, SND_PCM_CHANNEL_CAPTURE); + if(data->audio_fd < 0) { snd_pcm_close(data->pcmHandle); free(data); @@ -928,30 +882,19 @@ static const BackendFuncs qsa_funcs= { ALCboolean alc_qsa_init(BackendFuncs* func_list) { - *func_list=qsa_funcs; - + *func_list = qsa_funcs; return ALC_TRUE; } void alc_qsa_deinit(void) { - ALuint i; +#define FREE_NAME(iter) free((iter)->name) + VECTOR_FOR_EACH(DevMap, DeviceNameMap, FREE_NAME); + VECTOR_DEINIT(DeviceNameMap); - for (i=0; iname) + VECTOR_FOR_EACH(DevMap, DeviceNameMap, FREE_NAME); +#undef FREE_NAME + VECTOR_RESIZE(DeviceNameMap, 0); + + deviceList(SND_PCM_CHANNEL_PLAYBACK, &DeviceNameMap); +#define APPEND_DEVICE(iter) AppendAllDevicesList((iter)->name) + VECTOR_FOR_EACH(const DevMap, DeviceNameMap, APPEND_DEVICE); +#undef APPEND_DEVICE + break; + case CAPTURE_DEVICE_PROBE: - for (i=0; iname) + VECTOR_FOR_EACH(DevMap, CaptureNameMap, FREE_NAME); +#undef FREE_NAME + VECTOR_RESIZE(CaptureNameMap, 0); + + deviceList(SND_PCM_CHANNEL_CAPTURE, &CaptureNameMap); +#define APPEND_DEVICE(iter) AppendCaptureDeviceList((iter)->name) + VECTOR_FOR_EACH(const DevMap, CaptureNameMap, APPEND_DEVICE); +#undef APPEND_DEVICE + break; } } -- cgit v1.2.3