
    0;jiH>                         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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                   L   e Zd ZdZd Zd Zd Zed             Zd Zd Z	ed             Z
d	 Zed
             Zd Zed             Zd Zed             Zd Zed             Zd Zed             Zd Zed             Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d  Z"d!S )"
NRTContextzF
    An object providing access to NRT APIs in the lowering pass.
    c                 n    || _         || _        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enableds      T/root/voice-cloning/.venv/lib/python3.11/site-packages/numba/core/runtime/context.py__init__zNRTContext.__init__   s9      	9 5D 8D    c                 <    | j         st          j        d          d S )NzNRT required but not enabled)r   r   NumbaRuntimeError)r   s    r    _require_nrtzNRTContext._require_nrt(   s+    } 	K*+IJJJ	K 	Kr"   c                 F     t          j                    fd            }|S )Nc                 `     | |g|R i |}d}t          j        | j        |||           |S )Nz'Allocation failed (probably too large).)msg)r   guard_memory_errorr   )r   builderargskwargsmemptrr(   funcs         r    wrapz+NRTContext._check_null_result.<locals>.wrap-   sJ    T$9$999&99F;C&t}gv3OOOOMr"   )	functoolswraps)r.   r/   s   ` r    _check_null_resultzNRTContext._check_null_result,   s8    				 	 	 	 
		
 r"   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*   sizes      r    allocatezNRTContext.allocate5   s     &&w555r"   c                    |                                   |j        }t          j        t          j        t          j        g          }t	          j        ||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*   r6   modfntyfns         r    r4   zNRTContext.allocate_unchecked>   sv    
 	nw07>2BCC+C~FF
%%i000||B'''r"   c                     |                                   |j        }t          j        t          j                    t
          j        g          }t          j        ||d          }|                    ||g          S )zI
        Low-level free a memory area allocated with allocate().
        NRT_Free	r%   r<   r   r=   VoidTyper   r>   r@   rC   )r   r*   ptrrE   rF   rG   s         r    freezNRTContext.freeK   sd     	nr{}}w/@.ABB+CzBB||B&&&r"   c                 .    |                      ||          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_uncheckedr5   s      r    meminfo_alloczNRTContext.meminfo_allocV   s     ++GT:::r"   c                 .   |                                   |j        }t          j        t          j        t          j        g          }t	          j        ||| 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   rA   rB   rC   rD   s         r    rO   z"NRTContext.meminfo_alloc_uncheckedb   s     	nw07>2BCC+C,0,=,CE E
%%i000||B'''r"   c                 0    |                      |||          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*   r6   dtors       r    meminfo_alloc_dtorzNRTContext.meminfo_alloc_dtors   s     00$EEEr"   c                    |                                   |j        }t          j        t          j        t          j        t          j        g          }t	          j        ||| 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   rA   rB   rC   bitcastr   r*   r6   rU   rE   rF   rG   s          r    rS   z'NRTContext.meminfo_alloc_dtor_unchecked   s     	nw0 '0ABD D+C,0,=,HJ J
%%i000||B!(w7H!I!I!K L L 	Lr"   c                 0    |                      |||          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*   r6   aligns       r    meminfo_alloc_alignedz NRTContext.meminfo_alloc_aligned   s     33GT5IIIr"   c                    |                                   |j        }t          j        d          }t          j        t
          j        t
          j        |g          }t          j        ||| j	        j
                  }|j                            d           t          |t                    r&| 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.
            r:   zalign must be a uint32)r%   r<   r   IntTyper=   r   r>   r?   r@   r   r   rA   rB   
isinstanceintr   get_constantr   uint32typerC   )r   r*   r6   r\   rE   u32rF   rG   s           r    r[   z*NRTContext.meminfo_alloc_aligned_unchecked   s     	njnnw07>32GHH+C,0,=,KM M
%%i000eS!! 	?M..u|UCCEE:$$$&>$$$||Bu...r"   c                 .    |                      ||          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_uncheckedr5   s      r    meminfo_new_varsizezNRTContext.meminfo_new_varsize   s     11'4@@@r"   c                    |                                   |j        }t          j        t          j        t          j        g          }t	          j        ||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.
        NRT_MemInfo_new_varsizer:   r;   rD   s         r    rh   z(NRTContext.meminfo_new_varsize_unchecked   s}     	nw07>2BCC+C,EG G
%%i000||B'''r"   c                 0    |                      |||          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_uncheckedrT   s       r    meminfo_new_varsize_dtorz#NRTContext.meminfo_new_varsize_dtor   s     66wdKKKr"   c                     |                                   |j        }t          j        t          j        t          j        t          j        g          }t	          j        ||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@   rC   rY   s          r    rm   z-NRTContext.meminfo_new_varsize_dtor_unchecked   st     	nw0 '0ABD D+57 7||Bt---r"   c                 0    |                      |||          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*   meminfor6   s       r    meminfo_varsize_allocz NRTContext.meminfo_varsize_alloc   s     33GWdKKKr"   c                 2    |                      |||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_allocrs   s       r    rr   z*NRTContext.meminfo_varsize_alloc_unchecked  s(     ''$(CE E 	Er"   c                 0    |                      |||          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_uncheckedrs   s       r    meminfo_varsize_reallocz"NRTContext.meminfo_varsize_realloc  s     55gwMMMr"   c                 2    |                      |||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.
        NRT_MemInfo_varsize_reallocrx   rs   s       r    r{   z,NRTContext.meminfo_varsize_realloc_unchecked&  s(     ''$(EG G 	Gr"   c                    |                                   |j        }t          j        t          j                    t
          j        t
          j        g          }t          j        ||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_freerJ   )r   r*   rt   rL   rE   rF   rG   s          r    meminfo_varsize_freezNRTContext.meminfo_varsize_free0  sy    
 	nr{}} ' 173DEG G+C,FH H||B#///r"   c                 2   |                                   |j        }t          j        t          j        t          j        t          j        g          }t	          j        |||          }|j        	                    d           |
                    |||g          S )Nr:   r;   )r   r*   rt   r6   funcnamerE   rF   rG   s           r    ry   zNRTContext._call_varsize_alloc>  s    nw0 ' 17>BD D+Cx@@
%%i000||B$000r"   c                     |                                   ddlm} |j        }t	          j        ||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.nrtdynmodr   r<   r   r@   rC   )r   r*   rt   r   rE   rG   s         r    meminfo_datazNRTContext.meminfo_dataH  s`     	@@@@@@n+C,CE E||B	***r"   c                 X   | j         j        |         }|                    |          }g }|                                r-|                    ||          }|                    ||f           |D ]<\  }}	 |	|          }
|                     |||
          }|                    |           =|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_meminfoss               r    r   zNRTContext.get_meminfosW  s     M4R8	$$W--$$&& 	&**7C88BOORH%%%# 	, 	,LD&F3KKE!..weDDNOON++++r"   c                 `   |                                   ddlm} |                     |||          }|D ]y\  }}|j        }	t          j        |	||          }
