Hacked By AnonymousFox

Current Path : /opt/cloudlinux/venv/lib64/python3.11/site-packages/smmap/__pycache__/
Upload File :
Current File : //opt/cloudlinux/venv/lib64/python3.11/site-packages/smmap/__pycache__/mman.cpython-311.pyc

�

�܋f�]���dZddlmZmZmZmZddlZddlmZgd�Z	Gd�d��Z
Gd	�d
��ZGd�de��ZdS)
zlModule containing a memory memory manager which provides a sliding window on a number of memory mapped files�)�	MapWindow�	MapRegion�
MapRegionList�	is_64_bit�N)�reduce)�StaticWindowMapManager�SlidingWindowMapManager�WindowCursorc��eZdZdZdZdd�Zd�Zd�Zd�Zd�Z	d	�Z
d
�Zd�Zdd
�Z
d�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�ZdS)ra�
    Pointer into the mapped region of the memory manager, keeping the map
    alive until it is destroyed and no other client uses it.

    Cursors should not be created manually, but are instead returned by the SlidingWindowMapManager

    **Note:**: The current implementation is suited for static and sliding window managers, but it also means
    that it must be suited for the somewhat quite different sliding manager. It could be improved, but
    I see no real need to do so.��_manager�_rlist�_region�_ofs�_sizeNc�L�||_||_d|_d|_d|_dS)Nrr
)�self�manager�regionss   �[/builddir/build/BUILD/cloudlinux-venv-1.0.6/venv/lib/python3.11/site-packages/smmap/mman.py�__init__zWindowCursor.__init__%s)����
���������	���
�
�
�c�.�|���dS�N��_destroy�rs r�__del__zWindowCursor.__del__,����
�
�����rc��|Sr�rs r�	__enter__zWindowCursor.__enter__/s���rc�.�|���dSrr)r�exc_type�	exc_value�	tracebacks    r�__exit__zWindowCursor.__exit__2r rc��|���|j�k	t|j��dkr8|jj�|j�����dSdS#ttf$rYdSwxYwdS)z&Destruction code to decrement countersNr)	�unuse_regionr�lenr�_fdict�pop�
path_or_fd�	TypeError�KeyErrorrs rrzWindowCursor._destroy5s����������;�"�

