• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "perfetto/base/build_config.h"
21 
22 namespace perfetto {
23 namespace base {
24 
25 // PlatformHandle should be used only for types that are HANDLE(s) in Windows.
26 // It should NOT be used to blanket-replace "int fd" in the codebase.
27 // Windows has two types of "handles", which, in UNIX-land, both map to int:
28 // 1. File handles returned by the posix-compatibility API like _open().
29 //    These are just int(s) and should stay such, because all the posix-like API
30 //    in Windows.h take an int, not a HANDLE.
31 // 2. Handles returned by old-school WINAPI like CreateFile, CreateEvent etc.
32 //    These are proper HANDLE(s). PlatformHandle should be used here.
33 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
34 // Windows.h typedefs HANDLE to void*. We use void* here to avoid leaking
35 // Windows.h through our headers.
36 using PlatformHandle = void*;
37 
38 // On Windows both nullptr and 0xffff... (INVALID_HANDLE_VALUE) are invalid.
39 struct PlatformHandleChecker {
IsValidPlatformHandleChecker40   static inline bool IsValid(PlatformHandle h) {
41     return h && h != reinterpret_cast<PlatformHandle>(-1);
42   }
43 };
44 #else
45 using PlatformHandle = int;
46 struct PlatformHandleChecker {
47   static inline bool IsValid(PlatformHandle h) { return h >= 0; }
48 };
49 #endif
50 
51 // The definition of this lives in base/file_utils.cc (to avoid creating an
52 // extra build edge for a one liner). This is really an alias for close() (UNIX)
53 // CloseHandle() (Windows). THe indirection layer is just to avoid leaking
54 // system headers like Windows.h through perfetto headers.
55 // Thre return value is always UNIX-style: 0 on success, -1 on failure.
56 int ClosePlatformHandle(PlatformHandle);
57 
58 }  // namespace base
59 }  // namespace perfetto
60 
61 #endif  // INCLUDE_PERFETTO_BASE_PLATFORM_HANDLE_H_
62