o
    8ήc                     @   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 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)_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   =/tmp/pip-target-vg8gfxp4/lib/python/numba/core/runtime/nrt.py__init__   s   
z_Runtime.__init__c                 C   s   | j rdS tjD ]}|dr|}nd| }tj| }t|| qt|| _| j	d| _
| j	d| _| j	d| _t| j
| j t| j 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_nrt_atomic_addnrt_atomic_subnrt_atomic_casT)r   _nrt	c_helpers
startswithll
add_symbolr   compile_nrt_functions_libraryget_pointer_to_function_ptr_inc_ptr_dec_ptr_casmemsys_set_atomic_inc_decmemsys_set_atomic_cas)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_guard5   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   shutdown:   s   z_Runtime.shutdownc                 C   s   |    | jS )zQ
        Return the Library object containing the various NRT functions.
        )r0   r"   r   r   r   r   libraryB   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`.
        )r0   r   meminfo_newMemInfo)r   datapyobjmir   r   r   r4   J   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.)r0   
ValueErrorr   meminfo_alloc_safememinfo_allocMemoryErrorr5   )r   sizesafer/   r8   r   r   r   r<   T   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_statsk   s
   z_Runtime.get_allocation_statsN)F)__name__
__module____qualname__r   r   r-   r0   staticmethodr2   propertyr3   r4   r<   rE   r   r   r   r   r      s    
!



r   c                 C   s   t t jS )N)r   MemInfoPointervoidptr)valcr   r   r   typeof_meminfo{   s   rO   N)collectionsr   weakrefr   	_finalizenumba.core.runtimer   llvmliter   r   numba.core.compiler_lockr   numba.core.typing.typeofr   
numba.corer   r	   r   r@   objectr   _MemInfor5   registerrO   memsys_use_cpython_allocatorrtsysr2   r   r   r   r   <module>   s"    i