��t�{�#�#�q�(�(��M�(�,�,�T�[�-C�-C�-E�-E�F�F�F�F�F�)�(���x�(�
�
�
�
���
����#�"s�AA/�/B�Bc�4�|j|_t|j��|j��|_|j|_|j|_|j|_|jD]}|����|j�|j���dSdS)z>Copy all data from rhs into this instance, handles usage countN)r�typerrrr�increment_client_count)r�rhs�regions   r�
_copy_fromzWindowCursor._copy_fromJs������
�&�d�3�:�&�&�s�z�2�2����{����H��	��Y��
��k�	,�	,�F��)�)�+�+�+�+��<�#��L�/�/�1�1�1�1�1�$�#rc�^�t|����}|�|��|S)zcopy module interface)r2r6)r�cpys  r�__copy__zWindowCursor.__copy__Ys*���d�4�j�j�l�l�����t�����
rc�X�|���|�|��dS)z�Assign rhs to this instance. This is required in order to get a real copy.
        Alternativly, you can copy an existing instance using the copy moduleN)rr6)rr4s  r�assignzWindowCursor.assign`s)��	
�
�
������������rrc��d}|j}|j���}t|p||���p|��}|j�1|j�|��rd}n|���||kr|S|r<|�|j|||d��|_|j�	��||jj
z
|_t||j���|z
��|_
|S)a$Assure we point to a window which allows access to the given offset into the file

        :param offset: absolute offset in bytes into the file
        :param size: amount of bytes to map. If 0, all available bytes will be mapped
        :param flags: additional flags to be given to os.open in case a file handle is initially opened
            for mapping. Has no effect if a region can actually be reused.
        :return: this instance - it should be queried for whether it points to a valid memory region.
            This is not the case if the mapping failed because we reached the end of the file

        **Note:**: The size actually mapped may be smaller than the given size. If that is the case,
        either the file has reached its end, or the map was created between two existing regionsTNF)rr�	file_size�min�window_sizer�includes_ofsr*�_obtain_regionr3�_br�ofs_endr)r�offset�size�flags�need_region�man�fsizes       r�
use_regionzWindowCursor.use_regionfs�����m����%�%�'�'���4�=�5�#�/�/�"3�"3�"<�u�=�=���<�#��|�(�(��0�0�
$�#����!�!�#�#�#�
�U�?�?��K��	2��-�-�d�k�6�4��PU�V�V�D�L��L�/�/�1�1�1��T�\�_�,��	���t�|�3�3�5�5��>�?�?��
��rc�V�|j�|j�d��d|_dS)a/Unuse the current region. Does nothing if we have no current region

        **Note:** the cursor unuses the region automatically upon destruction. It is recommended
        to un-use the region once you are done reading from it in persistent cursors as it
        helps to free up resource more quicklyN���)rr3rs rr*zWindowCursor.unuse_region�s-���<�#��L�/�/��3�3�3�����rc��t|j�����|j|j|jz�S)a�Return a buffer object which allows access to our memory region from our offset
        to the window size. Please note that it might be smaller than you requested when calling use_region()

        **Note:** You can only obtain a buffer if this instance is_valid() !

        **Note:** buffers should not be cached passed the duration of your access as it will
        prevent resources from being freed even though they might not be accounted for anymore !)�
memoryviewr�bufferrrrs rrOzWindowCursor.buffer�s5���$�,�-�-�/�/�0�0���4�9�T�Z�;O�1O�P�Prc�4�|j���S)a
        :return: the underlying raw memory map. Please not that the offset and size is likely to be different
            to what you set as offset and size. Use it only if you are sure about the region it maps, which is the whole
            file in case of StaticWindowMapManager)r�maprs rrQzWindowCursor.map�s��
�|���!�!�!rc��|jduS)z2:return: True if we have a valid and usable regionN�rrs r�is_validzWindowCursor.is_valid�s���|�4�'�'rc��|jduS)z?:return: True if we are associated with a specific file alreadyN)rrs r�
is_associatedzWindowCursor.is_associated�s���{�$�&�&rc�*�|jj|jzS)zh:return: offset to the first byte pointed to by our cursor

        **Note:** only if is_valid() is True)rrBrrs r�	ofs_beginzWindowCursor.ofs_begin�s���|����*�*rc�:�|jj|jz|jzS)z3:return: offset to one past the last available byte�rrBrrrs rrCzWindowCursor.ofs_end�s���|����*�T�Z�7�7rc��|jS)z$:return: amount of bytes we point to)rrs rrEzWindowCursor.size�s
���z�rc��|jS)z�:return: our mapped region, or None if nothing is mapped yet
        :raise AssertionError: if we have no current region. This is only useful for debuggingrSrs rr5zWindowCursor.region�s���|�rc�z�|jj|jz|cxko|jj|jz|jzkncS)z�:return: True if the given absolute offset is contained in the cursors
            current region

        **Note:** cursor must be valid for this to workrZ)r�ofss  rr@zWindowCursor.includes_ofs�sE�����$�)�+��`�`�`�`�t�|����7R�UY�U_�7_�`�`�`�`�`rc�4�|j���S)z$:return: size of the underlying file)rr=rs rr=zWindowCursor.file_size�s���{�$�$�&�&�&rc�4�|j���S)z>:return: path or file descriptor of the underlying mapped file)rr.rs rr.zWindowCursor.path_or_fd�s���{�%�%�'�'�'rc��t|j���t��rt	d���|j���S)ze:return: path of the underlying mapped file
        :raise ValueError: if attached path is not a pathz>Path queried although mapping was applied to a file descriptor)�
isinstancerr.�int�
ValueErrorrs r�pathzWindowCursor.path�sH���d�k�,�,�.�.��4�4�	_��]�^�^�^��{�%�%�'�'�'rc��t|j���t��rt	d���|j���S)z�:return: file descriptor used to create the underlying mapping.

        **Note:** it is not required to be valid anymore
        :raise ValueError: if the mapping was not created by a file descriptorz@File descriptor queried although mapping was generated from path)rbrr.�strrdrs r�fdzWindowCursor.fd�sH��
