
    .Ph4                         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dS )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                 N    | t           z  t           z  }|r|| k    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      J/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/smmap/util.pyr   r      s5     ''+@
@C %SCZZ$$J    c                  "    t           j        dk    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    e Zd ZdZdZd Zd Zed             Zd Z	d Z
d Zd	 Zd
S )r	   zWUtility type which is used to snap windows towards each other, and to adjust their sizeofssizec                 "    || _         || _        d S Nr   )selfoffsetr   s      r   __init__zMapWindow.__init__-   s    			r   c                 $    d| j         | j        fz  S )NzMapWindow(%i, %i)r   r   s    r   __repr__zMapWindow.__repr__1   s    "dh	%:::r   c                 H     | |j         |                                          S )z!:return: new window from a region_br   )clsregions     r   from_regionzMapWindow.from_region4   s      s69fkkmm,,,r   c                      | j         | j        z   S r   r   r    s    r   ofs_endzMapWindow.ofs_end9   s    x$)##r   c                     t          | j        d          }| xj        | j        |z
  z  c_        || _        t          | j        d          | _        dS )z<Assures the previous window area is contained in the new oner      N)r   r   r   )r   nofss     r   alignzMapWindow.align<   sG    TXq))		TX_$		!$)Q//			r   c                     | j         |                                z
  }|| j        z   }||t          ||          z
  z  }| xj         |z  c_         | xj        |z  c_        dS )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_tozMapWindow.extend_left_toC   sc     x&..***ty E8,,,,D		T				r   c                 t    t          | j        |j        |                                 z
  z   |          | _        dS )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/     	VZ$,,..%@A8LL			r   N)__name__
__module____qualname____doc__	__slots__r   r!   classmethodr'   r)   r-   r4   r6   r   r   r   r	   r	   %   s        aaI
  ; ; ; - - [-$ $ $0 0 0  M M M M 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dS )r
   z{Defines a mapped region of memory, aligned to pagesizes

    **Note:** deallocates used region automatically on destruction)r$   _mf_uc_size__weakref__r   c           	         || _         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        |           n/# t          |t"                    rt          j        |           w w xY w|                                  dS )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#&& 	WBBR[72z13M3M%MPU%UVVB	S999F!NG
 bhrll2W<nMMKB66v66DH TXDJ*c**  *c** 
 	##%%%%%s   (A0D ,D.c                 >    d| j         |                                 fz  S )NzMapRegion<%i, %i>r#   r    s    r   r!   zMapRegion.__repr__   s    "dgtyy{{%;;;r   c                     | j         S )z':return: a buffer containing the memoryr>   r    s    r   bufferzMapRegion.buffer   	    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r   c                     | j         S )z1:return: total size of the mapped region in bytes)r@   r    s    r   r   zMapRegion.size   s
    zr   c                      | 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##r   c                 F    | j         |cxk    o| j         | j        z   k     n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#4444$* 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   r+   c                     | xj         |z  c_         | j         dk    sJ d| j         z              |                                 dk    r|                                  dS dS )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)r?   ri   releaserf   s     r   rQ   z MapRegion.increment_client_count   s`     	Cx"}}}\_c_gg}}}!##LLNNN45r   c                 8    | j                                          dS )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   s        F F  I%& %& %& %&N< < <
        $ $ $5 5 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                 F    t                                          |           S r   )super__new__)r%   path	__class__s     r   rt   zMapRegionList.__new__   s    wws###r   c                 "    || _         d | _        d S 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                     | j         at          | j        t                    r$t	          j        | j                  j        | _         n#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   sZ    ?"$*C00 E"$'$*:";";"C"$(4+;"<"<"D r   )
r7   r8   r9   r:   r;   rt   r   rR   r{   __classcell__)rv   s   @r   r   r      sr        PPI
$ $ $ $ $       	 	 	 	 	 	 	r   r   )r:   rG   r   r   r   r   __all__r   r   r	   r
   listr   r   r   r   <module>r      s   r r 				 



 " " " " " " " " & & & & & &O O O  ' ' '+M +M +M +M +M +M +M +M\h h h h h h h hZ    D     r   