@@ -154,6 +154,7 @@ using NvrtcProgramHandle = std::shared_ptr<const nvrtcProgram>;
154154using NvvmProgramHandle = std::shared_ptr<const NvvmProgramValue>;
155155using NvJitLinkHandle = std::shared_ptr<const NvJitLinkValue>;
156156using CuLinkHandle = std::shared_ptr<const CUlinkState>;
157+ using FileDescriptorHandle = std::shared_ptr<const int >;
157158
158159
159160// ============================================================================
@@ -477,6 +478,17 @@ CuLinkHandle create_culink_handle(CUlinkState state);
477478// The handle will NOT be destroyed when the last reference is released.
478479CuLinkHandle create_culink_handle_ref (CUlinkState state);
479480
481+ // ============================================================================
482+ // File descriptor handle functions
483+ // ============================================================================
484+
485+ // Create an owning file descriptor handle.
486+ // When the last reference is released, POSIX close() is called.
487+ FileDescriptorHandle create_fd_handle (int fd);
488+
489+ // Create a non-owning file descriptor handle (caller manages the fd).
490+ FileDescriptorHandle create_fd_handle_ref (int fd);
491+
480492// ============================================================================
481493// Overloaded helper functions to extract raw resources from handles
482494// ============================================================================
@@ -596,6 +608,10 @@ inline std::intptr_t as_intptr(const CuLinkHandle& h) noexcept {
596608 return reinterpret_cast <std::intptr_t >(as_cu (h));
597609}
598610
611+ inline std::intptr_t as_intptr (const FileDescriptorHandle& h) noexcept {
612+ return h ? static_cast <std::intptr_t >(*h) : -1 ;
613+ }
614+
599615// as_py() - convert handle to Python wrapper object (returns new reference)
600616#if PY_VERSION_HEX < 0x030D0000
601617extern " C" int _Py_IsFinalizing (void );
@@ -687,4 +703,8 @@ inline PyObject* as_py(const GraphicsResourceHandle& h) noexcept {
687703 return detail::make_py (" cuda.bindings.driver" , " CUgraphicsResource" , as_intptr (h));
688704}
689705
706+ inline PyObject* as_py (const FileDescriptorHandle& h) noexcept {
707+ return PyLong_FromSsize_t (as_intptr (h));
708+ }
709+
690710} // namespace cuda_core
0 commit comments