�d�k�,�,�.�.��4�4�	a��_�`�`�`��{�%�%�'�'�'r)NN)rrr)�__name__�
__module__�__qualname__�__doc__�	__slots__rrr#r(rr6r9r;rJr*rOrQrTrVrXrCrEr5r@r=r.rerhr"rrrrs�������$�$��I�����������������*2�2�2�������&�&�&�&�P���Q�Q�Q�"�"�"�(�(�(�'�'�'�+�+�+�8�8�8�
������
a�a�a�'�'�'�(�(�(�(�(�(�(�(�(�(�(rrc��eZdZdZgd�ZeZeZe	Z
eZdZ
ddejfd�Zd�Zd�Zd�Zd	�Zd
�Zd�Zd�Zd
�Zd�Zd�Zd�ZdS)r	a�Provides a manager which will produce single size cursors that are allowed
    to always map the whole file.

    Clients must be written to specifically know that they are accessing their data
    through a StaticWindowMapManager, as they otherwise have to deal with their window size.

    These clients would have to use a SlidingWindowMapBuffer to hide this fact.

    This type will always use a maximum window size, and optimize certain methods to
    accommodate this fact)r,�_window_size�_max_memory_size�_max_handle_count�_memory_size�
_handle_countirc��t��|_||_||_||_d|_d|_|dkr!d}t��rd}||jz|_|dkr#d}t��rd}||jz|_dSdS)a�initialize the manager with the given parameters.
        :param window_size: if -1, a default window size will be chosen depending on
            the operating system's architecture. It will internally be quantified to a multiple of the page size
            If 0, the window may have any size, which basically results in mapping the whole file at one
        :param max_memory_size: maximum amount of memory we may map at once before releasing mapped regions.
            If 0, a viable default will be set depending on the system's architecture.
            It is a soft limit that is tried to be kept, but nothing bad happens if we have to over-allocate
        :param max_open_handles: if not maxint, limit the amount of open file handles to the given number.
            Otherwise the amount is only limited by the system itself. If a system or soft limit is hit,
            the manager will free as many handles as possibler�@ii N)	�dictr,rorprqrrrsr�_MB_in_bytes)rr?�max_memory_size�max_open_handles�coeffs     rrzStaticWindowMapManager.__init__s����f�f���'��� /���!1�����������?�?��E��{�{�
��� %��(9� 9�D���a����E��{�{�
���$)�D�,=�$=�D�!�!�!� �rc���d}|dks|j|z|jkr�d}d}|j���D]5}|D]0}|���dkr|�|j|jkr|}|}�1�6|�n{|dz
}||�|��=|�d��|xj|���zc_|xj	dzc_	|dk��|j|z|jk��|S)amUnmap the region which was least-recently used and has no client
        :param size: size of the region we want to map next (assuming its not already mapped partially or full
            if 0, we try to free any available region
        :return: Amount of freed regions

        .. Note::
            We don't raise exceptions anymore, in order to keep the system working, allowing temporary overallocation.
            If the system runs out of memory, it will tell.

        .. TODO::
            implement a case where all unusued regions are discarded efficiently.
            Currently its only brute force
        rNrrL)
rrrpr,�values�client_count�_uc�indexr3rErs)rrE�	num_found�
lru_region�lru_listrr5s       r�_collect_lru_regionz*StaticWindowMapManager._collect_lru_region/s0���	��q�y�y�d�/�$�6��9N�N�N��J��H��;�-�-�/�/�
+�
+��%�+�+�F��+�+�-�-��2�2�'�/�6�:�
��3N�3N�%+�
�#*���+��!��
��N�I�����
�3�3�4��-�-�b�1�1�1�������!2�!2�2������!�#���-�q�y�y�d�/�$�6��9N�N�N�0�rc�D�|j|z|jkr|�|��d}|rt|��dksJ�|d}n�	|�|���dtj|��}nA#t$r4|r�|�d��|�	||||d��cYSwxYw|xj
dz
c_
|xj|���z
c_|�|��|�
|��sJ�|S)z�Utilty to create a new region - for more information on the parameters,
        see MapCursor.use_region.
        :param a: A regions (a)rray
        :return: The newly created regionNrrT)rrrpr�r+�MapRegionClsr.�sys�maxsize�	ExceptionrArsrE�appendr@)r�arDrErF�is_recursive�rs       rrAz%StaticWindowMapManager._obtain_regionXsJ��
��t�#�d�&;�;�;��$�$�T�*�*�*�
���	��q�6�6�Q�;�;�;�;��!��A�A�

I��%�%�a�l�l�n�n�a���e�L�L�����
I�
I�
I�
 ����(�(��+�+�+��*�*�1�f�d�E�4�H�H�H�H�H�
I����
���!�#�����������)���
�H�H�Q�K�K�K��~�~�f�%�%�%�%�%��s�4B�;B?�>B?c��|j�|��}|�|�|��}||j|<|�||��S)a�
        :return: a cursor pointing to the given path or file descriptor.
            It can be used to map new regions of the file into memory

        **Note:** if a file descriptor is given, it is assumed to be open and valid,
        but may be closed afterwards. To refer to the same file, you may reuse
        your existing file descriptor, but keep in mind that new windows can only
        be mapped as long as it stays valid. This is why the using actual file paths
        are preferred unless you plan to keep the file descriptor open.

        **Note:** file descriptors are problematic as they are not necessarily unique, as two
        different files opened and closed in succession might have the same file descriptor id.

        **Note:** Using file descriptors directly is faster once new windows are mapped as it
        prevents the file to be opened again just for the purpose of mapping it.)r,�get�MapRegionListCls�WindowCursorCls)rr.rs   r�make_cursorz"StaticWindowMapManager.make_cursor�sQ�� �+�/�/�*�-�-���?��+�+�J�7�7�G�&-�D�K�
�#��#�#�D�'�2�2�2rc�,�|�d��S)z]Collect all available free-to-collect mapped regions
        :return: Amount of freed handlesr)r�rs r�collectzStaticWindowMapManager.collect�s���'�'��*�*�*rc��|jS)zO:return: amount of file handles in use. Each mapped region uses one file handle)rsrs r�num_file_handlesz'StaticWindowMapManager.num_file_handles�s���!�!rc�h�td�d�|j���D��d��S)z$Amount of opened files in the systemc��||zSrr")�x�ys  r�<lambda>z7StaticWindowMapManager.num_open_files.<locals>.<lambda>�s
��1�q�5�rc3�@K�|]}t|��dk�dV��dS)rrN)r+)�.0�rlists  r�	<genexpr>z8StaticWindowMapManager.num_open_files.<locals>.<genexpr>�s7����*a�*a��RU�V[�R\�R\�_`�R`�R`�1�R`�R`�R`�R`�*a�*arr)rr,r|rs r�num_open_filesz%StaticWindowMapManager.num_open_files�s6���(�(�*a�*a�$�+�:L�:L�:N�:N�*a�*a�*a�cd�e�e�erc��|jS)z8:return: size of each window when allocating new regions)rors rr?z"StaticWindowMapManager.window_size����� � rc��|jS)z2:return: amount of bytes currently mapped in total)rrrs r�mapped_memory_sizez)StaticWindowMapManager.mapped_memory_size�r�rc��|jS)z6:return: maximium amount of handles we may have opened)rqrs r�max_file_handlesz'StaticWindowMapManager.max_file_handles�s���%�%rc��|jS)z1:return: maximum amount of memory we may allocate)rprs r�max_mapped_memory_sizez-StaticWindowMapManager.max_mapped_memory_size�s���$�$rc���tjdkrdSd}|j���D]8\}}|�|��r|D]}|���|dz
}��9|S)a�ONLY AVAILABLE ON WINDOWS
        On windows removing files is not allowed if anybody still has it opened.
        If this process is ourselves, and if the whole process uses this memory
        manager (as far as the parent framework is concerned) we can enforce
        closing all memory maps whose path matches the given base path to
        allow the respective operation after all.
        The respective system must NOT access the closed memory regions anymore !
        This really may only be used if you know that the items which keep
        the cursors alive will not be using it anymore. They need to be recreated !
        :return: Amount of closed handles

        **Note:** does nothing on non-windows platforms�win32Nrr)r��platformr,�items�
startswith�release)r�	base_path�
num_closedrer�r5s      r�force_map_handle_removal_winz3StaticWindowMapManager.force_map_handle_removal_win�s����<�7�"�"��F��
��;�,�,�.�.�	$�	$�K�D�%����y�)�)�
$�#�$�$�F��N�N�$�$�$��!�O�J�J���rN)rirjrkrlrmrr�r�MapWindowClsrr�rr�rwr�r�rr�rAr�r�r�r�r?r�r�r�r�r"rrr	r	�s������	�	����I�%���L��L�"�O��L�#$�a�#�+�>�>�>�>�H'�'�'�R$�$�$�R3�3�3�.+�+�+�
"�"�"�f�f�f�!�!�!�!�!�!�&�&�&�%�%�%�����rr	c�N��eZdZdZe��Zddejf�fd�	Zd�Z	�xZ
S)r
a�Maintains a list of ranges of mapped memory regions in one or more files and allows to easily
    obtain additional regions assuring there is no overlap.
    Once a certain memory limit is reached globally, or if there cannot be more open file handles
    which result from each mmap call, the least recently used, and currently unused mapped regions
    are unloaded automatically.

    **Note:** currently not thread-safe !

    **Note:** in the current implementation, we will automatically unload windows if we either cannot
        create more memory maps (as the open file handles limit is hit) or if we have allocated more than
        a safe amount of memory already, which would possibly cause memory allocations to fail as our address
        space is full.rLrc�N��t���|||��dS)z%Adjusts the default window size to -1N)�superr)rr?rxry�	__class__s    �rrz SlidingWindowMapManager.__init__�s&���
