Ë
    ëmxi4  ã                   ó†   — d Z ddlZddlZddlmZmZ ddlmZ g d¢Zd„ Zd„ Z G d„ d	«      Z	 G d
„ d«      Z
 G d„ de«      Zy)zlModule containing a memory memory manager which provides a sliding window on a number of memory mapped filesé    N)ÚmmapÚACCESS_READ©ÚALLOCATIONGRANULARITY)Úalign_to_mmapÚ	is_64_bitÚ	MapWindowÚ	MapRegionÚMapRegionListr   c                 óF   — | t         z  t         z  }|r|| k7  r	|t         z  }|S )a=  
    Align the given integer number to the closest page offset, which usually is 4096 bytes.

    :param round_up: if True, the next higher multiple of page size is used, otherwise
        the lower page_size will be used (i.e. if True, 1 becomes 4096, otherwise it becomes 0)
    :return: num rounded to closest pager   )ÚnumÚround_upÚress      úA/home/htdocs/ttos/venv/lib/python3.12/site-packages/smmap/util.pyr   r      s.   € ð Ô'Ñ'Ô+@Ñ
@€CÙ�S˜C’ZØÔ$Ñ$ˆà€Jó    c                  ó(   — t         j                  dkD  S )zO:return: True if the system is 64 bit. Otherwise it can be assumed to be 32 bitl   ÿÿ )ÚsysÚmaxsize© r   r   r   r      s   € ä�;‰;˜Ñ&Ð&r   c                   óH   — e Zd ZdZdZd„ Zd„ Zed„ «       Zd„ Z	d„ Z
d„ Zd	„ Zy
)r	   zWUtility type which is used to snap windows towards each other, and to adjust their size©ÚofsÚsizec                 ó    — || _         || _        y ©Nr   )ÚselfÚoffsetr   s      r   Ú__init__zMapWindow.__init__-   s   € ØˆŒØˆ�	r   c                 ó8   — d| j                   | j                  fz  S )NzMapWindow(%i, %i)r   ©r   s    r   Ú__repr__zMapWindow.__repr__1   s   € Ø" d§h¡h°·	±	Ð%:Ñ:Ð:r   c                 óD   —  | |j                   |j                  «       «      S )z!:return: new window from a region©Ú_br   )ÚclsÚregions     r   Úfrom_regionzMapWindow.from_region4   s   € ñ �6—9‘9˜fŸk™k›mÓ,Ð,r   c                 ó4   — | j                   | j                  z   S r   r   r    s    r   Úofs_endzMapWindow.ofs_end9   s   € Ø�x‰x˜$Ÿ)™)Ñ#Ð#r   c                 ó¸   — t        | j                  d«      }| xj                  | j                  |z
  z  c_        || _        t        | j                  d«      | _        y)z<Assures the previous window area is contained in the new oner   é   N)r   r   r   )r   Únofss     r   ÚalignzMapWindow.align<   sA   € ä˜TŸX™X qÓ)ˆØ�	Š	�T—X‘X ‘_Ñ$�	ØˆŒÜ! $§)¡)¨QÓ/ˆ�	r   c                 óÔ   — | j                   |j                  «       z
  }|| j                  z   }||t        ||«      z
  z  }| xj                   |z  c_         | xj                  |z  c_        y)zÛAdjust the offset to start where the given window on our left ends if possible,
        but don't make yourself larger than max_size.
        The resize will assure that the new window still contains the old window areaN)r   r)   r   Úmin)r   ÚwindowÚmax_sizeÚrofsÚnsizes        r   Úextend_left_tozMapWindow.extend_left_toC   sY   € ð �x‰x˜&Ÿ.™.Ó*Ñ*ˆØ�t—y‘yÑ ˆØ�œ˜E 8Ó,Ñ,Ñ,ˆØ�Š�DÑ�Ø�	Š	�TÑŽ	r   c                 óv   — t        | j                  |j                  | j                  «       z
  z   |«      | _        y)zpAdjust the size to make our window end where the right window begins, but don't
        get larger than max_sizeN)r/   r   r   r)   )r   r0   r1   s      r   Úextend_right_tozMapWindow.extend_right_toM   s*   € ô ˜Ÿ	™	 V§Z¡Z°$·,±,³.Ñ%@ÑAÀ8ÓLˆ�	r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   r!   Úclassmethodr'   r)   r-   r4   r6   r   r   r   r	   r	   %   s?   „ áað€Iò
