o
    ۾iH>                     @   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_alignedNRT_MemInfo_alloc_safeNRT_MemInfo_alloc_dtor_safeNRT_MemInfo_alloc_safe_alignedNRT_MemInfo_allocNRT_MemInfo_alloc_dtor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| _d S t| _d S )N)_context_enabledr   	DEBUG_NRT_NRT_MEMINFO_SAFE_API_meminfo_api_NRT_MEMINFO_DEFAULT_API)selfcontextenabled r   N/home/ubuntu/.local/lib/python3.10/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   NumbaRuntimeError)r   r   r   r   _require_nrt(   s   
zNRTContext._require_nrtc                    s   t   fdd}|S )Nc                    s4    | |g|R i |}d}t j| j|||d |S )Nz'Allocation failed (probably too large).)msg)r   guard_memory_errorr   )r   builderargskwargs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 )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   H   |    |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.
        NRT_Allocatenoaliasr!   moduler   FunctionTyper   	voidptr_tintp_tget_or_insert_functionreturn_valueadd_attributecallr   r$   r1   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().
        NRT_Free	r!   r7   r   r8   VoidTyper   r9   r;   r>   )r   r$   ptrr@   rA   rB   r   r   r   freeK   s
   zNRTContext.freec                 C   r.   )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_uncheckedr0   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.
        r5   )r!   r7   r   r8   r   r9   r:   r;   r   r
   r<   r=   r>   r?   r   r   r   rH   b   s   z"NRTContext.meminfo_alloc_uncheckedc                 C      |  |||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$   r1   dtorr   r   r   meminfo_alloc_dtors      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.
        r5   )r!   r7   r   r8   r   r9   r:   r;   r   r   r<   r=   r>   bitcastr   r$   r1   rM   r@   rA   rB   r   r   r   rK      s   	
z'NRTContext.meminfo_alloc_dtor_uncheckedc                 C   rJ   )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$   r1   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r3| jtj|}n	|j|ks<J 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.
            r5   zalign must be a uint32)r!   r7   r   IntTyper8   r   r9   r:   r;   r   r   r<   r=   
isinstanceintr   get_constantr   uint32typer>   )r   r$   r1   rS   r@   u32rA   rB   r   r   r   rR      s   


z*NRTContext.meminfo_alloc_aligned_uncheckedc                 C   r.   )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_uncheckedr0   r   r   r   meminfo_new_varsize   s   zNRTContext.meminfo_new_varsizec                 C   r3   )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.
        NRT_MemInfo_new_varsizer5   r6   r?   r   r   r   r]      s   
z(NRTContext.meminfo_new_varsize_uncheckedc                 C   rJ   )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_uncheckedrL   r   r   r   meminfo_new_varsize_dtor   rO   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.
        NRT_MemInfo_new_varsize_dtor)	r!   r7   r   r8   r   r9   r:   r;   r>   rQ   r   r   r   r`      s   	
z-NRTContext.meminfo_new_varsize_dtor_uncheckedc                 C   rJ   )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$   meminfor1   r   r   r   meminfo_varsize_alloc   s   z NRTContext.meminfo_varsize_allocc                 C      |  |||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.
        NRT_MemInfo_varsize_alloc_call_varsize_allocrd   r   r   r   rc     s   
z*NRTContext.meminfo_varsize_alloc_uncheckedc                 C   rJ   )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_uncheckedrd   r   r   r   meminfo_varsize_realloc  s   	z"NRTContext.meminfo_varsize_reallocc                 C   rg   )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.
        NRT_MemInfo_varsize_reallocri   rd   r   r   r   rk   &  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!
        NRT_MemInfo_varsize_freerD   )r   r$   re   rF   r@   rA   rB   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 )Nr5   r6   )r   r$   re   r1   funcnamer@   rA   rB   r   r   r   rj   >  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_tyNRT_MemInfo_data_fast)r!   numba.core.runtime.nrtdynmodrq   r7   r   r;   r>   )r   r$   re   rq   r@   rB   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   data_model_managertraversehas_nrt_meminfoget_nrt_meminfoappendget_meminfosextend)r   r$   tyval	datamodelmembersmeminfosmimtypgetterfieldinner_meminfosr   r   r   rz   W  s   
zNRTContext.get_meminfosc                 C   sv   |    ddlm} | |||}|D ]%\}}|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_tyr5   	nocaptureN)
r!   rs   r   rz   r7   r   r;   r%   r=   r>   )r   r$   typvaluerp   r   r   _r   r@   rB   r   r   r   _call_incref_decrefh  s   zNRTContext._call_incref_decrefc                 C      |  |||d dS )zG
        Recursively incref the given *value* and its members.
        
NRT_increfNr   r   r$   r   r   r   r   r   increfz     zNRTContext.increfc                 C   r   )zG
        Recursively decref the given *value* and its members.
        
NRT_decrefNr   r   r   r   r   decref  r   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   NRT_get_api)r!   r   r8   r   r9   r7   r;   r>   )r   r$   rA   r@   rB   r   r   r   get_nrt_api  s
   zNRTContext.get_nrt_apic                 C   sp   | j }|j}||}|j}|t||}tdk r6|| | 	| W d   |S 1 s1w   Y  |S )z(Check if an exception is raised
        )      N)
r   	call_convcheck_try_statusexcinfonot_r   is_nullr   if_then
eh_end_try)r   r$   ctxcc	trystatusr   
has_raisedr   r   r   eh_check  s   

zNRTContext.eh_checkc                 C      | j }|j}|| dS )zBegin a try-block.
        N)r   r   set_try_statusr   r$   r   r   r   r   r   eh_try     zNRTContext.eh_tryc                 C   r   )zEnd a try-block
        N)r   r   unset_try_statusr   r   r   r   r     r   zNRTContext.eh_end_tryN)#__name__
__module____qualname____doc__r   r!   r-   r2   r/   rG   rI   rH   rN   rK   rT   rR   r^   r]   ra   r`   rf   rc   rl   rk   ro   rj   rt   rz   r   r   r   r   r   r   r   r   r   r   r   r      sR    
	











r   )r+   collectionsr   llvmliter   
numba.corer   r   r   r   numba.core.utilsr   r	   r   r   objectr   r   r   r   r   <module>   s"    