o
    ۾i                     @   s   d dl mZ d dlmZ d dlmZ d dlmZ	 d dl
mZ d dlmZ d dlmZmZ d dlmZ ed	g d
ZG dd deZejZeedd Ze  e Zeeej [dS )    )
namedtuple)finalize)	nrtdynmod)binding)global_compiler_lock)typeof_impl)typesconfig)_nrt_python
nrt_mstatsallocfreemi_allocmi_freec                   @   sZ   e Zd Zdd Zedd Zdd Zedd Ze	d	d
 Z
dd ZdddZdd ZdS )_Runtimec                 C   s
   d| _ d S )NF)_initself r   J/home/ubuntu/.local/lib/python3.10/site-packages/numba/core/runtime/nrt.py__init__   s   
z_Runtime.__init__c                 C   sh   | j rdS tjrt  tjD ]}|dr|}nd| }tj| }t|| qt	
|| _d| _ dS )zInitializes the NRT

        Must be called before any actual call to the NRT API.
        Safe to be called multiple times.
        N_NRT_T)r   r	   	NRT_STATS_nrtmemsys_enable_stats	c_helpers
startswithll
add_symbolr   compile_nrt_functions_library)r   ctxpy_namec_name	c_addressr   r   r   
initialize   s   



z_Runtime.initializec                 C   s   | j s	d}t|d S )Nz'Runtime must be initialized before use.)r   RuntimeError)r   msgr   r   r   _init_guard0   s   z_Runtime._init_guardc                   C   s   t   dS )ze
        Shutdown the NRT
        Safe to be called without calling Runtime.initialize first
        N)r   memsys_shutdownr   r   r   r   shutdown5   s   z_Runtime.shutdownc                 C   s   |    | jS )zQ
        Return the Library object containing the various NRT functions.
        )r*   r"   r   r   r   r   library=   s   z_Runtime.libraryc                 C   s   |    t||}t|S )z
        Returns a MemInfo object that tracks memory at `data` owned by `pyobj`.
        MemInfo will acquire a reference on `pyobj`.
        The release of MemInfo will release a reference on `pyobj`.
        )r*   r   meminfo_newMemInfo)r   datapyobjmir   r   r   r.   E   s   z_Runtime.meminfo_newFc                 C   sb   |    |dk rd| d}t||rt|}nt|}|dkr-d| d}t|t|S )a  
        Allocate a new memory of `size` bytes and returns a MemInfo object
        that tracks the allocation.  When there is no more reference to the
        MemInfo object, the underlying memory will be deallocated.

        If `safe` flag is True, the memory is allocated using the `safe` scheme.
        This is used for debugging and testing purposes.
        See `NRT_MemInfo_alloc_safe()` in "nrt.h" for details.
        r   z,Cannot allocate a negative number of bytes: .zRequested allocation of z bytes failed.)r*   
ValueErrorr   meminfo_alloc_safememinfo_allocMemoryErrorr/   )r   sizesafer)   r2   r   r   r   r6   O   s   

z_Runtime.meminfo_allocc                 C   s    t t t t t dS )zw
        Returns a namedtuple of (alloc, free, mi_alloc, mi_free) for count of
        each memory operations.
        r   )_nrt_mstatsr   memsys_get_stats_allocmemsys_get_stats_freememsys_get_stats_mi_allocmemsys_get_stats_mi_freer   r   r   r   get_allocation_statsf   s
   z_Runtime.get_allocation_statsN)F)__name__
__module____qualname__r   r   r'   r*   staticmethodr,   propertyr-   r.   r6   r?   r   r   r   r   r      s    




r   c                 C   s   t t jS )N)r   MemInfoPointervoidptr)valcr   r   r   typeof_meminfov   s   rI   N)collectionsr   weakrefr   	_finalizenumba.core.runtimer   llvmliter   r   numba.core.compiler_lockr   numba.core.typing.typeofr   
numba.corer   r	   r
   r   r:   objectr   _MemInfor/   registerrI   memsys_use_cpython_allocatorrtsysr,   r   r   r   r   <module>   s"    d
