master
h 29 lines 1.15 KB
Raw
1 // Copyright (C) Microsoft Corporation. All rights reserved.
2
3 #pragma once
4
5 #include <wil/resource.h>
6 #include "wslc.h"
7
8 namespace wsl::windows::common::wslc {
9
10 // IWSLCSession::ListContainers allocates a string for every unbounded field on an entry, so each
11 // element owns memory that releasing the array alone would miss. Safe to call on a partially
12 // populated entry because unset fields are null.
13 inline void FreeContainerEntryStrings(_Inout_ WSLCContainerEntry* Entry)
14 {
15 CoTaskMemFree(Entry->Command);
16 CoTaskMemFree(Entry->Status);
17 CoTaskMemFree(Entry->Labels);
18 CoTaskMemFree(Entry->Networks);
19 CoTaskMemFree(Entry->Mounts);
20 }
21
22 // Owns both the entry array and the per-entry strings. Callers of ListContainers should use this
23 // rather than a plain cotaskmem array so the strings cannot be leaked. The array still holds raw
24 // entries, so it can be passed to the interface as-is.
25 using unique_container_entry = wil::unique_struct<WSLCContainerEntry, decltype(&FreeContainerEntryStrings), FreeContainerEntryStrings>;
26
27 using unique_container_entry_array = wil::unique_cotaskmem_array_ptr<unique_container_entry>;
28
29 } // namespace wsl::windows::common::wslc