|
j        d                             d           |
j        d                             d           |	                    |
|g           zdS )zGCall function of *funcname* on every meminfo found in *value*.
        r   )incref_decref_tyr:   	nocaptureN)
r%   r   r   r   r<   r   r@   r+   rB   rC   )r   r*   typvaluer   r   r   _r   rE   rG   s              r    _call_incref_decrefzNRTContext._call_incref_decrefh  s     	AAAAAA$$Wc599 	# 	#EAr.C/5E08: :B GAJ$$Y///GAJ$$[111LLbT""""	# 	#r"   c                 6    |                      |||d           dS )zG
        Recursively incref the given *value* and its members.
        
NRT_increfNr   r   r*   r   r   s       r    increfzNRTContext.increfz  $     	  #ulCCCCCr"   c                 6    |                      |||d           dS )zG
        Recursively decref the given *value* and its members.
        
NRT_decrefNr   r   s       r    decrefzNRTContext.decref  r   r"   c                     |                                   t          j        t          j        d          }|j        }t          j        ||d          }|                    |d          S )zGCalls NRT_get_api(), which returns the NRT API function table.
         NRT_get_api)r%   r   r=   r   r>   r<   r@   rC   )r   r*   rF   rE   rG   s        r    get_nrt_apizNRTContext.get_nrt_api  sY     	w0"55n+C}EE||B###r"   c                 D   | j         }|j        }|                    |          }|j        }|                    t          j        ||                    }t          dk     rB|                    |          5  | 	                    |           ddd           n# 1 swxY w 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_raiseds          r    eh_checkzNRTContext.eh_check  s     m]''00	#\\'/'7"C"CDD
w,, ) )((() ) ) ) ) ) ) ) ) ) ) ) ) ) )s   3BBBc                 L    | j         }|j        }|                    |           dS )zBegin a try-block.
        N)r   r   set_try_statusr   r*   r   r   s       r    eh_tryzNRTContext.eh_try  s,     m]
'"""""r"   c                 L    | j         }|j        }|                    |           dS )zEnd a try-block
        N)r   r   unset_try_statusr   s       r    r   zNRTContext.eh_end_try  s,     m]
G$$$$$r"   N)#__name__
__module____qualname____doc__r!   r%   r2   r7   r4   rM   rP   rO   rV   rS   r]   r[   ri   rh   rn   rm   ru   rr   r|   r{   r   ry   r   r   r   r   r   r   r   r   r   r   r"   r    r   r      s{        9 9 9K K K   6 6 6( ( (	' 	' 	' 	; 	; 	;( ( (" 
F 
F 
FL L L( J J J/ / /0 A A A( ( (& 
L 
L 
L. . .$ L L L E E E  N N NG G G0 0 01 1 1+ + +  "# # #$D D DD D D$ $ $  # # #% % % % %r"   r   )r0   collectionsr   llvmliter   
numba.corer   r   r   r   numba.core.utilsr	   r
   r   r   objectr   r   r"   r    <module>r      s       " " " " " "       5 5 5 5 5 5 5 5 5 5 5 5 & & & & & & $$<%67 7  /./G/L/OQ Q 
 212E2J2MO O 
Q% Q% Q% Q% Q% Q% Q% Q% Q% Q%r"   