U
    ?hH>  ã                   @   sz   d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	 d dl
mZ eddƒZedd	d
ƒZedddƒZG dd„ deƒZdS )é    N)Ú
namedtuple)Úir)ÚtypesÚcgutilsÚerrorsÚconfig)Ú	PYVERSIONÚ_NRT_Meminfo_Functions)ÚallocÚ
alloc_dtorÚalloc_alignedZNRT_MemInfo_alloc_safeZNRT_MemInfo_alloc_dtor_safeZNRT_MemInfo_alloc_safe_alignedZNRT_MemInfo_allocZNRT_MemInfo_alloc_dtorZNRT_MemInfo_alloc_alignedc                   @   s(  e Zd ZdZdd„ Zdd„ Zdd„ Zedd	„ ƒZd
d„ Zdd„ Z	edd„ ƒZ
dd„ Zedd„ ƒZdd„ Zedd„ ƒZdd„ Zedd„ ƒZdd„ Zedd„ ƒZd d!„ Zed"d#„ ƒZd$d%„ Zed&d'„ ƒZd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Zd:d;„ Z d<d=„ Z!d>d?„ Z"d@S )AÚ
NRTContextzF
    An object providing access to NRT APIs in the lowering pass.
    c                 C   s$   || _ || _tjrt| _nt| _d S )N)Ú_contextÚ_enabledr   Z	DEBUG_NRTÚ_NRT_MEMINFO_SAFE_APIÚ_meminfo_apiÚ_NRT_MEMINFO_DEFAULT_API)ÚselfÚcontextZenabled© r   úL/var/www/html/venv/lib/python3.8/site-packages/numba/core/runtime/context.pyÚ__init__   s
    zNRTContext.__init__c                 C   s   | j st d¡‚d S )NzNRT required but not enabled)r   r   ZNumbaRuntimeError)r   r   r   r   Ú_require_nrt(   s    zNRTContext._require_nrtc                    s   t  ˆ ¡‡ fdd„ƒ}|S )Nc                    s.   ˆ | |f|ž|Ž}d}t j| j|||d |S )Nz'Allocation failed (probably too large).)Úmsg)r   Zguard_memory_errorr   )r   ÚbuilderÚargsÚkwargsZmemptrr   ©Úfuncr   r   Úwrap-   s    z+NRTContext._check_null_result.<locals>.wrap)Ú	functoolsÚwraps)r   r   r   r   r   Ú_check_null_result,   s    zNRTContext._check_null_resultc                 C   s   |   ||¡S )zÂ
        Low-level allocate a new memory area of `size` bytes. The result of the
        call is checked and if it is NULL, i.e. allocation failed, then a
        MemoryError is raised.
        )Úallocate_unchecked©r   r   Úsizer   r   r   Úallocate5   s    zNRTContext.allocatec                 C   sH   |   ¡  |j}t tjtjg¡}t ||d¡}|j 	d¡ | 
||g¡S )zƒ
        Low-level allocate a new memory area of `size` bytes. Returns NULL to
        indicate error/failure to allocate.
        ZNRT_AllocateÚnoalias©r   Úmoduler   ÚFunctionTyper   Ú	voidptr_tÚintp_tÚget_or_insert_functionÚreturn_valueÚadd_attributeÚcall©r   r   r%   ÚmodÚfntyÚfnr   r   r   r#   >   s    zNRTContext.allocate_uncheckedc                 C   s>   |   ¡  |j}t t ¡ tjg¡}t ||d¡}| ||g¡S )zI
        Low-level free a memory area allocated with allocate().
        ZNRT_Free©	r   r)   r   r*   ZVoidTyper   r+   r-   r0   )r   r   Úptrr2   r3   r4   r   r   r   ÚfreeK   s
    zNRTContext.freec                 C   s   |   ||¡S )zø
        Allocate a new MemInfo with a data payload of `size` bytes.

        A pointer to the MemInfo is returned.

        The result of the call is checked and if it is NULL, i.e. allocation
        failed, then a MemoryError is raised.
        )Úmeminfo_alloc_uncheckedr$   r   r   r   Úmeminfo_allocV   s    
