| File: | out/../src/node_options-inl.h |
| Warning: | line 193, column 1 Potential leak of memory pointed to by field '_M_pi' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | #include "node_options.h" // NOLINT(build/include_inline) | |||
| 2 | #include "node_options-inl.h" | |||
| 3 | ||||
| 4 | #include "env-inl.h" | |||
| 5 | #include "node_binding.h" | |||
| 6 | #include "node_external_reference.h" | |||
| 7 | #include "node_internals.h" | |||
| 8 | #if HAVE_OPENSSL1 | |||
| 9 | #include "openssl/opensslv.h" | |||
| 10 | #endif | |||
| 11 | ||||
| 12 | #include <errno(*__errno_location ()).h> | |||
| 13 | #include <sstream> | |||
| 14 | #include <limits> | |||
| 15 | #include <algorithm> | |||
| 16 | #include <cstdlib> // strtoul, errno | |||
| 17 | ||||
| 18 | using v8::Boolean; | |||
| 19 | using v8::Context; | |||
| 20 | using v8::FunctionCallbackInfo; | |||
| 21 | using v8::Integer; | |||
| 22 | using v8::Isolate; | |||
| 23 | using v8::Local; | |||
| 24 | using v8::Map; | |||
| 25 | using v8::Number; | |||
| 26 | using v8::Object; | |||
| 27 | using v8::Undefined; | |||
| 28 | using v8::Value; | |||
| 29 | ||||
| 30 | namespace node { | |||
| 31 | ||||
| 32 | namespace per_process { | |||
| 33 | Mutex cli_options_mutex; | |||
| 34 | std::shared_ptr<PerProcessOptions> cli_options{new PerProcessOptions()}; | |||
| 35 | } // namespace per_process | |||
| 36 | ||||
| 37 | void DebugOptions::CheckOptions(std::vector<std::string>* errors) { | |||
| 38 | #if !NODE_USE_V8_PLATFORM1 && !HAVE_INSPECTOR1 | |||
| 39 | if (inspector_enabled) { | |||
| 40 | errors->push_back("Inspector is not available when Node is compiled " | |||
| 41 | "--without-v8-platform and --without-inspector."); | |||
| 42 | } | |||
| 43 | #endif | |||
| 44 | ||||
| 45 | if (deprecated_debug) { | |||
| 46 | errors->push_back("[DEP0062]: `node --debug` and `node --debug-brk` " | |||
| 47 | "are invalid. Please use `node --inspect` and " | |||
| 48 | "`node --inspect-brk` instead."); | |||
| 49 | } | |||
| 50 | ||||
| 51 | std::vector<std::string> destinations = | |||
| 52 | SplitString(inspect_publish_uid_string, ','); | |||
| 53 | inspect_publish_uid.console = false; | |||
| 54 | inspect_publish_uid.http = false; | |||
| 55 | for (const std::string& destination : destinations) { | |||
| 56 | if (destination == "stderr") { | |||
| 57 | inspect_publish_uid.console = true; | |||
| 58 | } else if (destination == "http") { | |||
| 59 | inspect_publish_uid.http = true; | |||
| 60 | } else { | |||
| 61 | errors->push_back("--inspect-publish-uid destination can be " | |||
| 62 | "stderr or http"); | |||
| 63 | } | |||
| 64 | } | |||
| 65 | } | |||
| 66 | ||||
| 67 | void PerProcessOptions::CheckOptions(std::vector<std::string>* errors) { | |||
| 68 | #if HAVE_OPENSSL1 | |||
| 69 | if (use_openssl_ca && use_bundled_ca) { | |||
| 70 | errors->push_back("either --use-openssl-ca or --use-bundled-ca can be " | |||
| 71 | "used, not both"); | |||
| 72 | } | |||
| 73 | ||||
| 74 | // Any value less than 2 disables use of the secure heap. | |||
| 75 | if (secure_heap >= 2) { | |||
| 76 | if ((secure_heap & (secure_heap - 1)) != 0) | |||
| 77 | errors->push_back("--secure-heap must be a power of 2"); | |||
| 78 | secure_heap_min = | |||
| 79 | std::min({ | |||
| 80 | secure_heap, | |||
| 81 | secure_heap_min, | |||
| 82 | static_cast<int64_t>(std::numeric_limits<int>::max())}); | |||
| 83 | secure_heap_min = std::max(static_cast<int64_t>(2), secure_heap_min); | |||
| 84 | if ((secure_heap_min & (secure_heap_min - 1)) != 0) | |||
| 85 | errors->push_back("--secure-heap-min must be a power of 2"); | |||
| 86 | } | |||
| 87 | #endif // HAVE_OPENSSL | |||
| 88 | ||||
| 89 | if (use_largepages != "off" && | |||
| 90 | use_largepages != "on" && | |||
| 91 | use_largepages != "silent") { | |||
| 92 | errors->push_back("invalid value for --use-largepages"); | |||
| 93 | } | |||
| 94 | per_isolate->CheckOptions(errors); | |||
| 95 | } | |||
| 96 | ||||
| 97 | void PerIsolateOptions::CheckOptions(std::vector<std::string>* errors) { | |||
| 98 | per_env->CheckOptions(errors); | |||
| 99 | } | |||
| 100 | ||||
| 101 | void EnvironmentOptions::CheckOptions(std::vector<std::string>* errors) { | |||
| 102 | if (has_policy_integrity_string && experimental_policy.empty()) { | |||
| 103 | errors->push_back("--policy-integrity requires " | |||
| 104 | "--experimental-policy be enabled"); | |||
| 105 | } | |||
| 106 | if (has_policy_integrity_string && experimental_policy_integrity.empty()) { | |||
| 107 | errors->push_back("--policy-integrity cannot be empty"); | |||
| 108 | } | |||
| 109 | ||||
| 110 | if (!module_type.empty()) { | |||
| 111 | if (module_type != "commonjs" && module_type != "module") { | |||
| 112 | errors->push_back("--input-type must be \"module\" or \"commonjs\""); | |||
| 113 | } | |||
| 114 | } | |||
| 115 | ||||
| 116 | if (!experimental_specifier_resolution.empty()) { | |||
| 117 | if (experimental_specifier_resolution != "node" && | |||
| 118 | experimental_specifier_resolution != "explicit") { | |||
| 119 | errors->push_back( | |||
| 120 | "invalid value for --experimental-specifier-resolution"); | |||
| 121 | } | |||
| 122 | } | |||
| 123 | ||||
| 124 | if (syntax_check_only && has_eval_string) { | |||
| 125 | errors->push_back("either --check or --eval can be used, not both"); | |||
| 126 | } | |||
| 127 | ||||
| 128 | if (!unhandled_rejections.empty() && | |||
| 129 | unhandled_rejections != "warn-with-error-code" && | |||
| 130 | unhandled_rejections != "throw" && | |||
| 131 | unhandled_rejections != "strict" && | |||
| 132 | unhandled_rejections != "warn" && | |||
| 133 | unhandled_rejections != "none") { | |||
| 134 | errors->push_back("invalid value for --unhandled-rejections"); | |||
| 135 | } | |||
| 136 | ||||
| 137 | if (tls_min_v1_3 && tls_max_v1_2) { | |||
| 138 | errors->push_back("either --tls-min-v1.3 or --tls-max-v1.2 can be " | |||
| 139 | "used, not both"); | |||
| 140 | } | |||
| 141 | ||||
| 142 | if (heap_snapshot_near_heap_limit < 0) { | |||
| 143 | errors->push_back("--heap-snapshot-near-heap-limit must not be negative"); | |||
| 144 | } | |||
| 145 | ||||
| 146 | if (test_runner) { | |||
| 147 | if (syntax_check_only) { | |||
| 148 | errors->push_back("either --test or --check can be used, not both"); | |||
| 149 | } | |||
| 150 | ||||
| 151 | if (has_eval_string) { | |||
| 152 | errors->push_back("either --test or --eval can be used, not both"); | |||
| 153 | } | |||
| 154 | ||||
| 155 | if (force_repl) { | |||
| 156 | errors->push_back("either --test or --interactive can be used, not both"); | |||
| 157 | } | |||
| 158 | ||||
| 159 | if (debug_options_.inspector_enabled) { | |||
| 160 | errors->push_back("the inspector cannot be used with --test"); | |||
| 161 | } | |||
| 162 | } | |||
| 163 | ||||
| 164 | #if HAVE_INSPECTOR1 | |||
| 165 | if (!cpu_prof) { | |||
| 166 | if (!cpu_prof_name.empty()) { | |||
| 167 | errors->push_back("--cpu-prof-name must be used with --cpu-prof"); | |||
| 168 | } | |||
| 169 | if (!cpu_prof_dir.empty()) { | |||
| 170 | errors->push_back("--cpu-prof-dir must be used with --cpu-prof"); | |||
| 171 | } | |||
| 172 | // We can't catch the case where the value passed is the default value, | |||
| 173 | // then the option just becomes a noop which is fine. | |||
| 174 | if (cpu_prof_interval != kDefaultCpuProfInterval) { | |||
| 175 | errors->push_back("--cpu-prof-interval must be used with --cpu-prof"); | |||
| 176 | } | |||
| 177 | } | |||
| 178 | ||||
| 179 | if (cpu_prof && cpu_prof_dir.empty() && !diagnostic_dir.empty()) { | |||
| 180 | cpu_prof_dir = diagnostic_dir; | |||
| 181 | } | |||
| 182 | ||||
| 183 | if (!heap_prof) { | |||
| 184 | if (!heap_prof_name.empty()) { | |||
| 185 | errors->push_back("--heap-prof-name must be used with --heap-prof"); | |||
| 186 | } | |||
| 187 | if (!heap_prof_dir.empty()) { | |||
| 188 | errors->push_back("--heap-prof-dir must be used with --heap-prof"); | |||
| 189 | } | |||
| 190 | // We can't catch the case where the value passed is the default value, | |||
| 191 | // then the option just becomes a noop which is fine. | |||
| 192 | if (heap_prof_interval != kDefaultHeapProfInterval) { | |||
| 193 | errors->push_back("--heap-prof-interval must be used with --heap-prof"); | |||
| 194 | } | |||
| 195 | } | |||
| 196 | ||||
| 197 | if (heap_prof && heap_prof_dir.empty() && !diagnostic_dir.empty()) { | |||
| 198 | heap_prof_dir = diagnostic_dir; | |||
| 199 | } | |||
| 200 | ||||
| 201 | debug_options_.CheckOptions(errors); | |||
| 202 | #endif // HAVE_INSPECTOR | |||
| 203 | } | |||
| 204 | ||||
| 205 | namespace options_parser { | |||
| 206 | ||||
| 207 | class DebugOptionsParser : public OptionsParser<DebugOptions> { | |||
| 208 | public: | |||
| 209 | DebugOptionsParser(); | |||
| 210 | }; | |||
| 211 | ||||
| 212 | class EnvironmentOptionsParser : public OptionsParser<EnvironmentOptions> { | |||
| 213 | public: | |||
| 214 | EnvironmentOptionsParser(); | |||
| 215 | explicit EnvironmentOptionsParser(const DebugOptionsParser& dop) | |||
| 216 | : EnvironmentOptionsParser() { | |||
| 217 | Insert(dop, &EnvironmentOptions::get_debug_options); | |||
| 218 | } | |||
| 219 | }; | |||
| 220 | ||||
| 221 | class PerIsolateOptionsParser : public OptionsParser<PerIsolateOptions> { | |||
| 222 | public: | |||
| 223 | PerIsolateOptionsParser() = delete; | |||
| 224 | explicit PerIsolateOptionsParser(const EnvironmentOptionsParser& eop); | |||
| 225 | }; | |||
| 226 | ||||
| 227 | class PerProcessOptionsParser : public OptionsParser<PerProcessOptions> { | |||
| 228 | public: | |||
| 229 | PerProcessOptionsParser() = delete; | |||
| 230 | explicit PerProcessOptionsParser(const PerIsolateOptionsParser& iop); | |||
| 231 | }; | |||
| 232 | ||||
| 233 | #if HAVE_INSPECTOR1 | |||
| 234 | const DebugOptionsParser _dop_instance{}; | |||
| 235 | const EnvironmentOptionsParser _eop_instance{_dop_instance}; | |||
| 236 | ||||
| 237 | // This Parse is not dead code. It is used by embedders (e.g., Electron). | |||
| 238 | template <> | |||
| 239 | void Parse( | |||
| 240 | StringVector* const args, StringVector* const exec_args, | |||
| 241 | StringVector* const v8_args, | |||
| 242 | DebugOptions* const options, | |||
| 243 | OptionEnvvarSettings required_env_settings, StringVector* const errors) { | |||
| 244 | _dop_instance.Parse( | |||
| 245 | args, exec_args, v8_args, options, required_env_settings, errors); | |||
| 246 | } | |||
| 247 | #else | |||
| 248 | const EnvironmentOptionsParser _eop_instance{}; | |||
| 249 | #endif // HAVE_INSPECTOR | |||
| 250 | const PerIsolateOptionsParser _piop_instance{_eop_instance}; | |||
| 251 | const PerProcessOptionsParser _ppop_instance{_piop_instance}; | |||
| 252 | ||||
| 253 | template <> | |||
| 254 | void Parse( | |||
| 255 | StringVector* const args, StringVector* const exec_args, | |||
| 256 | StringVector* const v8_args, | |||
| 257 | PerIsolateOptions* const options, | |||
| 258 | OptionEnvvarSettings required_env_settings, StringVector* const errors) { | |||
| 259 | _piop_instance.Parse( | |||
| 260 | args, exec_args, v8_args, options, required_env_settings, errors); | |||
| 261 | } | |||
| 262 | ||||
| 263 | template <> | |||
| 264 | void Parse( | |||
| 265 | StringVector* const args, StringVector* const exec_args, | |||
| 266 | StringVector* const v8_args, | |||
| 267 | PerProcessOptions* const options, | |||
| 268 | OptionEnvvarSettings required_env_settings, StringVector* const errors) { | |||
| 269 | _ppop_instance.Parse( | |||
| 270 | args, exec_args, v8_args, options, required_env_settings, errors); | |||
| 271 | } | |||
| 272 | ||||
| 273 | // XXX: If you add an option here, please also add it to doc/node.1 and | |||
| 274 | // doc/api/cli.md | |||
| 275 | // TODO(addaleax): Make that unnecessary. | |||
| 276 | ||||
| 277 | DebugOptionsParser::DebugOptionsParser() { | |||
| 278 | AddOption("--inspect-port", | |||
| 279 | "set host:port for inspector", | |||
| 280 | &DebugOptions::host_port, | |||
| 281 | kAllowedInEnvironment); | |||
| 282 | AddAlias("--debug-port", "--inspect-port"); | |||
| 283 | ||||
| 284 | AddOption("--inspect", | |||
| 285 | "activate inspector on host:port (default: 127.0.0.1:9229)", | |||
| 286 | &DebugOptions::inspector_enabled, | |||
| 287 | kAllowedInEnvironment); | |||
| 288 | AddAlias("--inspect=", { "--inspect-port", "--inspect" }); | |||
| 289 | ||||
| 290 | AddOption("--debug", "", &DebugOptions::deprecated_debug); | |||
| 291 | AddAlias("--debug=", "--debug"); | |||
| 292 | AddOption("--debug-brk", "", &DebugOptions::deprecated_debug); | |||
| 293 | AddAlias("--debug-brk=", "--debug-brk"); | |||
| 294 | ||||
| 295 | AddOption("--inspect-brk", | |||
| 296 | "activate inspector on host:port and break at start of user script", | |||
| 297 | &DebugOptions::break_first_line, | |||
| 298 | kAllowedInEnvironment); | |||
| 299 | Implies("--inspect-brk", "--inspect"); | |||
| 300 | AddAlias("--inspect-brk=", { "--inspect-port", "--inspect-brk" }); | |||
| 301 | ||||
| 302 | AddOption("--inspect-brk-node", "", &DebugOptions::break_node_first_line); | |||
| 303 | Implies("--inspect-brk-node", "--inspect"); | |||
| 304 | AddAlias("--inspect-brk-node=", { "--inspect-port", "--inspect-brk-node" }); | |||
| 305 | ||||
| 306 | AddOption("--inspect-publish-uid", | |||
| 307 | "comma separated list of destinations for inspector uid" | |||
| 308 | "(default: stderr,http)", | |||
| 309 | &DebugOptions::inspect_publish_uid_string, | |||
| 310 | kAllowedInEnvironment); | |||
| 311 | } | |||
| 312 | ||||
| 313 | EnvironmentOptionsParser::EnvironmentOptionsParser() { | |||
| 314 | AddOption("--conditions", | |||
| 315 | "additional user conditions for conditional exports and imports", | |||
| 316 | &EnvironmentOptions::conditions, | |||
| 317 | kAllowedInEnvironment); | |||
| 318 | AddAlias("-C", "--conditions"); | |||
| 319 | AddOption("--diagnostic-dir", | |||
| 320 | "set dir for all output files" | |||
| 321 | " (default: current working directory)", | |||
| 322 | &EnvironmentOptions::diagnostic_dir, | |||
| 323 | kAllowedInEnvironment); | |||
| 324 | AddOption("--dns-result-order", | |||
| 325 | "set default value of verbatim in dns.lookup. Options are " | |||
| 326 | "'ipv4first' (IPv4 addresses are placed before IPv6 addresses) " | |||
| 327 | "'verbatim' (addresses are in the order the DNS resolver " | |||
| 328 | "returned)", | |||
| 329 | &EnvironmentOptions::dns_result_order, | |||
| 330 | kAllowedInEnvironment); | |||
| 331 | AddOption("--enable-source-maps", | |||
| 332 | "Source Map V3 support for stack traces", | |||
| 333 | &EnvironmentOptions::enable_source_maps, | |||
| 334 | kAllowedInEnvironment); | |||
| 335 | AddOption("--experimental-abortcontroller", "", | |||
| 336 | NoOp{}, kAllowedInEnvironment); | |||
| 337 | AddOption("--experimental-fetch", | |||
| 338 | "experimental Fetch API", | |||
| 339 | &EnvironmentOptions::experimental_fetch, | |||
| 340 | kAllowedInEnvironment, | |||
| 341 | true); | |||
| 342 | AddOption("--experimental-global-webcrypto", | |||
| 343 | "expose experimental Web Crypto API on the global scope", | |||
| 344 | &EnvironmentOptions::experimental_global_web_crypto, | |||
| 345 | kAllowedInEnvironment); | |||
| 346 | AddOption("--experimental-json-modules", "", NoOp{}, kAllowedInEnvironment); | |||
| 347 | AddOption("--experimental-loader", | |||
| 348 | "use the specified module as a custom loader", | |||
| 349 | &EnvironmentOptions::userland_loaders, | |||
| 350 | kAllowedInEnvironment); | |||
| 351 | AddAlias("--loader", "--experimental-loader"); | |||
| 352 | AddOption("--experimental-modules", "", NoOp{}, kAllowedInEnvironment); | |||
| 353 | AddOption("--experimental-network-imports", | |||
| 354 | "experimental https: support for the ES Module loader", | |||
| 355 | &EnvironmentOptions::experimental_https_modules, | |||
| 356 | kAllowedInEnvironment); | |||
| 357 | AddOption("--experimental-wasm-modules", | |||
| 358 | "experimental ES Module support for webassembly modules", | |||
| 359 | &EnvironmentOptions::experimental_wasm_modules, | |||
| 360 | kAllowedInEnvironment); | |||
| 361 | AddOption("--experimental-import-meta-resolve", | |||
| 362 | "experimental ES Module import.meta.resolve() support", | |||
| 363 | &EnvironmentOptions::experimental_import_meta_resolve, | |||
| 364 | kAllowedInEnvironment); | |||
| 365 | AddOption("--experimental-policy", | |||
| 366 | "use the specified file as a " | |||
| 367 | "security policy", | |||
| 368 | &EnvironmentOptions::experimental_policy, | |||
| 369 | kAllowedInEnvironment); | |||
| 370 | AddOption("[has_policy_integrity_string]", | |||
| 371 | "", | |||
| 372 | &EnvironmentOptions::has_policy_integrity_string); | |||
| 373 | AddOption("--policy-integrity", | |||
| 374 | "ensure the security policy contents match " | |||
| 375 | "the specified integrity", | |||
| 376 | &EnvironmentOptions::experimental_policy_integrity, | |||
| 377 | kAllowedInEnvironment); | |||
| 378 | Implies("--policy-integrity", "[has_policy_integrity_string]"); | |||
| 379 | AddOption("--experimental-repl-await", | |||
| 380 | "experimental await keyword support in REPL", | |||
| 381 | &EnvironmentOptions::experimental_repl_await, | |||
| 382 | kAllowedInEnvironment, | |||
| 383 | true); | |||
| 384 | AddOption("--experimental-vm-modules", | |||
| 385 | "experimental ES Module support in vm module", | |||
| 386 | &EnvironmentOptions::experimental_vm_modules, | |||
| 387 | kAllowedInEnvironment); | |||
| 388 | AddOption("--experimental-worker", "", NoOp{}, kAllowedInEnvironment); | |||
| 389 | AddOption("--experimental-report", "", NoOp{}, kAllowedInEnvironment); | |||
| 390 | AddOption("--experimental-wasi-unstable-preview1", | |||
| 391 | "experimental WASI support", | |||
| 392 | &EnvironmentOptions::experimental_wasi, | |||
| 393 | kAllowedInEnvironment); | |||
| 394 | AddOption("--expose-internals", "", &EnvironmentOptions::expose_internals); | |||
| 395 | AddOption("--frozen-intrinsics", | |||
| 396 | "experimental frozen intrinsics support", | |||
| 397 | &EnvironmentOptions::frozen_intrinsics, | |||
| 398 | kAllowedInEnvironment); | |||
| 399 | AddOption("--heapsnapshot-signal", | |||
| 400 | "Generate heap snapshot on specified signal", | |||
| 401 | &EnvironmentOptions::heap_snapshot_signal, | |||
| 402 | kAllowedInEnvironment); | |||
| 403 | AddOption("--heapsnapshot-near-heap-limit", | |||
| 404 | "Generate heap snapshots whenever V8 is approaching " | |||
| 405 | "the heap limit. No more than the specified number of " | |||
| 406 | "heap snapshots will be generated.", | |||
| 407 | &EnvironmentOptions::heap_snapshot_near_heap_limit, | |||
| 408 | kAllowedInEnvironment); | |||
| 409 | AddOption("--http-parser", "", NoOp{}, kAllowedInEnvironment); | |||
| 410 | AddOption("--insecure-http-parser", | |||
| 411 | "use an insecure HTTP parser that accepts invalid HTTP headers", | |||
| 412 | &EnvironmentOptions::insecure_http_parser, | |||
| 413 | kAllowedInEnvironment); | |||
| 414 | AddOption("--input-type", | |||
| 415 | "set module type for string input", | |||
| 416 | &EnvironmentOptions::module_type, | |||
| 417 | kAllowedInEnvironment); | |||
| 418 | AddOption("--experimental-specifier-resolution", | |||
| 419 | "Select extension resolution algorithm for es modules; " | |||
| 420 | "either 'explicit' (default) or 'node'", | |||
| 421 | &EnvironmentOptions::experimental_specifier_resolution, | |||
| 422 | kAllowedInEnvironment); | |||
| 423 | AddAlias("--es-module-specifier-resolution", | |||
| 424 | "--experimental-specifier-resolution"); | |||
| 425 | AddOption("--deprecation", | |||
| 426 | "silence deprecation warnings", | |||
| 427 | &EnvironmentOptions::deprecation, | |||
| 428 | kAllowedInEnvironment, | |||
| 429 | true); | |||
| 430 | AddOption("--force-async-hooks-checks", | |||
| 431 | "disable checks for async_hooks", | |||
| 432 | &EnvironmentOptions::force_async_hooks_checks, | |||
| 433 | kAllowedInEnvironment, | |||
| 434 | true); | |||
| 435 | AddOption( | |||
| 436 | "--force-node-api-uncaught-exceptions-policy", | |||
| 437 | "enforces 'uncaughtException' event on Node API asynchronous callbacks", | |||
| 438 | &EnvironmentOptions::force_node_api_uncaught_exceptions_policy, | |||
| 439 | kAllowedInEnvironment, | |||
| 440 | false); | |||
| 441 | AddOption("--addons", | |||
| 442 | "disable loading native addons", | |||
| 443 | &EnvironmentOptions::allow_native_addons, | |||
| 444 | kAllowedInEnvironment, | |||
| 445 | true); | |||
| 446 | AddOption("--global-search-paths", | |||
| 447 | "disable global module search paths", | |||
| 448 | &EnvironmentOptions::global_search_paths, | |||
| 449 | kAllowedInEnvironment, | |||
| 450 | true); | |||
| 451 | AddOption("--warnings", | |||
| 452 | "silence all process warnings", | |||
| 453 | &EnvironmentOptions::warnings, | |||
| 454 | kAllowedInEnvironment, | |||
| 455 | true); | |||
| 456 | AddOption("--force-context-aware", | |||
| 457 | "disable loading non-context-aware addons", | |||
| 458 | &EnvironmentOptions::force_context_aware, | |||
| 459 | kAllowedInEnvironment); | |||
| 460 | AddOption("--pending-deprecation", | |||
| 461 | "emit pending deprecation warnings", | |||
| 462 | &EnvironmentOptions::pending_deprecation, | |||
| 463 | kAllowedInEnvironment); | |||
| 464 | AddOption("--preserve-symlinks", | |||
| 465 | "preserve symbolic links when resolving", | |||
| 466 | &EnvironmentOptions::preserve_symlinks, | |||
| 467 | kAllowedInEnvironment); | |||
| 468 | AddOption("--preserve-symlinks-main", | |||
| 469 | "preserve symbolic links when resolving the main module", | |||
| 470 | &EnvironmentOptions::preserve_symlinks_main, | |||
| 471 | kAllowedInEnvironment); | |||
| 472 | AddOption("--prof", | |||
| 473 | "Generate V8 profiler output.", | |||
| 474 | V8Option{}); | |||
| 475 | AddOption("--prof-process", | |||
| 476 | "process V8 profiler output generated using --prof", | |||
| 477 | &EnvironmentOptions::prof_process); | |||
| 478 | // Options after --prof-process are passed through to the prof processor. | |||
| 479 | AddAlias("--prof-process", { "--prof-process", "--" }); | |||
| 480 | #if HAVE_INSPECTOR1 | |||
| 481 | AddOption("--cpu-prof", | |||
| 482 | "Start the V8 CPU profiler on start up, and write the CPU profile " | |||
| 483 | "to disk before exit. If --cpu-prof-dir is not specified, write " | |||
| 484 | "the profile to the current working directory.", | |||
| 485 | &EnvironmentOptions::cpu_prof); | |||
| 486 | AddOption("--cpu-prof-name", | |||
| 487 | "specified file name of the V8 CPU profile generated with " | |||
| 488 | "--cpu-prof", | |||
| 489 | &EnvironmentOptions::cpu_prof_name); | |||
| 490 | AddOption("--cpu-prof-interval", | |||
| 491 | "specified sampling interval in microseconds for the V8 CPU " | |||
| 492 | "profile generated with --cpu-prof. (default: 1000)", | |||
| 493 | &EnvironmentOptions::cpu_prof_interval); | |||
| 494 | AddOption("--cpu-prof-dir", | |||
| 495 | "Directory where the V8 profiles generated by --cpu-prof will be " | |||
| 496 | "placed. Does not affect --prof.", | |||
| 497 | &EnvironmentOptions::cpu_prof_dir); | |||
| 498 | AddOption( | |||
| 499 | "--heap-prof", | |||
| 500 | "Start the V8 heap profiler on start up, and write the heap profile " | |||
| 501 | "to disk before exit. If --heap-prof-dir is not specified, write " | |||
| 502 | "the profile to the current working directory.", | |||
| 503 | &EnvironmentOptions::heap_prof); | |||
| 504 | AddOption("--heap-prof-name", | |||
| 505 | "specified file name of the V8 heap profile generated with " | |||
| 506 | "--heap-prof", | |||
| 507 | &EnvironmentOptions::heap_prof_name); | |||
| 508 | AddOption("--heap-prof-dir", | |||
| 509 | "Directory where the V8 heap profiles generated by --heap-prof " | |||
| 510 | "will be placed.", | |||
| 511 | &EnvironmentOptions::heap_prof_dir); | |||
| 512 | AddOption("--heap-prof-interval", | |||
| 513 | "specified sampling interval in bytes for the V8 heap " | |||
| 514 | "profile generated with --heap-prof. (default: 512 * 1024)", | |||
| 515 | &EnvironmentOptions::heap_prof_interval); | |||
| 516 | #endif // HAVE_INSPECTOR | |||
| 517 | AddOption("--max-http-header-size", | |||
| 518 | "set the maximum size of HTTP headers (default: 16384 (16KB))", | |||
| 519 | &EnvironmentOptions::max_http_header_size, | |||
| 520 | kAllowedInEnvironment); | |||
| 521 | AddOption("--redirect-warnings", | |||
| 522 | "write warnings to file instead of stderr", | |||
| 523 | &EnvironmentOptions::redirect_warnings, | |||
| 524 | kAllowedInEnvironment); | |||
| 525 | AddOption("--test", | |||
| 526 | "launch test runner on startup", | |||
| 527 | &EnvironmentOptions::test_runner); | |||
| 528 | AddOption("--test-only", | |||
| 529 | "run tests with 'only' option set", | |||
| 530 | &EnvironmentOptions::test_only, | |||
| 531 | kAllowedInEnvironment); | |||
| 532 | AddOption("--test-udp-no-try-send", "", // For testing only. | |||
| 533 | &EnvironmentOptions::test_udp_no_try_send); | |||
| 534 | AddOption("--throw-deprecation", | |||
| 535 | "throw an exception on deprecations", | |||
| 536 | &EnvironmentOptions::throw_deprecation, | |||
| 537 | kAllowedInEnvironment); | |||
| 538 | AddOption("--trace-atomics-wait", | |||
| 539 | "trace Atomics.wait() operations", | |||
| 540 | &EnvironmentOptions::trace_atomics_wait, | |||
| 541 | kAllowedInEnvironment); | |||
| 542 | AddOption("--trace-deprecation", | |||
| 543 | "show stack traces on deprecations", | |||
| 544 | &EnvironmentOptions::trace_deprecation, | |||
| 545 | kAllowedInEnvironment); | |||
| 546 | AddOption("--trace-exit", | |||
| 547 | "show stack trace when an environment exits", | |||
| 548 | &EnvironmentOptions::trace_exit, | |||
| 549 | kAllowedInEnvironment); | |||
| 550 | AddOption("--trace-sync-io", | |||
| 551 | "show stack trace when use of sync IO is detected after the " | |||
| 552 | "first tick", | |||
| 553 | &EnvironmentOptions::trace_sync_io, | |||
| 554 | kAllowedInEnvironment); | |||
| 555 | AddOption("--trace-tls", | |||
| 556 | "prints TLS packet trace information to stderr", | |||
| 557 | &EnvironmentOptions::trace_tls, | |||
| 558 | kAllowedInEnvironment); | |||
| 559 | AddOption("--trace-uncaught", | |||
| 560 | "show stack traces for the `throw` behind uncaught exceptions", | |||
| 561 | &EnvironmentOptions::trace_uncaught, | |||
| 562 | kAllowedInEnvironment); | |||
| 563 | AddOption("--trace-warnings", | |||
| 564 | "show stack traces on process warnings", | |||
| 565 | &EnvironmentOptions::trace_warnings, | |||
| 566 | kAllowedInEnvironment); | |||
| 567 | AddOption("--extra-info-on-fatal-exception", | |||
| 568 | "hide extra information on fatal exception that causes exit", | |||
| 569 | &EnvironmentOptions::extra_info_on_fatal_exception, | |||
| 570 | kAllowedInEnvironment, | |||
| 571 | true); | |||
| 572 | AddOption("--unhandled-rejections", | |||
| 573 | "define unhandled rejections behavior. Options are 'strict' " | |||
| 574 | "(always raise an error), 'throw' (raise an error unless " | |||
| 575 | "'unhandledRejection' hook is set), 'warn' (log a warning), 'none' " | |||
| 576 | "(silence warnings), 'warn-with-error-code' (log a warning and set " | |||
| 577 | "exit code 1 unless 'unhandledRejection' hook is set). (default: " | |||
| 578 | "throw)", | |||
| 579 | &EnvironmentOptions::unhandled_rejections, | |||
| 580 | kAllowedInEnvironment); | |||
| 581 | AddOption("--verify-base-objects", | |||
| 582 | "", /* undocumented, only for debugging */ | |||
| 583 | &EnvironmentOptions::verify_base_objects, | |||
| 584 | kAllowedInEnvironment); | |||
| 585 | ||||
| 586 | AddOption("--check", | |||
| 587 | "syntax check script without executing", | |||
| 588 | &EnvironmentOptions::syntax_check_only); | |||
| 589 | AddAlias("-c", "--check"); | |||
| 590 | // This option is only so that we can tell --eval with an empty string from | |||
| 591 | // no eval at all. Having it not start with a dash makes it inaccessible | |||
| 592 | // from the parser itself, but available for using Implies(). | |||
| 593 | // TODO(addaleax): When moving --help over to something generated from the | |||
| 594 | // programmatic descriptions, this will need some special care. | |||
| 595 | // (See also [ssl_openssl_cert_store] below.) | |||
| 596 | AddOption("[has_eval_string]", "", &EnvironmentOptions::has_eval_string); | |||
| 597 | AddOption("--eval", "evaluate script", &EnvironmentOptions::eval_string); | |||
| 598 | Implies("--eval", "[has_eval_string]"); | |||
| 599 | AddOption("--print", | |||
| 600 | "evaluate script and print result", | |||
| 601 | &EnvironmentOptions::print_eval); | |||
| 602 | AddAlias("-e", "--eval"); | |||
| 603 | AddAlias("--print <arg>", "-pe"); | |||
| 604 | AddAlias("-pe", { "--print", "--eval" }); | |||
| 605 | AddAlias("-p", "--print"); | |||
| 606 | AddOption("--require", | |||
| 607 | "module to preload (option can be repeated)", | |||
| 608 | &EnvironmentOptions::preload_modules, | |||
| 609 | kAllowedInEnvironment); | |||
| 610 | AddAlias("-r", "--require"); | |||
| 611 | AddOption("--interactive", | |||
| 612 | "always enter the REPL even if stdin does not appear " | |||
| 613 | "to be a terminal", | |||
| 614 | &EnvironmentOptions::force_repl); | |||
| 615 | AddAlias("-i", "--interactive"); | |||
| 616 | ||||
| 617 | AddOption("--napi-modules", "", NoOp{}, kAllowedInEnvironment); | |||
| 618 | ||||
| 619 | AddOption("--tls-keylog", | |||
| 620 | "log TLS decryption keys to named file for traffic analysis", | |||
| 621 | &EnvironmentOptions::tls_keylog, kAllowedInEnvironment); | |||
| 622 | ||||
| 623 | AddOption("--tls-min-v1.0", | |||
| 624 | "set default TLS minimum to TLSv1.0 (default: TLSv1.2)", | |||
| 625 | &EnvironmentOptions::tls_min_v1_0, | |||
| 626 | kAllowedInEnvironment); | |||
| 627 | AddOption("--tls-min-v1.1", | |||
| 628 | "set default TLS minimum to TLSv1.1 (default: TLSv1.2)", | |||
| 629 | &EnvironmentOptions::tls_min_v1_1, | |||
| 630 | kAllowedInEnvironment); | |||
| 631 | AddOption("--tls-min-v1.2", | |||
| 632 | "set default TLS minimum to TLSv1.2 (default: TLSv1.2)", | |||
| 633 | &EnvironmentOptions::tls_min_v1_2, | |||
| 634 | kAllowedInEnvironment); | |||
| 635 | AddOption("--tls-min-v1.3", | |||
| 636 | "set default TLS minimum to TLSv1.3 (default: TLSv1.2)", | |||
| 637 | &EnvironmentOptions::tls_min_v1_3, | |||
| 638 | kAllowedInEnvironment); | |||
| 639 | AddOption("--tls-max-v1.2", | |||
| 640 | "set default TLS maximum to TLSv1.2 (default: TLSv1.3)", | |||
| 641 | &EnvironmentOptions::tls_max_v1_2, | |||
| 642 | kAllowedInEnvironment); | |||
| 643 | // Current plan is: | |||
| 644 | // - 11.x and below: TLS1.3 is opt-in with --tls-max-v1.3 | |||
| 645 | // - 12.x: TLS1.3 is opt-out with --tls-max-v1.2 | |||
| 646 | // In either case, support both options they are uniformly available. | |||
| 647 | AddOption("--tls-max-v1.3", | |||
| 648 | "set default TLS maximum to TLSv1.3 (default: TLSv1.3)", | |||
| 649 | &EnvironmentOptions::tls_max_v1_3, | |||
| 650 | kAllowedInEnvironment); | |||
| 651 | } | |||
| 652 | ||||
| 653 | PerIsolateOptionsParser::PerIsolateOptionsParser( | |||
| 654 | const EnvironmentOptionsParser& eop) { | |||
| 655 | AddOption("--track-heap-objects", | |||
| 656 | "track heap object allocations for heap snapshots", | |||
| 657 | &PerIsolateOptions::track_heap_objects, | |||
| 658 | kAllowedInEnvironment); | |||
| 659 | ||||
| 660 | // Explicitly add some V8 flags to mark them as allowed in NODE_OPTIONS. | |||
| 661 | AddOption("--abort-on-uncaught-exception", | |||
| 662 | "aborting instead of exiting causes a core file to be generated " | |||
| 663 | "for analysis", | |||
| 664 | V8Option{}, | |||
| 665 | kAllowedInEnvironment); | |||
| 666 | AddOption("--interpreted-frames-native-stack", | |||
| 667 | "help system profilers to translate JavaScript interpreted frames", | |||
| 668 | V8Option{}, kAllowedInEnvironment); | |||
| 669 | AddOption("--max-old-space-size", "", V8Option{}, kAllowedInEnvironment); | |||
| 670 | AddOption("--perf-basic-prof", "", V8Option{}, kAllowedInEnvironment); | |||
| 671 | AddOption("--perf-basic-prof-only-functions", | |||
| 672 | "", | |||
| 673 | V8Option{}, | |||
| 674 | kAllowedInEnvironment); | |||
| 675 | AddOption("--perf-prof", "", V8Option{}, kAllowedInEnvironment); | |||
| 676 | AddOption("--perf-prof-unwinding-info", | |||
| 677 | "", | |||
| 678 | V8Option{}, | |||
| 679 | kAllowedInEnvironment); | |||
| 680 | AddOption("--stack-trace-limit", "", V8Option{}, kAllowedInEnvironment); | |||
| 681 | AddOption("--disallow-code-generation-from-strings", | |||
| 682 | "disallow eval and friends", | |||
| 683 | V8Option{}, | |||
| 684 | kAllowedInEnvironment); | |||
| 685 | AddOption("--huge-max-old-generation-size", | |||
| 686 | "increase default maximum heap size on machines with 16GB memory " | |||
| 687 | "or more", | |||
| 688 | V8Option{}, | |||
| 689 | kAllowedInEnvironment); | |||
| 690 | AddOption("--jitless", | |||
| 691 | "disable runtime allocation of executable memory", | |||
| 692 | V8Option{}, | |||
| 693 | kAllowedInEnvironment); | |||
| 694 | AddOption("--report-uncaught-exception", | |||
| 695 | "generate diagnostic report on uncaught exceptions", | |||
| 696 | &PerIsolateOptions::report_uncaught_exception, | |||
| 697 | kAllowedInEnvironment); | |||
| 698 | AddOption("--report-on-signal", | |||
| 699 | "generate diagnostic report upon receiving signals", | |||
| 700 | &PerIsolateOptions::report_on_signal, | |||
| 701 | kAllowedInEnvironment); | |||
| 702 | AddOption("--report-signal", | |||
| 703 | "causes diagnostic report to be produced on provided signal," | |||
| 704 | " unsupported in Windows. (default: SIGUSR2)", | |||
| 705 | &PerIsolateOptions::report_signal, | |||
| 706 | kAllowedInEnvironment); | |||
| 707 | Implies("--report-signal", "--report-on-signal"); | |||
| 708 | ||||
| 709 | AddOption( | |||
| 710 | "--experimental-top-level-await", "", NoOp{}, kAllowedInEnvironment); | |||
| 711 | ||||
| 712 | Insert(eop, &PerIsolateOptions::get_per_env_options); | |||
| ||||
| 713 | } | |||
| 714 | ||||
| 715 | PerProcessOptionsParser::PerProcessOptionsParser( | |||
| 716 | const PerIsolateOptionsParser& iop) { | |||
| 717 | AddOption("--title", | |||
| 718 | "the process title to use on startup", | |||
| 719 | &PerProcessOptions::title, | |||
| 720 | kAllowedInEnvironment); | |||
| 721 | AddOption("--trace-event-categories", | |||
| 722 | "comma separated list of trace event categories to record", | |||
| 723 | &PerProcessOptions::trace_event_categories, | |||
| 724 | kAllowedInEnvironment); | |||
| 725 | AddOption("--trace-event-file-pattern", | |||
| 726 | "Template string specifying the filepath for the trace-events " | |||
| 727 | "data, it supports ${rotation} and ${pid}.", | |||
| 728 | &PerProcessOptions::trace_event_file_pattern, | |||
| 729 | kAllowedInEnvironment); | |||
| 730 | AddAlias("--trace-events-enabled", { | |||
| 731 | "--trace-event-categories", "v8,node,node.async_hooks" }); | |||
| 732 | AddOption("--v8-pool-size", | |||
| 733 | "set V8's thread pool size", | |||
| 734 | &PerProcessOptions::v8_thread_pool_size, | |||
| 735 | kAllowedInEnvironment); | |||
| 736 | AddOption("--zero-fill-buffers", | |||
| 737 | "automatically zero-fill all newly allocated Buffer and " | |||
| 738 | "SlowBuffer instances", | |||
| 739 | &PerProcessOptions::zero_fill_all_buffers, | |||
| 740 | kAllowedInEnvironment); | |||
| 741 | AddOption("--debug-arraybuffer-allocations", | |||
| 742 | "", /* undocumented, only for debugging */ | |||
| 743 | &PerProcessOptions::debug_arraybuffer_allocations, | |||
| 744 | kAllowedInEnvironment); | |||
| 745 | AddOption("--disable-proto", | |||
| 746 | "disable Object.prototype.__proto__", | |||
| 747 | &PerProcessOptions::disable_proto, | |||
| 748 | kAllowedInEnvironment); | |||
| 749 | AddOption("--build-snapshot", | |||
| 750 | "Generate a snapshot blob when the process exits." | |||
| 751 | "Currently only supported in the node_mksnapshot binary.", | |||
| 752 | &PerProcessOptions::build_snapshot, | |||
| 753 | kDisallowedInEnvironment); | |||
| 754 | AddOption("--node-snapshot", | |||
| 755 | "", // It's a debug-only option. | |||
| 756 | &PerProcessOptions::node_snapshot, | |||
| 757 | kAllowedInEnvironment); | |||
| 758 | // 12.x renamed this inadvertently, so alias it for consistency within the | |||
| 759 | // release line, while using the original name for consistency with older | |||
| 760 | // release lines. | |||
| 761 | AddOption("--security-revert", "", &PerProcessOptions::security_reverts); | |||
| 762 | AddAlias("--security-reverts", "--security-revert"); | |||
| 763 | AddOption("--completion-bash", | |||
| 764 | "print source-able bash completion script", | |||
| 765 | &PerProcessOptions::print_bash_completion); | |||
| 766 | AddOption("--help", | |||
| 767 | "print node command line options", | |||
| 768 | &PerProcessOptions::print_help); | |||
| 769 | AddAlias("-h", "--help"); | |||
| 770 | AddOption( | |||
| 771 | "--version", "print Node.js version", &PerProcessOptions::print_version); | |||
| 772 | AddAlias("-v", "--version"); | |||
| 773 | AddOption("--v8-options", | |||
| 774 | "print V8 command line options", | |||
| 775 | &PerProcessOptions::print_v8_help); | |||
| 776 | AddOption("--report-compact", | |||
| 777 | "output compact single-line JSON", | |||
| 778 | &PerProcessOptions::report_compact, | |||
| 779 | kAllowedInEnvironment); | |||
| 780 | AddOption("--report-dir", | |||
| 781 | "define custom report pathname." | |||
| 782 | " (default: current working directory)", | |||
| 783 | &PerProcessOptions::report_directory, | |||
| 784 | kAllowedInEnvironment); | |||
| 785 | AddAlias("--report-directory", "--report-dir"); | |||
| 786 | AddOption("--report-filename", | |||
| 787 | "define custom report file name." | |||
| 788 | " (default: YYYYMMDD.HHMMSS.PID.SEQUENCE#.txt)", | |||
| 789 | &PerProcessOptions::report_filename, | |||
| 790 | kAllowedInEnvironment); | |||
| 791 | AddOption("--report-on-fatalerror", | |||
| 792 | "generate diagnostic report on fatal (internal) errors", | |||
| 793 | &PerProcessOptions::report_on_fatalerror, | |||
| 794 | kAllowedInEnvironment); | |||
| 795 | ||||
| 796 | #ifdef NODE_HAVE_I18N_SUPPORT1 | |||
| 797 | AddOption("--icu-data-dir", | |||
| 798 | "set ICU data load path to dir (overrides NODE_ICU_DATA)" | |||
| 799 | #ifndef NODE_HAVE_SMALL_ICU | |||
| 800 | " (note: linked-in ICU data is present)" | |||
| 801 | #endif | |||
| 802 | , | |||
| 803 | &PerProcessOptions::icu_data_dir, | |||
| 804 | kAllowedInEnvironment); | |||
| 805 | #endif | |||
| 806 | ||||
| 807 | #if HAVE_OPENSSL1 | |||
| 808 | AddOption("--openssl-config", | |||
| 809 | "load OpenSSL configuration from the specified file " | |||
| 810 | "(overrides OPENSSL_CONF)", | |||
| 811 | &PerProcessOptions::openssl_config, | |||
| 812 | kAllowedInEnvironment); | |||
| 813 | AddOption("--tls-cipher-list", | |||
| 814 | "use an alternative default TLS cipher list", | |||
| 815 | &PerProcessOptions::tls_cipher_list, | |||
| 816 | kAllowedInEnvironment); | |||
| 817 | AddOption("--use-openssl-ca", | |||
| 818 | "use OpenSSL's default CA store" | |||
| 819 | #if defined(NODE_OPENSSL_CERT_STORE) | |||
| 820 | " (default)" | |||
| 821 | #endif | |||
| 822 | , | |||
| 823 | &PerProcessOptions::use_openssl_ca, | |||
| 824 | kAllowedInEnvironment); | |||
| 825 | AddOption("--use-bundled-ca", | |||
| 826 | "use bundled CA store" | |||
| 827 | #if !defined(NODE_OPENSSL_CERT_STORE) | |||
| 828 | " (default)" | |||
| 829 | #endif | |||
| 830 | , | |||
| 831 | &PerProcessOptions::use_bundled_ca, | |||
| 832 | kAllowedInEnvironment); | |||
| 833 | // Similar to [has_eval_string] above, except that the separation between | |||
| 834 | // this and use_openssl_ca only exists for option validation after parsing. | |||
| 835 | // This is not ideal. | |||
| 836 | AddOption("[ssl_openssl_cert_store]", | |||
| 837 | "", | |||
| 838 | &PerProcessOptions::ssl_openssl_cert_store); | |||
| 839 | Implies("--use-openssl-ca", "[ssl_openssl_cert_store]"); | |||
| 840 | ImpliesNot("--use-bundled-ca", "[ssl_openssl_cert_store]"); | |||
| 841 | AddOption("--enable-fips", | |||
| 842 | "enable FIPS crypto at startup", | |||
| 843 | &PerProcessOptions::enable_fips_crypto, | |||
| 844 | kAllowedInEnvironment); | |||
| 845 | AddOption("--force-fips", | |||
| 846 | "force FIPS crypto (cannot be disabled)", | |||
| 847 | &PerProcessOptions::force_fips_crypto, | |||
| 848 | kAllowedInEnvironment); | |||
| 849 | AddOption("--secure-heap", | |||
| 850 | "total size of the OpenSSL secure heap", | |||
| 851 | &PerProcessOptions::secure_heap, | |||
| 852 | kAllowedInEnvironment); | |||
| 853 | AddOption("--secure-heap-min", | |||
| 854 | "minimum allocation size from the OpenSSL secure heap", | |||
| 855 | &PerProcessOptions::secure_heap_min, | |||
| 856 | kAllowedInEnvironment); | |||
| 857 | #endif // HAVE_OPENSSL | |||
| 858 | #if OPENSSL_VERSION_MAJOR3 >= 3 | |||
| 859 | AddOption("--openssl-legacy-provider", | |||
| 860 | "enable OpenSSL 3.0 legacy provider", | |||
| 861 | &PerProcessOptions::openssl_legacy_provider, | |||
| 862 | kAllowedInEnvironment); | |||
| 863 | AddOption("--openssl-shared-config", | |||
| 864 | "enable OpenSSL shared configuration", | |||
| 865 | &PerProcessOptions::openssl_shared_config, | |||
| 866 | kAllowedInEnvironment); | |||
| 867 | ||||
| 868 | #endif // OPENSSL_VERSION_MAJOR | |||
| 869 | AddOption("--use-largepages", | |||
| 870 | "Map the Node.js static code to large pages. Options are " | |||
| 871 | "'off' (the default value, meaning do not map), " | |||
| 872 | "'on' (map and ignore failure, reporting it to stderr), " | |||
| 873 | "or 'silent' (map and silently ignore failure)", | |||
| 874 | &PerProcessOptions::use_largepages, | |||
| 875 | kAllowedInEnvironment); | |||
| 876 | ||||
| 877 | AddOption("--trace-sigint", | |||
| 878 | "enable printing JavaScript stacktrace on SIGINT", | |||
| 879 | &PerProcessOptions::trace_sigint, | |||
| 880 | kAllowedInEnvironment); | |||
| 881 | ||||
| 882 | Insert(iop, &PerProcessOptions::get_per_isolate_options); | |||
| 883 | ||||
| 884 | AddOption("--node-memory-debug", | |||
| 885 | "Run with extra debug checks for memory leaks in Node.js itself", | |||
| 886 | NoOp{}, kAllowedInEnvironment); | |||
| 887 | Implies("--node-memory-debug", "--debug-arraybuffer-allocations"); | |||
| 888 | Implies("--node-memory-debug", "--verify-base-objects"); | |||
| 889 | } | |||
| 890 | ||||
| 891 | inline std::string RemoveBrackets(const std::string& host) { | |||
| 892 | if (!host.empty() && host.front() == '[' && host.back() == ']') | |||
| 893 | return host.substr(1, host.size() - 2); | |||
| 894 | else | |||
| 895 | return host; | |||
| 896 | } | |||
| 897 | ||||
| 898 | inline int ParseAndValidatePort(const std::string& port, | |||
| 899 | std::vector<std::string>* errors) { | |||
| 900 | char* endptr; | |||
| 901 | errno(*__errno_location ()) = 0; | |||
| 902 | const unsigned long result = // NOLINT(runtime/int) | |||
| 903 | strtoul(port.c_str(), &endptr, 10); | |||
| 904 | if (errno(*__errno_location ()) != 0 || *endptr != '\0'|| | |||
| 905 | (result != 0 && result < 1024) || result > 65535) { | |||
| 906 | errors->push_back(" must be 0 or in range 1024 to 65535."); | |||
| 907 | } | |||
| 908 | return static_cast<int>(result); | |||
| 909 | } | |||
| 910 | ||||
| 911 | HostPort SplitHostPort(const std::string& arg, | |||
| 912 | std::vector<std::string>* errors) { | |||
| 913 | // remove_brackets only works if no port is specified | |||
| 914 | // so if it has an effect only an IPv6 address was specified. | |||
| 915 | std::string host = RemoveBrackets(arg); | |||
| 916 | if (host.length() < arg.length()) | |||
| 917 | return HostPort{host, DebugOptions::kDefaultInspectorPort}; | |||
| 918 | ||||
| 919 | size_t colon = arg.rfind(':'); | |||
| 920 | if (colon == std::string::npos) { | |||
| 921 | // Either a port number or a host name. Assume that | |||
| 922 | // if it's not all decimal digits, it's a host name. | |||
| 923 | for (char c : arg) { | |||
| 924 | if (c < '0' || c > '9') { | |||
| 925 | return HostPort{arg, DebugOptions::kDefaultInspectorPort}; | |||
| 926 | } | |||
| 927 | } | |||
| 928 | return HostPort { "", ParseAndValidatePort(arg, errors) }; | |||
| 929 | } | |||
| 930 | // Host and port found: | |||
| 931 | return HostPort { RemoveBrackets(arg.substr(0, colon)), | |||
| 932 | ParseAndValidatePort(arg.substr(colon + 1), errors) }; | |||
| 933 | } | |||
| 934 | ||||
| 935 | std::string GetBashCompletion() { | |||
| 936 | Mutex::ScopedLock lock(per_process::cli_options_mutex); | |||
| 937 | const auto& parser = _ppop_instance; | |||
| 938 | ||||
| 939 | std::ostringstream out; | |||
| 940 | ||||
| 941 | out << "_node_complete() {\n" | |||
| 942 | " local cur_word options\n" | |||
| 943 | " cur_word=\"${COMP_WORDS[COMP_CWORD]}\"\n" | |||
| 944 | " if [[ \"${cur_word}\" == -* ]] ; then\n" | |||
| 945 | " COMPREPLY=( $(compgen -W '"; | |||
| 946 | ||||
| 947 | for (const auto& item : parser.options_) { | |||
| 948 | if (item.first[0] != '[') { | |||
| 949 | out << item.first << " "; | |||
| 950 | } | |||
| 951 | } | |||
| 952 | for (const auto& item : parser.aliases_) { | |||
| 953 | if (item.first[0] != '[') { | |||
| 954 | out << item.first << " "; | |||
| 955 | } | |||
| 956 | } | |||
| 957 | if (parser.aliases_.size() > 0) { | |||
| 958 | out.seekp(-1, out.cur); // Strip the trailing space | |||
| 959 | } | |||
| 960 | ||||
| 961 | out << "' -- \"${cur_word}\") )\n" | |||
| 962 | " return 0\n" | |||
| 963 | " else\n" | |||
| 964 | " COMPREPLY=( $(compgen -f \"${cur_word}\") )\n" | |||
| 965 | " return 0\n" | |||
| 966 | " fi\n" | |||
| 967 | "}\n" | |||
| 968 | "complete -o filenames -o nospace -o bashdefault " | |||
| 969 | "-F _node_complete node node_g"; | |||
| 970 | return out.str(); | |||
| 971 | } | |||
| 972 | ||||
| 973 | // Return a map containing all the options and their metadata as well | |||
| 974 | // as the aliases | |||
| 975 | void GetCLIOptions(const FunctionCallbackInfo<Value>& args) { | |||
| 976 | Mutex::ScopedLock lock(per_process::cli_options_mutex); | |||
| 977 | Environment* env = Environment::GetCurrent(args); | |||
| 978 | if (!env->has_run_bootstrapping_code()) { | |||
| 979 | // No code because this is an assertion. | |||
| 980 | return env->ThrowError( | |||
| 981 | "Should not query options before bootstrapping is done"); | |||
| 982 | } | |||
| 983 | env->set_has_serialized_options(true); | |||
| 984 | ||||
| 985 | Isolate* isolate = env->isolate(); | |||
| 986 | Local<Context> context = env->context(); | |||
| 987 | ||||
| 988 | // Temporarily act as if the current Environment's/IsolateData's options were | |||
| 989 | // the default options, i.e. like they are the ones we'd access for global | |||
| 990 | // options parsing, so that all options are available from the main parser. | |||
| 991 | auto original_per_isolate = per_process::cli_options->per_isolate; | |||
| 992 | per_process::cli_options->per_isolate = env->isolate_data()->options(); | |||
| 993 | auto original_per_env = per_process::cli_options->per_isolate->per_env; | |||
| 994 | per_process::cli_options->per_isolate->per_env = env->options(); | |||
| 995 | auto on_scope_leave = OnScopeLeave([&]() { | |||
| 996 | per_process::cli_options->per_isolate->per_env = original_per_env; | |||
| 997 | per_process::cli_options->per_isolate = original_per_isolate; | |||
| 998 | }); | |||
| 999 | ||||
| 1000 | Local<Map> options = Map::New(isolate); | |||
| 1001 | if (options | |||
| 1002 | ->SetPrototype(context, env->primordials_safe_map_prototype_object()) | |||
| 1003 | .IsNothing()) { | |||
| 1004 | return; | |||
| 1005 | } | |||
| 1006 | ||||
| 1007 | for (const auto& item : _ppop_instance.options_) { | |||
| 1008 | Local<Value> value; | |||
| 1009 | const auto& option_info = item.second; | |||
| 1010 | auto field = option_info.field; | |||
| 1011 | PerProcessOptions* opts = per_process::cli_options.get(); | |||
| 1012 | switch (option_info.type) { | |||
| 1013 | case kNoOp: | |||
| 1014 | case kV8Option: | |||
| 1015 | // Special case for --abort-on-uncaught-exception which is also | |||
| 1016 | // respected by Node.js internals | |||
| 1017 | if (item.first == "--abort-on-uncaught-exception") { | |||
| 1018 | value = Boolean::New( | |||
| 1019 | isolate, original_per_env->abort_on_uncaught_exception); | |||
| 1020 | } else { | |||
| 1021 | value = Undefined(isolate); | |||
| 1022 | } | |||
| 1023 | break; | |||
| 1024 | case kBoolean: | |||
| 1025 | value = Boolean::New(isolate, | |||
| 1026 | *_ppop_instance.Lookup<bool>(field, opts)); | |||
| 1027 | break; | |||
| 1028 | case kInteger: | |||
| 1029 | value = Number::New( | |||
| 1030 | isolate, | |||
| 1031 | static_cast<double>(*_ppop_instance.Lookup<int64_t>(field, opts))); | |||
| 1032 | break; | |||
| 1033 | case kUInteger: | |||
| 1034 | value = Number::New( | |||
| 1035 | isolate, | |||
| 1036 | static_cast<double>(*_ppop_instance.Lookup<uint64_t>(field, opts))); | |||
| 1037 | break; | |||
| 1038 | case kString: | |||
| 1039 | if (!ToV8Value(context, | |||
| 1040 | *_ppop_instance.Lookup<std::string>(field, opts)) | |||
| 1041 | .ToLocal(&value)) { | |||
| 1042 | return; | |||
| 1043 | } | |||
| 1044 | break; | |||
| 1045 | case kStringList: | |||
| 1046 | if (!ToV8Value(context, | |||
| 1047 | *_ppop_instance.Lookup<StringVector>(field, opts)) | |||
| 1048 | .ToLocal(&value)) { | |||
| 1049 | return; | |||
| 1050 | } | |||
| 1051 | break; | |||
| 1052 | case kHostPort: { | |||
| 1053 | const HostPort& host_port = | |||
| 1054 | *_ppop_instance.Lookup<HostPort>(field, opts); | |||
| 1055 | Local<Object> obj = Object::New(isolate); | |||
| 1056 | Local<Value> host; | |||
| 1057 | if (!ToV8Value(context, host_port.host()).ToLocal(&host) || | |||
| 1058 | obj->Set(context, env->host_string(), host).IsNothing() || | |||
| 1059 | obj->Set(context, | |||
| 1060 | env->port_string(), | |||
| 1061 | Integer::New(isolate, host_port.port())) | |||
| 1062 | .IsNothing()) { | |||
| 1063 | return; | |||
| 1064 | } | |||
| 1065 | value = obj; | |||
| 1066 | break; | |||
| 1067 | } | |||
| 1068 | default: | |||
| 1069 | UNREACHABLE()do { static const node::AssertionInfo args = { "../src/node_options.cc" ":" "1069", "\"Unreachable code reached\"", __PRETTY_FUNCTION__ }; node::Assert(args); } while (0); | |||
| 1070 | } | |||
| 1071 | CHECK(!value.IsEmpty())do { if (__builtin_expect(!!(!(!value.IsEmpty())), 0)) { do { static const node::AssertionInfo args = { "../src/node_options.cc" ":" "1071", "!value.IsEmpty()", __PRETTY_FUNCTION__ }; node:: Assert(args); } while (0); } } while (0); | |||
| 1072 | ||||
| 1073 | Local<Value> name = ToV8Value(context, item.first).ToLocalChecked(); | |||
| 1074 | Local<Object> info = Object::New(isolate); | |||
| 1075 | Local<Value> help_text; | |||
| 1076 | if (!ToV8Value(context, option_info.help_text).ToLocal(&help_text) || | |||
| 1077 | !info->Set(context, env->help_text_string(), help_text) | |||
| 1078 | .FromMaybe(false) || | |||
| 1079 | !info->Set(context, | |||
| 1080 | env->env_var_settings_string(), | |||
| 1081 | Integer::New(isolate, | |||
| 1082 | static_cast<int>(option_info.env_setting))) | |||
| 1083 | .FromMaybe(false) || | |||
| 1084 | !info->Set(context, | |||
| 1085 | env->type_string(), | |||
| 1086 | Integer::New(isolate, static_cast<int>(option_info.type))) | |||
| 1087 | .FromMaybe(false) || | |||
| 1088 | !info->Set(context, | |||
| 1089 | env->default_is_true_string(), | |||
| 1090 | Boolean::New(isolate, option_info.default_is_true)) | |||
| 1091 | .FromMaybe(false) || | |||
| 1092 | info->Set(context, env->value_string(), value).IsNothing() || | |||
| 1093 | options->Set(context, name, info).IsEmpty()) { | |||
| 1094 | return; | |||
| 1095 | } | |||
| 1096 | } | |||
| 1097 | ||||
| 1098 | Local<Value> aliases; | |||
| 1099 | if (!ToV8Value(context, _ppop_instance.aliases_).ToLocal(&aliases)) return; | |||
| 1100 | ||||
| 1101 | if (aliases.As<Object>() | |||
| 1102 | ->SetPrototype(context, env->primordials_safe_map_prototype_object()) | |||
| 1103 | .IsNothing()) { | |||
| 1104 | return; | |||
| 1105 | } | |||
| 1106 | ||||
| 1107 | Local<Object> ret = Object::New(isolate); | |||
| 1108 | if (ret->Set(context, env->options_string(), options).IsNothing() || | |||
| 1109 | ret->Set(context, env->aliases_string(), aliases).IsNothing()) { | |||
| 1110 | return; | |||
| 1111 | } | |||
| 1112 | ||||
| 1113 | args.GetReturnValue().Set(ret); | |||
| 1114 | } | |||
| 1115 | ||||
| 1116 | void GetEmbedderOptions(const FunctionCallbackInfo<Value>& args) { | |||
| 1117 | Environment* env = Environment::GetCurrent(args); | |||
| 1118 | if (!env->has_run_bootstrapping_code()) { | |||
| 1119 | // No code because this is an assertion. | |||
| 1120 | return env->ThrowError( | |||
| 1121 | "Should not query options before bootstrapping is done"); | |||
| 1122 | } | |||
| 1123 | Isolate* isolate = args.GetIsolate(); | |||
| 1124 | Local<Context> context = env->context(); | |||
| 1125 | Local<Object> ret = Object::New(isolate); | |||
| 1126 | ||||
| 1127 | if (ret->Set(context, | |||
| 1128 | FIXED_ONE_BYTE_STRING(env->isolate(), "shouldNotRegisterESMLoader"), | |||
| 1129 | Boolean::New(isolate, env->should_not_register_esm_loader())) | |||
| 1130 | .IsNothing()) return; | |||
| 1131 | ||||
| 1132 | if (ret->Set(context, | |||
| 1133 | FIXED_ONE_BYTE_STRING(env->isolate(), "noGlobalSearchPaths"), | |||
| 1134 | Boolean::New(isolate, env->no_global_search_paths())) | |||
| 1135 | .IsNothing()) return; | |||
| 1136 | ||||
| 1137 | args.GetReturnValue().Set(ret); | |||
| 1138 | } | |||
| 1139 | ||||
| 1140 | void Initialize(Local<Object> target, | |||
| 1141 | Local<Value> unused, | |||
| 1142 | Local<Context> context, | |||
| 1143 | void* priv) { | |||
| 1144 | Environment* env = Environment::GetCurrent(context); | |||
| 1145 | Isolate* isolate = env->isolate(); | |||
| 1146 | env->SetMethodNoSideEffect(target, "getCLIOptions", GetCLIOptions); | |||
| 1147 | env->SetMethodNoSideEffect(target, "getEmbedderOptions", GetEmbedderOptions); | |||
| 1148 | ||||
| 1149 | Local<Object> env_settings = Object::New(isolate); | |||
| 1150 | NODE_DEFINE_CONSTANT(env_settings, kAllowedInEnvironment)do { v8::Isolate* isolate = env_settings->GetIsolate(); v8 ::Local<v8::Context> context = isolate->GetCurrentContext (); v8::Local<v8::String> constant_name = v8::String::NewFromUtf8 (isolate, "kAllowedInEnvironment", v8::NewStringType::kInternalized ).ToLocalChecked(); v8::Local<v8::Number> constant_value = v8::Number::New(isolate, static_cast<double>(kAllowedInEnvironment )); v8::PropertyAttribute constant_attributes = static_cast< v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete); (env_settings )->DefineOwnProperty(context, constant_name, constant_value , constant_attributes).Check(); } while (0); | |||
| 1151 | NODE_DEFINE_CONSTANT(env_settings, kDisallowedInEnvironment)do { v8::Isolate* isolate = env_settings->GetIsolate(); v8 ::Local<v8::Context> context = isolate->GetCurrentContext (); v8::Local<v8::String> constant_name = v8::String::NewFromUtf8 (isolate, "kDisallowedInEnvironment", v8::NewStringType::kInternalized ).ToLocalChecked(); v8::Local<v8::Number> constant_value = v8::Number::New(isolate, static_cast<double>(kDisallowedInEnvironment )); v8::PropertyAttribute constant_attributes = static_cast< v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete); (env_settings )->DefineOwnProperty(context, constant_name, constant_value , constant_attributes).Check(); } while (0); | |||
| 1152 | target | |||
| 1153 | ->Set( | |||
| 1154 | context, FIXED_ONE_BYTE_STRING(isolate, "envSettings"), env_settings) | |||
| 1155 | .Check(); | |||
| 1156 | ||||
| 1157 | Local<Object> types = Object::New(isolate); | |||
| 1158 | NODE_DEFINE_CONSTANT(types, kNoOp)do { v8::Isolate* isolate = types->GetIsolate(); v8::Local <v8::Context> context = isolate->GetCurrentContext() ; v8::Local<v8::String> constant_name = v8::String::NewFromUtf8 (isolate, "kNoOp", v8::NewStringType::kInternalized).ToLocalChecked (); v8::Local<v8::Number> constant_value = v8::Number:: New(isolate, static_cast<double>(kNoOp)); v8::PropertyAttribute constant_attributes = static_cast<v8::PropertyAttribute> (v8::ReadOnly | v8::DontDelete); (types)->DefineOwnProperty (context, constant_name, constant_value, constant_attributes) .Check(); } while (0); | |||
| 1159 | NODE_DEFINE_CONSTANT(types, kV8Option)do { v8::Isolate* isolate = types->GetIsolate(); v8::Local <v8::Context> context = isolate->GetCurrentContext() ; v8::Local<v8::String> constant_name = v8::String::NewFromUtf8 (isolate, "kV8Option", v8::NewStringType::kInternalized).ToLocalChecked (); v8::Local<v8::Number> constant_value = v8::Number:: New(isolate, static_cast<double>(kV8Option)); v8::PropertyAttribute constant_attributes = static_cast<v8::PropertyAttribute> (v8::ReadOnly | v8::DontDelete); (types)->DefineOwnProperty (context, constant_name, constant_value, constant_attributes) .Check(); } while (0); | |||
| 1160 | NODE_DEFINE_CONSTANT(types, kBoolean)do { v8::Isolate* isolate = types->GetIsolate(); v8::Local <v8::Context> context = isolate->GetCurrentContext() ; v8::Local<v8::String> constant_name = v8::String::NewFromUtf8 (isolate, "kBoolean", v8::NewStringType::kInternalized).ToLocalChecked (); v8::Local<v8::Number> constant_value = v8::Number:: New(isolate, static_cast<double>(kBoolean)); v8::PropertyAttribute constant_attributes = static_cast<v8::PropertyAttribute> (v8::ReadOnly | v8::DontDelete); (types)->DefineOwnProperty (context, constant_name, constant_value, constant_attributes) .Check(); } while (0); | |||
| 1161 | NODE_DEFINE_CONSTANT(types, kInteger)do { v8::Isolate* isolate = types->GetIsolate(); v8::Local <v8::Context> context = isolate->GetCurrentContext() ; v8::Local<v8::String> constant_name = v8::String::NewFromUtf8 (isolate, "kInteger", v8::NewStringType::kInternalized).ToLocalChecked (); v8::Local<v8::Number> constant_value = v8::Number:: New(isolate, static_cast<double>(kInteger)); v8::PropertyAttribute constant_attributes = static_cast<v8::PropertyAttribute> (v8::ReadOnly | v8::DontDelete); (types)->DefineOwnProperty (context, constant_name, constant_value, constant_attributes) .Check(); } while (0); | |||
| 1162 | NODE_DEFINE_CONSTANT(types, kUInteger)do { v8::Isolate* isolate = types->GetIsolate(); v8::Local <v8::Context> context = isolate->GetCurrentContext() ; v8::Local<v8::String> constant_name = v8::String::NewFromUtf8 (isolate, "kUInteger", v8::NewStringType::kInternalized).ToLocalChecked (); v8::Local<v8::Number> constant_value = v8::Number:: New(isolate, static_cast<double>(kUInteger)); v8::PropertyAttribute constant_attributes = static_cast<v8::PropertyAttribute> (v8::ReadOnly | v8::DontDelete); (types)->DefineOwnProperty (context, constant_name, constant_value, constant_attributes) .Check(); } while (0); | |||
| 1163 | NODE_DEFINE_CONSTANT(types, kString)do { v8::Isolate* isolate = types->GetIsolate(); v8::Local <v8::Context> context = isolate->GetCurrentContext() ; v8::Local<v8::String> constant_name = v8::String::NewFromUtf8 (isolate, "kString", v8::NewStringType::kInternalized).ToLocalChecked (); v8::Local<v8::Number> constant_value = v8::Number:: New(isolate, static_cast<double>(kString)); v8::PropertyAttribute constant_attributes = static_cast<v8::PropertyAttribute> (v8::ReadOnly | v8::DontDelete); (types)->DefineOwnProperty (context, constant_name, constant_value, constant_attributes) .Check(); } while (0); | |||
| 1164 | NODE_DEFINE_CONSTANT(types, kHostPort)do { v8::Isolate* isolate = types->GetIsolate(); v8::Local <v8::Context> context = isolate->GetCurrentContext() ; v8::Local<v8::String> constant_name = v8::String::NewFromUtf8 (isolate, "kHostPort", v8::NewStringType::kInternalized).ToLocalChecked (); v8::Local<v8::Number> constant_value = v8::Number:: New(isolate, static_cast<double>(kHostPort)); v8::PropertyAttribute constant_attributes = static_cast<v8::PropertyAttribute> (v8::ReadOnly | v8::DontDelete); (types)->DefineOwnProperty (context, constant_name, constant_value, constant_attributes) .Check(); } while (0); | |||
| 1165 | NODE_DEFINE_CONSTANT(types, kStringList)do { v8::Isolate* isolate = types->GetIsolate(); v8::Local <v8::Context> context = isolate->GetCurrentContext() ; v8::Local<v8::String> constant_name = v8::String::NewFromUtf8 (isolate, "kStringList", v8::NewStringType::kInternalized).ToLocalChecked (); v8::Local<v8::Number> constant_value = v8::Number:: New(isolate, static_cast<double>(kStringList)); v8::PropertyAttribute constant_attributes = static_cast<v8::PropertyAttribute> (v8::ReadOnly | v8::DontDelete); (types)->DefineOwnProperty (context, constant_name, constant_value, constant_attributes) .Check(); } while (0); | |||
| 1166 | target->Set(context, FIXED_ONE_BYTE_STRING(isolate, "types"), types) | |||
| 1167 | .Check(); | |||
| 1168 | } | |||
| 1169 | ||||
| 1170 | void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | |||
| 1171 | registry->Register(GetCLIOptions); | |||
| 1172 | registry->Register(GetEmbedderOptions); | |||
| 1173 | } | |||
| 1174 | } // namespace options_parser | |||
| 1175 | ||||
| 1176 | void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options) { | |||
| 1177 | HandleEnvOptions(env_options, [](const char* name) { | |||
| 1178 | std::string text; | |||
| 1179 | return credentials::SafeGetenv(name, &text) ? text : ""; | |||
| 1180 | }); | |||
| 1181 | } | |||
| 1182 | ||||
| 1183 | void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options, | |||
| 1184 | std::function<std::string(const char*)> opt_getter) { | |||
| 1185 | env_options->pending_deprecation = | |||
| 1186 | opt_getter("NODE_PENDING_DEPRECATION") == "1"; | |||
| 1187 | ||||
| 1188 | env_options->preserve_symlinks = opt_getter("NODE_PRESERVE_SYMLINKS") == "1"; | |||
| 1189 | ||||
| 1190 | env_options->preserve_symlinks_main = | |||
| 1191 | opt_getter("NODE_PRESERVE_SYMLINKS_MAIN") == "1"; | |||
| 1192 | ||||
| 1193 | if (env_options->redirect_warnings.empty()) | |||
| 1194 | env_options->redirect_warnings = opt_getter("NODE_REDIRECT_WARNINGS"); | |||
| 1195 | } | |||
| 1196 | ||||
| 1197 | std::vector<std::string> ParseNodeOptionsEnvVar( | |||
| 1198 | const std::string& node_options, std::vector<std::string>* errors) { | |||
| 1199 | std::vector<std::string> env_argv; | |||
| 1200 | ||||
| 1201 | bool is_in_string = false; | |||
| 1202 | bool will_start_new_arg = true; | |||
| 1203 | for (std::string::size_type index = 0; index < node_options.size(); ++index) { | |||
| 1204 | char c = node_options.at(index); | |||
| 1205 | ||||
| 1206 | // Backslashes escape the following character | |||
| 1207 | if (c == '\\' && is_in_string) { | |||
| 1208 | if (index + 1 == node_options.size()) { | |||
| 1209 | errors->push_back("invalid value for NODE_OPTIONS " | |||
| 1210 | "(invalid escape)\n"); | |||
| 1211 | return env_argv; | |||
| 1212 | } else { | |||
| 1213 | c = node_options.at(++index); | |||
| 1214 | } | |||
| 1215 | } else if (c == ' ' && !is_in_string) { | |||
| 1216 | will_start_new_arg = true; | |||
| 1217 | continue; | |||
| 1218 | } else if (c == '"') { | |||
| 1219 | is_in_string = !is_in_string; | |||
| 1220 | continue; | |||
| 1221 | } | |||
| 1222 | ||||
| 1223 | if (will_start_new_arg) { | |||
| 1224 | env_argv.emplace_back(std::string(1, c)); | |||
| 1225 | will_start_new_arg = false; | |||
| 1226 | } else { | |||
| 1227 | env_argv.back() += c; | |||
| 1228 | } | |||
| 1229 | } | |||
| 1230 | ||||
| 1231 | if (is_in_string) { | |||
| 1232 | errors->push_back("invalid value for NODE_OPTIONS " | |||
| 1233 | "(unterminated string)\n"); | |||
| 1234 | } | |||
| 1235 | return env_argv; | |||
| 1236 | } | |||
| 1237 | } // namespace node | |||
| 1238 | ||||
| 1239 | NODE_MODULE_CONTEXT_AWARE_INTERNAL(options, node::options_parser::Initialize)static node::node_module _module = { 108, NM_F_INTERNAL, nullptr , "../src/node_options.cc", nullptr, (node::addon_context_register_func )(node::options_parser::Initialize), "options", nullptr, nullptr }; void _register_options() { node_module_register(&_module ); } | |||
| 1240 | NODE_MODULE_EXTERNAL_REFERENCE(options,void _register_external_reference_options( node::ExternalReferenceRegistry * registry) { node::options_parser::RegisterExternalReferences (registry); } | |||
| 1241 | node::options_parser::RegisterExternalReferences)void _register_external_reference_options( node::ExternalReferenceRegistry * registry) { node::options_parser::RegisterExternalReferences (registry); } |
| 1 | #ifndef SRC_NODE_OPTIONS_INL_H_ | |||
| 2 | #define SRC_NODE_OPTIONS_INL_H_ | |||
| 3 | ||||
| 4 | #if defined(NODE_WANT_INTERNALS1) && NODE_WANT_INTERNALS1 | |||
| 5 | ||||
| 6 | #include <cstdlib> | |||
| 7 | #include "node_options.h" | |||
| 8 | #include "util.h" | |||
| 9 | ||||
| 10 | namespace node { | |||
| 11 | ||||
| 12 | PerIsolateOptions* PerProcessOptions::get_per_isolate_options() { | |||
| 13 | return per_isolate.get(); | |||
| 14 | } | |||
| 15 | ||||
| 16 | EnvironmentOptions* PerIsolateOptions::get_per_env_options() { | |||
| 17 | return per_env.get(); | |||
| 18 | } | |||
| 19 | ||||
| 20 | namespace options_parser { | |||
| 21 | ||||
| 22 | template <typename Options> | |||
| 23 | void OptionsParser<Options>::AddOption(const char* name, | |||
| 24 | const char* help_text, | |||
| 25 | bool Options::* field, | |||
| 26 | OptionEnvvarSettings env_setting, | |||
| 27 | bool default_is_true) { | |||
| 28 | options_.emplace(name, | |||
| 29 | OptionInfo{kBoolean, | |||
| 30 | std::make_shared<SimpleOptionField<bool>>(field), | |||
| 31 | env_setting, | |||
| 32 | help_text, | |||
| 33 | default_is_true}); | |||
| 34 | } | |||
| 35 | ||||
| 36 | template <typename Options> | |||
| 37 | void OptionsParser<Options>::AddOption(const char* name, | |||
| 38 | const char* help_text, | |||
| 39 | uint64_t Options::* field, | |||
| 40 | OptionEnvvarSettings env_setting) { | |||
| 41 | options_.emplace( | |||
| 42 | name, | |||
| 43 | OptionInfo{kUInteger, | |||
| 44 | std::make_shared<SimpleOptionField<uint64_t>>(field), | |||
| 45 | env_setting, | |||
| 46 | help_text}); | |||
| 47 | } | |||
| 48 | ||||
| 49 | template <typename Options> | |||
| 50 | void OptionsParser<Options>::AddOption(const char* name, | |||
| 51 | const char* help_text, | |||
| 52 | int64_t Options::* field, | |||
| 53 | OptionEnvvarSettings env_setting) { | |||
| 54 | options_.emplace( | |||
| 55 | name, | |||
| 56 | OptionInfo{kInteger, | |||
| 57 | std::make_shared<SimpleOptionField<int64_t>>(field), | |||
| 58 | env_setting, | |||
| 59 | help_text}); | |||
| 60 | } | |||
| 61 | ||||
| 62 | template <typename Options> | |||
| 63 | void OptionsParser<Options>::AddOption(const char* name, | |||
| 64 | const char* help_text, | |||
| 65 | std::string Options::* field, | |||
| 66 | OptionEnvvarSettings env_setting) { | |||
| 67 | options_.emplace( | |||
| 68 | name, | |||
| 69 | OptionInfo{kString, | |||
| 70 | std::make_shared<SimpleOptionField<std::string>>(field), | |||
| 71 | env_setting, | |||
| 72 | help_text}); | |||
| 73 | } | |||
| 74 | ||||
| 75 | template <typename Options> | |||
| 76 | void OptionsParser<Options>::AddOption( | |||
| 77 | const char* name, | |||
| 78 | const char* help_text, | |||
| 79 | std::vector<std::string> Options::* field, | |||
| 80 | OptionEnvvarSettings env_setting) { | |||
| 81 | options_.emplace(name, OptionInfo { | |||
| 82 | kStringList, | |||
| 83 | std::make_shared<SimpleOptionField<std::vector<std::string>>>(field), | |||
| 84 | env_setting, | |||
| 85 | help_text | |||
| 86 | }); | |||
| 87 | } | |||
| 88 | ||||
| 89 | template <typename Options> | |||
| 90 | void OptionsParser<Options>::AddOption(const char* name, | |||
| 91 | const char* help_text, | |||
| 92 | HostPort Options::* field, | |||
| 93 | OptionEnvvarSettings env_setting) { | |||
| 94 | options_.emplace( | |||
| 95 | name, | |||
| 96 | OptionInfo{kHostPort, | |||
| 97 | std::make_shared<SimpleOptionField<HostPort>>(field), | |||
| 98 | env_setting, | |||
| 99 | help_text}); | |||
| 100 | } | |||
| 101 | ||||
| 102 | template <typename Options> | |||
| 103 | void OptionsParser<Options>::AddOption(const char* name, | |||
| 104 | const char* help_text, | |||
| 105 | NoOp no_op_tag, | |||
| 106 | OptionEnvvarSettings env_setting) { | |||
| 107 | options_.emplace(name, OptionInfo{kNoOp, nullptr, env_setting, help_text}); | |||
| 108 | } | |||
| 109 | ||||
| 110 | template <typename Options> | |||
| 111 | void OptionsParser<Options>::AddOption(const char* name, | |||
| 112 | const char* help_text, | |||
| 113 | V8Option v8_option_tag, | |||
| 114 | OptionEnvvarSettings env_setting) { | |||
| 115 | options_.emplace(name, | |||
| 116 | OptionInfo{kV8Option, nullptr, env_setting, help_text}); | |||
| 117 | } | |||
| 118 | ||||
| 119 | template <typename Options> | |||
| 120 | void OptionsParser<Options>::AddAlias(const char* from, | |||
| 121 | const char* to) { | |||
| 122 | aliases_[from] = { to }; | |||
| 123 | } | |||
| 124 | ||||
| 125 | template <typename Options> | |||
| 126 | void OptionsParser<Options>::AddAlias(const char* from, | |||
| 127 | const std::vector<std::string>& to) { | |||
| 128 | aliases_[from] = to; | |||
| 129 | } | |||
| 130 | ||||
| 131 | template <typename Options> | |||
| 132 | void OptionsParser<Options>::AddAlias( | |||
| 133 | const char* from, | |||
| 134 | const std::initializer_list<std::string>& to) { | |||
| 135 | AddAlias(from, std::vector<std::string>(to)); | |||
| 136 | } | |||
| 137 | ||||
| 138 | template <typename Options> | |||
| 139 | void OptionsParser<Options>::Implies(const char* from, | |||
| 140 | const char* to) { | |||
| 141 | auto it = options_.find(to); | |||
| 142 | CHECK_NE(it, options_.end())do { if (__builtin_expect(!!(!((it) != (options_.end()))), 0) ) { do { static const node::AssertionInfo args = { "../src/node_options-inl.h" ":" "142", "(it) != (options_.end())", __PRETTY_FUNCTION__ } ; node::Assert(args); } while (0); } } while (0); | |||
| 143 | CHECK(it->second.type == kBoolean || it->second.type == kV8Option)do { if (__builtin_expect(!!(!(it->second.type == kBoolean || it->second.type == kV8Option)), 0)) { do { static const node::AssertionInfo args = { "../src/node_options-inl.h" ":" "143", "it->second.type == kBoolean || it->second.type == kV8Option" , __PRETTY_FUNCTION__ }; node::Assert(args); } while (0); } } while (0); | |||
| 144 | implications_.emplace( | |||
| 145 | from, Implication{it->second.type, to, it->second.field, true}); | |||
| 146 | } | |||
| 147 | ||||
| 148 | template <typename Options> | |||
| 149 | void OptionsParser<Options>::ImpliesNot(const char* from, | |||
| 150 | const char* to) { | |||
| 151 | auto it = options_.find(to); | |||
| 152 | CHECK_NE(it, options_.end())do { if (__builtin_expect(!!(!((it) != (options_.end()))), 0) ) { do { static const node::AssertionInfo args = { "../src/node_options-inl.h" ":" "152", "(it) != (options_.end())", __PRETTY_FUNCTION__ } ; node::Assert(args); } while (0); } } while (0); | |||
| 153 | CHECK_EQ(it->second.type, kBoolean)do { if (__builtin_expect(!!(!((it->second.type) == (kBoolean ))), 0)) { do { static const node::AssertionInfo args = { "../src/node_options-inl.h" ":" "153", "(it->second.type) == (kBoolean)", __PRETTY_FUNCTION__ }; node::Assert(args); } while (0); } } while (0); | |||
| 154 | implications_.emplace( | |||
| 155 | from, Implication{it->second.type, to, it->second.field, false}); | |||
| 156 | } | |||
| 157 | ||||
| 158 | template <typename Options> | |||
| 159 | template <typename OriginalField, typename ChildOptions> | |||
| 160 | auto OptionsParser<Options>::Convert( | |||
| 161 | std::shared_ptr<OriginalField> original, | |||
| 162 | ChildOptions* (Options::* get_child)()) { | |||
| 163 | // If we have a field on ChildOptions, and we want to access it from an | |||
| 164 | // Options instance, we call get_child() on the original Options and then | |||
| 165 | // access it, i.e. this class implements a kind of function chaining. | |||
| 166 | struct AdaptedField : BaseOptionField { | |||
| 167 | void* LookupImpl(Options* options) const override { | |||
| 168 | return original->LookupImpl((options->*get_child)()); | |||
| 169 | } | |||
| 170 | ||||
| 171 | AdaptedField( | |||
| 172 | std::shared_ptr<OriginalField> original, | |||
| 173 | ChildOptions* (Options::* get_child)()) | |||
| 174 | : original(original), get_child(get_child) {} | |||
| 175 | ||||
| 176 | std::shared_ptr<OriginalField> original; | |||
| 177 | ChildOptions* (Options::* get_child)(); | |||
| 178 | }; | |||
| 179 | ||||
| 180 | return std::shared_ptr<BaseOptionField>( | |||
| 181 | new AdaptedField(original, get_child)); | |||
| 182 | } | |||
| 183 | template <typename Options> | |||
| 184 | template <typename ChildOptions> | |||
| 185 | auto OptionsParser<Options>::Convert( | |||
| 186 | typename OptionsParser<ChildOptions>::OptionInfo original, | |||
| 187 | ChildOptions* (Options::* get_child)()) { | |||
| 188 | return OptionInfo{original.type, | |||
| 189 | Convert(original.field, get_child), | |||
| 190 | original.env_setting, | |||
| 191 | original.help_text, | |||
| 192 | original.default_is_true}; | |||
| 193 | } | |||
| ||||
| 194 | ||||
| 195 | template <typename Options> | |||
| 196 | template <typename ChildOptions> | |||
| 197 | auto OptionsParser<Options>::Convert( | |||
| 198 | typename OptionsParser<ChildOptions>::Implication original, | |||
| 199 | ChildOptions* (Options::* get_child)()) { | |||
| 200 | return Implication{ | |||
| 201 | original.type, | |||
| 202 | original.name, | |||
| 203 | Convert(original.target_field, get_child), | |||
| 204 | original.target_value, | |||
| 205 | }; | |||
| 206 | } | |||
| 207 | ||||
| 208 | template <typename Options> | |||
| 209 | template <typename ChildOptions> | |||
| 210 | void OptionsParser<Options>::Insert( | |||
| 211 | const OptionsParser<ChildOptions>& child_options_parser, | |||
| 212 | ChildOptions* (Options::* get_child)()) { | |||
| 213 | aliases_.insert(std::begin(child_options_parser.aliases_), | |||
| 214 | std::end(child_options_parser.aliases_)); | |||
| 215 | ||||
| 216 | for (const auto& pair : child_options_parser.options_) | |||
| 217 | options_.emplace(pair.first, Convert(pair.second, get_child)); | |||
| 218 | ||||
| 219 | for (const auto& pair : child_options_parser.implications_) | |||
| 220 | implications_.emplace(pair.first, Convert(pair.second, get_child)); | |||
| 221 | } | |||
| 222 | ||||
| 223 | inline std::string NotAllowedInEnvErr(const std::string& arg) { | |||
| 224 | return arg + " is not allowed in NODE_OPTIONS"; | |||
| 225 | } | |||
| 226 | ||||
| 227 | inline std::string RequiresArgumentErr(const std::string& arg) { | |||
| 228 | return arg + " requires an argument"; | |||
| 229 | } | |||
| 230 | ||||
| 231 | inline std::string NegationImpliesBooleanError(const std::string& arg) { | |||
| 232 | return arg + " is an invalid negation because it is not a boolean option"; | |||
| 233 | } | |||
| 234 | ||||
| 235 | // We store some of the basic information around a single Parse call inside | |||
| 236 | // this struct, to separate storage of command line arguments and their | |||
| 237 | // handling. In particular, this makes it easier to introduce 'synthetic' | |||
| 238 | // arguments that get inserted by expanding option aliases. | |||
| 239 | struct ArgsInfo { | |||
| 240 | // Generally, the idea here is that the first entry in `*underlying` stores | |||
| 241 | // the "0th" argument (the program name), then `synthetic_args` are inserted, | |||
| 242 | // followed by the remainder of `*underlying`. | |||
| 243 | std::vector<std::string>* underlying; | |||
| 244 | std::vector<std::string> synthetic_args; | |||
| 245 | ||||
| 246 | std::vector<std::string>* exec_args; | |||
| 247 | ||||
| 248 | ArgsInfo(std::vector<std::string>* args, | |||
| 249 | std::vector<std::string>* exec_args) | |||
| 250 | : underlying(args), exec_args(exec_args) {} | |||
| 251 | ||||
| 252 | size_t remaining() const { | |||
| 253 | // -1 to account for the program name. | |||
| 254 | return underlying->size() - 1 + synthetic_args.size(); | |||
| 255 | } | |||
| 256 | ||||
| 257 | bool empty() const { return remaining() == 0; } | |||
| 258 | const std::string& program_name() const { return underlying->at(0); } | |||
| 259 | ||||
| 260 | std::string& first() { | |||
| 261 | return synthetic_args.empty() ? underlying->at(1) : synthetic_args.front(); | |||
| 262 | } | |||
| 263 | ||||
| 264 | std::string pop_first() { | |||
| 265 | std::string ret = std::move(first()); | |||
| 266 | if (synthetic_args.empty()) { | |||
| 267 | // Only push arguments to `exec_args` that were also originally passed | |||
| 268 | // on the command line (i.e. not generated through alias expansion). | |||
| 269 | // '--' is a special case here since its purpose is to end `exec_argv`, | |||
| 270 | // which is why we do not include it. | |||
| 271 | if (exec_args != nullptr && ret != "--") | |||
| 272 | exec_args->push_back(ret); | |||
| 273 | underlying->erase(underlying->begin() + 1); | |||
| 274 | } else { | |||
| 275 | synthetic_args.erase(synthetic_args.begin()); | |||
| 276 | } | |||
| 277 | return ret; | |||
| 278 | } | |||
| 279 | }; | |||
| 280 | ||||
| 281 | template <typename Options> | |||
| 282 | void OptionsParser<Options>::Parse( | |||
| 283 | std::vector<std::string>* const orig_args, | |||
| 284 | std::vector<std::string>* const exec_args, | |||
| 285 | std::vector<std::string>* const v8_args, | |||
| 286 | Options* const options, | |||
| 287 | OptionEnvvarSettings required_env_settings, | |||
| 288 | std::vector<std::string>* const errors) const { | |||
| 289 | ArgsInfo args(orig_args, exec_args); | |||
| 290 | ||||
| 291 | // The first entry is the process name. Make sure it ends up in the V8 argv, | |||
| 292 | // since V8::SetFlagsFromCommandLine() expects that to hold true for that | |||
| 293 | // array as well. | |||
| 294 | if (v8_args->empty()) | |||
| 295 | v8_args->push_back(args.program_name()); | |||
| 296 | ||||
| 297 | while (!args.empty() && errors->empty()) { | |||
| 298 | if (args.first().size() <= 1 || args.first()[0] != '-') break; | |||
| 299 | ||||
| 300 | // We know that we're either going to consume this | |||
| 301 | // argument or fail completely. | |||
| 302 | const std::string arg = args.pop_first(); | |||
| 303 | ||||
| 304 | if (arg == "--") { | |||
| 305 | if (required_env_settings == kAllowedInEnvironment) | |||
| 306 | errors->push_back(NotAllowedInEnvErr("--")); | |||
| 307 | break; | |||
| 308 | } | |||
| 309 | ||||
| 310 | // Only allow --foo=bar notation for options starting with double dashes. | |||
| 311 | // (E.g. -e=a is not allowed as shorthand for --eval=a, which would | |||
| 312 | // otherwise be the result of alias expansion.) | |||
| 313 | const std::string::size_type equals_index = | |||
| 314 | arg[0] == '-' && arg[1] == '-' ? arg.find('=') : std::string::npos; | |||
| 315 | std::string name = | |||
| 316 | equals_index == std::string::npos ? arg : arg.substr(0, equals_index); | |||
| 317 | ||||
| 318 | // Store the 'original name' of the argument. This name differs from | |||
| 319 | // 'name' in that it contains a possible '=' sign and is not affected | |||
| 320 | // by alias expansion. | |||
| 321 | std::string original_name = name; | |||
| 322 | if (equals_index != std::string::npos) | |||
| 323 | original_name += '='; | |||
| 324 | ||||
| 325 | auto missing_argument = [&]() { | |||
| 326 | errors->push_back(RequiresArgumentErr(original_name)); | |||
| 327 | }; | |||
| 328 | ||||
| 329 | // Normalize by replacing `_` with `-` in options. | |||
| 330 | for (std::string::size_type i = 2; i < name.size(); ++i) { | |||
| 331 | if (name[i] == '_') | |||
| 332 | name[i] = '-'; | |||
| 333 | } | |||
| 334 | ||||
| 335 | // Convert --no-foo to --foo and keep in mind that we're negating. | |||
| 336 | bool is_negation = false; | |||
| 337 | if (name.find("--no-") == 0) { | |||
| 338 | name.erase(2, 3); // remove no- | |||
| 339 | is_negation = true; | |||
| 340 | } | |||
| 341 | ||||
| 342 | { | |||
| 343 | auto it = aliases_.end(); | |||
| 344 | // Expand aliases: | |||
| 345 | // - If `name` can be found in `aliases_`. | |||
| 346 | // - If `name` + '=' can be found in `aliases_`. | |||
| 347 | // - If `name` + " <arg>" can be found in `aliases_`, and we have | |||
| 348 | // a subsequent argument that does not start with '-' itself. | |||
| 349 | while ((it = aliases_.find(name)) != aliases_.end() || | |||
| 350 | (equals_index != std::string::npos && | |||
| 351 | (it = aliases_.find(name + '=')) != aliases_.end()) || | |||
| 352 | (!args.empty() && | |||
| 353 | !args.first().empty() && | |||
| 354 | args.first()[0] != '-' && | |||
| 355 | (it = aliases_.find(name + " <arg>")) != aliases_.end())) { | |||
| 356 | const std::string prev_name = std::move(name); | |||
| 357 | const std::vector<std::string>& expansion = it->second; | |||
| 358 | ||||
| 359 | // Use the first entry in the expansion as the new 'name'. | |||
| 360 | name = expansion.front(); | |||
| 361 | ||||
| 362 | if (expansion.size() > 1) { | |||
| 363 | // The other arguments, if any, are going to be handled later. | |||
| 364 | args.synthetic_args.insert( | |||
| 365 | args.synthetic_args.begin(), | |||
| 366 | expansion.begin() + 1, | |||
| 367 | expansion.end()); | |||
| 368 | } | |||
| 369 | ||||
| 370 | if (name == prev_name) break; | |||
| 371 | } | |||
| 372 | } | |||
| 373 | ||||
| 374 | auto it = options_.find(name); | |||
| 375 | ||||
| 376 | if ((it == options_.end() || | |||
| 377 | it->second.env_setting == kDisallowedInEnvironment) && | |||
| 378 | required_env_settings == kAllowedInEnvironment) { | |||
| 379 | errors->push_back(NotAllowedInEnvErr(original_name)); | |||
| 380 | break; | |||
| 381 | } | |||
| 382 | ||||
| 383 | { | |||
| 384 | std::string implied_name = name; | |||
| 385 | if (is_negation) { | |||
| 386 | // Implications for negated options are defined with "--no-". | |||
| 387 | implied_name.insert(2, "no-"); | |||
| 388 | } | |||
| 389 | auto implications = implications_.equal_range(implied_name); | |||
| 390 | for (auto it = implications.first; it != implications.second; ++it) { | |||
| 391 | if (it->second.type == kV8Option) { | |||
| 392 | v8_args->push_back(it->second.name); | |||
| 393 | } else { | |||
| 394 | *it->second.target_field->template Lookup<bool>(options) = | |||
| 395 | it->second.target_value; | |||
| 396 | } | |||
| 397 | } | |||
| 398 | } | |||
| 399 | ||||
| 400 | if (it == options_.end()) { | |||
| 401 | v8_args->push_back(arg); | |||
| 402 | continue; | |||
| 403 | } | |||
| 404 | ||||
| 405 | const OptionInfo& info = it->second; | |||
| 406 | ||||
| 407 | // Some V8 options can be negated and they are validated by V8 later. | |||
| 408 | if (is_negation && info.type != kBoolean && info.type != kV8Option) { | |||
| 409 | errors->push_back(NegationImpliesBooleanError(arg)); | |||
| 410 | break; | |||
| 411 | } | |||
| 412 | ||||
| 413 | std::string value; | |||
| 414 | if (info.type != kBoolean && info.type != kNoOp && info.type != kV8Option) { | |||
| 415 | if (equals_index != std::string::npos) { | |||
| 416 | value = arg.substr(equals_index + 1); | |||
| 417 | if (value.empty()) { | |||
| 418 | missing_argument(); | |||
| 419 | break; | |||
| 420 | } | |||
| 421 | } else { | |||
| 422 | if (args.empty()) { | |||
| 423 | missing_argument(); | |||
| 424 | break; | |||
| 425 | } | |||
| 426 | ||||
| 427 | value = args.pop_first(); | |||
| 428 | ||||
| 429 | if (!value.empty() && value[0] == '-') { | |||
| 430 | missing_argument(); | |||
| 431 | break; | |||
| 432 | } else { | |||
| 433 | if (!value.empty() && value[0] == '\\' && value[1] == '-') | |||
| 434 | value = value.substr(1); // Treat \- as escaping an -. | |||
| 435 | } | |||
| 436 | } | |||
| 437 | } | |||
| 438 | ||||
| 439 | switch (info.type) { | |||
| 440 | case kBoolean: | |||
| 441 | *Lookup<bool>(info.field, options) = !is_negation; | |||
| 442 | break; | |||
| 443 | case kInteger: | |||
| 444 | *Lookup<int64_t>(info.field, options) = std::atoll(value.c_str()); | |||
| 445 | break; | |||
| 446 | case kUInteger: | |||
| 447 | *Lookup<uint64_t>(info.field, options) = std::stoull(value); | |||
| 448 | break; | |||
| 449 | case kString: | |||
| 450 | *Lookup<std::string>(info.field, options) = value; | |||
| 451 | break; | |||
| 452 | case kStringList: | |||
| 453 | Lookup<std::vector<std::string>>(info.field, options) | |||
| 454 | ->emplace_back(std::move(value)); | |||
| 455 | break; | |||
| 456 | case kHostPort: | |||
| 457 | Lookup<HostPort>(info.field, options) | |||
| 458 | ->Update(SplitHostPort(value, errors)); | |||
| 459 | break; | |||
| 460 | case kNoOp: | |||
| 461 | break; | |||
| 462 | case kV8Option: | |||
| 463 | v8_args->push_back(arg); | |||
| 464 | break; | |||
| 465 | default: | |||
| 466 | UNREACHABLE()do { static const node::AssertionInfo args = { "../src/node_options-inl.h" ":" "466", "\"Unreachable code reached\"", __PRETTY_FUNCTION__ }; node::Assert(args); } while (0); | |||
| 467 | } | |||
| 468 | } | |||
| 469 | options->CheckOptions(errors); | |||
| 470 | } | |||
| 471 | ||||
| 472 | } // namespace options_parser | |||
| 473 | } // namespace node | |||
| 474 | ||||
| 475 | #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | |||
| 476 | ||||
| 477 | #endif // SRC_NODE_OPTIONS_INL_H_ |
| 1 | // shared_ptr and weak_ptr implementation -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 2007-2018 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // Under Section 7 of GPL version 3, you are granted additional |
| 17 | // permissions described in the GCC Runtime Library Exception, version |
| 18 | // 3.1, as published by the Free Software Foundation. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License and |
| 21 | // a copy of the GCC Runtime Library Exception along with this program; |
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 | // <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | // GCC Note: Based on files from version 1.32.0 of the Boost library. |
| 26 | |
| 27 | // shared_count.hpp |
| 28 | // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd. |
| 29 | |
| 30 | // shared_ptr.hpp |
| 31 | // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes. |
| 32 | // Copyright (C) 2001, 2002, 2003 Peter Dimov |
| 33 | |
| 34 | // weak_ptr.hpp |
| 35 | // Copyright (C) 2001, 2002, 2003 Peter Dimov |
| 36 | |
| 37 | // enable_shared_from_this.hpp |
| 38 | // Copyright (C) 2002 Peter Dimov |
| 39 | |
| 40 | // Distributed under the Boost Software License, Version 1.0. (See |
| 41 | // accompanying file LICENSE_1_0.txt or copy at |
| 42 | // http://www.boost.org/LICENSE_1_0.txt) |
| 43 | |
| 44 | /** @file |
| 45 | * This is an internal header file, included by other library headers. |
| 46 | * Do not attempt to use it directly. @headername{memory} |
| 47 | */ |
| 48 | |
| 49 | #ifndef _SHARED_PTR_H1 |
| 50 | #define _SHARED_PTR_H1 1 |
| 51 | |
| 52 | #include <bits/shared_ptr_base.h> |
| 53 | |
| 54 | namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
| 55 | { |
| 56 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 57 | |
| 58 | /** |
| 59 | * @addtogroup pointer_abstractions |
| 60 | * @{ |
| 61 | */ |
| 62 | |
| 63 | /// 20.7.2.2.11 shared_ptr I/O |
| 64 | template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp> |
| 65 | inline std::basic_ostream<_Ch, _Tr>& |
| 66 | operator<<(std::basic_ostream<_Ch, _Tr>& __os, |
| 67 | const __shared_ptr<_Tp, _Lp>& __p) |
| 68 | { |
| 69 | __os << __p.get(); |
| 70 | return __os; |
| 71 | } |
| 72 | |
| 73 | template<typename _Del, typename _Tp, _Lock_policy _Lp> |
| 74 | inline _Del* |
| 75 | get_deleter(const __shared_ptr<_Tp, _Lp>& __p) noexcept |
| 76 | { |
| 77 | #if __cpp_rtti |
| 78 | return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); |
| 79 | #else |
| 80 | return 0; |
| 81 | #endif |
| 82 | } |
| 83 | |
| 84 | /// 20.7.2.2.10 shared_ptr get_deleter |
| 85 | template<typename _Del, typename _Tp> |
| 86 | inline _Del* |
| 87 | get_deleter(const shared_ptr<_Tp>& __p) noexcept |
| 88 | { |
| 89 | #if __cpp_rtti |
| 90 | return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); |
| 91 | #else |
| 92 | return 0; |
| 93 | #endif |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @brief A smart pointer with reference-counted copy semantics. |
| 98 | * |
| 99 | * The object pointed to is deleted when the last shared_ptr pointing to |
| 100 | * it is destroyed or reset. |
| 101 | */ |
| 102 | template<typename _Tp> |
| 103 | class shared_ptr : public __shared_ptr<_Tp> |
| 104 | { |
| 105 | template<typename... _Args> |
| 106 | using _Constructible = typename enable_if< |
| 107 | is_constructible<__shared_ptr<_Tp>, _Args...>::value |
| 108 | >::type; |
| 109 | |
| 110 | template<typename _Arg> |
| 111 | using _Assignable = typename enable_if< |
| 112 | is_assignable<__shared_ptr<_Tp>&, _Arg>::value, shared_ptr& |
| 113 | >::type; |
| 114 | |
| 115 | public: |
| 116 | |
| 117 | using element_type = typename __shared_ptr<_Tp>::element_type; |
| 118 | |
| 119 | #if __cplusplus201703L > 201402L |
| 120 | # define __cpp_lib_shared_ptr_weak_type201606 201606 |
| 121 | using weak_type = weak_ptr<_Tp>; |
| 122 | #endif |
| 123 | /** |
| 124 | * @brief Construct an empty %shared_ptr. |
| 125 | * @post use_count()==0 && get()==0 |
| 126 | */ |
| 127 | constexpr shared_ptr() noexcept : __shared_ptr<_Tp>() { } |
| 128 | |
| 129 | shared_ptr(const shared_ptr&) noexcept = default; |
| 130 | |
| 131 | /** |
| 132 | * @brief Construct a %shared_ptr that owns the pointer @a __p. |
| 133 | * @param __p A pointer that is convertible to element_type*. |
| 134 | * @post use_count() == 1 && get() == __p |
| 135 | * @throw std::bad_alloc, in which case @c delete @a __p is called. |
| 136 | */ |
| 137 | template<typename _Yp, typename = _Constructible<_Yp*>> |
| 138 | explicit |
| 139 | shared_ptr(_Yp* __p) : __shared_ptr<_Tp>(__p) { } |
| 140 | |
| 141 | /** |
| 142 | * @brief Construct a %shared_ptr that owns the pointer @a __p |
| 143 | * and the deleter @a __d. |
| 144 | * @param __p A pointer. |
| 145 | * @param __d A deleter. |
| 146 | * @post use_count() == 1 && get() == __p |
| 147 | * @throw std::bad_alloc, in which case @a __d(__p) is called. |
| 148 | * |
| 149 | * Requirements: _Deleter's copy constructor and destructor must |
| 150 | * not throw |
| 151 | * |
| 152 | * __shared_ptr will release __p by calling __d(__p) |
| 153 | */ |
| 154 | template<typename _Yp, typename _Deleter, |
| 155 | typename = _Constructible<_Yp*, _Deleter>> |
| 156 | shared_ptr(_Yp* __p, _Deleter __d) |
| 157 | : __shared_ptr<_Tp>(__p, std::move(__d)) { } |
| 158 | |
| 159 | /** |
| 160 | * @brief Construct a %shared_ptr that owns a null pointer |
| 161 | * and the deleter @a __d. |
| 162 | * @param __p A null pointer constant. |
| 163 | * @param __d A deleter. |
| 164 | * @post use_count() == 1 && get() == __p |
| 165 | * @throw std::bad_alloc, in which case @a __d(__p) is called. |
| 166 | * |
| 167 | * Requirements: _Deleter's copy constructor and destructor must |
| 168 | * not throw |
| 169 | * |
| 170 | * The last owner will call __d(__p) |
| 171 | */ |
| 172 | template<typename _Deleter> |
| 173 | shared_ptr(nullptr_t __p, _Deleter __d) |
| 174 | : __shared_ptr<_Tp>(__p, std::move(__d)) { } |
| 175 | |
| 176 | /** |
| 177 | * @brief Construct a %shared_ptr that owns the pointer @a __p |
| 178 | * and the deleter @a __d. |
| 179 | * @param __p A pointer. |
| 180 | * @param __d A deleter. |
| 181 | * @param __a An allocator. |
| 182 | * @post use_count() == 1 && get() == __p |
| 183 | * @throw std::bad_alloc, in which case @a __d(__p) is called. |
| 184 | * |
| 185 | * Requirements: _Deleter's copy constructor and destructor must |
| 186 | * not throw _Alloc's copy constructor and destructor must not |
| 187 | * throw. |
| 188 | * |
| 189 | * __shared_ptr will release __p by calling __d(__p) |
| 190 | */ |
| 191 | template<typename _Yp, typename _Deleter, typename _Alloc, |
| 192 | typename = _Constructible<_Yp*, _Deleter, _Alloc>> |
| 193 | shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a) |
| 194 | : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { } |
| 195 | |
| 196 | /** |
| 197 | * @brief Construct a %shared_ptr that owns a null pointer |
| 198 | * and the deleter @a __d. |
| 199 | * @param __p A null pointer constant. |
| 200 | * @param __d A deleter. |
| 201 | * @param __a An allocator. |
| 202 | * @post use_count() == 1 && get() == __p |
| 203 | * @throw std::bad_alloc, in which case @a __d(__p) is called. |
| 204 | * |
| 205 | * Requirements: _Deleter's copy constructor and destructor must |
| 206 | * not throw _Alloc's copy constructor and destructor must not |
| 207 | * throw. |
| 208 | * |
| 209 | * The last owner will call __d(__p) |
| 210 | */ |
| 211 | template<typename _Deleter, typename _Alloc> |
| 212 | shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a) |
| 213 | : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { } |
| 214 | |
| 215 | // Aliasing constructor |
| 216 | |
| 217 | /** |
| 218 | * @brief Constructs a %shared_ptr instance that stores @a __p |
| 219 | * and shares ownership with @a __r. |
| 220 | * @param __r A %shared_ptr. |
| 221 | * @param __p A pointer that will remain valid while @a *__r is valid. |
| 222 | * @post get() == __p && use_count() == __r.use_count() |
| 223 | * |
| 224 | * This can be used to construct a @c shared_ptr to a sub-object |
| 225 | * of an object managed by an existing @c shared_ptr. |
| 226 | * |
| 227 | * @code |
| 228 | * shared_ptr< pair<int,int> > pii(new pair<int,int>()); |
| 229 | * shared_ptr<int> pi(pii, &pii->first); |
| 230 | * assert(pii.use_count() == 2); |
| 231 | * @endcode |
| 232 | */ |
| 233 | template<typename _Yp> |
| 234 | shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) noexcept |
| 235 | : __shared_ptr<_Tp>(__r, __p) { } |
| 236 | |
| 237 | /** |
| 238 | * @brief If @a __r is empty, constructs an empty %shared_ptr; |
| 239 | * otherwise construct a %shared_ptr that shares ownership |
| 240 | * with @a __r. |
| 241 | * @param __r A %shared_ptr. |
| 242 | * @post get() == __r.get() && use_count() == __r.use_count() |
| 243 | */ |
| 244 | template<typename _Yp, |
| 245 | typename = _Constructible<const shared_ptr<_Yp>&>> |
| 246 | shared_ptr(const shared_ptr<_Yp>& __r) noexcept |
| 247 | : __shared_ptr<_Tp>(__r) { } |
| 248 | |
| 249 | /** |
| 250 | * @brief Move-constructs a %shared_ptr instance from @a __r. |
| 251 | * @param __r A %shared_ptr rvalue. |
| 252 | * @post *this contains the old value of @a __r, @a __r is empty. |
| 253 | */ |
| 254 | shared_ptr(shared_ptr&& __r) noexcept |
| 255 | : __shared_ptr<_Tp>(std::move(__r)) { } |
| 256 | |
| 257 | /** |
| 258 | * @brief Move-constructs a %shared_ptr instance from @a __r. |
| 259 | * @param __r A %shared_ptr rvalue. |
| 260 | * @post *this contains the old value of @a __r, @a __r is empty. |
| 261 | */ |
| 262 | template<typename _Yp, typename = _Constructible<shared_ptr<_Yp>>> |
| 263 | shared_ptr(shared_ptr<_Yp>&& __r) noexcept |
| 264 | : __shared_ptr<_Tp>(std::move(__r)) { } |
| 265 | |
| 266 | /** |
| 267 | * @brief Constructs a %shared_ptr that shares ownership with @a __r |
| 268 | * and stores a copy of the pointer stored in @a __r. |
| 269 | * @param __r A weak_ptr. |
| 270 | * @post use_count() == __r.use_count() |
| 271 | * @throw bad_weak_ptr when __r.expired(), |
| 272 | * in which case the constructor has no effect. |
| 273 | */ |
| 274 | template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>> |
| 275 | explicit shared_ptr(const weak_ptr<_Yp>& __r) |
| 276 | : __shared_ptr<_Tp>(__r) { } |
| 277 | |
| 278 | #if _GLIBCXX_USE_DEPRECATED1 |
| 279 | #pragma GCC diagnostic push |
| 280 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 281 | template<typename _Yp, typename = _Constructible<auto_ptr<_Yp>>> |
| 282 | shared_ptr(auto_ptr<_Yp>&& __r); |
| 283 | #pragma GCC diagnostic pop |
| 284 | #endif |
| 285 | |
| 286 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 287 | // 2399. shared_ptr's constructor from unique_ptr should be constrained |
| 288 | template<typename _Yp, typename _Del, |
| 289 | typename = _Constructible<unique_ptr<_Yp, _Del>>> |
| 290 | shared_ptr(unique_ptr<_Yp, _Del>&& __r) |
| 291 | : __shared_ptr<_Tp>(std::move(__r)) { } |
| 292 | |
| 293 | #if __cplusplus201703L <= 201402L && _GLIBCXX_USE_DEPRECATED1 |
| 294 | // This non-standard constructor exists to support conversions that |
| 295 | // were possible in C++11 and C++14 but are ill-formed in C++17. |
| 296 | // If an exception is thrown this constructor has no effect. |
| 297 | template<typename _Yp, typename _Del, |
| 298 | _Constructible<unique_ptr<_Yp, _Del>, __sp_array_delete>* = 0> |
| 299 | shared_ptr(unique_ptr<_Yp, _Del>&& __r) |
| 300 | : __shared_ptr<_Tp>(std::move(__r), __sp_array_delete()) { } |
| 301 | #endif |
| 302 | |
| 303 | /** |
| 304 | * @brief Construct an empty %shared_ptr. |
| 305 | * @post use_count() == 0 && get() == nullptr |
| 306 | */ |
| 307 | constexpr shared_ptr(nullptr_t) noexcept : shared_ptr() { } |
| 308 | |
| 309 | shared_ptr& operator=(const shared_ptr&) noexcept = default; |
| 310 | |
| 311 | template<typename _Yp> |
| 312 | _Assignable<const shared_ptr<_Yp>&> |
| 313 | operator=(const shared_ptr<_Yp>& __r) noexcept |
| 314 | { |
| 315 | this->__shared_ptr<_Tp>::operator=(__r); |
| 316 | return *this; |
| 317 | } |
| 318 | |
| 319 | #if _GLIBCXX_USE_DEPRECATED1 |
| 320 | #pragma GCC diagnostic push |
| 321 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 322 | template<typename _Yp> |
| 323 | _Assignable<auto_ptr<_Yp>> |
| 324 | operator=(auto_ptr<_Yp>&& __r) |
| 325 | { |
| 326 | this->__shared_ptr<_Tp>::operator=(std::move(__r)); |
| 327 | return *this; |
| 328 | } |
| 329 | #pragma GCC diagnostic pop |
| 330 | #endif |
| 331 | |
| 332 | shared_ptr& |
| 333 | operator=(shared_ptr&& __r) noexcept |
| 334 | { |
| 335 | this->__shared_ptr<_Tp>::operator=(std::move(__r)); |
| 336 | return *this; |
| 337 | } |
| 338 | |
| 339 | template<class _Yp> |
| 340 | _Assignable<shared_ptr<_Yp>> |
| 341 | operator=(shared_ptr<_Yp>&& __r) noexcept |
| 342 | { |
| 343 | this->__shared_ptr<_Tp>::operator=(std::move(__r)); |
| 344 | return *this; |
| 345 | } |
| 346 | |
| 347 | template<typename _Yp, typename _Del> |
| 348 | _Assignable<unique_ptr<_Yp, _Del>> |
| 349 | operator=(unique_ptr<_Yp, _Del>&& __r) |
| 350 | { |
| 351 | this->__shared_ptr<_Tp>::operator=(std::move(__r)); |
| 352 | return *this; |
| 353 | } |
| 354 | |
| 355 | private: |
| 356 | // This constructor is non-standard, it is used by allocate_shared. |
| 357 | template<typename _Alloc, typename... _Args> |
| 358 | shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args) |
| 359 | : __shared_ptr<_Tp>(__tag, std::forward<_Args>(__args)...) |
| 360 | { } |
| 361 | |
| 362 | template<typename _Yp, typename _Alloc, typename... _Args> |
| 363 | friend shared_ptr<_Yp> |
| 364 | allocate_shared(const _Alloc& __a, _Args&&... __args); |
| 365 | |
| 366 | // This constructor is non-standard, it is used by weak_ptr::lock(). |
| 367 | shared_ptr(const weak_ptr<_Tp>& __r, std::nothrow_t) |
| 368 | : __shared_ptr<_Tp>(__r, std::nothrow) { } |
| 369 | |
| 370 | friend class weak_ptr<_Tp>; |
| 371 | }; |
| 372 | |
| 373 | #if __cpp_deduction_guides201703L >= 201606 |
| 374 | template<typename _Tp> |
| 375 | shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>; |
| 376 | template<typename _Tp, typename _Del> |
| 377 | shared_ptr(unique_ptr<_Tp, _Del>) -> shared_ptr<_Tp>; |
| 378 | #endif |
| 379 | |
| 380 | // 20.7.2.2.7 shared_ptr comparisons |
| 381 | template<typename _Tp, typename _Up> |
| 382 | inline bool |
| 383 | operator==(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
| 384 | { return __a.get() == __b.get(); } |
| 385 | |
| 386 | template<typename _Tp> |
| 387 | inline bool |
| 388 | operator==(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
| 389 | { return !__a; } |
| 390 | |
| 391 | template<typename _Tp> |
| 392 | inline bool |
| 393 | operator==(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
| 394 | { return !__a; } |
| 395 | |
| 396 | template<typename _Tp, typename _Up> |
| 397 | inline bool |
| 398 | operator!=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
| 399 | { return __a.get() != __b.get(); } |
| 400 | |
| 401 | template<typename _Tp> |
| 402 | inline bool |
| 403 | operator!=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
| 404 | { return (bool)__a; } |
| 405 | |
| 406 | template<typename _Tp> |
| 407 | inline bool |
| 408 | operator!=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
| 409 | { return (bool)__a; } |
| 410 | |
| 411 | template<typename _Tp, typename _Up> |
| 412 | inline bool |
| 413 | operator<(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
| 414 | { |
| 415 | using _Tp_elt = typename shared_ptr<_Tp>::element_type; |
| 416 | using _Up_elt = typename shared_ptr<_Up>::element_type; |
| 417 | using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type; |
| 418 | return less<_Vp>()(__a.get(), __b.get()); |
| 419 | } |
| 420 | |
| 421 | template<typename _Tp> |
| 422 | inline bool |
| 423 | operator<(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
| 424 | { |
| 425 | using _Tp_elt = typename shared_ptr<_Tp>::element_type; |
| 426 | return less<_Tp_elt*>()(__a.get(), nullptr); |
| 427 | } |
| 428 | |
| 429 | template<typename _Tp> |
| 430 | inline bool |
| 431 | operator<(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
| 432 | { |
| 433 | using _Tp_elt = typename shared_ptr<_Tp>::element_type; |
| 434 | return less<_Tp_elt*>()(nullptr, __a.get()); |
| 435 | } |
| 436 | |
| 437 | template<typename _Tp, typename _Up> |
| 438 | inline bool |
| 439 | operator<=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
| 440 | { return !(__b < __a); } |
| 441 | |
| 442 | template<typename _Tp> |
| 443 | inline bool |
| 444 | operator<=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
| 445 | { return !(nullptr < __a); } |
| 446 | |
| 447 | template<typename _Tp> |
| 448 | inline bool |
| 449 | operator<=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
| 450 | { return !(__a < nullptr); } |
| 451 | |
| 452 | template<typename _Tp, typename _Up> |
| 453 | inline bool |
| 454 | operator>(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
| 455 | { return (__b < __a); } |
| 456 | |
| 457 | template<typename _Tp> |
| 458 | inline bool |
| 459 | operator>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
| 460 | { return nullptr < __a; } |
| 461 | |
| 462 | template<typename _Tp> |
| 463 | inline bool |
| 464 | operator>(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
| 465 | { return __a < nullptr; } |
| 466 | |
| 467 | template<typename _Tp, typename _Up> |
| 468 | inline bool |
| 469 | operator>=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
| 470 | { return !(__a < __b); } |
| 471 | |
| 472 | template<typename _Tp> |
| 473 | inline bool |
| 474 | operator>=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
| 475 | { return !(__a < nullptr); } |
| 476 | |
| 477 | template<typename _Tp> |
| 478 | inline bool |
| 479 | operator>=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
| 480 | { return !(nullptr < __a); } |
| 481 | |
| 482 | template<typename _Tp> |
| 483 | struct less<shared_ptr<_Tp>> : public _Sp_less<shared_ptr<_Tp>> |
| 484 | { }; |
| 485 | |
| 486 | // 20.7.2.2.8 shared_ptr specialized algorithms. |
| 487 | template<typename _Tp> |
| 488 | inline void |
| 489 | swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) noexcept |
| 490 | { __a.swap(__b); } |
| 491 | |
| 492 | // 20.7.2.2.9 shared_ptr casts. |
| 493 | template<typename _Tp, typename _Up> |
| 494 | inline shared_ptr<_Tp> |
| 495 | static_pointer_cast(const shared_ptr<_Up>& __r) noexcept |
| 496 | { |
| 497 | using _Sp = shared_ptr<_Tp>; |
| 498 | return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get())); |
| 499 | } |
| 500 | |
| 501 | template<typename _Tp, typename _Up> |
| 502 | inline shared_ptr<_Tp> |
| 503 | const_pointer_cast(const shared_ptr<_Up>& __r) noexcept |
| 504 | { |
| 505 | using _Sp = shared_ptr<_Tp>; |
| 506 | return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get())); |
| 507 | } |
| 508 | |
| 509 | template<typename _Tp, typename _Up> |
| 510 | inline shared_ptr<_Tp> |
| 511 | dynamic_pointer_cast(const shared_ptr<_Up>& __r) noexcept |
| 512 | { |
| 513 | using _Sp = shared_ptr<_Tp>; |
| 514 | if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get())) |
| 515 | return _Sp(__r, __p); |
| 516 | return _Sp(); |
| 517 | } |
| 518 | |
| 519 | #if __cplusplus201703L > 201402L |
| 520 | template<typename _Tp, typename _Up> |
| 521 | inline shared_ptr<_Tp> |
| 522 | reinterpret_pointer_cast(const shared_ptr<_Up>& __r) noexcept |
| 523 | { |
| 524 | using _Sp = shared_ptr<_Tp>; |
| 525 | return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get())); |
| 526 | } |
| 527 | #endif |
| 528 | |
| 529 | /** |
| 530 | * @brief A smart pointer with weak semantics. |
| 531 | * |
| 532 | * With forwarding constructors and assignment operators. |
| 533 | */ |
| 534 | template<typename _Tp> |
| 535 | class weak_ptr : public __weak_ptr<_Tp> |
| 536 | { |
| 537 | template<typename _Arg> |
| 538 | using _Constructible = typename enable_if< |
| 539 | is_constructible<__weak_ptr<_Tp>, _Arg>::value |
| 540 | >::type; |
| 541 | |
| 542 | template<typename _Arg> |
| 543 | using _Assignable = typename enable_if< |
| 544 | is_assignable<__weak_ptr<_Tp>&, _Arg>::value, weak_ptr& |
| 545 | >::type; |
| 546 | |
| 547 | public: |
| 548 | constexpr weak_ptr() noexcept = default; |
| 549 | |
| 550 | template<typename _Yp, |
| 551 | typename = _Constructible<const shared_ptr<_Yp>&>> |
| 552 | weak_ptr(const shared_ptr<_Yp>& __r) noexcept |
| 553 | : __weak_ptr<_Tp>(__r) { } |
| 554 | |
| 555 | weak_ptr(const weak_ptr&) noexcept = default; |
| 556 | |
| 557 | template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>> |
| 558 | weak_ptr(const weak_ptr<_Yp>& __r) noexcept |
| 559 | : __weak_ptr<_Tp>(__r) { } |
| 560 | |
| 561 | weak_ptr(weak_ptr&&) noexcept = default; |
| 562 | |
| 563 | template<typename _Yp, typename = _Constructible<weak_ptr<_Yp>>> |
| 564 | weak_ptr(weak_ptr<_Yp>&& __r) noexcept |
| 565 | : __weak_ptr<_Tp>(std::move(__r)) { } |
| 566 | |
| 567 | weak_ptr& |
| 568 | operator=(const weak_ptr& __r) noexcept = default; |
| 569 | |
| 570 | template<typename _Yp> |
| 571 | _Assignable<const weak_ptr<_Yp>&> |
| 572 | operator=(const weak_ptr<_Yp>& __r) noexcept |
| 573 | { |
| 574 | this->__weak_ptr<_Tp>::operator=(__r); |
| 575 | return *this; |
| 576 | } |
| 577 | |
| 578 | template<typename _Yp> |
| 579 | _Assignable<const shared_ptr<_Yp>&> |
| 580 | operator=(const shared_ptr<_Yp>& __r) noexcept |
| 581 | { |
| 582 | this->__weak_ptr<_Tp>::operator=(__r); |
| 583 | return *this; |
| 584 | } |
| 585 | |
| 586 | weak_ptr& |
| 587 | operator=(weak_ptr&& __r) noexcept = default; |
| 588 | |
| 589 | template<typename _Yp> |
| 590 | _Assignable<weak_ptr<_Yp>> |
| 591 | operator=(weak_ptr<_Yp>&& __r) noexcept |
| 592 | { |
| 593 | this->__weak_ptr<_Tp>::operator=(std::move(__r)); |
| 594 | return *this; |
| 595 | } |
| 596 | |
| 597 | shared_ptr<_Tp> |
| 598 | lock() const noexcept |
| 599 | { return shared_ptr<_Tp>(*this, std::nothrow); } |
| 600 | }; |
| 601 | |
| 602 | #if __cpp_deduction_guides201703L >= 201606 |
| 603 | template<typename _Tp> |
| 604 | weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>; |
| 605 | #endif |
| 606 | |
| 607 | // 20.7.2.3.6 weak_ptr specialized algorithms. |
| 608 | template<typename _Tp> |
| 609 | inline void |
| 610 | swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b) noexcept |
| 611 | { __a.swap(__b); } |
| 612 | |
| 613 | |
| 614 | /// Primary template owner_less |
| 615 | template<typename _Tp = void> |
| 616 | struct owner_less; |
| 617 | |
| 618 | /// Void specialization of owner_less |
| 619 | template<> |
| 620 | struct owner_less<void> : _Sp_owner_less<void, void> |
| 621 | { }; |
| 622 | |
| 623 | /// Partial specialization of owner_less for shared_ptr. |
| 624 | template<typename _Tp> |
| 625 | struct owner_less<shared_ptr<_Tp>> |
| 626 | : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>> |
| 627 | { }; |
| 628 | |
| 629 | /// Partial specialization of owner_less for weak_ptr. |
| 630 | template<typename _Tp> |
| 631 | struct owner_less<weak_ptr<_Tp>> |
| 632 | : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>> |
| 633 | { }; |
| 634 | |
| 635 | /** |
| 636 | * @brief Base class allowing use of member function shared_from_this. |
| 637 | */ |
| 638 | template<typename _Tp> |
| 639 | class enable_shared_from_this |
| 640 | { |
| 641 | protected: |
| 642 | constexpr enable_shared_from_this() noexcept { } |
| 643 | |
| 644 | enable_shared_from_this(const enable_shared_from_this&) noexcept { } |
| 645 | |
| 646 | enable_shared_from_this& |
| 647 | operator=(const enable_shared_from_this&) noexcept |
| 648 | { return *this; } |
| 649 | |
| 650 | ~enable_shared_from_this() { } |
| 651 | |
| 652 | public: |
| 653 | shared_ptr<_Tp> |
| 654 | shared_from_this() |
| 655 | { return shared_ptr<_Tp>(this->_M_weak_this); } |
| 656 | |
| 657 | shared_ptr<const _Tp> |
| 658 | shared_from_this() const |
| 659 | { return shared_ptr<const _Tp>(this->_M_weak_this); } |
| 660 | |
| 661 | #if __cplusplus201703L > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 |
| 662 | #define __cpp_lib_enable_shared_from_this201603 201603 |
| 663 | weak_ptr<_Tp> |
| 664 | weak_from_this() noexcept |
| 665 | { return this->_M_weak_this; } |
| 666 | |
| 667 | weak_ptr<const _Tp> |
| 668 | weak_from_this() const noexcept |
| 669 | { return this->_M_weak_this; } |
| 670 | #endif |
| 671 | |
| 672 | private: |
| 673 | template<typename _Tp1> |
| 674 | void |
| 675 | _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept |
| 676 | { _M_weak_this._M_assign(__p, __n); } |
| 677 | |
| 678 | // Found by ADL when this is an associated class. |
| 679 | friend const enable_shared_from_this* |
| 680 | __enable_shared_from_this_base(const __shared_count<>&, |
| 681 | const enable_shared_from_this* __p) |
| 682 | { return __p; } |
| 683 | |
| 684 | template<typename, _Lock_policy> |
| 685 | friend class __shared_ptr; |
| 686 | |
| 687 | mutable weak_ptr<_Tp> _M_weak_this; |
| 688 | }; |
| 689 | |
| 690 | /** |
| 691 | * @brief Create an object that is owned by a shared_ptr. |
| 692 | * @param __a An allocator. |
| 693 | * @param __args Arguments for the @a _Tp object's constructor. |
| 694 | * @return A shared_ptr that owns the newly created object. |
| 695 | * @throw An exception thrown from @a _Alloc::allocate or from the |
| 696 | * constructor of @a _Tp. |
| 697 | * |
| 698 | * A copy of @a __a will be used to allocate memory for the shared_ptr |
| 699 | * and the new object. |
| 700 | */ |
| 701 | template<typename _Tp, typename _Alloc, typename... _Args> |
| 702 | inline shared_ptr<_Tp> |
| 703 | allocate_shared(const _Alloc& __a, _Args&&... __args) |
| 704 | { |
| 705 | return shared_ptr<_Tp>(_Sp_alloc_shared_tag<_Alloc>{__a}, |
| 706 | std::forward<_Args>(__args)...); |
| 707 | } |
| 708 | |
| 709 | /** |
| 710 | * @brief Create an object that is owned by a shared_ptr. |
| 711 | * @param __args Arguments for the @a _Tp object's constructor. |
| 712 | * @return A shared_ptr that owns the newly created object. |
| 713 | * @throw std::bad_alloc, or an exception thrown from the |
| 714 | * constructor of @a _Tp. |
| 715 | */ |
| 716 | template<typename _Tp, typename... _Args> |
| 717 | inline shared_ptr<_Tp> |
| 718 | make_shared(_Args&&... __args) |
| 719 | { |
| 720 | typedef typename std::remove_cv<_Tp>::type _Tp_nc; |
| 721 | return std::allocate_shared<_Tp>(std::allocator<_Tp_nc>(), |
| 722 | std::forward<_Args>(__args)...); |
| 723 | } |
| 724 | |
| 725 | /// std::hash specialization for shared_ptr. |
| 726 | template<typename _Tp> |
| 727 | struct hash<shared_ptr<_Tp>> |
| 728 | : public __hash_base<size_t, shared_ptr<_Tp>> |
| 729 | { |
| 730 | size_t |
| 731 | operator()(const shared_ptr<_Tp>& __s) const noexcept |
| 732 | { |
| 733 | return std::hash<typename shared_ptr<_Tp>::element_type*>()(__s.get()); |
| 734 | } |
| 735 | }; |
| 736 | |
| 737 | // @} group pointer_abstractions |
| 738 | |
| 739 | _GLIBCXX_END_NAMESPACE_VERSION |
| 740 | } // namespace |
| 741 | |
| 742 | #endif // _SHARED_PTR_H |
| 1 | // shared_ptr and weak_ptr implementation details -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 2007-2018 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // Under Section 7 of GPL version 3, you are granted additional |
| 17 | // permissions described in the GCC Runtime Library Exception, version |
| 18 | // 3.1, as published by the Free Software Foundation. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License and |
| 21 | // a copy of the GCC Runtime Library Exception along with this program; |
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 | // <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | // GCC Note: Based on files from version 1.32.0 of the Boost library. |
| 26 | |
| 27 | // shared_count.hpp |
| 28 | // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd. |
| 29 | |
| 30 | // shared_ptr.hpp |
| 31 | // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes. |
| 32 | // Copyright (C) 2001, 2002, 2003 Peter Dimov |
| 33 | |
| 34 | // weak_ptr.hpp |
| 35 | // Copyright (C) 2001, 2002, 2003 Peter Dimov |
| 36 | |
| 37 | // enable_shared_from_this.hpp |
| 38 | // Copyright (C) 2002 Peter Dimov |
| 39 | |
| 40 | // Distributed under the Boost Software License, Version 1.0. (See |
| 41 | // accompanying file LICENSE_1_0.txt or copy at |
| 42 | // http://www.boost.org/LICENSE_1_0.txt) |
| 43 | |
| 44 | /** @file bits/shared_ptr_base.h |
| 45 | * This is an internal header file, included by other library headers. |
| 46 | * Do not attempt to use it directly. @headername{memory} |
| 47 | */ |
| 48 | |
| 49 | #ifndef _SHARED_PTR_BASE_H1 |
| 50 | #define _SHARED_PTR_BASE_H1 1 |
| 51 | |
| 52 | #include <typeinfo> |
| 53 | #include <bits/allocated_ptr.h> |
| 54 | #include <bits/refwrap.h> |
| 55 | #include <bits/stl_function.h> |
| 56 | #include <ext/aligned_buffer.h> |
| 57 | |
| 58 | namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
| 59 | { |
| 60 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 61 | |
| 62 | #if _GLIBCXX_USE_DEPRECATED1 |
| 63 | #pragma GCC diagnostic push |
| 64 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 65 | template<typename> class auto_ptr; |
| 66 | #pragma GCC diagnostic pop |
| 67 | #endif |
| 68 | |
| 69 | /** |
| 70 | * @brief Exception possibly thrown by @c shared_ptr. |
| 71 | * @ingroup exceptions |
| 72 | */ |
| 73 | class bad_weak_ptr : public std::exception |
| 74 | { |
| 75 | public: |
| 76 | virtual char const* what() const noexcept; |
| 77 | |
| 78 | virtual ~bad_weak_ptr() noexcept; |
| 79 | }; |
| 80 | |
| 81 | // Substitute for bad_weak_ptr object in the case of -fno-exceptions. |
| 82 | inline void |
| 83 | __throw_bad_weak_ptr() |
| 84 | { _GLIBCXX_THROW_OR_ABORT(bad_weak_ptr())(__builtin_abort()); } |
| 85 | |
| 86 | using __gnu_cxx::_Lock_policy; |
| 87 | using __gnu_cxx::__default_lock_policy; |
| 88 | using __gnu_cxx::_S_single; |
| 89 | using __gnu_cxx::_S_mutex; |
| 90 | using __gnu_cxx::_S_atomic; |
| 91 | |
| 92 | // Empty helper class except when the template argument is _S_mutex. |
| 93 | template<_Lock_policy _Lp> |
| 94 | class _Mutex_base |
| 95 | { |
| 96 | protected: |
| 97 | // The atomic policy uses fully-fenced builtins, single doesn't care. |
| 98 | enum { _S_need_barriers = 0 }; |
| 99 | }; |
| 100 | |
| 101 | template<> |
| 102 | class _Mutex_base<_S_mutex> |
| 103 | : public __gnu_cxx::__mutex |
| 104 | { |
| 105 | protected: |
| 106 | // This policy is used when atomic builtins are not available. |
| 107 | // The replacement atomic operations might not have the necessary |
| 108 | // memory barriers. |
| 109 | enum { _S_need_barriers = 1 }; |
| 110 | }; |
| 111 | |
| 112 | template<_Lock_policy _Lp = __default_lock_policy> |
| 113 | class _Sp_counted_base |
| 114 | : public _Mutex_base<_Lp> |
| 115 | { |
| 116 | public: |
| 117 | _Sp_counted_base() noexcept |
| 118 | : _M_use_count(1), _M_weak_count(1) { } |
| 119 | |
| 120 | virtual |
| 121 | ~_Sp_counted_base() noexcept |
| 122 | { } |
| 123 | |
| 124 | // Called when _M_use_count drops to zero, to release the resources |
| 125 | // managed by *this. |
| 126 | virtual void |
| 127 | _M_dispose() noexcept = 0; |
| 128 | |
| 129 | // Called when _M_weak_count drops to zero. |
| 130 | virtual void |
| 131 | _M_destroy() noexcept |
| 132 | { delete this; } |
| 133 | |
| 134 | virtual void* |
| 135 | _M_get_deleter(const std::type_info&) noexcept = 0; |
| 136 | |
| 137 | void |
| 138 | _M_add_ref_copy() |
| 139 | { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); } |
| 140 | |
| 141 | void |
| 142 | _M_add_ref_lock(); |
| 143 | |
| 144 | bool |
| 145 | _M_add_ref_lock_nothrow(); |
| 146 | |
| 147 | void |
| 148 | _M_release() noexcept |
| 149 | { |
| 150 | // Be race-detector-friendly. For more info see bits/c++config. |
| 151 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count); |
| 152 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1) |
| 153 | { |
| 154 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count); |
| 155 | _M_dispose(); |
| 156 | // There must be a memory barrier between dispose() and destroy() |
| 157 | // to ensure that the effects of dispose() are observed in the |
| 158 | // thread that runs destroy(). |
| 159 | // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html |
| 160 | if (_Mutex_base<_Lp>::_S_need_barriers) |
| 161 | { |
| 162 | __atomic_thread_fence (__ATOMIC_ACQ_REL4); |
| 163 | } |
| 164 | |
| 165 | // Be race-detector-friendly. For more info see bits/c++config. |
| 166 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count); |
| 167 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, |
| 168 | -1) == 1) |
| 169 | { |
| 170 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count); |
| 171 | _M_destroy(); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | void |
| 177 | _M_weak_add_ref() noexcept |
| 178 | { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); } |
| 179 | |
| 180 | void |
| 181 | _M_weak_release() noexcept |
| 182 | { |
| 183 | // Be race-detector-friendly. For more info see bits/c++config. |
| 184 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count); |
| 185 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1) |
| 186 | { |
| 187 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count); |
| 188 | if (_Mutex_base<_Lp>::_S_need_barriers) |
| 189 | { |
| 190 | // See _M_release(), |
| 191 | // destroy() must observe results of dispose() |
| 192 | __atomic_thread_fence (__ATOMIC_ACQ_REL4); |
| 193 | } |
| 194 | _M_destroy(); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | long |
| 199 | _M_get_use_count() const noexcept |
| 200 | { |
| 201 | // No memory barrier is used here so there is no synchronization |
| 202 | // with other threads. |
| 203 | return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED0); |
| 204 | } |
| 205 | |
| 206 | private: |
| 207 | _Sp_counted_base(_Sp_counted_base const&) = delete; |
| 208 | _Sp_counted_base& operator=(_Sp_counted_base const&) = delete; |
| 209 | |
| 210 | _Atomic_word _M_use_count; // #shared |
| 211 | _Atomic_word _M_weak_count; // #weak + (#shared != 0) |
| 212 | }; |
| 213 | |
| 214 | template<> |
| 215 | inline void |
| 216 | _Sp_counted_base<_S_single>:: |
| 217 | _M_add_ref_lock() |
| 218 | { |
| 219 | if (_M_use_count == 0) |
| 220 | __throw_bad_weak_ptr(); |
| 221 | ++_M_use_count; |
| 222 | } |
| 223 | |
| 224 | template<> |
| 225 | inline void |
| 226 | _Sp_counted_base<_S_mutex>:: |
| 227 | _M_add_ref_lock() |
| 228 | { |
| 229 | __gnu_cxx::__scoped_lock sentry(*this); |
| 230 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0) |
| 231 | { |
| 232 | _M_use_count = 0; |
| 233 | __throw_bad_weak_ptr(); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | template<> |
| 238 | inline void |
| 239 | _Sp_counted_base<_S_atomic>:: |
| 240 | _M_add_ref_lock() |
| 241 | { |
| 242 | // Perform lock-free add-if-not-zero operation. |
| 243 | _Atomic_word __count = _M_get_use_count(); |
| 244 | do |
| 245 | { |
| 246 | if (__count == 0) |
| 247 | __throw_bad_weak_ptr(); |
| 248 | // Replace the current counter value with the old value + 1, as |
| 249 | // long as it's not changed meanwhile. |
| 250 | } |
| 251 | while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1, |
| 252 | true, __ATOMIC_ACQ_REL4, |
| 253 | __ATOMIC_RELAXED0)); |
| 254 | } |
| 255 | |
| 256 | template<> |
| 257 | inline bool |
| 258 | _Sp_counted_base<_S_single>:: |
| 259 | _M_add_ref_lock_nothrow() |
| 260 | { |
| 261 | if (_M_use_count == 0) |
| 262 | return false; |
| 263 | ++_M_use_count; |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | template<> |
| 268 | inline bool |
| 269 | _Sp_counted_base<_S_mutex>:: |
| 270 | _M_add_ref_lock_nothrow() |
| 271 | { |
| 272 | __gnu_cxx::__scoped_lock sentry(*this); |
| 273 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0) |
| 274 | { |
| 275 | _M_use_count = 0; |
| 276 | return false; |
| 277 | } |
| 278 | return true; |
| 279 | } |
| 280 | |
| 281 | template<> |
| 282 | inline bool |
| 283 | _Sp_counted_base<_S_atomic>:: |
| 284 | _M_add_ref_lock_nothrow() |
| 285 | { |
| 286 | // Perform lock-free add-if-not-zero operation. |
| 287 | _Atomic_word __count = _M_get_use_count(); |
| 288 | do |
| 289 | { |
| 290 | if (__count == 0) |
| 291 | return false; |
| 292 | // Replace the current counter value with the old value + 1, as |
| 293 | // long as it's not changed meanwhile. |
| 294 | } |
| 295 | while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1, |
| 296 | true, __ATOMIC_ACQ_REL4, |
| 297 | __ATOMIC_RELAXED0)); |
| 298 | return true; |
| 299 | } |
| 300 | |
| 301 | template<> |
| 302 | inline void |
| 303 | _Sp_counted_base<_S_single>::_M_add_ref_copy() |
| 304 | { ++_M_use_count; } |
| 305 | |
| 306 | template<> |
| 307 | inline void |
| 308 | _Sp_counted_base<_S_single>::_M_release() noexcept |
| 309 | { |
| 310 | if (--_M_use_count == 0) |
| 311 | { |
| 312 | _M_dispose(); |
| 313 | if (--_M_weak_count == 0) |
| 314 | _M_destroy(); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | template<> |
| 319 | inline void |
| 320 | _Sp_counted_base<_S_single>::_M_weak_add_ref() noexcept |
| 321 | { ++_M_weak_count; } |
| 322 | |
| 323 | template<> |
| 324 | inline void |
| 325 | _Sp_counted_base<_S_single>::_M_weak_release() noexcept |
| 326 | { |
| 327 | if (--_M_weak_count == 0) |
| 328 | _M_destroy(); |
| 329 | } |
| 330 | |
| 331 | template<> |
| 332 | inline long |
| 333 | _Sp_counted_base<_S_single>::_M_get_use_count() const noexcept |
| 334 | { return _M_use_count; } |
| 335 | |
| 336 | |
| 337 | // Forward declarations. |
| 338 | template<typename _Tp, _Lock_policy _Lp = __default_lock_policy> |
| 339 | class __shared_ptr; |
| 340 | |
| 341 | template<typename _Tp, _Lock_policy _Lp = __default_lock_policy> |
| 342 | class __weak_ptr; |
| 343 | |
| 344 | template<typename _Tp, _Lock_policy _Lp = __default_lock_policy> |
| 345 | class __enable_shared_from_this; |
| 346 | |
| 347 | template<typename _Tp> |
| 348 | class shared_ptr; |
| 349 | |
| 350 | template<typename _Tp> |
| 351 | class weak_ptr; |
| 352 | |
| 353 | template<typename _Tp> |
| 354 | struct owner_less; |
| 355 | |
| 356 | template<typename _Tp> |
| 357 | class enable_shared_from_this; |
| 358 | |
| 359 | template<_Lock_policy _Lp = __default_lock_policy> |
| 360 | class __weak_count; |
| 361 | |
| 362 | template<_Lock_policy _Lp = __default_lock_policy> |
| 363 | class __shared_count; |
| 364 | |
| 365 | |
| 366 | // Counted ptr with no deleter or allocator support |
| 367 | template<typename _Ptr, _Lock_policy _Lp> |
| 368 | class _Sp_counted_ptr final : public _Sp_counted_base<_Lp> |
| 369 | { |
| 370 | public: |
| 371 | explicit |
| 372 | _Sp_counted_ptr(_Ptr __p) noexcept |
| 373 | : _M_ptr(__p) { } |
| 374 | |
| 375 | virtual void |
| 376 | _M_dispose() noexcept |
| 377 | { delete _M_ptr; } |
| 378 | |
| 379 | virtual void |
| 380 | _M_destroy() noexcept |
| 381 | { delete this; } |
| 382 | |
| 383 | virtual void* |
| 384 | _M_get_deleter(const std::type_info&) noexcept |
| 385 | { return nullptr; } |
| 386 | |
| 387 | _Sp_counted_ptr(const _Sp_counted_ptr&) = delete; |
| 388 | _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete; |
| 389 | |
| 390 | private: |
| 391 | _Ptr _M_ptr; |
| 392 | }; |
| 393 | |
| 394 | template<> |
| 395 | inline void |
| 396 | _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { } |
| 397 | |
| 398 | template<> |
| 399 | inline void |
| 400 | _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { } |
| 401 | |
| 402 | template<> |
| 403 | inline void |
| 404 | _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { } |
| 405 | |
| 406 | template<int _Nm, typename _Tp, |
| 407 | bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)> |
| 408 | struct _Sp_ebo_helper; |
| 409 | |
| 410 | /// Specialization using EBO. |
| 411 | template<int _Nm, typename _Tp> |
| 412 | struct _Sp_ebo_helper<_Nm, _Tp, true> : private _Tp |
| 413 | { |
| 414 | explicit _Sp_ebo_helper(const _Tp& __tp) : _Tp(__tp) { } |
| 415 | explicit _Sp_ebo_helper(_Tp&& __tp) : _Tp(std::move(__tp)) { } |
| 416 | |
| 417 | static _Tp& |
| 418 | _S_get(_Sp_ebo_helper& __eboh) { return static_cast<_Tp&>(__eboh); } |
| 419 | }; |
| 420 | |
| 421 | /// Specialization not using EBO. |
| 422 | template<int _Nm, typename _Tp> |
| 423 | struct _Sp_ebo_helper<_Nm, _Tp, false> |
| 424 | { |
| 425 | explicit _Sp_ebo_helper(const _Tp& __tp) : _M_tp(__tp) { } |
| 426 | explicit _Sp_ebo_helper(_Tp&& __tp) : _M_tp(std::move(__tp)) { } |
| 427 | |
| 428 | static _Tp& |
| 429 | _S_get(_Sp_ebo_helper& __eboh) |
| 430 | { return __eboh._M_tp; } |
| 431 | |
| 432 | private: |
| 433 | _Tp _M_tp; |
| 434 | }; |
| 435 | |
| 436 | // Support for custom deleter and/or allocator |
| 437 | template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp> |
| 438 | class _Sp_counted_deleter final : public _Sp_counted_base<_Lp> |
| 439 | { |
| 440 | class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc> |
| 441 | { |
| 442 | typedef _Sp_ebo_helper<0, _Deleter> _Del_base; |
| 443 | typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base; |
| 444 | |
| 445 | public: |
| 446 | _Impl(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept |
| 447 | : _M_ptr(__p), _Del_base(std::move(__d)), _Alloc_base(__a) |
| 448 | { } |
| 449 | |
| 450 | _Deleter& _M_del() noexcept { return _Del_base::_S_get(*this); } |
| 451 | _Alloc& _M_alloc() noexcept { return _Alloc_base::_S_get(*this); } |
| 452 | |
| 453 | _Ptr _M_ptr; |
| 454 | }; |
| 455 | |
| 456 | public: |
| 457 | using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>; |
| 458 | |
| 459 | // __d(__p) must not throw. |
| 460 | _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept |
| 461 | : _M_impl(__p, std::move(__d), _Alloc()) { } |
| 462 | |
| 463 | // __d(__p) must not throw. |
| 464 | _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept |
| 465 | : _M_impl(__p, std::move(__d), __a) { } |
| 466 | |
| 467 | ~_Sp_counted_deleter() noexcept { } |
| 468 | |
| 469 | virtual void |
| 470 | _M_dispose() noexcept |
| 471 | { _M_impl._M_del()(_M_impl._M_ptr); } |
| 472 | |
| 473 | virtual void |
| 474 | _M_destroy() noexcept |
| 475 | { |
| 476 | __allocator_type __a(_M_impl._M_alloc()); |
| 477 | __allocated_ptr<__allocator_type> __guard_ptr{ __a, this }; |
| 478 | this->~_Sp_counted_deleter(); |
| 479 | } |
| 480 | |
| 481 | virtual void* |
| 482 | _M_get_deleter(const std::type_info& __ti) noexcept |
| 483 | { |
| 484 | #if __cpp_rtti |
| 485 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 486 | // 2400. shared_ptr's get_deleter() should use addressof() |
| 487 | return __ti == typeid(_Deleter) |
| 488 | ? std::__addressof(_M_impl._M_del()) |
| 489 | : nullptr; |
| 490 | #else |
| 491 | return nullptr; |
| 492 | #endif |
| 493 | } |
| 494 | |
| 495 | private: |
| 496 | _Impl _M_impl; |
| 497 | }; |
| 498 | |
| 499 | // helpers for make_shared / allocate_shared |
| 500 | |
| 501 | struct _Sp_make_shared_tag |
| 502 | { |
| 503 | private: |
| 504 | template<typename _Tp, typename _Alloc, _Lock_policy _Lp> |
| 505 | friend class _Sp_counted_ptr_inplace; |
| 506 | |
| 507 | static const type_info& |
| 508 | _S_ti() noexcept _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
| 509 | { |
| 510 | alignas(type_info) static constexpr char __tag[sizeof(type_info)] = { }; |
| 511 | return reinterpret_cast<const type_info&>(__tag); |
| 512 | } |
| 513 | }; |
| 514 | |
| 515 | template<typename _Alloc> |
| 516 | struct _Sp_alloc_shared_tag |
| 517 | { |
| 518 | const _Alloc& _M_a; |
| 519 | }; |
| 520 | |
| 521 | template<typename _Tp, typename _Alloc, _Lock_policy _Lp> |
| 522 | class _Sp_counted_ptr_inplace final : public _Sp_counted_base<_Lp> |
| 523 | { |
| 524 | class _Impl : _Sp_ebo_helper<0, _Alloc> |
| 525 | { |
| 526 | typedef _Sp_ebo_helper<0, _Alloc> _A_base; |
| 527 | |
| 528 | public: |
| 529 | explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { } |
| 530 | |
| 531 | _Alloc& _M_alloc() noexcept { return _A_base::_S_get(*this); } |
| 532 | |
| 533 | __gnu_cxx::__aligned_buffer<_Tp> _M_storage; |
| 534 | }; |
| 535 | |
| 536 | public: |
| 537 | using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>; |
| 538 | |
| 539 | template<typename... _Args> |
| 540 | _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args) |
| 541 | : _M_impl(__a) |
| 542 | { |
| 543 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 544 | // 2070. allocate_shared should use allocator_traits<A>::construct |
| 545 | allocator_traits<_Alloc>::construct(__a, _M_ptr(), |
| 546 | std::forward<_Args>(__args)...); // might throw |
| 547 | } |
| 548 | |
| 549 | ~_Sp_counted_ptr_inplace() noexcept { } |
| 550 | |
| 551 | virtual void |
| 552 | _M_dispose() noexcept |
| 553 | { |
| 554 | allocator_traits<_Alloc>::destroy(_M_impl._M_alloc(), _M_ptr()); |
| 555 | } |
| 556 | |
| 557 | // Override because the allocator needs to know the dynamic type |
| 558 | virtual void |
| 559 | _M_destroy() noexcept |
| 560 | { |
| 561 | __allocator_type __a(_M_impl._M_alloc()); |
| 562 | __allocated_ptr<__allocator_type> __guard_ptr{ __a, this }; |
| 563 | this->~_Sp_counted_ptr_inplace(); |
| 564 | } |
| 565 | |
| 566 | private: |
| 567 | friend class __shared_count<_Lp>; // To be able to call _M_ptr(). |
| 568 | |
| 569 | // No longer used, but code compiled against old libstdc++ headers |
| 570 | // might still call it from __shared_ptr ctor to get the pointer out. |
| 571 | virtual void* |
| 572 | _M_get_deleter(const std::type_info& __ti) noexcept override |
| 573 | { |
| 574 | // Check for the fake type_info first, so we don't try to access it |
| 575 | // as a real type_info object. |
| 576 | if (&__ti == &_Sp_make_shared_tag::_S_ti()) |
| 577 | return const_cast<typename remove_cv<_Tp>::type*>(_M_ptr()); |
| 578 | #if __cpp_rtti |
| 579 | // Callers compiled with old libstdc++ headers and RTTI enabled |
| 580 | // might pass this instead: |
| 581 | else if (__ti == typeid(_Sp_make_shared_tag)) |
| 582 | return const_cast<typename remove_cv<_Tp>::type*>(_M_ptr()); |
| 583 | #else |
| 584 | // Cannot detect a real type_info object. If the linker keeps a |
| 585 | // definition of this function compiled with -fno-rtti then callers |
| 586 | // that have RTTI enabled and pass a real type_info object will get |
| 587 | // a null pointer returned. |
| 588 | #endif |
| 589 | return nullptr; |
| 590 | } |
| 591 | |
| 592 | _Tp* _M_ptr() noexcept { return _M_impl._M_storage._M_ptr(); } |
| 593 | |
| 594 | _Impl _M_impl; |
| 595 | }; |
| 596 | |
| 597 | // The default deleter for shared_ptr<T[]> and shared_ptr<T[N]>. |
| 598 | struct __sp_array_delete |
| 599 | { |
| 600 | template<typename _Yp> |
| 601 | void operator()(_Yp* __p) const { delete[] __p; } |
| 602 | }; |
| 603 | |
| 604 | template<_Lock_policy _Lp> |
| 605 | class __shared_count |
| 606 | { |
| 607 | template<typename _Tp> |
| 608 | struct __not_alloc_shared_tag { using type = void; }; |
| 609 | |
| 610 | template<typename _Tp> |
| 611 | struct __not_alloc_shared_tag<_Sp_alloc_shared_tag<_Tp>> { }; |
| 612 | |
| 613 | public: |
| 614 | constexpr __shared_count() noexcept : _M_pi(0) |
| 615 | { } |
| 616 | |
| 617 | template<typename _Ptr> |
| 618 | explicit |
| 619 | __shared_count(_Ptr __p) : _M_pi(0) |
| 620 | { |
| 621 | __tryif (true) |
| 622 | { |
| 623 | _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p); |
| 624 | } |
| 625 | __catch(...)if (false) |
| 626 | { |
| 627 | delete __p; |
| 628 | __throw_exception_again; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | template<typename _Ptr> |
| 633 | __shared_count(_Ptr __p, /* is_array = */ false_type) |
| 634 | : __shared_count(__p) |
| 635 | { } |
| 636 | |
| 637 | template<typename _Ptr> |
| 638 | __shared_count(_Ptr __p, /* is_array = */ true_type) |
| 639 | : __shared_count(__p, __sp_array_delete{}, allocator<void>()) |
| 640 | { } |
| 641 | |
| 642 | template<typename _Ptr, typename _Deleter, |
| 643 | typename = typename __not_alloc_shared_tag<_Deleter>::type> |
| 644 | __shared_count(_Ptr __p, _Deleter __d) |
| 645 | : __shared_count(__p, std::move(__d), allocator<void>()) |
| 646 | { } |
| 647 | |
| 648 | template<typename _Ptr, typename _Deleter, typename _Alloc, |
| 649 | typename = typename __not_alloc_shared_tag<_Deleter>::type> |
| 650 | __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0) |
| 651 | { |
| 652 | typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type; |
| 653 | __tryif (true) |
| 654 | { |
| 655 | typename _Sp_cd_type::__allocator_type __a2(__a); |
| 656 | auto __guard = std::__allocate_guarded(__a2); |
| 657 | _Sp_cd_type* __mem = __guard.get(); |
| 658 | ::new (__mem) _Sp_cd_type(__p, std::move(__d), std::move(__a)); |
| 659 | _M_pi = __mem; |
| 660 | __guard = nullptr; |
| 661 | } |
| 662 | __catch(...)if (false) |
| 663 | { |
| 664 | __d(__p); // Call _Deleter on __p. |
| 665 | __throw_exception_again; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | template<typename _Tp, typename _Alloc, typename... _Args> |
| 670 | __shared_count(_Tp*& __p, _Sp_alloc_shared_tag<_Alloc> __a, |
| 671 | _Args&&... __args) |
| 672 | { |
| 673 | typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type; |
| 674 | typename _Sp_cp_type::__allocator_type __a2(__a._M_a); |
| 675 | auto __guard = std::__allocate_guarded(__a2); |
| 676 | _Sp_cp_type* __mem = __guard.get(); |
| 677 | auto __pi = ::new (__mem) |
| 678 | _Sp_cp_type(__a._M_a, std::forward<_Args>(__args)...); |
| 679 | __guard = nullptr; |
| 680 | _M_pi = __pi; |
| 681 | __p = __pi->_M_ptr(); |
| 682 | } |
| 683 | |
| 684 | #if _GLIBCXX_USE_DEPRECATED1 |
| 685 | #pragma GCC diagnostic push |
| 686 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 687 | // Special case for auto_ptr<_Tp> to provide the strong guarantee. |
| 688 | template<typename _Tp> |
| 689 | explicit |
| 690 | __shared_count(std::auto_ptr<_Tp>&& __r); |
| 691 | #pragma GCC diagnostic pop |
| 692 | #endif |
| 693 | |
| 694 | // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee. |
| 695 | template<typename _Tp, typename _Del> |
| 696 | explicit |
| 697 | __shared_count(std::unique_ptr<_Tp, _Del>&& __r) : _M_pi(0) |
| 698 | { |
| 699 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 700 | // 2415. Inconsistency between unique_ptr and shared_ptr |
| 701 | if (__r.get() == nullptr) |
| 702 | return; |
| 703 | |
| 704 | using _Ptr = typename unique_ptr<_Tp, _Del>::pointer; |
| 705 | using _Del2 = typename conditional<is_reference<_Del>::value, |
| 706 | reference_wrapper<typename remove_reference<_Del>::type>, |
| 707 | _Del>::type; |
| 708 | using _Sp_cd_type |
| 709 | = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>; |
| 710 | using _Alloc = allocator<_Sp_cd_type>; |
| 711 | using _Alloc_traits = allocator_traits<_Alloc>; |
| 712 | _Alloc __a; |
| 713 | _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1); |
| 714 | _Alloc_traits::construct(__a, __mem, __r.release(), |
| 715 | __r.get_deleter()); // non-throwing |
| 716 | _M_pi = __mem; |
| 717 | } |
| 718 | |
| 719 | // Throw bad_weak_ptr when __r._M_get_use_count() == 0. |
| 720 | explicit __shared_count(const __weak_count<_Lp>& __r); |
| 721 | |
| 722 | // Does not throw if __r._M_get_use_count() == 0, caller must check. |
| 723 | explicit __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t); |
| 724 | |
| 725 | ~__shared_count() noexcept |
| 726 | { |
| 727 | if (_M_pi != nullptr) |
| 728 | _M_pi->_M_release(); |
| 729 | } |
| 730 | |
| 731 | __shared_count(const __shared_count& __r) noexcept |
| 732 | : _M_pi(__r._M_pi) |
| 733 | { |
| 734 | if (_M_pi != 0) |
| 735 | _M_pi->_M_add_ref_copy(); |
| 736 | } |
| 737 | |
| 738 | __shared_count& |
| 739 | operator=(const __shared_count& __r) noexcept |
| 740 | { |
| 741 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
| 742 | if (__tmp != _M_pi) |
| 743 | { |
| 744 | if (__tmp != 0) |
| 745 | __tmp->_M_add_ref_copy(); |
| 746 | if (_M_pi != 0) |
| 747 | _M_pi->_M_release(); |
| 748 | _M_pi = __tmp; |
| 749 | } |
| 750 | return *this; |
| 751 | } |
| 752 | |
| 753 | void |
| 754 | _M_swap(__shared_count& __r) noexcept |
| 755 | { |
| 756 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
| 757 | __r._M_pi = _M_pi; |
| 758 | _M_pi = __tmp; |
| 759 | } |
| 760 | |
| 761 | long |
| 762 | _M_get_use_count() const noexcept |
| 763 | { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; } |
| 764 | |
| 765 | bool |
| 766 | _M_unique() const noexcept |
| 767 | { return this->_M_get_use_count() == 1; } |
| 768 | |
| 769 | void* |
| 770 | _M_get_deleter(const std::type_info& __ti) const noexcept |
| 771 | { return _M_pi ? _M_pi->_M_get_deleter(__ti) : nullptr; } |
| 772 | |
| 773 | bool |
| 774 | _M_less(const __shared_count& __rhs) const noexcept |
| 775 | { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); } |
| 776 | |
| 777 | bool |
| 778 | _M_less(const __weak_count<_Lp>& __rhs) const noexcept |
| 779 | { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); } |
| 780 | |
| 781 | // Friend function injected into enclosing namespace and found by ADL |
| 782 | friend inline bool |
| 783 | operator==(const __shared_count& __a, const __shared_count& __b) noexcept |
| 784 | { return __a._M_pi == __b._M_pi; } |
| 785 | |
| 786 | private: |
| 787 | friend class __weak_count<_Lp>; |
| 788 | |
| 789 | _Sp_counted_base<_Lp>* _M_pi; |
| 790 | }; |
| 791 | |
| 792 | |
| 793 | template<_Lock_policy _Lp> |
| 794 | class __weak_count |
| 795 | { |
| 796 | public: |
| 797 | constexpr __weak_count() noexcept : _M_pi(nullptr) |
| 798 | { } |
| 799 | |
| 800 | __weak_count(const __shared_count<_Lp>& __r) noexcept |
| 801 | : _M_pi(__r._M_pi) |
| 802 | { |
| 803 | if (_M_pi != nullptr) |
| 804 | _M_pi->_M_weak_add_ref(); |
| 805 | } |
| 806 | |
| 807 | __weak_count(const __weak_count& __r) noexcept |
| 808 | : _M_pi(__r._M_pi) |
| 809 | { |
| 810 | if (_M_pi != nullptr) |
| 811 | _M_pi->_M_weak_add_ref(); |
| 812 | } |
| 813 | |
| 814 | __weak_count(__weak_count&& __r) noexcept |
| 815 | : _M_pi(__r._M_pi) |
| 816 | { __r._M_pi = nullptr; } |
| 817 | |
| 818 | ~__weak_count() noexcept |
| 819 | { |
| 820 | if (_M_pi != nullptr) |
| 821 | _M_pi->_M_weak_release(); |
| 822 | } |
| 823 | |
| 824 | __weak_count& |
| 825 | operator=(const __shared_count<_Lp>& __r) noexcept |
| 826 | { |
| 827 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
| 828 | if (__tmp != nullptr) |
| 829 | __tmp->_M_weak_add_ref(); |
| 830 | if (_M_pi != nullptr) |
| 831 | _M_pi->_M_weak_release(); |
| 832 | _M_pi = __tmp; |
| 833 | return *this; |
| 834 | } |
| 835 | |
| 836 | __weak_count& |
| 837 | operator=(const __weak_count& __r) noexcept |
| 838 | { |
| 839 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
| 840 | if (__tmp != nullptr) |
| 841 | __tmp->_M_weak_add_ref(); |
| 842 | if (_M_pi != nullptr) |
| 843 | _M_pi->_M_weak_release(); |
| 844 | _M_pi = __tmp; |
| 845 | return *this; |
| 846 | } |
| 847 | |
| 848 | __weak_count& |
| 849 | operator=(__weak_count&& __r) noexcept |
| 850 | { |
| 851 | if (_M_pi != nullptr) |
| 852 | _M_pi->_M_weak_release(); |
| 853 | _M_pi = __r._M_pi; |
| 854 | __r._M_pi = nullptr; |
| 855 | return *this; |
| 856 | } |
| 857 | |
| 858 | void |
| 859 | _M_swap(__weak_count& __r) noexcept |
| 860 | { |
| 861 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
| 862 | __r._M_pi = _M_pi; |
| 863 | _M_pi = __tmp; |
| 864 | } |
| 865 | |
| 866 | long |
| 867 | _M_get_use_count() const noexcept |
| 868 | { return _M_pi != nullptr ? _M_pi->_M_get_use_count() : 0; } |
| 869 | |
| 870 | bool |
| 871 | _M_less(const __weak_count& __rhs) const noexcept |
| 872 | { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); } |
| 873 | |
| 874 | bool |
| 875 | _M_less(const __shared_count<_Lp>& __rhs) const noexcept |
| 876 | { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); } |
| 877 | |
| 878 | // Friend function injected into enclosing namespace and found by ADL |
| 879 | friend inline bool |
| 880 | operator==(const __weak_count& __a, const __weak_count& __b) noexcept |
| 881 | { return __a._M_pi == __b._M_pi; } |
| 882 | |
| 883 | private: |
| 884 | friend class __shared_count<_Lp>; |
| 885 | |
| 886 | _Sp_counted_base<_Lp>* _M_pi; |
| 887 | }; |
| 888 | |
| 889 | // Now that __weak_count is defined we can define this constructor: |
| 890 | template<_Lock_policy _Lp> |
| 891 | inline |
| 892 | __shared_count<_Lp>::__shared_count(const __weak_count<_Lp>& __r) |
| 893 | : _M_pi(__r._M_pi) |
| 894 | { |
| 895 | if (_M_pi != nullptr) |
| 896 | _M_pi->_M_add_ref_lock(); |
| 897 | else |
| 898 | __throw_bad_weak_ptr(); |
| 899 | } |
| 900 | |
| 901 | // Now that __weak_count is defined we can define this constructor: |
| 902 | template<_Lock_policy _Lp> |
| 903 | inline |
| 904 | __shared_count<_Lp>:: |
| 905 | __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t) |
| 906 | : _M_pi(__r._M_pi) |
| 907 | { |
| 908 | if (_M_pi != nullptr) |
| 909 | if (!_M_pi->_M_add_ref_lock_nothrow()) |
| 910 | _M_pi = nullptr; |
| 911 | } |
| 912 | |
| 913 | #define __cpp_lib_shared_ptr_arrays201603 201603 |
| 914 | |
| 915 | // Helper traits for shared_ptr of array: |
| 916 | |
| 917 | // A pointer type Y* is said to be compatible with a pointer type T* when |
| 918 | // either Y* is convertible to T* or Y is U[N] and T is U cv []. |
| 919 | template<typename _Yp_ptr, typename _Tp_ptr> |
| 920 | struct __sp_compatible_with |
| 921 | : false_type |
| 922 | { }; |
| 923 | |
| 924 | template<typename _Yp, typename _Tp> |
| 925 | struct __sp_compatible_with<_Yp*, _Tp*> |
| 926 | : is_convertible<_Yp*, _Tp*>::type |
| 927 | { }; |
| 928 | |
| 929 | template<typename _Up, size_t _Nm> |
| 930 | struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]> |
| 931 | : true_type |
| 932 | { }; |
| 933 | |
| 934 | template<typename _Up, size_t _Nm> |
| 935 | struct __sp_compatible_with<_Up(*)[_Nm], const _Up(*)[]> |
| 936 | : true_type |
| 937 | { }; |
| 938 | |
| 939 | template<typename _Up, size_t _Nm> |
| 940 | struct __sp_compatible_with<_Up(*)[_Nm], volatile _Up(*)[]> |
| 941 | : true_type |
| 942 | { }; |
| 943 | |
| 944 | template<typename _Up, size_t _Nm> |
| 945 | struct __sp_compatible_with<_Up(*)[_Nm], const volatile _Up(*)[]> |
| 946 | : true_type |
| 947 | { }; |
| 948 | |
| 949 | // Test conversion from Y(*)[N] to U(*)[N] without forming invalid type Y[N]. |
| 950 | template<typename _Up, size_t _Nm, typename _Yp, typename = void> |
| 951 | struct __sp_is_constructible_arrN |
| 952 | : false_type |
| 953 | { }; |
| 954 | |
| 955 | template<typename _Up, size_t _Nm, typename _Yp> |
| 956 | struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>> |
| 957 | : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type |
| 958 | { }; |
| 959 | |
| 960 | // Test conversion from Y(*)[] to U(*)[] without forming invalid type Y[]. |
| 961 | template<typename _Up, typename _Yp, typename = void> |
| 962 | struct __sp_is_constructible_arr |
| 963 | : false_type |
| 964 | { }; |
| 965 | |
| 966 | template<typename _Up, typename _Yp> |
| 967 | struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>> |
| 968 | : is_convertible<_Yp(*)[], _Up(*)[]>::type |
| 969 | { }; |
| 970 | |
| 971 | // Trait to check if shared_ptr<T> can be constructed from Y*. |
| 972 | template<typename _Tp, typename _Yp> |
| 973 | struct __sp_is_constructible; |
| 974 | |
| 975 | // When T is U[N], Y(*)[N] shall be convertible to T*; |
| 976 | template<typename _Up, size_t _Nm, typename _Yp> |
| 977 | struct __sp_is_constructible<_Up[_Nm], _Yp> |
| 978 | : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type |
| 979 | { }; |
| 980 | |
| 981 | // when T is U[], Y(*)[] shall be convertible to T*; |
| 982 | template<typename _Up, typename _Yp> |
| 983 | struct __sp_is_constructible<_Up[], _Yp> |
| 984 | : __sp_is_constructible_arr<_Up, _Yp>::type |
| 985 | { }; |
| 986 | |
| 987 | // otherwise, Y* shall be convertible to T*. |
| 988 | template<typename _Tp, typename _Yp> |
| 989 | struct __sp_is_constructible |
| 990 | : is_convertible<_Yp*, _Tp*>::type |
| 991 | { }; |
| 992 | |
| 993 | |
| 994 | // Define operator* and operator-> for shared_ptr<T>. |
| 995 | template<typename _Tp, _Lock_policy _Lp, |
| 996 | bool = is_array<_Tp>::value, bool = is_void<_Tp>::value> |
| 997 | class __shared_ptr_access |
| 998 | { |
| 999 | public: |
| 1000 | using element_type = _Tp; |
| 1001 | |
| 1002 | element_type& |
| 1003 | operator*() const noexcept |
| 1004 | { |
| 1005 | __glibcxx_assert(_M_get() != nullptr); |
| 1006 | return *_M_get(); |
| 1007 | } |
| 1008 | |
| 1009 | element_type* |
| 1010 | operator->() const noexcept |
| 1011 | { |
| 1012 | _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr); |
| 1013 | return _M_get(); |
| 1014 | } |
| 1015 | |
| 1016 | private: |
| 1017 | element_type* |
| 1018 | _M_get() const noexcept |
| 1019 | { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); } |
| 1020 | }; |
| 1021 | |
| 1022 | // Define operator-> for shared_ptr<cv void>. |
| 1023 | template<typename _Tp, _Lock_policy _Lp> |
| 1024 | class __shared_ptr_access<_Tp, _Lp, false, true> |
| 1025 | { |
| 1026 | public: |
| 1027 | using element_type = _Tp; |
| 1028 | |
| 1029 | element_type* |
| 1030 | operator->() const noexcept |
| 1031 | { |
| 1032 | auto __ptr = static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); |
| 1033 | _GLIBCXX_DEBUG_PEDASSERT(__ptr != nullptr); |
| 1034 | return __ptr; |
| 1035 | } |
| 1036 | }; |
| 1037 | |
| 1038 | // Define operator[] for shared_ptr<T[]> and shared_ptr<T[N]>. |
| 1039 | template<typename _Tp, _Lock_policy _Lp> |
| 1040 | class __shared_ptr_access<_Tp, _Lp, true, false> |
| 1041 | { |
| 1042 | public: |
| 1043 | using element_type = typename remove_extent<_Tp>::type; |
| 1044 | |
| 1045 | #if __cplusplus201703L <= 201402L |
| 1046 | [[__deprecated__("shared_ptr<T[]>::operator* is absent from C++17")]] |
| 1047 | element_type& |
| 1048 | operator*() const noexcept |
| 1049 | { |
| 1050 | __glibcxx_assert(_M_get() != nullptr); |
| 1051 | return *_M_get(); |
| 1052 | } |
| 1053 | |
| 1054 | [[__deprecated__("shared_ptr<T[]>::operator-> is absent from C++17")]] |
| 1055 | element_type* |
| 1056 | operator->() const noexcept |
| 1057 | { |
| 1058 | _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr); |
| 1059 | return _M_get(); |
| 1060 | } |
| 1061 | #endif |
| 1062 | |
| 1063 | element_type& |
| 1064 | operator[](ptrdiff_t __i) const |
| 1065 | { |
| 1066 | __glibcxx_assert(_M_get() != nullptr); |
| 1067 | __glibcxx_assert(!extent<_Tp>::value || __i < extent<_Tp>::value); |
| 1068 | return _M_get()[__i]; |
| 1069 | } |
| 1070 | |
| 1071 | private: |
| 1072 | element_type* |
| 1073 | _M_get() const noexcept |
| 1074 | { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); } |
| 1075 | }; |
| 1076 | |
| 1077 | template<typename _Tp, _Lock_policy _Lp> |
| 1078 | class __shared_ptr |
| 1079 | : public __shared_ptr_access<_Tp, _Lp> |
| 1080 | { |
| 1081 | public: |
| 1082 | using element_type = typename remove_extent<_Tp>::type; |
| 1083 | |
| 1084 | private: |
| 1085 | // Constraint for taking ownership of a pointer of type _Yp*: |
| 1086 | template<typename _Yp> |
| 1087 | using _SafeConv |
| 1088 | = typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type; |
| 1089 | |
| 1090 | // Constraint for construction from shared_ptr and weak_ptr: |
| 1091 | template<typename _Yp, typename _Res = void> |
| 1092 | using _Compatible = typename |
| 1093 | enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type; |
| 1094 | |
| 1095 | // Constraint for assignment from shared_ptr and weak_ptr: |
| 1096 | template<typename _Yp> |
| 1097 | using _Assignable = _Compatible<_Yp, __shared_ptr&>; |
| 1098 | |
| 1099 | // Constraint for construction from unique_ptr: |
| 1100 | template<typename _Yp, typename _Del, typename _Res = void, |
| 1101 | typename _Ptr = typename unique_ptr<_Yp, _Del>::pointer> |
| 1102 | using _UniqCompatible = typename enable_if<__and_< |
| 1103 | __sp_compatible_with<_Yp*, _Tp*>, is_convertible<_Ptr, element_type*> |
| 1104 | >::value, _Res>::type; |
| 1105 | |
| 1106 | // Constraint for assignment from unique_ptr: |
| 1107 | template<typename _Yp, typename _Del> |
| 1108 | using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>; |
| 1109 | |
| 1110 | public: |
| 1111 | |
| 1112 | #if __cplusplus201703L > 201402L |
| 1113 | using weak_type = __weak_ptr<_Tp, _Lp>; |
| 1114 | #endif |
| 1115 | |
| 1116 | constexpr __shared_ptr() noexcept |
| 1117 | : _M_ptr(0), _M_refcount() |
| 1118 | { } |
| 1119 | |
| 1120 | template<typename _Yp, typename = _SafeConv<_Yp>> |
| 1121 | explicit |
| 1122 | __shared_ptr(_Yp* __p) |
| 1123 | : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type()) |
| 1124 | { |
| 1125 | static_assert( !is_void<_Yp>::value, "incomplete type" ); |
| 1126 | static_assert( sizeof(_Yp) > 0, "incomplete type" ); |
| 1127 | _M_enable_shared_from_this_with(__p); |
| 1128 | } |
| 1129 | |
| 1130 | template<typename _Yp, typename _Deleter, typename = _SafeConv<_Yp>> |
| 1131 | __shared_ptr(_Yp* __p, _Deleter __d) |
| 1132 | : _M_ptr(__p), _M_refcount(__p, std::move(__d)) |
| 1133 | { |
| 1134 | static_assert(__is_invocable<_Deleter&, _Yp*&>::value, |
| 1135 | "deleter expression d(p) is well-formed"); |
| 1136 | _M_enable_shared_from_this_with(__p); |
| 1137 | } |
| 1138 | |
| 1139 | template<typename _Yp, typename _Deleter, typename _Alloc, |
| 1140 | typename = _SafeConv<_Yp>> |
| 1141 | __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a) |
| 1142 | : _M_ptr(__p), _M_refcount(__p, std::move(__d), std::move(__a)) |
| 1143 | { |
| 1144 | static_assert(__is_invocable<_Deleter&, _Yp*&>::value, |
| 1145 | "deleter expression d(p) is well-formed"); |
| 1146 | _M_enable_shared_from_this_with(__p); |
| 1147 | } |
| 1148 | |
| 1149 | template<typename _Deleter> |
| 1150 | __shared_ptr(nullptr_t __p, _Deleter __d) |
| 1151 | : _M_ptr(0), _M_refcount(__p, std::move(__d)) |
| 1152 | { } |
| 1153 | |
| 1154 | template<typename _Deleter, typename _Alloc> |
| 1155 | __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a) |
| 1156 | : _M_ptr(0), _M_refcount(__p, std::move(__d), std::move(__a)) |
| 1157 | { } |
| 1158 | |
| 1159 | template<typename _Yp> |
| 1160 | __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r, |
| 1161 | element_type* __p) noexcept |
| 1162 | : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws |
| 1163 | { } |
| 1164 | |
| 1165 | __shared_ptr(const __shared_ptr&) noexcept = default; |
| 1166 | __shared_ptr& operator=(const __shared_ptr&) noexcept = default; |
| 1167 | ~__shared_ptr() = default; |
| 1168 | |
| 1169 | template<typename _Yp, typename = _Compatible<_Yp>> |
| 1170 | __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept |
| 1171 | : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) |
| 1172 | { } |
| 1173 | |
| 1174 | __shared_ptr(__shared_ptr&& __r) noexcept |
| 1175 | : _M_ptr(__r._M_ptr), _M_refcount() |
| 1176 | { |
| 1177 | _M_refcount._M_swap(__r._M_refcount); |
| 1178 | __r._M_ptr = 0; |
| 1179 | } |
| 1180 | |
| 1181 | template<typename _Yp, typename = _Compatible<_Yp>> |
| 1182 | __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept |
| 1183 | : _M_ptr(__r._M_ptr), _M_refcount() |
| 1184 | { |
| 1185 | _M_refcount._M_swap(__r._M_refcount); |
| 1186 | __r._M_ptr = 0; |
| 1187 | } |
| 1188 | |
| 1189 | template<typename _Yp, typename = _Compatible<_Yp>> |
| 1190 | explicit __shared_ptr(const __weak_ptr<_Yp, _Lp>& __r) |
| 1191 | : _M_refcount(__r._M_refcount) // may throw |
| 1192 | { |
| 1193 | // It is now safe to copy __r._M_ptr, as |
| 1194 | // _M_refcount(__r._M_refcount) did not throw. |
| 1195 | _M_ptr = __r._M_ptr; |
| 1196 | } |
| 1197 | |
| 1198 | // If an exception is thrown this constructor has no effect. |
| 1199 | template<typename _Yp, typename _Del, |
| 1200 | typename = _UniqCompatible<_Yp, _Del>> |
| 1201 | __shared_ptr(unique_ptr<_Yp, _Del>&& __r) |
| 1202 | : _M_ptr(__r.get()), _M_refcount() |
| 1203 | { |
| 1204 | auto __raw = __to_address(__r.get()); |
| 1205 | _M_refcount = __shared_count<_Lp>(std::move(__r)); |
| 1206 | _M_enable_shared_from_this_with(__raw); |
| 1207 | } |
| 1208 | |
| 1209 | #if __cplusplus201703L <= 201402L && _GLIBCXX_USE_DEPRECATED1 |
| 1210 | protected: |
| 1211 | // If an exception is thrown this constructor has no effect. |
| 1212 | template<typename _Tp1, typename _Del, |
| 1213 | typename enable_if<__and_< |
| 1214 | __not_<is_array<_Tp>>, is_array<_Tp1>, |
| 1215 | is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*> |
| 1216 | >::value, bool>::type = true> |
| 1217 | __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete) |
| 1218 | : _M_ptr(__r.get()), _M_refcount() |
| 1219 | { |
| 1220 | auto __raw = __to_address(__r.get()); |
| 1221 | _M_refcount = __shared_count<_Lp>(std::move(__r)); |
| 1222 | _M_enable_shared_from_this_with(__raw); |
| 1223 | } |
| 1224 | public: |
| 1225 | #endif |
| 1226 | |
| 1227 | #if _GLIBCXX_USE_DEPRECATED1 |
| 1228 | #pragma GCC diagnostic push |
| 1229 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 1230 | // Postcondition: use_count() == 1 and __r.get() == 0 |
| 1231 | template<typename _Yp, typename = _Compatible<_Yp>> |
| 1232 | __shared_ptr(auto_ptr<_Yp>&& __r); |
| 1233 | #pragma GCC diagnostic pop |
| 1234 | #endif |
| 1235 | |
| 1236 | constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { } |
| 1237 | |
| 1238 | template<typename _Yp> |
| 1239 | _Assignable<_Yp> |
| 1240 | operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept |
| 1241 | { |
| 1242 | _M_ptr = __r._M_ptr; |
| 1243 | _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw |
| 1244 | return *this; |
| 1245 | } |
| 1246 | |
| 1247 | #if _GLIBCXX_USE_DEPRECATED1 |
| 1248 | #pragma GCC diagnostic push |
| 1249 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 1250 | template<typename _Yp> |
| 1251 | _Assignable<_Yp> |
| 1252 | operator=(auto_ptr<_Yp>&& __r) |
| 1253 | { |
| 1254 | __shared_ptr(std::move(__r)).swap(*this); |
| 1255 | return *this; |
| 1256 | } |
| 1257 | #pragma GCC diagnostic pop |
| 1258 | #endif |
| 1259 | |
| 1260 | __shared_ptr& |
| 1261 | operator=(__shared_ptr&& __r) noexcept |
| 1262 | { |
| 1263 | __shared_ptr(std::move(__r)).swap(*this); |
| 1264 | return *this; |
| 1265 | } |
| 1266 | |
| 1267 | template<class _Yp> |
| 1268 | _Assignable<_Yp> |
| 1269 | operator=(__shared_ptr<_Yp, _Lp>&& __r) noexcept |
| 1270 | { |
| 1271 | __shared_ptr(std::move(__r)).swap(*this); |
| 1272 | return *this; |
| 1273 | } |
| 1274 | |
| 1275 | template<typename _Yp, typename _Del> |
| 1276 | _UniqAssignable<_Yp, _Del> |
| 1277 | operator=(unique_ptr<_Yp, _Del>&& __r) |
| 1278 | { |
| 1279 | __shared_ptr(std::move(__r)).swap(*this); |
| 1280 | return *this; |
| 1281 | } |
| 1282 | |
| 1283 | void |
| 1284 | reset() noexcept |
| 1285 | { __shared_ptr().swap(*this); } |
| 1286 | |
| 1287 | template<typename _Yp> |
| 1288 | _SafeConv<_Yp> |
| 1289 | reset(_Yp* __p) // _Yp must be complete. |
| 1290 | { |
| 1291 | // Catch self-reset errors. |
| 1292 | __glibcxx_assert(__p == 0 || __p != _M_ptr); |
| 1293 | __shared_ptr(__p).swap(*this); |
| 1294 | } |
| 1295 | |
| 1296 | template<typename _Yp, typename _Deleter> |
| 1297 | _SafeConv<_Yp> |
| 1298 | reset(_Yp* __p, _Deleter __d) |
| 1299 | { __shared_ptr(__p, std::move(__d)).swap(*this); } |
| 1300 | |
| 1301 | template<typename _Yp, typename _Deleter, typename _Alloc> |
| 1302 | _SafeConv<_Yp> |
| 1303 | reset(_Yp* __p, _Deleter __d, _Alloc __a) |
| 1304 | { __shared_ptr(__p, std::move(__d), std::move(__a)).swap(*this); } |
| 1305 | |
| 1306 | element_type* |
| 1307 | get() const noexcept |
| 1308 | { return _M_ptr; } |
| 1309 | |
| 1310 | explicit operator bool() const // never throws |
| 1311 | { return _M_ptr == 0 ? false : true; } |
| 1312 | |
| 1313 | bool |
| 1314 | unique() const noexcept |
| 1315 | { return _M_refcount._M_unique(); } |
| 1316 | |
| 1317 | long |
| 1318 | use_count() const noexcept |
| 1319 | { return _M_refcount._M_get_use_count(); } |
| 1320 | |
| 1321 | void |
| 1322 | swap(__shared_ptr<_Tp, _Lp>& __other) noexcept |
| 1323 | { |
| 1324 | std::swap(_M_ptr, __other._M_ptr); |
| 1325 | _M_refcount._M_swap(__other._M_refcount); |
| 1326 | } |
| 1327 | |
| 1328 | template<typename _Tp1> |
| 1329 | bool |
| 1330 | owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const noexcept |
| 1331 | { return _M_refcount._M_less(__rhs._M_refcount); } |
| 1332 | |
| 1333 | template<typename _Tp1> |
| 1334 | bool |
| 1335 | owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const noexcept |
| 1336 | { return _M_refcount._M_less(__rhs._M_refcount); } |
| 1337 | |
| 1338 | protected: |
| 1339 | // This constructor is non-standard, it is used by allocate_shared. |
| 1340 | template<typename _Alloc, typename... _Args> |
| 1341 | __shared_ptr(_Sp_alloc_shared_tag<_Alloc> __tag, _Args&&... __args) |
| 1342 | : _M_ptr(), _M_refcount(_M_ptr, __tag, std::forward<_Args>(__args)...) |
| 1343 | { _M_enable_shared_from_this_with(_M_ptr); } |
| 1344 | |
| 1345 | template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc, |
| 1346 | typename... _Args> |
| 1347 | friend __shared_ptr<_Tp1, _Lp1> |
| 1348 | __allocate_shared(const _Alloc& __a, _Args&&... __args); |
| 1349 | |
| 1350 | // This constructor is used by __weak_ptr::lock() and |
| 1351 | // shared_ptr::shared_ptr(const weak_ptr&, std::nothrow_t). |
| 1352 | __shared_ptr(const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t) |
| 1353 | : _M_refcount(__r._M_refcount, std::nothrow) |
| 1354 | { |
| 1355 | _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr : nullptr; |
| 1356 | } |
| 1357 | |
| 1358 | friend class __weak_ptr<_Tp, _Lp>; |
| 1359 | |
| 1360 | private: |
| 1361 | |
| 1362 | template<typename _Yp> |
| 1363 | using __esft_base_t = decltype(__enable_shared_from_this_base( |
| 1364 | std::declval<const __shared_count<_Lp>&>(), |
| 1365 | std::declval<_Yp*>())); |
| 1366 | |
| 1367 | // Detect an accessible and unambiguous enable_shared_from_this base. |
| 1368 | template<typename _Yp, typename = void> |
| 1369 | struct __has_esft_base |
| 1370 | : false_type { }; |
| 1371 | |
| 1372 | template<typename _Yp> |
| 1373 | struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>> |
| 1374 | : __not_<is_array<_Tp>> { }; // No enable shared_from_this for arrays |
| 1375 | |
| 1376 | template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type> |
| 1377 | typename enable_if<__has_esft_base<_Yp2>::value>::type |
| 1378 | _M_enable_shared_from_this_with(_Yp* __p) noexcept |
| 1379 | { |
| 1380 | if (auto __base = __enable_shared_from_this_base(_M_refcount, __p)) |
| 1381 | __base->_M_weak_assign(const_cast<_Yp2*>(__p), _M_refcount); |
| 1382 | } |
| 1383 | |
| 1384 | template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type> |
| 1385 | typename enable_if<!__has_esft_base<_Yp2>::value>::type |
| 1386 | _M_enable_shared_from_this_with(_Yp*) noexcept |
| 1387 | { } |
| 1388 | |
| 1389 | void* |
| 1390 | _M_get_deleter(const std::type_info& __ti) const noexcept |
| 1391 | { return _M_refcount._M_get_deleter(__ti); } |
| 1392 | |
| 1393 | template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr; |
| 1394 | template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr; |
| 1395 | |
| 1396 | template<typename _Del, typename _Tp1, _Lock_policy _Lp1> |
| 1397 | friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept; |
| 1398 | |
| 1399 | template<typename _Del, typename _Tp1> |
| 1400 | friend _Del* get_deleter(const shared_ptr<_Tp1>&) noexcept; |
| 1401 | |
| 1402 | element_type* _M_ptr; // Contained pointer. |
| 1403 | __shared_count<_Lp> _M_refcount; // Reference counter. |
| 1404 | }; |
| 1405 | |
| 1406 | |
| 1407 | // 20.7.2.2.7 shared_ptr comparisons |
| 1408 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
| 1409 | inline bool |
| 1410 | operator==(const __shared_ptr<_Tp1, _Lp>& __a, |
| 1411 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
| 1412 | { return __a.get() == __b.get(); } |
| 1413 | |
| 1414 | template<typename _Tp, _Lock_policy _Lp> |
| 1415 | inline bool |
| 1416 | operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
| 1417 | { return !__a; } |
| 1418 | |
| 1419 | template<typename _Tp, _Lock_policy _Lp> |
| 1420 | inline bool |
| 1421 | operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
| 1422 | { return !__a; } |
| 1423 | |
| 1424 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
| 1425 | inline bool |
| 1426 | operator!=(const __shared_ptr<_Tp1, _Lp>& __a, |
| 1427 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
| 1428 | { return __a.get() != __b.get(); } |
| 1429 | |
| 1430 | template<typename _Tp, _Lock_policy _Lp> |
| 1431 | inline bool |
| 1432 | operator!=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
| 1433 | { return (bool)__a; } |
| 1434 | |
| 1435 | template<typename _Tp, _Lock_policy _Lp> |
| 1436 | inline bool |
| 1437 | operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
| 1438 | { return (bool)__a; } |
| 1439 | |
| 1440 | template<typename _Tp, typename _Up, _Lock_policy _Lp> |
| 1441 | inline bool |
| 1442 | operator<(const __shared_ptr<_Tp, _Lp>& __a, |
| 1443 | const __shared_ptr<_Up, _Lp>& __b) noexcept |
| 1444 | { |
| 1445 | using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type; |
| 1446 | using _Up_elt = typename __shared_ptr<_Up, _Lp>::element_type; |
| 1447 | using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type; |
| 1448 | return less<_Vp>()(__a.get(), __b.get()); |
| 1449 | } |
| 1450 | |
| 1451 | template<typename _Tp, _Lock_policy _Lp> |
| 1452 | inline bool |
| 1453 | operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
| 1454 | { |
| 1455 | using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type; |
| 1456 | return less<_Tp_elt*>()(__a.get(), nullptr); |
| 1457 | } |
| 1458 | |
| 1459 | template<typename _Tp, _Lock_policy _Lp> |
| 1460 | inline bool |
| 1461 | operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
| 1462 | { |
| 1463 | using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type; |
| 1464 | return less<_Tp_elt*>()(nullptr, __a.get()); |
| 1465 | } |
| 1466 | |
| 1467 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
| 1468 | inline bool |
| 1469 | operator<=(const __shared_ptr<_Tp1, _Lp>& __a, |
| 1470 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
| 1471 | { return !(__b < __a); } |
| 1472 | |
| 1473 | template<typename _Tp, _Lock_policy _Lp> |
| 1474 | inline bool |
| 1475 | operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
| 1476 | { return !(nullptr < __a); } |
| 1477 | |
| 1478 | template<typename _Tp, _Lock_policy _Lp> |
| 1479 | inline bool |
| 1480 | operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
| 1481 | { return !(__a < nullptr); } |
| 1482 | |
| 1483 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
| 1484 | inline bool |
| 1485 | operator>(const __shared_ptr<_Tp1, _Lp>& __a, |
| 1486 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
| 1487 | { return (__b < __a); } |
| 1488 | |
| 1489 | template<typename _Tp, _Lock_policy _Lp> |
| 1490 | inline bool |
| 1491 | operator>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
| 1492 | { return nullptr < __a; } |
| 1493 | |
| 1494 | template<typename _Tp, _Lock_policy _Lp> |
| 1495 | inline bool |
| 1496 | operator>(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
| 1497 | { return __a < nullptr; } |
| 1498 | |
| 1499 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
| 1500 | inline bool |
| 1501 | operator>=(const __shared_ptr<_Tp1, _Lp>& __a, |
| 1502 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
| 1503 | { return !(__a < __b); } |
| 1504 | |
| 1505 | template<typename _Tp, _Lock_policy _Lp> |
| 1506 | inline bool |
| 1507 | operator>=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
| 1508 | { return !(__a < nullptr); } |
| 1509 | |
| 1510 | template<typename _Tp, _Lock_policy _Lp> |
| 1511 | inline bool |
| 1512 | operator>=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
| 1513 | { return !(nullptr < __a); } |
| 1514 | |
| 1515 | template<typename _Sp> |
| 1516 | struct _Sp_less : public binary_function<_Sp, _Sp, bool> |
| 1517 | { |
| 1518 | bool |
| 1519 | operator()(const _Sp& __lhs, const _Sp& __rhs) const noexcept |
| 1520 | { |
| 1521 | typedef typename _Sp::element_type element_type; |
| 1522 | return std::less<element_type*>()(__lhs.get(), __rhs.get()); |
| 1523 | } |
| 1524 | }; |
| 1525 | |
| 1526 | template<typename _Tp, _Lock_policy _Lp> |
| 1527 | struct less<__shared_ptr<_Tp, _Lp>> |
| 1528 | : public _Sp_less<__shared_ptr<_Tp, _Lp>> |
| 1529 | { }; |
| 1530 | |
| 1531 | // 20.7.2.2.8 shared_ptr specialized algorithms. |
| 1532 | template<typename _Tp, _Lock_policy _Lp> |
| 1533 | inline void |
| 1534 | swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept |
| 1535 | { __a.swap(__b); } |
| 1536 | |
| 1537 | // 20.7.2.2.9 shared_ptr casts |
| 1538 | |
| 1539 | // The seemingly equivalent code: |
| 1540 | // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get())) |
| 1541 | // will eventually result in undefined behaviour, attempting to |
| 1542 | // delete the same object twice. |
| 1543 | /// static_pointer_cast |
| 1544 | template<typename _Tp, typename _Tp1, _Lock_policy _Lp> |
| 1545 | inline __shared_ptr<_Tp, _Lp> |
| 1546 | static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
| 1547 | { |
| 1548 | using _Sp = __shared_ptr<_Tp, _Lp>; |
| 1549 | return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get())); |
| 1550 | } |
| 1551 | |
| 1552 | // The seemingly equivalent code: |
| 1553 | // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get())) |
| 1554 | // will eventually result in undefined behaviour, attempting to |
| 1555 | // delete the same object twice. |
| 1556 | /// const_pointer_cast |
| 1557 | template<typename _Tp, typename _Tp1, _Lock_policy _Lp> |
| 1558 | inline __shared_ptr<_Tp, _Lp> |
| 1559 | const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
| 1560 | { |
| 1561 | using _Sp = __shared_ptr<_Tp, _Lp>; |
| 1562 | return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get())); |
| 1563 | } |
| 1564 | |
| 1565 | // The seemingly equivalent code: |
| 1566 | // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get())) |
| 1567 | // will eventually result in undefined behaviour, attempting to |
| 1568 | // delete the same object twice. |
| 1569 | /// dynamic_pointer_cast |
| 1570 | template<typename _Tp, typename _Tp1, _Lock_policy _Lp> |
| 1571 | inline __shared_ptr<_Tp, _Lp> |
| 1572 | dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
| 1573 | { |
| 1574 | using _Sp = __shared_ptr<_Tp, _Lp>; |
| 1575 | if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get())) |
| 1576 | return _Sp(__r, __p); |
| 1577 | return _Sp(); |
| 1578 | } |
| 1579 | |
| 1580 | #if __cplusplus201703L > 201402L |
| 1581 | template<typename _Tp, typename _Tp1, _Lock_policy _Lp> |
| 1582 | inline __shared_ptr<_Tp, _Lp> |
| 1583 | reinterpret_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
| 1584 | { |
| 1585 | using _Sp = __shared_ptr<_Tp, _Lp>; |
| 1586 | return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get())); |
| 1587 | } |
| 1588 | #endif |
| 1589 | |
| 1590 | template<typename _Tp, _Lock_policy _Lp> |
| 1591 | class __weak_ptr |
| 1592 | { |
| 1593 | template<typename _Yp, typename _Res = void> |
| 1594 | using _Compatible = typename |
| 1595 | enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type; |
| 1596 | |
| 1597 | // Constraint for assignment from shared_ptr and weak_ptr: |
| 1598 | template<typename _Yp> |
| 1599 | using _Assignable = _Compatible<_Yp, __weak_ptr&>; |
| 1600 | |
| 1601 | public: |
| 1602 | using element_type = typename remove_extent<_Tp>::type; |
| 1603 | |
| 1604 | constexpr __weak_ptr() noexcept |
| 1605 | : _M_ptr(nullptr), _M_refcount() |
| 1606 | { } |
| 1607 | |
| 1608 | __weak_ptr(const __weak_ptr&) noexcept = default; |
| 1609 | |
| 1610 | ~__weak_ptr() = default; |
| 1611 | |
| 1612 | // The "obvious" converting constructor implementation: |
| 1613 | // |
| 1614 | // template<typename _Tp1> |
| 1615 | // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r) |
| 1616 | // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws |
| 1617 | // { } |
| 1618 | // |
| 1619 | // has a serious problem. |
| 1620 | // |
| 1621 | // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr) |
| 1622 | // conversion may require access to *__r._M_ptr (virtual inheritance). |
| 1623 | // |
| 1624 | // It is not possible to avoid spurious access violations since |
| 1625 | // in multithreaded programs __r._M_ptr may be invalidated at any point. |
| 1626 | template<typename _Yp, typename = _Compatible<_Yp>> |
| 1627 | __weak_ptr(const __weak_ptr<_Yp, _Lp>& __r) noexcept |
| 1628 | : _M_refcount(__r._M_refcount) |
| 1629 | { _M_ptr = __r.lock().get(); } |
| 1630 | |
| 1631 | template<typename _Yp, typename = _Compatible<_Yp>> |
| 1632 | __weak_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept |
| 1633 | : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) |
| 1634 | { } |
| 1635 | |
| 1636 | __weak_ptr(__weak_ptr&& __r) noexcept |
| 1637 | : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount)) |
| 1638 | { __r._M_ptr = nullptr; } |
| 1639 | |
| 1640 | template<typename _Yp, typename = _Compatible<_Yp>> |
| 1641 | __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept |
| 1642 | : _M_ptr(__r.lock().get()), _M_refcount(std::move(__r._M_refcount)) |
| 1643 | { __r._M_ptr = nullptr; } |
| 1644 | |
| 1645 | __weak_ptr& |
| 1646 | operator=(const __weak_ptr& __r) noexcept = default; |
| 1647 | |
| 1648 | template<typename _Yp> |
| 1649 | _Assignable<_Yp> |
| 1650 | operator=(const __weak_ptr<_Yp, _Lp>& __r) noexcept |
| 1651 | { |
| 1652 | _M_ptr = __r.lock().get(); |
| 1653 | _M_refcount = __r._M_refcount; |
| 1654 | return *this; |
| 1655 | } |
| 1656 | |
| 1657 | template<typename _Yp> |
| 1658 | _Assignable<_Yp> |
| 1659 | operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept |
| 1660 | { |
| 1661 | _M_ptr = __r._M_ptr; |
| 1662 | _M_refcount = __r._M_refcount; |
| 1663 | return *this; |
| 1664 | } |
| 1665 | |
| 1666 | __weak_ptr& |
| 1667 | operator=(__weak_ptr&& __r) noexcept |
| 1668 | { |
| 1669 | _M_ptr = __r._M_ptr; |
| 1670 | _M_refcount = std::move(__r._M_refcount); |
| 1671 | __r._M_ptr = nullptr; |
| 1672 | return *this; |
| 1673 | } |
| 1674 | |
| 1675 | template<typename _Yp> |
| 1676 | _Assignable<_Yp> |
| 1677 | operator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept |
| 1678 | { |
| 1679 | _M_ptr = __r.lock().get(); |
| 1680 | _M_refcount = std::move(__r._M_refcount); |
| 1681 | __r._M_ptr = nullptr; |
| 1682 | return *this; |
| 1683 | } |
| 1684 | |
| 1685 | __shared_ptr<_Tp, _Lp> |
| 1686 | lock() const noexcept |
| 1687 | { return __shared_ptr<element_type, _Lp>(*this, std::nothrow); } |
| 1688 | |
| 1689 | long |
| 1690 | use_count() const noexcept |
| 1691 | { return _M_refcount._M_get_use_count(); } |
| 1692 | |
| 1693 | bool |
| 1694 | expired() const noexcept |
| 1695 | { return _M_refcount._M_get_use_count() == 0; } |
| 1696 | |
| 1697 | template<typename _Tp1> |
| 1698 | bool |
| 1699 | owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const noexcept |
| 1700 | { return _M_refcount._M_less(__rhs._M_refcount); } |
| 1701 | |
| 1702 | template<typename _Tp1> |
| 1703 | bool |
| 1704 | owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const noexcept |
| 1705 | { return _M_refcount._M_less(__rhs._M_refcount); } |
| 1706 | |
| 1707 | void |
| 1708 | reset() noexcept |
| 1709 | { __weak_ptr().swap(*this); } |
| 1710 | |
| 1711 | void |
| 1712 | swap(__weak_ptr& __s) noexcept |
| 1713 | { |
| 1714 | std::swap(_M_ptr, __s._M_ptr); |
| 1715 | _M_refcount._M_swap(__s._M_refcount); |
| 1716 | } |
| 1717 | |
| 1718 | private: |
| 1719 | // Used by __enable_shared_from_this. |
| 1720 | void |
| 1721 | _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount) noexcept |
| 1722 | { |
| 1723 | if (use_count() == 0) |
| 1724 | { |
| 1725 | _M_ptr = __ptr; |
| 1726 | _M_refcount = __refcount; |
| 1727 | } |
| 1728 | } |
| 1729 | |
| 1730 | template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr; |
| 1731 | template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr; |
| 1732 | friend class __enable_shared_from_this<_Tp, _Lp>; |
| 1733 | friend class enable_shared_from_this<_Tp>; |
| 1734 | |
| 1735 | element_type* _M_ptr; // Contained pointer. |
| 1736 | __weak_count<_Lp> _M_refcount; // Reference counter. |
| 1737 | }; |
| 1738 | |
| 1739 | // 20.7.2.3.6 weak_ptr specialized algorithms. |
| 1740 | template<typename _Tp, _Lock_policy _Lp> |
| 1741 | inline void |
| 1742 | swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept |
| 1743 | { __a.swap(__b); } |
| 1744 | |
| 1745 | template<typename _Tp, typename _Tp1> |
| 1746 | struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool> |
| 1747 | { |
| 1748 | bool |
| 1749 | operator()(const _Tp& __lhs, const _Tp& __rhs) const noexcept |
| 1750 | { return __lhs.owner_before(__rhs); } |
| 1751 | |
| 1752 | bool |
| 1753 | operator()(const _Tp& __lhs, const _Tp1& __rhs) const noexcept |
| 1754 | { return __lhs.owner_before(__rhs); } |
| 1755 | |
| 1756 | bool |
| 1757 | operator()(const _Tp1& __lhs, const _Tp& __rhs) const noexcept |
| 1758 | { return __lhs.owner_before(__rhs); } |
| 1759 | }; |
| 1760 | |
| 1761 | template<> |
| 1762 | struct _Sp_owner_less<void, void> |
| 1763 | { |
| 1764 | template<typename _Tp, typename _Up> |
| 1765 | auto |
| 1766 | operator()(const _Tp& __lhs, const _Up& __rhs) const noexcept |
| 1767 | -> decltype(__lhs.owner_before(__rhs)) |
| 1768 | { return __lhs.owner_before(__rhs); } |
| 1769 | |
| 1770 | using is_transparent = void; |
| 1771 | }; |
| 1772 | |
| 1773 | template<typename _Tp, _Lock_policy _Lp> |
| 1774 | struct owner_less<__shared_ptr<_Tp, _Lp>> |
| 1775 | : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>> |
| 1776 | { }; |
| 1777 | |
| 1778 | template<typename _Tp, _Lock_policy _Lp> |
| 1779 | struct owner_less<__weak_ptr<_Tp, _Lp>> |
| 1780 | : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>> |
| 1781 | { }; |
| 1782 | |
| 1783 | |
| 1784 | template<typename _Tp, _Lock_policy _Lp> |
| 1785 | class __enable_shared_from_this |
| 1786 | { |
| 1787 | protected: |
| 1788 | constexpr __enable_shared_from_this() noexcept { } |
| 1789 | |
| 1790 | __enable_shared_from_this(const __enable_shared_from_this&) noexcept { } |
| 1791 | |
| 1792 | __enable_shared_from_this& |
| 1793 | operator=(const __enable_shared_from_this&) noexcept |
| 1794 | { return *this; } |
| 1795 | |
| 1796 | ~__enable_shared_from_this() { } |
| 1797 | |
| 1798 | public: |
| 1799 | __shared_ptr<_Tp, _Lp> |
| 1800 | shared_from_this() |
| 1801 | { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); } |
| 1802 | |
| 1803 | __shared_ptr<const _Tp, _Lp> |
| 1804 | shared_from_this() const |
| 1805 | { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); } |
| 1806 | |
| 1807 | #if __cplusplus201703L > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 |
| 1808 | __weak_ptr<_Tp, _Lp> |
| 1809 | weak_from_this() noexcept |
| 1810 | { return this->_M_weak_this; } |
| 1811 | |
| 1812 | __weak_ptr<const _Tp, _Lp> |
| 1813 | weak_from_this() const noexcept |
| 1814 | { return this->_M_weak_this; } |
| 1815 | #endif |
| 1816 | |
| 1817 | private: |
| 1818 | template<typename _Tp1> |
| 1819 | void |
| 1820 | _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept |
| 1821 | { _M_weak_this._M_assign(__p, __n); } |
| 1822 | |
| 1823 | friend const __enable_shared_from_this* |
| 1824 | __enable_shared_from_this_base(const __shared_count<_Lp>&, |
| 1825 | const __enable_shared_from_this* __p) |
| 1826 | { return __p; } |
| 1827 | |
| 1828 | template<typename, _Lock_policy> |
| 1829 | friend class __shared_ptr; |
| 1830 | |
| 1831 | mutable __weak_ptr<_Tp, _Lp> _M_weak_this; |
| 1832 | }; |
| 1833 | |
| 1834 | template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args> |
| 1835 | inline __shared_ptr<_Tp, _Lp> |
| 1836 | __allocate_shared(const _Alloc& __a, _Args&&... __args) |
| 1837 | { |
| 1838 | return __shared_ptr<_Tp, _Lp>(_Sp_alloc_shared_tag<_Alloc>{__a}, |
| 1839 | std::forward<_Args>(__args)...); |
| 1840 | } |
| 1841 | |
| 1842 | template<typename _Tp, _Lock_policy _Lp, typename... _Args> |
| 1843 | inline __shared_ptr<_Tp, _Lp> |
| 1844 | __make_shared(_Args&&... __args) |
| 1845 | { |
| 1846 | typedef typename std::remove_const<_Tp>::type _Tp_nc; |
| 1847 | return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(), |
| 1848 | std::forward<_Args>(__args)...); |
| 1849 | } |
| 1850 | |
| 1851 | /// std::hash specialization for __shared_ptr. |
| 1852 | template<typename _Tp, _Lock_policy _Lp> |
| 1853 | struct hash<__shared_ptr<_Tp, _Lp>> |
| 1854 | : public __hash_base<size_t, __shared_ptr<_Tp, _Lp>> |
| 1855 | { |
| 1856 | size_t |
| 1857 | operator()(const __shared_ptr<_Tp, _Lp>& __s) const noexcept |
| 1858 | { |
| 1859 | return hash<typename __shared_ptr<_Tp, _Lp>::element_type*>()( |
| 1860 | __s.get()); |
| 1861 | } |
| 1862 | }; |
| 1863 | |
| 1864 | _GLIBCXX_END_NAMESPACE_VERSION |
| 1865 | } // namespace |
| 1866 | |
| 1867 | #endif // _SHARED_PTR_BASE_H |