File indexing completed on 2025-09-17 09:08:10
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #pragma once
0011
0012 #include "common.h"
0013
0014 #if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
0015 # include <pybind11/gil.h>
0016 #endif
0017
0018 #include <pybind11/pytypes.h>
0019
0020 #include <exception>
0021 #include <mutex>
0022 #include <thread>
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 #ifndef PYBIND11_INTERNALS_VERSION
0039 # if PY_VERSION_HEX >= 0x030C0000 || defined(_MSC_VER)
0040
0041
0042 # define PYBIND11_INTERNALS_VERSION 5
0043 # else
0044 # define PYBIND11_INTERNALS_VERSION 4
0045 # endif
0046 #endif
0047
0048
0049 static_assert(PY_VERSION_HEX < 0x030C0000 || PYBIND11_INTERNALS_VERSION >= 5,
0050 "pybind11 ABI version 5 is the minimum for Python 3.12+");
0051
0052 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
0053
0054 using ExceptionTranslator = void (*)(std::exception_ptr);
0055
0056 PYBIND11_NAMESPACE_BEGIN(detail)
0057
0058 constexpr const char *internals_function_record_capsule_name = "pybind11_function_record_capsule";
0059
0060
0061 inline PyTypeObject *make_static_property_type();
0062 inline PyTypeObject *make_default_metaclass();
0063 inline PyObject *make_object_base_type(PyTypeObject *metaclass);
0064
0065
0066
0067
0068
0069 #if PYBIND11_INTERNALS_VERSION > 4
0070 # define PYBIND11_TLS_KEY_REF Py_tss_t &
0071 # if defined(__clang__)
0072 # define PYBIND11_TLS_KEY_INIT(var) \
0073 _Pragma("clang diagnostic push") \
0074 _Pragma("clang diagnostic ignored \"-Wmissing-field-initializers\"") \
0075 Py_tss_t var \
0076 = Py_tss_NEEDS_INIT; \
0077 _Pragma("clang diagnostic pop")
0078 # elif defined(__GNUC__) && !defined(__INTEL_COMPILER)
0079 # define PYBIND11_TLS_KEY_INIT(var) \
0080 _Pragma("GCC diagnostic push") \
0081 _Pragma("GCC diagnostic ignored \"-Wmissing-field-initializers\"") \
0082 Py_tss_t var \
0083 = Py_tss_NEEDS_INIT; \
0084 _Pragma("GCC diagnostic pop")
0085 # else
0086 # define PYBIND11_TLS_KEY_INIT(var) Py_tss_t var = Py_tss_NEEDS_INIT;
0087 # endif
0088 # define PYBIND11_TLS_KEY_CREATE(var) (PyThread_tss_create(&(var)) == 0)
0089 # define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get(&(key))
0090 # define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set(&(key), (value))
0091 # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set(&(key), nullptr)
0092 # define PYBIND11_TLS_FREE(key) PyThread_tss_delete(&(key))
0093 #else
0094 # define PYBIND11_TLS_KEY_REF Py_tss_t *
0095 # define PYBIND11_TLS_KEY_INIT(var) Py_tss_t *var = nullptr;
0096 # define PYBIND11_TLS_KEY_CREATE(var) \
0097 (((var) = PyThread_tss_alloc()) != nullptr && (PyThread_tss_create((var)) == 0))
0098 # define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get((key))
0099 # define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set((key), (value))
0100 # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set((key), nullptr)
0101 # define PYBIND11_TLS_FREE(key) PyThread_tss_free(key)
0102 #endif
0103
0104
0105
0106
0107
0108
0109
0110 #if (PYBIND11_INTERNALS_VERSION <= 4 && defined(__GLIBCXX__)) \
0111 || (PYBIND11_INTERNALS_VERSION >= 5 && !defined(_LIBCPP_VERSION))
0112 inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { return lhs == rhs; }
0113 using type_hash = std::hash<std::type_index>;
0114 using type_equal_to = std::equal_to<std::type_index>;
0115 #else
0116 inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) {
0117 return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
0118 }
0119
0120 struct type_hash {
0121 size_t operator()(const std::type_index &t) const {
0122 size_t hash = 5381;
0123 const char *ptr = t.name();
0124 while (auto c = static_cast<unsigned char>(*ptr++)) {
0125 hash = (hash * 33) ^ c;
0126 }
0127 return hash;
0128 }
0129 };
0130
0131 struct type_equal_to {
0132 bool operator()(const std::type_index &lhs, const std::type_index &rhs) const {
0133 return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
0134 }
0135 };
0136 #endif
0137
0138 template <typename value_type>
0139 using type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>;
0140
0141 struct override_hash {
0142 inline size_t operator()(const std::pair<const PyObject *, const char *> &v) const {
0143 size_t value = std::hash<const void *>()(v.first);
0144 value ^= std::hash<const void *>()(v.second) + 0x9e3779b9 + (value << 6) + (value >> 2);
0145 return value;
0146 }
0147 };
0148
0149 using instance_map = std::unordered_multimap<const void *, instance *>;
0150
0151 #ifdef Py_GIL_DISABLED
0152
0153 class pymutex {
0154 PyMutex mutex;
0155
0156 public:
0157 pymutex() : mutex({}) {}
0158 void lock() { PyMutex_Lock(&mutex); }
0159 void unlock() { PyMutex_Unlock(&mutex); }
0160 };
0161
0162
0163 struct instance_map_shard {
0164 instance_map registered_instances;
0165 pymutex mutex;
0166
0167 char padding[64 - (sizeof(instance_map) + sizeof(pymutex)) % 64];
0168 };
0169
0170 static_assert(sizeof(instance_map_shard) % 64 == 0,
0171 "instance_map_shard size is not a multiple of 64 bytes");
0172 #endif
0173
0174
0175
0176
0177 struct internals {
0178 #ifdef Py_GIL_DISABLED
0179 pymutex mutex;
0180 #endif
0181
0182 type_map<type_info *> registered_types_cpp;
0183
0184 std::unordered_map<PyTypeObject *, std::vector<type_info *>> registered_types_py;
0185 #ifdef Py_GIL_DISABLED
0186 std::unique_ptr<instance_map_shard[]> instance_shards;
0187 size_t instance_shards_mask;
0188 #else
0189 instance_map registered_instances;
0190 #endif
0191 std::unordered_set<std::pair<const PyObject *, const char *>, override_hash>
0192 inactive_override_cache;
0193 type_map<std::vector<bool (*)(PyObject *, void *&)>> direct_conversions;
0194 std::unordered_map<const PyObject *, std::vector<PyObject *>> patients;
0195 std::forward_list<ExceptionTranslator> registered_exception_translators;
0196 std::unordered_map<std::string, void *> shared_data;
0197
0198 #if PYBIND11_INTERNALS_VERSION == 4
0199 std::vector<PyObject *> unused_loader_patient_stack_remove_at_v5;
0200 #endif
0201 std::forward_list<std::string> static_strings;
0202
0203 PyTypeObject *static_property_type;
0204 PyTypeObject *default_metaclass;
0205 PyObject *instance_base;
0206
0207 PYBIND11_TLS_KEY_INIT(tstate)
0208 #if PYBIND11_INTERNALS_VERSION > 4
0209 PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key)
0210 #endif
0211
0212 PyInterpreterState *istate = nullptr;
0213
0214 #if PYBIND11_INTERNALS_VERSION > 4
0215
0216
0217 std::string function_record_capsule_name = internals_function_record_capsule_name;
0218 #endif
0219
0220 internals() = default;
0221 internals(const internals &other) = delete;
0222 internals &operator=(const internals &other) = delete;
0223 ~internals() {
0224 #if PYBIND11_INTERNALS_VERSION > 4
0225 PYBIND11_TLS_FREE(loader_life_support_tls_key);
0226 #endif
0227
0228
0229
0230
0231
0232
0233
0234
0235 PYBIND11_TLS_FREE(tstate);
0236 }
0237 };
0238
0239
0240
0241 struct type_info {
0242 PyTypeObject *type;
0243 const std::type_info *cpptype;
0244 size_t type_size, type_align, holder_size_in_ptrs;
0245 void *(*operator_new)(size_t);
0246 void (*init_instance)(instance *, const void *);
0247 void (*dealloc)(value_and_holder &v_h);
0248 std::vector<PyObject *(*) (PyObject *, PyTypeObject *)> implicit_conversions;
0249 std::vector<std::pair<const std::type_info *, void *(*) (void *)>> implicit_casts;
0250 std::vector<bool (*)(PyObject *, void *&)> *direct_conversions;
0251 buffer_info *(*get_buffer)(PyObject *, void *) = nullptr;
0252 void *get_buffer_data = nullptr;
0253 void *(*module_local_load)(PyObject *, const type_info *) = nullptr;
0254
0255
0256
0257
0258 bool simple_type : 1;
0259
0260 bool simple_ancestors : 1;
0261
0262 bool default_holder : 1;
0263
0264 bool module_local : 1;
0265 };
0266
0267
0268 #if defined(_MSC_VER) && defined(_DEBUG)
0269 # define PYBIND11_BUILD_TYPE "_debug"
0270 #else
0271 # define PYBIND11_BUILD_TYPE ""
0272 #endif
0273
0274
0275
0276
0277 #ifndef PYBIND11_COMPILER_TYPE
0278 # if defined(_MSC_VER)
0279 # define PYBIND11_COMPILER_TYPE "_msvc"
0280 # elif defined(__INTEL_COMPILER)
0281 # define PYBIND11_COMPILER_TYPE "_icc"
0282 # elif defined(__clang__)
0283 # define PYBIND11_COMPILER_TYPE "_clang"
0284 # elif defined(__PGI)
0285 # define PYBIND11_COMPILER_TYPE "_pgi"
0286 # elif defined(__MINGW32__)
0287 # define PYBIND11_COMPILER_TYPE "_mingw"
0288 # elif defined(__CYGWIN__)
0289 # define PYBIND11_COMPILER_TYPE "_gcc_cygwin"
0290 # elif defined(__GNUC__)
0291 # define PYBIND11_COMPILER_TYPE "_gcc"
0292 # else
0293 # define PYBIND11_COMPILER_TYPE "_unknown"
0294 # endif
0295 #endif
0296
0297
0298 #ifndef PYBIND11_STDLIB
0299 # if defined(_LIBCPP_VERSION)
0300 # define PYBIND11_STDLIB "_libcpp"
0301 # elif defined(__GLIBCXX__) || defined(__GLIBCPP__)
0302 # define PYBIND11_STDLIB "_libstdcpp"
0303 # else
0304 # define PYBIND11_STDLIB ""
0305 # endif
0306 #endif
0307
0308
0309
0310 #ifndef PYBIND11_BUILD_ABI
0311 # if defined(__GXX_ABI_VERSION)
0312 # define PYBIND11_BUILD_ABI "_cxxabi" PYBIND11_TOSTRING(__GXX_ABI_VERSION)
0313 # elif defined(_MSC_VER)
0314 # define PYBIND11_BUILD_ABI "_mscver" PYBIND11_TOSTRING(_MSC_VER)
0315 # else
0316 # define PYBIND11_BUILD_ABI ""
0317 # endif
0318 #endif
0319
0320 #ifndef PYBIND11_INTERNALS_KIND
0321 # define PYBIND11_INTERNALS_KIND ""
0322 #endif
0323
0324 #define PYBIND11_PLATFORM_ABI_ID \
0325 PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \
0326 PYBIND11_BUILD_TYPE
0327
0328 #define PYBIND11_INTERNALS_ID \
0329 "__pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
0330 PYBIND11_PLATFORM_ABI_ID "__"
0331
0332 #define PYBIND11_MODULE_LOCAL_ID \
0333 "__pybind11_module_local_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
0334 PYBIND11_PLATFORM_ABI_ID "__"
0335
0336
0337
0338 inline internals **&get_internals_pp() {
0339 static internals **internals_pp = nullptr;
0340 return internals_pp;
0341 }
0342
0343
0344 inline void translate_exception(std::exception_ptr);
0345
0346 template <class T,
0347 enable_if_t<std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0>
0348 bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
0349 std::exception_ptr nested = exc.nested_ptr();
0350 if (nested != nullptr && nested != p) {
0351 translate_exception(nested);
0352 return true;
0353 }
0354 return false;
0355 }
0356
0357 template <class T,
0358 enable_if_t<!std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0>
0359 bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
0360 if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(exc))) {
0361 return handle_nested_exception(*nep, p);
0362 }
0363 return false;
0364 }
0365
0366 inline bool raise_err(PyObject *exc_type, const char *msg) {
0367 if (PyErr_Occurred()) {
0368 raise_from(exc_type, msg);
0369 return true;
0370 }
0371 set_error(exc_type, msg);
0372 return false;
0373 }
0374
0375 inline void translate_exception(std::exception_ptr p) {
0376 if (!p) {
0377 return;
0378 }
0379 try {
0380 std::rethrow_exception(p);
0381 } catch (error_already_set &e) {
0382 handle_nested_exception(e, p);
0383 e.restore();
0384 return;
0385 } catch (const builtin_exception &e) {
0386
0387 if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(e))) {
0388 handle_nested_exception(*nep, p);
0389 }
0390 e.set_error();
0391 return;
0392 } catch (const std::bad_alloc &e) {
0393 handle_nested_exception(e, p);
0394 raise_err(PyExc_MemoryError, e.what());
0395 return;
0396 } catch (const std::domain_error &e) {
0397 handle_nested_exception(e, p);
0398 raise_err(PyExc_ValueError, e.what());
0399 return;
0400 } catch (const std::invalid_argument &e) {
0401 handle_nested_exception(e, p);
0402 raise_err(PyExc_ValueError, e.what());
0403 return;
0404 } catch (const std::length_error &e) {
0405 handle_nested_exception(e, p);
0406 raise_err(PyExc_ValueError, e.what());
0407 return;
0408 } catch (const std::out_of_range &e) {
0409 handle_nested_exception(e, p);
0410 raise_err(PyExc_IndexError, e.what());
0411 return;
0412 } catch (const std::range_error &e) {
0413 handle_nested_exception(e, p);
0414 raise_err(PyExc_ValueError, e.what());
0415 return;
0416 } catch (const std::overflow_error &e) {
0417 handle_nested_exception(e, p);
0418 raise_err(PyExc_OverflowError, e.what());
0419 return;
0420 } catch (const std::exception &e) {
0421 handle_nested_exception(e, p);
0422 raise_err(PyExc_RuntimeError, e.what());
0423 return;
0424 } catch (const std::nested_exception &e) {
0425 handle_nested_exception(e, p);
0426 raise_err(PyExc_RuntimeError, "Caught an unknown nested exception!");
0427 return;
0428 } catch (...) {
0429 raise_err(PyExc_RuntimeError, "Caught an unknown exception!");
0430 return;
0431 }
0432 }
0433
0434 #if !defined(__GLIBCXX__)
0435 inline void translate_local_exception(std::exception_ptr p) {
0436 try {
0437 if (p) {
0438 std::rethrow_exception(p);
0439 }
0440 } catch (error_already_set &e) {
0441 e.restore();
0442 return;
0443 } catch (const builtin_exception &e) {
0444 e.set_error();
0445 return;
0446 }
0447 }
0448 #endif
0449
0450 inline object get_python_state_dict() {
0451 object state_dict;
0452 #if PYBIND11_INTERNALS_VERSION <= 4 || PY_VERSION_HEX < 0x03080000 || defined(PYPY_VERSION)
0453 state_dict = reinterpret_borrow<object>(PyEval_GetBuiltins());
0454 #else
0455 # if PY_VERSION_HEX < 0x03090000
0456 PyInterpreterState *istate = _PyInterpreterState_Get();
0457 # else
0458 PyInterpreterState *istate = PyInterpreterState_Get();
0459 # endif
0460 if (istate) {
0461 state_dict = reinterpret_borrow<object>(PyInterpreterState_GetDict(istate));
0462 }
0463 #endif
0464 if (!state_dict) {
0465 raise_from(PyExc_SystemError, "pybind11::detail::get_python_state_dict() FAILED");
0466 throw error_already_set();
0467 }
0468 return state_dict;
0469 }
0470
0471 inline object get_internals_obj_from_state_dict(handle state_dict) {
0472 return reinterpret_steal<object>(
0473 dict_getitemstringref(state_dict.ptr(), PYBIND11_INTERNALS_ID));
0474 }
0475
0476 inline internals **get_internals_pp_from_capsule(handle obj) {
0477 void *raw_ptr = PyCapsule_GetPointer(obj.ptr(), nullptr);
0478 if (raw_ptr == nullptr) {
0479 raise_from(PyExc_SystemError, "pybind11::detail::get_internals_pp_from_capsule() FAILED");
0480 throw error_already_set();
0481 }
0482 return static_cast<internals **>(raw_ptr);
0483 }
0484
0485 inline uint64_t round_up_to_next_pow2(uint64_t x) {
0486
0487
0488 x--;
0489 x |= (x >> 1);
0490 x |= (x >> 2);
0491 x |= (x >> 4);
0492 x |= (x >> 8);
0493 x |= (x >> 16);
0494 x |= (x >> 32);
0495 x++;
0496 return x;
0497 }
0498
0499
0500 PYBIND11_NOINLINE internals &get_internals() {
0501 auto **&internals_pp = get_internals_pp();
0502 if (internals_pp && *internals_pp) {
0503 return **internals_pp;
0504 }
0505
0506 #if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
0507 gil_scoped_acquire gil;
0508 #else
0509
0510
0511 struct gil_scoped_acquire_local {
0512 gil_scoped_acquire_local() : state(PyGILState_Ensure()) {}
0513 gil_scoped_acquire_local(const gil_scoped_acquire_local &) = delete;
0514 gil_scoped_acquire_local &operator=(const gil_scoped_acquire_local &) = delete;
0515 ~gil_scoped_acquire_local() { PyGILState_Release(state); }
0516 const PyGILState_STATE state;
0517 } gil;
0518 #endif
0519 error_scope err_scope;
0520
0521 dict state_dict = get_python_state_dict();
0522 if (object internals_obj = get_internals_obj_from_state_dict(state_dict)) {
0523 internals_pp = get_internals_pp_from_capsule(internals_obj);
0524 }
0525 if (internals_pp && *internals_pp) {
0526
0527
0528
0529
0530
0531
0532
0533 #if !defined(__GLIBCXX__)
0534 (*internals_pp)->registered_exception_translators.push_front(&translate_local_exception);
0535 #endif
0536 } else {
0537 if (!internals_pp) {
0538 internals_pp = new internals *();
0539 }
0540 auto *&internals_ptr = *internals_pp;
0541 internals_ptr = new internals();
0542
0543 PyThreadState *tstate = PyThreadState_Get();
0544
0545 if (!PYBIND11_TLS_KEY_CREATE(internals_ptr->tstate)) {
0546 pybind11_fail("get_internals: could not successfully initialize the tstate TSS key!");
0547 }
0548 PYBIND11_TLS_REPLACE_VALUE(internals_ptr->tstate, tstate);
0549
0550 #if PYBIND11_INTERNALS_VERSION > 4
0551
0552 if (!PYBIND11_TLS_KEY_CREATE(internals_ptr->loader_life_support_tls_key)) {
0553 pybind11_fail("get_internals: could not successfully initialize the "
0554 "loader_life_support TSS key!");
0555 }
0556 #endif
0557 internals_ptr->istate = tstate->interp;
0558 state_dict[PYBIND11_INTERNALS_ID] = capsule(reinterpret_cast<void *>(internals_pp));
0559 internals_ptr->registered_exception_translators.push_front(&translate_exception);
0560 internals_ptr->static_property_type = make_static_property_type();
0561 internals_ptr->default_metaclass = make_default_metaclass();
0562 internals_ptr->instance_base = make_object_base_type(internals_ptr->default_metaclass);
0563 #ifdef Py_GIL_DISABLED
0564
0565 auto num_shards
0566 = static_cast<size_t>(round_up_to_next_pow2(2 * std::thread::hardware_concurrency()));
0567 if (num_shards == 0) {
0568 num_shards = 1;
0569 }
0570 internals_ptr->instance_shards.reset(new instance_map_shard[num_shards]);
0571 internals_ptr->instance_shards_mask = num_shards - 1;
0572 #endif
0573 }
0574 return **internals_pp;
0575 }
0576
0577
0578
0579
0580
0581
0582
0583 struct local_internals {
0584 type_map<type_info *> registered_types_cpp;
0585 std::forward_list<ExceptionTranslator> registered_exception_translators;
0586 #if PYBIND11_INTERNALS_VERSION == 4
0587
0588
0589
0590
0591
0592
0593
0594 PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key)
0595
0596
0597 struct shared_loader_life_support_data {
0598 PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key)
0599 shared_loader_life_support_data() {
0600
0601 if (!PYBIND11_TLS_KEY_CREATE(loader_life_support_tls_key)) {
0602 pybind11_fail("local_internals: could not successfully initialize the "
0603 "loader_life_support TLS key!");
0604 }
0605 }
0606
0607 };
0608
0609 local_internals() {
0610 auto &internals = get_internals();
0611
0612 auto &ptr = internals.shared_data["_life_support"];
0613 if (!ptr) {
0614 ptr = new shared_loader_life_support_data;
0615 }
0616 loader_life_support_tls_key
0617 = static_cast<shared_loader_life_support_data *>(ptr)->loader_life_support_tls_key;
0618 }
0619 #endif
0620 };
0621
0622
0623 inline local_internals &get_local_internals() {
0624
0625
0626
0627
0628
0629 static auto *locals = new local_internals();
0630 return *locals;
0631 }
0632
0633 #ifdef Py_GIL_DISABLED
0634 # define PYBIND11_LOCK_INTERNALS(internals) std::unique_lock<pymutex> lock((internals).mutex)
0635 #else
0636 # define PYBIND11_LOCK_INTERNALS(internals)
0637 #endif
0638
0639 template <typename F>
0640 inline auto with_internals(const F &cb) -> decltype(cb(get_internals())) {
0641 auto &internals = get_internals();
0642 PYBIND11_LOCK_INTERNALS(internals);
0643 return cb(internals);
0644 }
0645
0646 inline std::uint64_t mix64(std::uint64_t z) {
0647
0648
0649
0650 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
0651 z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
0652 return z ^ (z >> 31);
0653 }
0654
0655 template <typename F>
0656 inline auto with_instance_map(const void *ptr,
0657 const F &cb) -> decltype(cb(std::declval<instance_map &>())) {
0658 auto &internals = get_internals();
0659
0660 #ifdef Py_GIL_DISABLED
0661
0662
0663
0664
0665
0666 auto addr = reinterpret_cast<std::uintptr_t>(ptr);
0667 auto hash = mix64(static_cast<std::uint64_t>(addr >> 20));
0668 auto idx = static_cast<size_t>(hash & internals.instance_shards_mask);
0669
0670 auto &shard = internals.instance_shards[idx];
0671 std::unique_lock<pymutex> lock(shard.mutex);
0672 return cb(shard.registered_instances);
0673 #else
0674 (void) ptr;
0675 return cb(internals.registered_instances);
0676 #endif
0677 }
0678
0679
0680
0681 inline size_t num_registered_instances() {
0682 auto &internals = get_internals();
0683 #ifdef Py_GIL_DISABLED
0684 size_t count = 0;
0685 for (size_t i = 0; i <= internals.instance_shards_mask; ++i) {
0686 auto &shard = internals.instance_shards[i];
0687 std::unique_lock<pymutex> lock(shard.mutex);
0688 count += shard.registered_instances.size();
0689 }
0690 return count;
0691 #else
0692 return internals.registered_instances.size();
0693 #endif
0694 }
0695
0696
0697
0698
0699
0700 template <typename... Args>
0701 const char *c_str(Args &&...args) {
0702
0703
0704 auto &internals = get_internals();
0705 PYBIND11_LOCK_INTERNALS(internals);
0706 auto &strings = internals.static_strings;
0707 strings.emplace_front(std::forward<Args>(args)...);
0708 return strings.front().c_str();
0709 }
0710
0711 inline const char *get_function_record_capsule_name() {
0712 #if PYBIND11_INTERNALS_VERSION > 4
0713 return get_internals().function_record_capsule_name.c_str();
0714 #else
0715 return nullptr;
0716 #endif
0717 }
0718
0719
0720
0721
0722
0723
0724
0725 inline bool is_function_record_capsule(const capsule &cap) {
0726
0727 return cap.name() == get_function_record_capsule_name();
0728 }
0729
0730 PYBIND11_NAMESPACE_END(detail)
0731
0732
0733
0734
0735 PYBIND11_NOINLINE void *get_shared_data(const std::string &name) {
0736 return detail::with_internals([&](detail::internals &internals) {
0737 auto it = internals.shared_data.find(name);
0738 return it != internals.shared_data.end() ? it->second : nullptr;
0739 });
0740 }
0741
0742
0743 PYBIND11_NOINLINE void *set_shared_data(const std::string &name, void *data) {
0744 return detail::with_internals([&](detail::internals &internals) {
0745 internals.shared_data[name] = data;
0746 return data;
0747 });
0748 }
0749
0750
0751
0752
0753 template <typename T>
0754 T &get_or_create_shared_data(const std::string &name) {
0755 return *detail::with_internals([&](detail::internals &internals) {
0756 auto it = internals.shared_data.find(name);
0757 T *ptr = (T *) (it != internals.shared_data.end() ? it->second : nullptr);
0758 if (!ptr) {
0759 ptr = new T();
0760 internals.shared_data[name] = ptr;
0761 }
0762 return ptr;
0763 });
0764 }
0765
0766 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)