Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/os/windows/vm/iphlp_interface.cpp
32285 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "iphlp_interface.hpp"25#include "runtime/os.hpp"2627// IPHLP API28typedef DWORD(WINAPI *GetIfTable2_Fn)(PMIB_IF_TABLE2*);29typedef DWORD(WINAPI *FreeMibTable_Fn)(PVOID);3031// IPHLP statics32GetIfTable2_Fn IphlpDll::_GetIfTable2 = NULL;33FreeMibTable_Fn IphlpDll::_FreeMibTable = NULL;3435LONG IphlpDll::_critical_section = 0;36LONG IphlpDll::_initialized = 0;37LONG IphlpDll::_iphlp_reference_count = 0;38HMODULE IphlpDll::_hModule = NULL;3940void IphlpDll::initialize(void) {41_hModule = os::win32::load_Windows_dll("iphlpapi.dll", NULL, 0);4243if (NULL == _hModule) {44return;45}4647// The 'A' at the end means the ANSI (not the UNICODE) vesions of the methods48_GetIfTable2 = (GetIfTable2_Fn)::GetProcAddress(_hModule, "GetIfTable2");49_FreeMibTable = (FreeMibTable_Fn)::GetProcAddress(_hModule, "FreeMibTable");5051// interlock is used for fencing52InterlockedExchange(&_initialized, 1);53}5455bool IphlpDll::IphlpDetach(void) {56LONG prev_ref_count = InterlockedExchangeAdd(&_iphlp_reference_count, -1);57BOOL ret = false;5859if (1 == prev_ref_count) {60if (_initialized && _hModule != NULL) {61ret = FreeLibrary(_hModule);62if (ret) {63_hModule = NULL;64_GetIfTable2 = NULL;65_FreeMibTable = NULL;66InterlockedExchange(&_initialized, 0);67}68}69}70return ret != 0;71}7273bool IphlpDll::IphlpAttach(void) {74InterlockedExchangeAdd(&_iphlp_reference_count, 1);7576if (1 == _initialized) {77return true;78}7980while (InterlockedCompareExchange(&_critical_section, 1, 0) == 1);8182if (0 == _initialized) {83initialize();84}8586while (InterlockedCompareExchange(&_critical_section, 0, 1) == 0);8788return (_GetIfTable2 != NULL && _FreeMibTable != NULL);89}9091DWORD IphlpDll::GetIfTable2(PMIB_IF_TABLE2* Table) {92assert(_initialized && _GetIfTable2 != NULL,93"IphlpAttach() not yet called");9495return _GetIfTable2(Table);96}9798DWORD IphlpDll::FreeMibTable(PVOID Memory) {99assert(_initialized && _FreeMibTable != NULL,100"IphlpAttach() not yet called");101102return _FreeMibTable(Memory);103}104105106