zNRTContext.meminfo_allocc                 C   sL   |   ¡  |j}t tjtjg¡}t ||| jj	¡}|j
 d¡ | ||g¡S )z¹
        Allocate a new MemInfo with a data payload of `size` bytes.

        A pointer to the MemInfo is returned.

        Returns NULL to indicate error/failure to allocate.
        r'   )r   r)   r   r*   r   r+   r,   r-   r   r
   r.   r/   r0   r1   r   r   r   r8   b   s    ÿz"NRTContext.meminfo_alloc_uncheckedc                 C   s   |   |||¡S )a  
        Allocate a new MemInfo with a data payload of `size` bytes and a
        destructor `dtor`.

        A pointer to the MemInfo is returned.

        The result of the call is checked and if it is NULL, i.e. allocation
        failed, then a MemoryError is raised.
        )Úmeminfo_alloc_dtor_unchecked©r   r   r%   Údtorr   r   r   Úmeminfo_alloc_dtors   s    zNRTContext.meminfo_alloc_dtorc                 C   s\   |   ¡  |j}t tjtjtjg¡}t ||| jj	¡}|j
 d¡ | ||| |tj¡g¡S )zÙ
        Allocate a new MemInfo with a data payload of `size` bytes and a
        destructor `dtor`.

        A pointer to the MemInfo is returned.

        Returns NULL to indicate error/failure to allocate.
        r'   )r   r)   r   r*   r   r+   r,   r-   r   r   r.   r/   r0   Zbitcast©r   r   r%   r<   r2   r3   r4   r   r   r   r:   €   s    	
ÿÿÿz'NRTContext.meminfo_alloc_dtor_uncheckedc                 C   s   |   |||¡S )a{  
        Allocate a new MemInfo with an aligned data payload of `size` bytes.
        The data pointer is aligned to `align` bytes.  `align` can be either
        a Python int or a LLVM uint32 value.

        A pointer to the MemInfo is returned.

        The result of the call is checked and if it is NULL, i.e. allocation
        failed, then a MemoryError is raised.
        )Úmeminfo_alloc_aligned_unchecked)r   r   r%   Úalignr   r   r   Úmeminfo_alloc_aligned”   s    z NRTContext.meminfo_alloc_alignedc                 C   sˆ   |   ¡  |j}t d¡}t tjtj|g¡}t ||| j	j
¡}|j d¡ t|tƒrf| j tj|¡}n|j|ksxtdƒ‚| |||g¡S )a<  
        Allocate a new MemInfo with an aligned data payload of `size` bytes.
        The data pointer is aligned to `align` bytes.  `align` can be either
        a Python int or a LLVM uint32 value.

        A pointer to the MemInfo is returned.

        Returns NULL to indicate error/failure to allocate.
        é    r'   zalign must be a uint32)r   r)   r   ZIntTyper*   r   r+   r,   r-   r   r   r.   r/   Ú
isinstanceÚintr   Zget_constantr   Zuint32ÚtypeÚAssertionErrorr0   )r   r   r%   r@   r2   Úu32r3   r4   r   r   r   r?   ¢   s    

ÿ
z*NRTContext.meminfo_alloc_aligned_uncheckedc                 C   s   |   ||¡S )aˆ  
        Allocate a MemInfo pointing to a variable-sized data area.  The area
        is separately allocated (i.e. two allocations are made) so that
        re-allocating it doesn't change the MemInfo's address.

        A pointer to the MemInfo is returned.

        The result of the call is checked and if it is NULL, i.e. allocation
        failed, then a MemoryError is raised.
        )Úmeminfo_new_varsize_uncheckedr$   r   r   r   Úmeminfo_new_varsizeº   s    zNRTContext.meminfo_new_varsizec                 C   sH   |   ¡  |j}t tjtjg¡}t ||d¡}|j 	d¡ | 
||g¡S )aI  
        Allocate a MemInfo pointing to a variable-sized data area.  The area
        is separately allocated (i.e. two allocations are made) so that
        re-allocating it doesn't change the MemInfo's address.

        A pointer to the MemInfo is returned.

        Returns NULL to indicate error/failure to allocate.
        ZNRT_MemInfo_new_varsizer'   r(   r1   r   r   r   rH   È   s    
