mirror of https://github.com/PCSX2/pcsx2.git
DEV9: Use unique_ptr to manage buffer lifetime
This commit is contained in:
parent
a8d1a7cba1
commit
c121b180be
|
@ -14,6 +14,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "PrecompiledHeader.h"
|
#include "PrecompiledHeader.h"
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
|
@ -54,12 +55,12 @@ mac_address host_mac;
|
||||||
//IP_ADAPTER_ADDRESSES is a structure that contains ptrs to data in other regions
|
//IP_ADAPTER_ADDRESSES is a structure that contains ptrs to data in other regions
|
||||||
//of the buffer, se we need to return both so the caller can free the buffer
|
//of the buffer, se we need to return both so the caller can free the buffer
|
||||||
//after it's finished reading the needed data from IP_ADAPTER_ADDRESSES
|
//after it's finished reading the needed data from IP_ADAPTER_ADDRESSES
|
||||||
bool GetWin32Adapter(const char* name, PIP_ADAPTER_ADDRESSES adapter, PIP_ADAPTER_ADDRESSES* buffer)
|
bool GetWin32Adapter(const char* name, PIP_ADAPTER_ADDRESSES adapter, std::unique_ptr<IP_ADAPTER_ADDRESSES[]>* buffer)
|
||||||
{
|
{
|
||||||
const int guidindex = strlen("\\Device\\NPF_");
|
const int guidindex = strlen("\\Device\\NPF_");
|
||||||
|
|
||||||
int neededSize = 128;
|
int neededSize = 128;
|
||||||
PIP_ADAPTER_ADDRESSES AdapterInfo = new IP_ADAPTER_ADDRESSES[neededSize];
|
std::unique_ptr<IP_ADAPTER_ADDRESSES[]> AdapterInfo = std::make_unique<IP_ADAPTER_ADDRESSES[]>(neededSize);
|
||||||
ULONG dwBufLen = sizeof(IP_ADAPTER_ADDRESSES) * neededSize;
|
ULONG dwBufLen = sizeof(IP_ADAPTER_ADDRESSES) * neededSize;
|
||||||
|
|
||||||
PIP_ADAPTER_ADDRESSES pAdapterInfo;
|
PIP_ADAPTER_ADDRESSES pAdapterInfo;
|
||||||
|
@ -68,15 +69,14 @@ bool GetWin32Adapter(const char* name, PIP_ADAPTER_ADDRESSES adapter, PIP_ADAPTE
|
||||||
AF_UNSPEC,
|
AF_UNSPEC,
|
||||||
GAA_FLAG_INCLUDE_PREFIX,
|
GAA_FLAG_INCLUDE_PREFIX,
|
||||||
NULL,
|
NULL,
|
||||||
AdapterInfo,
|
AdapterInfo.get(),
|
||||||
&dwBufLen);
|
&dwBufLen);
|
||||||
|
|
||||||
if (dwStatus == ERROR_BUFFER_OVERFLOW)
|
if (dwStatus == ERROR_BUFFER_OVERFLOW)
|
||||||
{
|
{
|
||||||
DevCon.WriteLn("GetWin32Adapter() buffer too small, resizing");
|
DevCon.WriteLn("GetWin32Adapter() buffer too small, resizing");
|
||||||
delete[] AdapterInfo;
|
|
||||||
neededSize = dwBufLen / sizeof(IP_ADAPTER_ADDRESSES) + 1;
|
neededSize = dwBufLen / sizeof(IP_ADAPTER_ADDRESSES) + 1;
|
||||||
AdapterInfo = new IP_ADAPTER_ADDRESSES[neededSize];
|
AdapterInfo = std::make_unique<IP_ADAPTER_ADDRESSES[]>(neededSize);
|
||||||
dwBufLen = sizeof(IP_ADAPTER_ADDRESSES) * neededSize;
|
dwBufLen = sizeof(IP_ADAPTER_ADDRESSES) * neededSize;
|
||||||
DevCon.WriteLn("New size %i", neededSize);
|
DevCon.WriteLn("New size %i", neededSize);
|
||||||
|
|
||||||
|
@ -84,27 +84,25 @@ bool GetWin32Adapter(const char* name, PIP_ADAPTER_ADDRESSES adapter, PIP_ADAPTE
|
||||||
AF_UNSPEC,
|
AF_UNSPEC,
|
||||||
GAA_FLAG_INCLUDE_PREFIX,
|
GAA_FLAG_INCLUDE_PREFIX,
|
||||||
NULL,
|
NULL,
|
||||||
AdapterInfo,
|
AdapterInfo.get(),
|
||||||
&dwBufLen);
|
&dwBufLen);
|
||||||
}
|
}
|
||||||
if (dwStatus != ERROR_SUCCESS)
|
if (dwStatus != ERROR_SUCCESS)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
pAdapterInfo = AdapterInfo;
|
pAdapterInfo = AdapterInfo.get();
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if (0 == strcmp(pAdapterInfo->AdapterName, &name[guidindex]))
|
if (0 == strcmp(pAdapterInfo->AdapterName, &name[guidindex]))
|
||||||
{
|
{
|
||||||
*adapter = *pAdapterInfo;
|
*adapter = *pAdapterInfo;
|
||||||
*buffer = AdapterInfo;
|
buffer->swap(AdapterInfo);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
pAdapterInfo = pAdapterInfo->Next;
|
pAdapterInfo = pAdapterInfo->Next;
|
||||||
} while (pAdapterInfo);
|
} while (pAdapterInfo);
|
||||||
|
|
||||||
delete[] AdapterInfo;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -115,12 +113,11 @@ int GetMACAddress(char* adapter, mac_address* addr)
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
IP_ADAPTER_ADDRESSES adapterInfo;
|
IP_ADAPTER_ADDRESSES adapterInfo;
|
||||||
PIP_ADAPTER_ADDRESSES buffer;
|
std::unique_ptr<IP_ADAPTER_ADDRESSES[]> buffer;
|
||||||
|
|
||||||
if (GetWin32Adapter(adapter, &adapterInfo, &buffer))
|
if (GetWin32Adapter(adapter, &adapterInfo, &buffer))
|
||||||
{
|
{
|
||||||
memcpy(addr, adapterInfo.PhysicalAddress, 6);
|
memcpy(addr, adapterInfo.PhysicalAddress, 6);
|
||||||
delete[] buffer;
|
|
||||||
retval = 1;
|
retval = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -405,13 +402,10 @@ std::vector<AdapterEntry> PCAPAdapter::GetAdapters()
|
||||||
entry.guid = std::wstring(wEth);
|
entry.guid = std::wstring(wEth);
|
||||||
|
|
||||||
IP_ADAPTER_ADDRESSES adapterInfo;
|
IP_ADAPTER_ADDRESSES adapterInfo;
|
||||||
PIP_ADAPTER_ADDRESSES buffer;
|
std::unique_ptr<IP_ADAPTER_ADDRESSES[]> buffer;
|
||||||
|
|
||||||
if (GetWin32Adapter(d->name, &adapterInfo, &buffer))
|
if (GetWin32Adapter(d->name, &adapterInfo, &buffer))
|
||||||
{
|
|
||||||
entry.name = std::wstring(adapterInfo.FriendlyName);
|
entry.name = std::wstring(adapterInfo.FriendlyName);
|
||||||
delete[] buffer;
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//have to use description
|
//have to use description
|
||||||
|
|
Loading…
Reference in New Issue