
    g4                         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      ?/var/www/openai/venv/lib/python3.12/site-packages/smmap/util.pyr   r      s.     ''+@
@CSCZ$$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    "dhh		%:::r   c                 D     | |j                   |j                               S )z!:return: new window from a region_br   )clsregions     r   from_regionzMapWindow.from_region4   s     699fkkm,,r   c                 4    | j                   | j                  z   S r   r   r    s    r   ofs_endzMapWindow.ofs_end9   s    xx$))##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    TXXq)		TXX_$	!$))Q/	r   c                     | j                   |j                         z
  }|| j                  z   }||t        ||      z
  z  }| xj                   |z  c_         | xj                  |z  c_        y)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   sY     xx&..**tyy E8,,,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*     		VZZ$,,.%@A8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[[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                 @    d| j                   | j                         fz  S )NzMapRegion<%i, %i>r#   r    s    r   r!   zMapRegion.__repr__   s    "dggtyy{%;;;r   c                     | j                   S )z':return: a buffer containing the memoryr>   r    s    r   bufferzMapRegion.buffer       xx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    wwr   c                     | j                   S )z1:return: total size of the mapped region in bytes)r@   r    s    r   r   zMapRegion.size   s    zz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    ww##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)    ww#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)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   sS     	Cxx"}g\_c_g_ggg}!#LLN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   