ò;ð ñ-ó ð-ò$ò0òóMr   r	   c                   ó^   — e Zd ZdZg d¢Zdd„Zd„ Zd„ Zd„ Zd„ Z	d„ Z
d	„ Zd
„ Zd„ Zdd„Zd„ Zy)r
   z{Defines a mapped region of memory, aligned to pagesizes

    **Note:** deallocates used region automatically on destruction)r$   Ú_mfÚ_ucÚ_sizeÚ__weakref__c           	      ód  — || _         d| _        d| _        t        |t        «      r|}n9t        j                  |t
        j                  t        t
        dd«      z  |z  «      }	 t        t        |¬«      }|}|}t        t        j                  |«      j                  |z
  |«      }	t        ||	fi |¤Ž| _        t!        | j                  «      | _        t        |t"        «      rt        j$                  |«       	 | j'                  «        y# t        |t"        «      rt        j$                  |«       w w xY w)aÑ  Initialize a region, allocate the memory map
        :param path_or_fd: path to the file to map, or the opened file descriptor
        :param ofs: **aligned** offset into the file to be mapped
        :param size: if size is larger then the file on disk, the whole file will be
            allocated the the size automatically adjusted
        :param flags: additional flags to be given when opening the file.
        :raise Exception: if no memory can be allocatedr   ÚO_BINARY)Úaccessr   N)r$   r@   r?   Ú
isinstanceÚintÚosÚopenÚO_RDONLYÚgetattrÚdictr   r/   ÚfstatÚst_sizer   r>   ÚlenÚstrÚcloseÚincrement_client_count)
r   Ú
path_or_fdr   r   ÚflagsÚfdÚkwargsÚcorrected_sizeÚsizeofsÚactual_sizes
             r   r   zMapRegion.__init__c   sñ   € ð ˆŒØˆŒ
