From 3277d995a3e60855c7fb5a3ae52751c22360ecd9 Mon Sep 17 00:00:00 2001 From: David Reid Date: Tue, 13 Jan 2026 17:34:52 +1000 Subject: [PATCH] ALSA: Reduce the size of a memory allocation. --- miniaudio.h | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/miniaudio.h b/miniaudio.h index aaff32d3..eaf608c0 100644 --- a/miniaudio.h +++ b/miniaudio.h @@ -28998,18 +28998,17 @@ static ma_result ma_device_init_by_type__alsa(ma_context* pContext, ma_context_s deviceNameCount = iDeviceName; - /* Allocate memory for our hardware and software params. We do this with a single allocation. */ - paramsMemorySize = 0; - paramsMemorySize += ma_align_64(pContextStateALSA->snd_pcm_hw_params_sizeof()); - paramsMemorySize += ma_align_64(pContextStateALSA->snd_pcm_sw_params_sizeof()); + /* Allocate memory for our hardware and software params. We do this with a single allocation. They are used independently so we can just alias this allocation. */ + paramsMemorySize = ma_max(pContextStateALSA->snd_pcm_hw_params_sizeof(), pContextStateALSA->snd_pcm_sw_params_sizeof()); pParamsMemory = ma_calloc(paramsMemorySize, ma_context_get_allocation_callbacks(pContext)); if (pParamsMemory == NULL) { return MA_OUT_OF_MEMORY; } + /* Alias the memory allocation. */ pHWParams = (ma_snd_pcm_hw_params_t*)pParamsMemory; - pSWParams = (ma_snd_pcm_sw_params_t*)ma_offset_ptr(pParamsMemory, ma_align_64(pContextStateALSA->snd_pcm_hw_params_sizeof())); + pSWParams = (ma_snd_pcm_sw_params_t*)pParamsMemory; for (iDeviceName = 0; iDeviceName < deviceNameCount; iDeviceName += 1) { const char* pDeviceName = pDeviceNames[iDeviceName]; @@ -29238,6 +29237,12 @@ static ma_result ma_device_init_by_type__alsa(ma_context* pContext, ma_context_s continue; } + /* + The hardware parameters memory is aliased with the software parameters memory, so just clear out this pointer + for safety and to make it clear that it should not be used from here on out. + */ + pHWParams = NULL; + /* Software parameters. */ resultALSA = pContextStateALSA->snd_pcm_sw_params_current(pPCM, pSWParams); @@ -29284,6 +29289,8 @@ static ma_result ma_device_init_by_type__alsa(ma_context* pContext, ma_context_s continue; } + pSWParams = NULL; + /* Grab the internal channel map. For now we're not going to bother trying to change the channel map and instead just do it ourselves. */ {