������o�7G�H�H�H�H�Hrc���d}d}t|��}||krM||zdz}	||	j}
|
|kr*||	�|��r	||	}n|	dz}n|	}||k�M|���|j}|�dd��}|�||��}	|�|���d��}
|j|z|jkr|�|��d}t|��}|dkr|dj|krd}n&|}t|��D]\}}|j|kr|}n�|dkr#|r |j�
||��}
nI||kr |j�
||��}
|j�
||dz
��}|	�||��|	�|
|��|	�
��|	���|
jkr|
j|	jz
|	_	|j|jkrt&�|�|���|	j|	j|��}nA#t&$r4|r�|�d��|�||||d��cYSwxYw|xjdz
c_|xj|���z
c_|�||��|S)Nr�rT)r+rBr@ror�r=rrrpr��	enumerate�from_region�extend_left_to�extend_right_to�alignrCr^rErsrqr�r�r.rA�insert)rr�rDrErFr�r��lo�hi�midr^r?�left�right�
insert_pos�len_regions�ir5s                  rrAz&SlidingWindowMapManager._obtain_region�s?��
��
��
��V�V���2�g�g���7�q�.�C��C�&�)�C��f�}�}��S�6�&�&�v�.�.���#��A���1�W������2�g�g�
�9��+�K��$�$�Q��*�*�D��#�#�F�D�1�1�C��%�%�a�k�k�m�m�Q�7�7�E�
� �;�.��1F�F�F��(�(��5�5�5��J��a�&�&�K��a����Q�4�7�f�$�$�!"�J��)�
�!*�1�����I�A�v��y�6�)�)�%&�
���*��Q����I� �-�9�9�!�J�-�H�H�E����,�,� �-�9�9�!�J�-�H�H�E��(�4�4�Q�z�A�~�5F�G�G��
���t�[�1�1�1�����{�3�3�3��I�I�K�K�K��{�{�}�}�u�y�(�(� �9�s�w�.���
I��%��)?�?�?�#�O��%�%�a�l�l�n�n�c�g�s�x��O�O�����
I�
I�
I�
 ����(�(��+�+�+��*�*�1�f�d�E�4�H�H�H�H�H�
I����
���!�#�����������)���
�H�H�Z��#�#�#��s�"AI.�.;J,�+J,)rirjrkrl�tuplermr�r�rrA�
__classcell__)r�s@rr
r
�sy�������������I�#%�q�3�;�I�I�I�I�I�I�a�a�a�a�a�a�arr
)
rl�utilrrrrr��	functoolsr�__all__rr	r
r"rr�<module>r�s��r�r��������������
�
�
�������
O�
O�
O��U(�U(�U(�U(�U(�U(�U(�U(�tf�f�f�f�f�f�f�f�Tv�v�v�v�v�4�v�v�v�v�vr

Hacked By AnonymousFox1.0, Coded By AnonymousFox