
    ch4                         S r SSKrSSKrSSKJrJr  SSKJr  / SQrS rS r " S S	5      r	 " S
 S5      r
 " S S\5      rg)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                 P    U [         -  [         -  nU(       a  X :w  a	  U[         -  nU$ )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      ]C:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\smmap/util.pyr   r      s,     ''+@
@CSZ$$J    c                  (    [         R                  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                   P    \ rS rSrSrSrS rS r\S 5       r	S r
S rS	 rS
 rSrg)r	   %   zWUtility type which is used to snap windows towards each other, and to adjust their sizeofssizec                     Xl         X l        g Nr   )selfoffsetr   s      r   __init__MapWindow.__init__-   s    	r   c                 8    SU R                   U R                  4-  $ )NzMapWindow(%i, %i)r   r   s    r   __repr__MapWindow.__repr__1   s    "dhh		%:::r   c                 D    U " UR                   UR                  5       5      $ )z!:return: new window from a region_br   )clsregions     r   from_regionMapWindow.from_region4   s     699fkkm,,r   c                 4    U R                   U R                  -   $ r   r   r"   s    r   ofs_endMapWindow.ofs_end9   s    xx$))##r   c                     [        U R                  S5      nU =R                  U R                  U-
  -  sl        Xl        [        U R                  S5      U l        g)z<Assures the previous window area is contained in the new oner      N)r   r   r   )r   nofss     r   alignMapWindow.align<   s?    TXXq)		TXX_$	!$))Q/	r   c                     U R                   UR                  5       -
  nX0R                  -   nX4[        XB5      -
  -  nU =R                   U-  sl         U =R                  U-  sl        g)zAdjust 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_toMapWindow.extend_left_toC   sS     xx&..**yy E,,,D		T	r   c                 v    [        U R                  UR                  U R                  5       -
  -   U5      U l        g)zhAdjust the size to make our window end where the right window begins, but don't
get larger than max_sizeN)r5   r   r   r-   )r   r6   r7   s      r   extend_right_toMapWindow.extend_right_toM   s*     		VZZ$,,.%@A8L	r   N)__name__
__module____qualname____firstlineno____doc__	__slots__r   r#   classmethodr*   r-   r2   r:   r=   __static_attributes__r   r   r   r	   r	   %   s?    aI
; - -$0Mr   r	   c                   j    \ rS rSrSr/ SQrSS jrS rS rS r	S r
S	 rS
 rS rS rSS jrS rSrg)r
   S   zwDefines a mapped region of memory, aligned to pagesizes

**Note:** deallocates used region automatically on destruction)r'   _mf_uc_size__weakref__c           	      ~   X l         SU l        SU l        [        U[        5      (       a  UnO:[
        R                  " U[
        R                  [        [
        SS5      -  U-  5      n [        [        US9nUnUn[        [
        R                  " U5      R                  U-
  U5      n	[        XY40 UD6U l        [!        U R                  5      U l        [        U["        5      (       a  [
        R$                  " U5        U R'                  5         g! [        U["        5      (       a  [
        R$                  " U5        f f = f)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'   rK   rJ   
isinstanceintosopenO_RDONLYgetattrdictr   r5   fstatst_sizer   rI   lenstrcloseincrement_client_count)
r   
path_or_fdr   r   flagsfdkwargscorrected_sizesizeofsactual_sizes
             r   r   MapRegion.__init__c   s     
j#&&BR[[72z13M%MPU%UVB	S9F!NG
 bhhrl22W<nMKB6v6DH TXXDJ*c** 	##% *c** +s   (A*D .D<c                 @    SU R                   U R                  5       4-  $ )NzMapRegion<%i, %i>r&   r"   s    r   r#   MapRegion.__repr__   s    "dggtyy{%;;;r   c                     U R                   $ )z':return: a buffer containing the memoryrI   r"   s    r   bufferMapRegion.buffer       xxr   c                     U R                   $ )z+:return: a memory map containing the memoryrh   r"   s    r   mapMapRegion.map   rk   r   c                     U R                   $ )z>:return: absolute byte offset to the first byte of the mapping)r'   r"   s    r   	ofs_beginMapRegion.ofs_begin   s    wwr   c                     U R                   $ )z1:return: total size of the mapped region in bytes)rK   r"   s    r   r   MapRegion.size   s    zzr   c                 4    U R                   U R                  -   $ )zE:return: Absolute offset to one byte beyond the mapping into the filer'   rK   r"   s    r   r-   MapRegion.ofs_end   s    ww##r   c                 n    U R                   Us=:*  =(       a    U R                   U R                  -   :  $ s  $ )zB:return: True if the given offset can be read in our mapped regionru   r   r   s     r   includes_ofsMapRegion.includes_ofs   s,    ww#44$** 44444r   c                     U R                   $ )z6:return: number of clients currently using this region)rJ   r"   s    r   client_countMapRegion.client_count   rk   r   c                     U =R                   U-  sl         U R                   S:  d   SU R                   -  5       eU R                  5       S:X  a  U R                  5         gg)zAdjust 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 usedz<Increments must match decrements, usage counter negative: %ir   TF)rJ   r|   releaserx   s     r   r\    MapRegion.increment_client_count   sS     	Cxx"}g\_c_g_ggg}!#LLNr   c                 8    U R                   R                  5         g)zbRelease all resources this instance might hold. Must only be called if there usage_count() is zeroN)rI   r[   r"   s    r   r   MapRegion.release   s    r   )r'   rI   rK   rJ   N)r   )r0   )r?   r@   rA   rB   rC   rD   r   r#   ri   rm   rp   r   r-   ry   r|   r\   r   rF   r   r   r   r
   r
   S   sG    FI%&N<
$5r   r
   c                   B   ^  \ rS rSrSrSrU 4S jrS rS rS r	Sr
U =r$ )	r      zFList of MapRegion instances associating a path with a list of regions._path_or_fd
_file_sizec                 "   > [         TU ]  U 5      $ r   )super__new__)r(   path	__class__s     r   r   MapRegionList.__new__   s    ws##r   c                     Xl         S U l        g r   r   )r   r]   s     r   r   MapRegionList.__init__   s    %r   c                     U R                   $ )z3:return: path or file descriptor we are attached to)r   r"   s    r   r]   MapRegionList.path_or_fd   s    r   c                 F   U R                   c  [        U R                  [        5      (       a;  [        R
                  " U R                  5      R                  U l         U R                   $ [        R                  " U R                  5      R                  U l         U R                   $ )z :return: size of file we manager)r   rP   r   rZ   rR   statrX   rW   r"   s    r   	file_sizeMapRegionList.file_size   sq    ??"$**C00"$''$*:*:";"C"C
  #%((4+;+;"<"D"D r   )r   r   )r?   r@   rA   rB   rC   rD   r   r   r]   r   rF   __classcell__)r   s   @r   r   r      s&    PI
$ 	 	r   r   )rC   rR   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   