[PATCH v2 0/2] MR3658: mmdevapi: Simplify load_driver_devices() by combining loops and removing unneeded allocation(s), move name string duplication in MMDevice_Create() from caller.
-- v2: mmdevapi: Move name string duplication in MMDevice_Create() from caller. mmdevapi: Simplify load_driver_devices() by combining loops and removing unneeded allocation(s). https://gitlab.winehq.org/wine/wine/-/merge_requests/3658
From: Davide Beatrici <git(a)davidebeatrici.dev> --- dlls/mmdevapi/devenum.c | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/dlls/mmdevapi/devenum.c b/dlls/mmdevapi/devenum.c index 2900d222795..f93bfa83ec7 100644 --- a/dlls/mmdevapi/devenum.c +++ b/dlls/mmdevapi/devenum.c @@ -528,8 +528,6 @@ static HRESULT set_format(MMDevice *dev) HRESULT load_driver_devices(EDataFlow flow) { struct get_endpoint_ids_params params; - WCHAR **ids = NULL; - GUID *guids = NULL; UINT i; params.flow = flow; @@ -544,38 +542,28 @@ HRESULT load_driver_devices(EDataFlow flow) if (FAILED(params.result)) goto end; - ids = malloc(params.num * sizeof(*ids)); - guids = malloc(params.num * sizeof(*guids)); - if (!ids || !guids) { - params.result = E_OUTOFMEMORY; - goto end; - } - for (i = 0; i < params.num; i++) { + GUID guid; + WCHAR *id; + MMDevice *dev; const WCHAR *name = (WCHAR *)((char *)params.endpoints + params.endpoints[i].name); const char *dev_name = (char *)params.endpoints + params.endpoints[i].device; const unsigned int size = (wcslen(name) + 1) * sizeof(WCHAR); - if (!(ids[i] = malloc(size))) { - while (i--) free(ids[i]); + if (!(id = malloc(size))) { params.result = E_OUTOFMEMORY; goto end; } - memcpy(ids[i], name, size); - drvs.pget_device_guid(flow, dev_name, &guids[i]); - } + memcpy(id, name, size); - for (i = 0; i < params.num; i++) { - MMDevice *dev; - dev = MMDevice_Create(ids[i], &guids[i], flow, DEVICE_STATE_ACTIVE, - params.default_idx == i); + drvs.pget_device_guid(flow, dev_name, &guid); + + dev = MMDevice_Create(id, &guid, flow, DEVICE_STATE_ACTIVE, params.default_idx == i); set_format(dev); } end: free(params.endpoints); - free(guids); - free(ids); return params.result; } -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/3658
From: Davide Beatrici <git(a)davidebeatrici.dev> --- dlls/mmdevapi/devenum.c | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/dlls/mmdevapi/devenum.c b/dlls/mmdevapi/devenum.c index f93bfa83ec7..10037a7affe 100644 --- a/dlls/mmdevapi/devenum.c +++ b/dlls/mmdevapi/devenum.c @@ -312,7 +312,7 @@ static const WCHAR *find_product_name_override(const WCHAR *device_id) * If GUID is null, a random guid will be assigned * and the device will be created */ -static MMDevice *MMDevice_Create(WCHAR *name, GUID *id, EDataFlow flow, DWORD state, BOOL setdefault) +static MMDevice *MMDevice_Create(const WCHAR *name, GUID *id, EDataFlow flow, DWORD state, BOOL setdefault) { HKEY key, root; MMDevice *device, *cur = NULL; @@ -351,7 +351,7 @@ static MMDevice *MMDevice_Create(WCHAR *name, GUID *id, EDataFlow flow, DWORD st WARN("Modifying an MMDevice with postitive reference count!\n"); free(cur->drv_id); - cur->drv_id = name; + cur->drv_id = wcsdup(name); cur->flow = flow; cur->state = state; @@ -373,7 +373,7 @@ static MMDevice *MMDevice_Create(WCHAR *name, GUID *id, EDataFlow flow, DWORD st PROPVARIANT pv; pv.vt = VT_LPWSTR; - pv.pwszVal = name; + pv.pwszVal = cur->drv_id; if (SUCCEEDED(set_driver_prop_value(id, flow, &devicepath_key))) { PROPVARIANT pv2; @@ -482,10 +482,7 @@ HRESULT load_devices_from_reg(void) && SUCCEEDED(MMDevice_GetPropValue(&guid, curflow, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv)) && pv.vt == VT_LPWSTR) { - DWORD size_bytes = (lstrlenW(pv.pwszVal) + 1) * sizeof(WCHAR); - WCHAR *name = malloc(size_bytes); - memcpy(name, pv.pwszVal, size_bytes); - MMDevice_Create(name, &guid, curflow, + MMDevice_Create(pv.pwszVal, &guid, curflow, DEVICE_STATE_NOTPRESENT, FALSE); CoTaskMemFree(pv.pwszVal); } @@ -544,21 +541,13 @@ HRESULT load_driver_devices(EDataFlow flow) for (i = 0; i < params.num; i++) { GUID guid; - WCHAR *id; MMDevice *dev; const WCHAR *name = (WCHAR *)((char *)params.endpoints + params.endpoints[i].name); const char *dev_name = (char *)params.endpoints + params.endpoints[i].device; - const unsigned int size = (wcslen(name) + 1) * sizeof(WCHAR); - - if (!(id = malloc(size))) { - params.result = E_OUTOFMEMORY; - goto end; - } - memcpy(id, name, size); drvs.pget_device_guid(flow, dev_name, &guid); - dev = MMDevice_Create(id, &guid, flow, DEVICE_STATE_ACTIVE, params.default_idx == i); + dev = MMDevice_Create(name, &guid, flow, DEVICE_STATE_ACTIVE, params.default_idx == i); set_format(dev); } -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/3658
On Wed Aug 30 00:17:34 2023 +0000, Huw Davies wrote:
Could we add a second commit to this MR that moves the duplication of the `name` parameter to `MMDevice_Create()`? (using `wcsdup()` for that would be good too). Done.
-- https://gitlab.winehq.org/wine/wine/-/merge_requests/3658#note_43791
This merge request was approved by Huw Davies. -- https://gitlab.winehq.org/wine/wine/-/merge_requests/3658
participants (3)
-
Davide Beatrici -
Davide Beatrici (@davidebeatrici) -
Huw Davies (@huw)