관리-도구
편집 파일: mman.cpython-311.pyc
� �܋f�] � � � d Z ddlmZmZmZmZ ddlZddlmZ g d�Z G d� d� � Z G d � d � � Z G d� de� � ZdS ) 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 � � e Zd ZdZdZdd�Zd� Zd� Zd� Zd� Z d � Z d � Zd� Zdd �Z d� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Zd� ZdS )r a� 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| _ d S )Nr r )�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 �. � | � � � d S �N��_destroy�r s r �__del__zWindowCursor.__del__, � � �� � �����r c � � | S r � r s r � __enter__zWindowCursor.__enter__/ s � ��r c �. � | � � � d S r r )r �exc_type� exc_value� tracebacks r �__exit__zWindowCursor.__exit__2 r r c � � | � � � | j �k t | j � � dk r8| j j � | j � � � � � dS dS # t t f$ r Y dS w xY wdS )z&Destruction code to decrement countersNr ) �unuse_regionr �lenr �_fdict�pop� path_or_fd� TypeError�KeyErrorr s r r zWindowCursor._destroy5 s� � ��������;�"� ��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 | _ | j D ]}|� � � �| j �| j � � � dS dS )z>Copy all data from rhs into this instance, handles usage countN)r �typer r r r �increment_client_count)r �rhs�regions r � _copy_fromzWindowCursor._copy_fromJ s� � ���� �&�d�3�:�&�&�s�z�2�2����{����H�� ��Y�� ��k� ,� ,�F��)�)�+�+�+�+��<�#��L�/�/�1�1�1�1�1� $�#r c �^ � t | � � � � }|� | � � |S )zcopy module interface)r2 r6 )r �cpys r �__copy__zWindowCursor.__copy__Y s* � ��d�4�j�j�l�l�����t����� r c �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)r r6 )r r4 s r �assignzWindowCursor.assign` s) � � � � ������������r r c � � d}| j }| j � � � }t |p||� � � p|� � }| j �1| j � |� � rd}n| � � � ||k r| S |r<|� | j |||d� � | _ | j � � � || j j 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)r r � file_size�min�window_sizer �includes_ofsr* �_obtain_regionr3 �_br �ofs_endr )r �offset�size�flags�need_region�man�fsizes r � use_regionzWindowCursor.use_regionf s � � ���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��>�?�?�� ��r c �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���)r r3 r s r r* zWindowCursor.unuse_region� s- � � �<�#��L�/�/��3�3�3�����r c � � t | j � � � � � | j | j | j z � 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 �bufferr r r s r rO zWindowCursor.buffer� s5 � � �$�,�-�-�/�/�0�0���4�9�T�Z�;O�1O�P�Pr c �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 �mapr s r rQ zWindowCursor.map� s � � �|���!�!�!r c � � | j duS )z2:return: True if we have a valid and usable regionN�r r s r �is_validzWindowCursor.is_valid� s � ��|�4�'�'r c � � | j duS )z?:return: True if we are associated with a specific file alreadyN)r r s r � is_associatedzWindowCursor.is_associated� s � ��{�$�&�&r c �* � | j j | j z S )zh:return: offset to the first byte pointed to by our cursor **Note:** only if is_valid() is True)r rB r r s r � ofs_beginzWindowCursor.ofs_begin� s � � �|����*�*r c �: � | j j | j z | j z S )z3:return: offset to one past the last available byte�r rB r r r s r rC zWindowCursor.ofs_end� s � � �|����*�T�Z�7�7r c � � | j S )z$:return: amount of bytes we point to)r r s r rE zWindowCursor.size� s � ��z�r c � � | j S )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 debuggingrS r s r r5 zWindowCursor.region� s � � �|�r c �z � | j j | j z |cxk o| j j | j z | j z k nc S )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 r r@ zWindowCursor.includes_ofs� sE � � ���$�)�+��`�`�`�`�t�|����7R�UY�U_�7_�`�`�`�`�`r c �4 � | j � � � S )z$:return: size of the underlying file)r r= r s r r= zWindowCursor.file_size� s � ��{�$�$�&�&�&r c �4 � | j � � � S )z>:return: path or file descriptor of the underlying mapped file)r r. r s r r. zWindowCursor.path_or_fd� s � ��{�%�%�'�'�'r c � � 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)� isinstancer r. �int� ValueErrorr s r �pathzWindowCursor.path� sH � � �d�k�,�,�.�.��4�4� _��]�^�^�^��{�%�%�'�'�'r c � � 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)rb r r. �strrd r s r �fdzWindowCursor.fd� sH � � �d�k�,�,�.�.��4�4� a��_�`�`�`��{�%�%�'�'�'r )NN)r r r )�__name__� __module__�__qualname__�__doc__� __slots__r r r# r( r r6 r9 r; rJ r* rO rQ rT rV rX rC rE r5 r@ r= r. re rh r"