testaddr.ha (541B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 use rt; 5 6 fn isaddrmapped(addr: *opaque) bool = { 7 // This is a hack, but it's a common pattern on POSIX for testing the 8 // validity of an address. 9 static let pipefd: [2]int = [0, 0]; 10 if (pipefd[0] == 0) { 11 match (rt::pipe2(&pipefd, 0)) { 12 case rt::errno => 13 return false; 14 case void => yield; 15 }; 16 }; 17 18 match (rt::write(pipefd[1], addr, 1)) { 19 case let err: rt::errno => 20 assert(err == rt::EFAULT); 21 return false; 22 case size => 23 return true; 24 }; 25 };