1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef INCLUDE_PERFETTO_BASE_PLATFORM_HANDLE_H_ 18 #define INCLUDE_PERFETTO_BASE_PLATFORM_HANDLE_H_ 19 20 #include <stdint.h> 21 22 #include "perfetto/base/build_config.h" 23 24 namespace perfetto { 25 namespace base { 26 27 // PlatformHandle should be used only for types that are HANDLE(s) in Windows. 28 // It should NOT be used to blanket-replace "int fd" in the codebase. 29 // Windows has two types of "handles", which, in UNIX-land, both map to int: 30 // 1. File handles returned by the posix-compatibility API like _open(). 31 // These are just int(s) and should stay such, because all the posix-like API 32 // in Windows.h take an int, not a HANDLE. 33 // 2. Handles returned by old-school WINAPI like CreateFile, CreateEvent etc. 34 // These are proper HANDLE(s). PlatformHandle should be used here. 35 // 36 // On Windows, sockets have their own type (SOCKET) which is neither a HANDLE 37 // nor an int. However Windows SOCKET(s) can have an event HANDLE attached 38 // to them (which in Perfetto is a PlatformHandle), and that can be used in 39 // WaitForMultipleObjects, hence in base::TaskRunner.AddFileDescriptorWatch(). 40 // On POSIX OSes, a SocketHandle is really just an int (a file descriptor). 41 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) 42 // Windows.h typedefs HANDLE to void*, and SOCKET to uintptr_t. We use their 43 // types to avoid leaking Windows.h through our headers. 44 using PlatformHandle = void*; 45 using SocketHandle = uintptr_t; 46 47 // On Windows both nullptr and 0xffff... (INVALID_HANDLE_VALUE) are invalid. 48 struct PlatformHandleChecker { IsValidPlatformHandleChecker49 static inline bool IsValid(PlatformHandle h) { 50 return h && h != reinterpret_cast<PlatformHandle>(-1); 51 } 52 }; 53 #else 54 using PlatformHandle = int; 55 using SocketHandle = int; 56 struct PlatformHandleChecker { 57 static inline bool IsValid(PlatformHandle h) { return h >= 0; } 58 }; 59 #endif 60 61 // The definition of this lives in base/file_utils.cc (to avoid creating an 62 // extra build edge for a one liner). This is really an alias for close() (UNIX) 63 // CloseHandle() (Windows). THe indirection layer is just to avoid leaking 64 // system headers like Windows.h through perfetto headers. 65 // Thre return value is always UNIX-style: 0 on success, -1 on failure. 66 int ClosePlatformHandle(PlatformHandle); 67 68 } // namespace base 69 } // namespace perfetto 70 71 #endif // INCLUDE_PERFETTO_BASE_PLATFORM_HANDLE_H_ 72