Path: blob/master/src/hotspot/os/windows/iphlp_interface.cpp
40931 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 "precompiled.hpp"25#include "iphlp_interface.hpp"26#include "runtime/os.hpp"2728// IPHLP API29typedef DWORD(WINAPI *GetIfTable2_Fn)(PMIB_IF_TABLE2*);30typedef DWORD(WINAPI *FreeMibTable_Fn)(PVOID);3132// IPHLP statics33GetIfTable2_Fn IphlpDll::_GetIfTable2 = NULL;34FreeMibTable_Fn IphlpDll::_FreeMibTable = NULL;3536LONG IphlpDll::_critical_section = 0;37LONG IphlpDll::_initialized = 0;38LONG IphlpDll::_iphlp_reference_count = 0;39HMODULE IphlpDll::_hModule = NULL;4041void IphlpDll::initialize(void) {42_hModule = os::win32::load_Windows_dll("iphlpapi.dll", NULL, 0);4344if (NULL == _hModule) {45return;46}4748// The 'A' at the end means the ANSI (not the UNICODE) vesions of the methods49_GetIfTable2 = (GetIfTable2_Fn)::GetProcAddress(_hModule, "GetIfTable2");50_FreeMibTable = (FreeMibTable_Fn)::GetProcAddress(_hModule, "FreeMibTable");5152// interlock is used for fencing53InterlockedExchange(&_initialized, 1);54}5556bool IphlpDll::IphlpDetach(void) {57LONG prev_ref_count = InterlockedExchangeAdd(&_iphlp_reference_count, -1);58BOOL ret = false;5960if (1 == prev_ref_count) {61if (_initialized && _hModule != NULL) {62ret = FreeLibrary(_hModule);63if (ret) {64_hModule = NULL;65_GetIfTable2 = NULL;66_FreeMibTable = NULL;67InterlockedExchange(&_initialized, 0);68}69}70}71return ret != 0;72}7374bool IphlpDll::IphlpAttach(void) {75InterlockedExchangeAdd(&_iphlp_reference_count, 1);7677if (1 == _initialized) {78return true;79}8081while (InterlockedCompareExchange(&_critical_section, 1, 0) == 1);8283if (0 == _initialized) {84initialize();85}8687while (InterlockedCompareExchange(&_critical_section, 0, 1) == 0);8889return (_GetIfTable2 != NULL && _FreeMibTable != NULL);90}9192DWORD IphlpDll::GetIfTable2(PMIB_IF_TABLE2* Table) {93assert(_initialized && _GetIfTable2 != NULL,94"IphlpAttach() not yet called");9596return _GetIfTable2(Table);97}9899DWORD IphlpDll::FreeMibTable(PVOID Memory) {100assert(_initialized && _FreeMibTable != NULL,101"IphlpAttach() not yet called");102103return _FreeMibTable(Memory);104}105106107