ÿz(NRTContext.meminfo_new_varsize_uncheckedc                 C   s   |   |||¡S )a8  
        Like meminfo_new_varsize() but also set the destructor for
        cleaning up references to objects inside the allocation.

        A pointer to the MemInfo is returned.

        The result of the call is checked and if it is NULL, i.e. allocation
        failed, then a MemoryError is raised.
        )Ú"meminfo_new_varsize_dtor_uncheckedr;   r   r   r   Úmeminfo_new_varsize_dtorÛ   s    z#NRTContext.meminfo_new_varsize_dtorc                 C   sB   |   ¡  |j}t tjtjtjg¡}t ||d¡}| |||g¡S )zù
        Like meminfo_new_varsize() but also set the destructor for
        cleaning up references to objects inside the allocation.

        A pointer to the MemInfo is returned.

        Returns NULL to indicate error/failure to allocate.
        ZNRT_MemInfo_new_varsize_dtor)	r   r)   r   r*   r   r+   r,   r-   r0   r>   r   r   r   rJ   è   s    	
ÿ  ÿz-NRTContext.meminfo_new_varsize_dtor_uncheckedc                 C   s   |   |||¡S )a:  
        Allocate a new data area for a MemInfo created by meminfo_new_varsize().
        The new data pointer is returned, for convenience.

        Contrary to realloc(), this always allocates a new area and doesn't
        copy the old data.  This is useful if resizing a container needs
        more than simply copying the data area (e.g. for hash tables).

        The old pointer will have to be freed with meminfo_varsize_free().

        The result of the call is checked and if it is NULL, i.e. allocation
        failed, then a MemoryError is raised.
        )Úmeminfo_varsize_alloc_unchecked©r   r   Úmeminfor%   r   r   r   Úmeminfo_varsize_allocú   s    z NRTContext.meminfo_varsize_allocc                 C   s   |   |||d¡S )aû  
        Allocate a new data area for a MemInfo created by meminfo_new_varsize().
        The new data pointer is returned, for convenience.

        Contrary to realloc(), this always allocates a new area and doesn't
        copy the old data.  This is useful if resizing a container needs
        more than simply copying the data area (e.g. for hash tables).

        The old pointer will have to be freed with meminfo_varsize_free().

        Returns NULL to indicate error/failure to allocate.
        ZNRT_MemInfo_varsize_alloc©Ú_call_varsize_allocrM   r   r   r   rL     s    
ÿz*NRTContext.meminfo_varsize_alloc_uncheckedc                 C   s   |   |||¡S )a  
        Reallocate a data area allocated by meminfo_new_varsize().
        The new data pointer is returned, for convenience.

        The result of the call is checked and if it is NULL, i.e. allocation
        failed, then a MemoryError is raised.
        )Ú!meminfo_varsize_realloc_uncheckedrM   r   r   r   Úmeminfo_varsize_realloc  s    	z"NRTContext.meminfo_varsize_reallocc                 C   s   |   |||d¡S )zÄ
        Reallocate a data area allocated by meminfo_new_varsize().
        The new data pointer is returned, for convenience.

        Returns NULL to indicate error/failure to allocate.
        ZNRT_MemInfo_varsize_reallocrP   rM   r   r   r   rR   &  s    
ÿz,NRTContext.meminfo_varsize_realloc_uncheckedc                 C   sD   |   ¡  |j}t t ¡ tjtjg¡}t ||d¡}| |||f¡S )z
        Free a memory area allocated for a NRT varsize object.
        Note this does *not* free the NRT object itself!
        ZNRT_MemInfo_varsize_freer5   )r   r   rN   r6   r2   r3   r4   r   r   r   Úmeminfo_varsize_free0  s    

