o
    8ήc9                     @   s<   d dl Z d dlmZ d dlmZmZmZ G dd deZdS )    N)ir)typescgutilserrorsc                   @   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   || _ || _d S )N)_context_enabled)selfcontextenabled r   A/tmp/pip-target-vg8gfxp4/lib/python/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   allocate   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   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().
        NRT_Free	r   r&   r   r'   VoidTyper   r(   r*   r-   )r	   r   ptrr/   r0   r1   r   r   r   free4   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_uncheckedr   r   r   r   meminfo_alloc?   s   
zNRTContext.meminfo_allocc                 C   r"   )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.
        NRT_MemInfo_alloc_safer$   r%   r.   r   r   r   r7   K   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   r    dtorr   r   r   meminfo_alloc_dtor[      zNRTContext.meminfo_alloc_dtorc                 C   sX   |    |j}ttjtjtjg}t||d}|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.
        NRT_MemInfo_alloc_dtor_safer$   )r   r&   r   r'   r   r(   r)   r*   r+   r,   r-   bitcastr	   r   r    r=   r/   r0   r1   r   r   r   r;   h   s   	
z'NRTContext.meminfo_alloc_dtor_uncheckedc                 C   r:   )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||d}|j	
d t|tr1| 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.
            NRT_MemInfo_alloc_safe_alignedr$   zalign must be a uint32)r   r&   r   IntTyper'   r   r(   r)   r*   r+   r,   
isinstanceintr   get_constantr   uint32typer-   )r	   r   r    rD   r/   u32r0   r1   r   r   r   rC      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_uncheckedr   r   r   r   meminfo_new_varsize   s   zNRTContext.meminfo_new_varsizec                 C   r"   )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_varsizer$   r%   r.   r   r   r   rO      s   
z(NRTContext.meminfo_new_varsize_uncheckedc                 C   r:   )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   r?   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   r&   r   r'   r   r(   r)   r*   r-   rB   r   r   r   rR      s   	
z-NRTContext.meminfo_new_varsize_dtor_uncheckedc                 C   r:   )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      |  |||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_allocrV   r   r   r   rU      s   
z*NRTContext.meminfo_varsize_alloc_uncheckedc                 C   r:   )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_uncheckedrV   r   r   r   meminfo_varsize_realloc  s   	z"NRTContext.meminfo_varsize_reallocc                 C   rY   )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_reallocr[   rV   r   r   r   r]     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_freer3   )r	   r   rW   r5   r/   r0   r1   r   r   r   meminfo_varsize_free  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   rW   r    funcnamer/   r0   r1   r   r   r   r\   &  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.nrtdynmodrc   r&   r   r*   r-   )r	   r   rW   rc   r/   r1   r   r   r   meminfo_data0  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   rl   ?  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_tyr$   	nocaptureN)
r   re   rx   rl   r&   r   r*   r   r,   r-   )r	   r   typvaluerb   rx   rr   _rs   r/   r1   r   r   r   _call_incref_decrefP  s   zNRTContext._call_incref_decrefc                 C      |  |||d dS )zG
        Recursively incref the given *value* and its members.
        
NRT_increfNr}   r	   r   rz   r{   r   r   r   increfb     zNRTContext.increfc                 C   r~   )zG
        Recursively decref the given *value* and its members.
        
NRT_decrefNr   r   r   r   r   decrefh  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   r'   r   r(   r&   r*   r-   )r	   r   r0   r/   r1   r   r   r   get_nrt_apin  s
   zNRTContext.get_nrt_apic                 C   sh   | j }|j}||}|j}|t||}|| | | W d   |S 1 s-w   Y  |S )z(Check if an exception is raised
        N)	r   	call_convcheck_try_statusexcinfonot_r   is_nullif_then
eh_end_try)r	   r   ctxcc	trystatusr   
has_raisedr   r   r   eh_checkx  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   r!   r   r6   r8   r7   r>   r;   rE   rC   rP   rO   rS   rR   rX   rU   r^   r]   ra   r\   rf   rl   r}   r   r   r   r   r   r   r   r   r   r   r      sR    	











r   )	r   llvmliter   
numba.corer   r   r   objectr   r   r   r   r   <module>   s    