# SPDX-FileCopyrightText: Copyright (c) 2021-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE

cimport cuda.bindings.driver as driver
cimport cuda.bindings.cydriver as cydriver
cimport cuda.bindings.cyruntime as cyruntime
from libcpp.vector cimport vector
from cpython.buffer cimport PyBuffer_Release, Py_buffer

cdef class _HelperKernelParams:
    cdef Py_buffer _pybuffer
    cdef bint _pyobj_acquired
    cdef void** _ckernelParams
    cdef char* _ckernelParamsData
    cdef int _length
    cdef bint _malloc_list_created

cdef struct _HelperInputVoidPtrStruct:
    Py_buffer _pybuffer

cdef class _HelperInputVoidPtr:
    cdef _HelperInputVoidPtrStruct _helper
    cdef void* _cptr

cdef void * _helper_input_void_ptr(ptr, _HelperInputVoidPtrStruct *buffer)

cdef inline void * _helper_input_void_ptr_free(_HelperInputVoidPtrStruct *helper):
    if helper[0]._pybuffer.buf != NULL:
        PyBuffer_Release(&helper[0]._pybuffer)

{{if 'CUmemPool_attribute_enum' in found_types}}

cdef class _HelperCUmemPool_attribute:
    cdef void* _cptr
    cdef cydriver.CUmemPool_attribute_enum _attr
    cdef bint _is_getter

    # Return values
    cdef int _int_val
    cdef driver.cuuint64_t _cuuint64_t_val
{{endif}}
{{if 'CUmem_range_attribute_enum' in found_types}}

cdef class _HelperCUmem_range_attribute:
    cdef void* _cptr
    cdef cydriver.CUmem_range_attribute_enum _attr
    cdef size_t _data_size

    # Return values
    cdef int _int_val # 32 bit integer
    cdef int* _int_val_list # 32 bit integer array
{{endif}}
{{if 'CUpointer_attribute_enum' in found_types}}

cdef class _HelperCUpointer_attribute:
    cdef void* _cptr
    cdef cydriver.CUpointer_attribute_enum _attr
    cdef bint _is_getter

    # Return values
    cdef driver.CUcontext _ctx
    cdef unsigned int _uint
    cdef int _int
    cdef driver.CUdeviceptr _devptr
    cdef void** _void
    cdef driver.CUDA_POINTER_ATTRIBUTE_P2P_TOKENS _token
    cdef bint _bool
    cdef unsigned long long _ull
    cdef size_t _size
    cdef driver.CUmemoryPool _mempool
{{endif}}
{{if 'CUgraphMem_attribute_enum' in found_types}}

cdef class _HelperCUgraphMem_attribute:
    cdef void* _cptr
    cdef cydriver.CUgraphMem_attribute_enum _attr
    cdef bint _is_getter

    # Return values
    cdef driver.cuuint64_t _cuuint64_t_val
{{endif}}
{{if 'CUjit_option_enum' in found_types}}

cdef class _HelperCUjit_option:
    cdef void* _cptr
    cdef cydriver.CUjit_option_enum _attr

    # Return values
    cdef unsigned int _uint
    cdef float _float
    cdef char* _charstar
    cdef cydriver.CUjit_target_enum _target
    cdef cydriver.CUjit_fallback_enum _fallback
    cdef int _int
    cdef cydriver.CUjit_cacheMode_enum _cacheMode
    cdef vector[char*] _charstarstar # list of names
    cdef _InputVoidPtrPtrHelper _voidstarstar # list of addresses
{{endif}}
{{if 'cudaJitOption' in found_types}}

cdef class _HelperCudaJitOption:
    cdef void* _cptr
    cdef cyruntime.cudaJitOption _attr

    # Return values
    cdef unsigned int _uint
    cdef float _float
    cdef char* _charstar
    cdef cyruntime.cudaJit_Fallback _fallback
    cdef int _int
    cdef cyruntime.cudaJit_CacheMode _cacheMode
{{endif}}
{{if 'CUlibraryOption_enum' in found_types}}

cdef class _HelperCUlibraryOption:
    cdef void* _cptr
    cdef cydriver.CUlibraryOption_enum _attr

    # Return values
    cdef unsigned int _uint
{{endif}}
{{if 'cudaLibraryOption' in found_types}}

cdef class _HelperCudaLibraryOption:
    cdef void* _cptr
    cdef cyruntime.cudaLibraryOption _attr

    # Return values
    cdef unsigned int _uint
{{endif}}
{{if 'CUmemAllocationHandleType_enum' in found_types}}

cdef class _HelperCUmemAllocationHandleType:
    cdef void* _cptr
    cdef cydriver.CUmemAllocationHandleType_enum _type

    # Return values
    cdef int _int
    cdef void* _handle
    cdef unsigned int _d3dkmt_handle
    {{if 'CUmemFabricHandle' in found_types}}
    cdef driver.CUmemFabricHandle _mem_fabric_handle
    {{endif}}
{{endif}}

cdef class _InputVoidPtrPtrHelper:
    cdef object _references
    cdef void** _cptr

{{if 'CUcoredumpSettings_enum' in found_types}}

cdef class _HelperCUcoredumpSettings:
    cdef void* _cptr
    cdef cydriver.CUcoredumpSettings_enum _attrib
    cdef bint _is_getter
    cdef size_t _size

    # Return values
    cdef bint _bool
    cdef char* _charstar
{{endif}}
