Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
unique_fd.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_FML_UNIQUE_FD_H_
6#define FLUTTER_FML_UNIQUE_FD_H_
7
8#include "flutter/fml/build_config.h"
9#include "flutter/fml/unique_object.h"
10
11#if FML_OS_WIN
12#include <windows.h>
13#include <map>
14#include <mutex>
15#include <optional>
16#else // FML_OS_WIN
17#include <dirent.h>
18#include <unistd.h>
19#endif // FML_OS_WIN
20
21namespace fml {
22namespace internal {
23
24#if FML_OS_WIN
25
26namespace os_win {
27
28struct DirCacheEntry {
29 std::wstring filename;
30 FILE_ID_128 id;
31};
32
33// The order of these is important. Must come before UniqueFDTraits struct
34// else linker error. Embedding in struct also causes linker error.
35
36struct UniqueFDTraits {
37 static std::mutex file_map_mutex;
38 static std::map<HANDLE, DirCacheEntry> file_map;
39
40 static HANDLE InvalidValue() { return INVALID_HANDLE_VALUE; }
41 static bool IsValid(HANDLE value) { return value != InvalidValue(); }
42 static void Free_Handle(HANDLE fd);
43
44 static void Free(HANDLE fd) {
45 RemoveCacheEntry(fd);
46
47 UniqueFDTraits::Free_Handle(fd);
48 }
49
50 static void RemoveCacheEntry(HANDLE fd) {
51 const std::lock_guard<std::mutex> lock(file_map_mutex);
52
53 file_map.erase(fd);
54 }
55
56 static void StoreCacheEntry(HANDLE fd, DirCacheEntry state) {
57 const std::lock_guard<std::mutex> lock(file_map_mutex);
58 file_map[fd] = state;
59 }
60
61 static std::optional<DirCacheEntry> GetCacheEntry(HANDLE fd) {
62 const std::lock_guard<std::mutex> lock(file_map_mutex);
63 auto found = file_map.find(fd);
64 return found == file_map.end()
65 ? std::nullopt
66 : std::optional<DirCacheEntry>{found->second};
67 }
68};
69
70} // namespace os_win
71
72#else // FML_OS_WIN
73
74namespace os_unix {
75
77 static int InvalidValue() { return -1; }
78 static bool IsValid(int value) { return value >= 0; }
79 static void Free(int fd);
80};
81
83 static DIR* InvalidValue() { return nullptr; }
84 static bool IsValid(DIR* value) { return value != nullptr; }
85 static void Free(DIR* dir);
86};
87
88} // namespace os_unix
89
90#endif // FML_OS_WIN
91
92} // namespace internal
93
94#if FML_OS_WIN
95
96using UniqueFD = UniqueObject<HANDLE, internal::os_win::UniqueFDTraits>;
97
98#else // FML_OS_WIN
99
102
103#endif // FML_OS_WIN
104
105} // namespace fml
106
107#endif // FLUTTER_FML_UNIQUE_FD_H_
AtkStateType state
uint8_t value
UniqueObject< int, internal::os_unix::UniqueFDTraits > UniqueFD
Definition unique_fd.h:100
static bool IsValid(DIR *value)
Definition unique_fd.h:84
static bool IsValid(int value)
Definition unique_fd.h:78
const uintptr_t id
#define INVALID_HANDLE_VALUE
void * HANDLE