ØˆŒä�j¤#Ô&Ø‰Bä—‘˜¤R§[¡[´7¼2¸zÈ1Ó3MÑ%MÐPUÑ%UÓVˆBð	Ü¤°SÔ9ˆFØ!ˆNØˆGô
 œbŸh™h r›l×2Ñ2°WÑ<¸nÓMˆKÜ˜B Ñ6¨vÑ6ˆDŒHô ˜TŸX™X›ˆDŒJä˜*¤cÔ*Ü—‘˜•ð 	×#Ñ#Õ%øô ˜*¤cÔ*Ü—‘˜•ð +ús   Á#A-D Ä(D/c                 ó@   — d| j                   | j                  «       fz  S )NzMapRegion<%i, %i>r#   r    s    r   r!   zMapRegion.__repr__Š   s   € Ø" d§g¡g¨t¯y©y«{Ð%;Ñ;Ð;r   c                 ó   — | j                   S )z':return: a buffer containing the memory©r>   r    s    r   ÚbufferzMapRegion.buffer�   ó   € à�x‰xˆr   c                 ó   — | j                   S )z+:return: a memory map containing the memoryr[   r    s    r   ÚmapzMapRegion.map“   r]   r   c                 ó   — | j                   S )z>:return: absolute byte offset to the first byte of the mapping)r$   r    s    r   Ú	ofs_beginzMapRegion.ofs_begin—   s   € à�w‰wˆr   c                 ó   — | j                   S )z1:return: total size of the mapped region in bytes)r@   r    s    r   r   zMapRegion.size›   s   € à�z‰zÐr   c                 ó4   — | j                   | j                  z   S )zE:return: Absolute offset to one byte beyond the mapping into the file©r$   r@   r    s    r   r)   zMapRegion.ofs_endŸ   s   € à�w‰w˜Ÿ™Ñ#Ð#r   c                 ód   — | j                   |cxk  xr | j                   | j                  z   k  S c S )zB:return: True if the given offset can be read in our mapped regionrd   ©r   r   s     r   Úincludes_ofszMapRegion.includes_ofs£   s)   € à�w‰w˜#Ö4 §¡¨$¯*©*Ñ 4Ñ4Ð4Ñ4Ð4r   c                 ó   — | j                   S )z6:return: number of clients currently using this region)r?   r    s    r   Úclient_countzMapRegion.client_count§   r]   r   c                 ó¼   — | xj                   |z  c_         | j                   dkD  sJ d| j                   z  «       ‚| j                  «       dk(  r| j                  «        yy)zîAdjust the usage count by the given positive or negative offset.
        If usage count equals 0, we will auto-release our resources
        :return: True if we released resources, False otherwise. In the latter case, we can still be usedéÿÿÿÿz<Increments must match decrements, usage counter negative: %ir   TF)r?   ri   Úreleaserf   s     r   rQ   z MapRegion.increment_client_count«   sS   € ð 	�Š�C‰�Ø�x‰x˜"Š}ÐgÐ\Ð_c×_gÑ_gÑgÓgˆ}à×ÑÓ !Ò#Ø�L‰LŒNØàr   c                 ó8   — | j                   j                  «        y)zbRelease all resources this instance might hold. Must only be called if there usage_count() is zeroN)r>   rP   r    s    r   rl   zMapRegion.release¹   s   € à�‰�‰Õr   N)r   )r+   )r7   r8   r9   r:   r;   r   r!   r\   r_   ra   r   r)   rg   ri   rQ   rl   r   r   r   r
   r
   S   sG   „ ñFò€Ió%&òN<ò
òòòò$ò5òóór   r
   c                   ó8   ‡ — e Zd ZdZdZˆ fd„Zd„ Zd„ Zd„ Zˆ xZ	S )r   zFList of MapRegion instances associating a path with a list of regions.©Ú_path_or_fdÚ
_file_sizec                 ó"   •— t         ‰| �  | «      S r   )ÚsuperÚ__new__)r%   ÚpathÚ	__class__s     €r   rt   zMapRegionList.__new__È   s   ø€ Ü‰w‰˜sÓ#Ð#r   c                 ó    — || _         d | _        y r   ro   )r   rR   s     r   r   zMapRegionList.__init__Ë   s   € Ø%ˆÔØˆ�r   c                 ó   — | j                   S )z3:return: path or file descriptor we are attached to)rp   r    s    r   rR   zMapRegionList.path_or_fdÏ   s   € à×ÑÐr   c                 ó6  — | j                   €‚t        | j                  t        «      r:t	        j
                  | j                  «      j                  | _         | j                   S t	        j                  | j                  «      j                  | _         | j                   S )z :return: size of file we manager)rq   rE   rp   rO   rG   ÚstatrM   rL   r    s    r   Ú	file_sizezMapRegionList.file_sizeÓ   sn   € à�?‰?Ð"Ü˜$×*Ñ*¬CÔ0Ü"$§'¡'¨$×*:Ñ*:Ó";×"CÑ"C�”ð
 �‰Ðô #%§(¡(¨4×+;Ñ+;Ó"<×"DÑ"D�”ð �‰Ðr   )
r7   r8   r9   r:   r;   rt   r   rR   r{   Ú__classcell__)rv   s   @r   r   r   À   s!   ø„ áPð€Iô
$òò ö	r   r   )r:   rG   r   r   r   r   Ú__all__r   r   r	   r
   Úlistr   r   r   r   ú<module>r      sL   ðÙ rÛ 	Û 
ç "Ý &òO€òò'÷+Mñ +M÷\hñ hôZ�Dõ r   