ÿÿzNRTContext.meminfo_varsize_freec                 C   sN   |   ¡  |j}t tjtjtjg¡}t |||¡}|j 	d¡ | 
|||g¡S )Nr'   r(   )r   r   rN   r%   Úfuncnamer2   r3   r4   r   r   r   rQ   >  s    
ÿzNRTContext._call_varsize_allocc                 C   s6   |   ¡  ddlm} |j}t ||d¡}| ||g¡S )z¯
        Given a MemInfo pointer, return a pointer to the allocated data
        managed by it.  This works for MemInfos allocated with all the
        above methods.
        r   )Úmeminfo_data_tyZNRT_MemInfo_data_fast)r   Únumba.core.runtime.nrtdynmodrV   r)   r   r-   r0   )r   r   rN   rV   r2   r4   r   r   r   Úmeminfo_dataH  s    ÿzNRTContext.meminfo_datac                 C   sn   | j j| }| |¡}g }| ¡ r<| ||¡}| ||f¡ |D ](\}}	|	|ƒ}
|  |||
¡}| |¡ q@|S )zCReturn a list of *(type, meminfo)* inside the given value.
        )r   Zdata_model_managerÚtraverseZhas_nrt_meminfoZget_nrt_meminfoÚappendÚget_meminfosÚextend)r   r   ÚtyÚvalZ	datamodelÚmembersÚmeminfosÚmiZmtypÚgetterÚfieldZinner_meminfosr   r   r   r[   W  s    
zNRTContext.get_meminfosc                 C   sv   |   ¡  ddlm} |  |||¡}|D ]J\}}|j}	t |	||¡}
|
jd  d¡ |
jd  d¡ | 	|
|g¡ q&dS )zGCall function of *funcname* on every meminfo found in *value*.
        r   )Úincref_decref_tyr'   Z	nocaptureN)
r   rW   rd   r[   r)   r   r-   r   r/   r0   )r   r   ÚtypÚvaluerU   rd   r`   Ú_ra   r2   r4   r   r   r   Ú_call_incref_decrefh  s    ÿzNRTContext._call_incref_decrefc                 C   s   |   |||d¡ dS )zG
        Recursively incref the given *value* and its members.
        Z
NRT_increfN©rh   ©r   r   re   rf   r   r   r   Úincrefz  s    zNRTContext.increfc                 C   s   |   |||d¡ dS )zG
        Recursively decref the given *value* and its members.
        Z
NRT_decrefNri   rj   r   r   r   Údecref€  s    zNRTContext.decrefc                 C   s6   |   ¡  t tjd¡}|j}t ||d¡}| |d¡S )zGCalls NRT_get_api(), which returns the NRT API function table.
        r   ZNRT_get_api)r   r   r*   r   r+   r)   r-   r0   )r   r   r3   r2   r4   r   r   r   Úget_nrt_api†  s
    zNRTContext.get_nrt_apic              	   C   sZ   | j }|j}| |¡}|j}| t ||¡¡}tdk rV| |¡ |  	|¡ W 5 Q R X |S )z(Check if an exception is raised
        )é   é   )
r   Ú	call_convZcheck_try_statusÚexcinfoÚnot_r   Zis_nullr   Zif_thenÚ
eh_end_try)r   r   ÚctxÚccZ	trystatusrq   Z
has_raisedr   r   r   Úeh_check  s    
zNRTContext.eh_checkc                 C   s   | j }|j}| |¡ dS )zBegin a try-block.
        N)r   rp   Zset_try_status©r   r   rt   ru   r   r   r   Úeh_tryž  s    zNRTContext.eh_tryc                 C   s   | j }|j}| |¡ dS )zEnd a try-block
        N)r   rp   Zunset_try_statusrw   r   r   r   rs   ¥  s    zNRTContext.eh_end_tryN)#Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r"   r&   r#   r7   r9   r8   r=   r:   rA   r?   rI   rH   rK   rJ   rO   rL   rS   rR   rT   rQ   rX   r[   rh   rk   rl   rm   rv   rx   rs   r   r   r   r   r      sP   
	











r   )r    Úcollectionsr   Zllvmliter   Z
numba.corer   r   r   r   Znumba.core.utilsr   r	   r   r   Úobjectr   r   r   r   r   Ú<module>   s    ÿþþ