77// specific language governing permissions and limitations relating to use of the SAFE Network
88// Software.
99
10- use libc:: { self , c_ulong, c_void, size_t} ;
1110use std:: ffi:: CStr ;
1211use std:: { io, ptr} ;
1312use windows_sys:: Win32 :: Foundation :: { ERROR_BUFFER_OVERFLOW , ERROR_SUCCESS } ;
@@ -16,6 +15,9 @@ use windows_sys::Win32::NetworkManagement::IpHelper::{
1615 GAA_FLAG_SKIP_FRIENDLY_NAME , GAA_FLAG_SKIP_MULTICAST , IP_ADAPTER_ADDRESSES_LH ,
1716 IP_ADAPTER_PREFIX_XP , IP_ADAPTER_UNICAST_ADDRESS_LH ,
1817} ;
18+ use windows_sys:: Win32 :: System :: Memory :: {
19+ GetProcessHeap , HeapAlloc , HeapFree , HEAP_NONE , HEAP_ZERO_MEMORY ,
20+ } ;
1921
2022#[ repr( transparent) ]
2123pub struct IpAdapterAddresses ( * const IP_ADAPTER_ADDRESSES_LH ) ;
@@ -68,12 +70,13 @@ pub struct IfAddrs {
6870impl IfAddrs {
6971 #[ allow( unsafe_code) ]
7072 pub fn new ( ) -> io:: Result < Self > {
71- let mut buffersize: c_ulong = 15000 ;
73+ let mut buffersize = 15000 ;
7274 let mut ifaddrs: * mut IP_ADAPTER_ADDRESSES_LH ;
7375
7476 loop {
7577 unsafe {
76- ifaddrs = libc:: malloc ( buffersize as size_t ) as * mut IP_ADAPTER_ADDRESSES_LH ;
78+ ifaddrs = HeapAlloc ( GetProcessHeap ( ) , HEAP_ZERO_MEMORY , buffersize as _ )
79+ as * mut IP_ADAPTER_ADDRESSES_LH ;
7780 if ifaddrs. is_null ( ) {
7881 panic ! ( "Failed to allocate buffer in get_if_addrs()" ) ;
7982 }
@@ -93,12 +96,12 @@ impl IfAddrs {
9396 match retcode {
9497 ERROR_SUCCESS => break ,
9598 ERROR_BUFFER_OVERFLOW => {
96- libc :: free ( ifaddrs as * mut c_void ) ;
99+ HeapFree ( GetProcessHeap ( ) , HEAP_NONE , ifaddrs as _ ) ;
97100 buffersize *= 2 ;
98101 continue ;
99102 }
100103 _ => {
101- libc :: free ( ifaddrs as * mut c_void ) ;
104+ HeapFree ( GetProcessHeap ( ) , HEAP_NONE , ifaddrs as _ ) ;
102105 return Err ( io:: Error :: last_os_error ( ) ) ;
103106 }
104107 }
@@ -122,7 +125,7 @@ impl Drop for IfAddrs {
122125 #[ allow( unsafe_code) ]
123126 fn drop ( & mut self ) {
124127 unsafe {
125- libc :: free ( self . inner . 0 as * mut c_void ) ;
128+ HeapFree ( GetProcessHeap ( ) , HEAP_NONE , self . inner . 0 as _ ) ;
126129 }
127130 }
128131}
0 commit comments