B
     \Y                 @   s   d dl mZmZ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 dd
lmZ edddddgZG dd deZejZeedd Ze  e Zeeej [dS )    )print_functionabsolute_importdivision)
namedtuple   )	nrtdynmod)binding)finalize)global_compiler_lock)typeof_impl)types)_nrt_pythonZ
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   0lib/python3.7/site-packages/numba/runtime/nrt.py__init__   s    z_Runtime.__init__c             C   s   | j r
dS x,tjD ]"}d| }tj| }t|| qW 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.
        NZNRT_Znrt_atomic_addZnrt_atomic_subZnrt_atomic_casT)r   _nrtZ	c_helpersllZ
add_symbolr   Zcompile_nrt_functions_libraryZget_pointer_to_functionZ_ptr_incZ_ptr_decZ_ptr_casZmemsys_set_atomic_inc_decZmemsys_set_atomic_cas)r   ZctxZpy_nameZc_nameZ	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_guard3   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   Zmemsys_shutdownr   r   r   r   shutdown8   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   dataZpyobjmir   r   r   r!   H   s    z_Runtime.meminfo_newFc             C   s*   |    |rt|}n
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   r   Zmeminfo_alloc_safememinfo_allocr"   )r   sizeZsafer$   r   r   r   r%   R   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   r   r   r   )_nrt_mstatsr   Zmemsys_get_stats_allocZmemsys_get_stats_freeZmemsys_get_stats_mi_allocZmemsys_get_stats_mi_free)r   r   r   r   get_allocation_statsc   s    z_Runtime.get_allocation_statsN)F)__name__
__module____qualname__r   r
   r   r   staticmethodr   propertyr    r!   r%   r(   r   r   r   r   r      s   

r   c             C   s   t t jS )N)r   ZMemInfoPointerZvoidptr)valcr   r   r   typeof_meminfos   s    r0   N) Z
__future__r   r   r   collectionsr    r   Zllvmliter   r   Znumba.utilsr	   Z	_finalizeZnumba.compiler_lockr
   Znumba.typing.typeofr   Znumbar   r   r   r'   objectr   Z_MemInfor"   registerr0   Zmemsys_use_cpython_allocatorZrtsysr   r   r   r   r   <module>   s    _