Files
intellecton/venv/lib/python3.12/site-packages/smmap/__pycache__/mman.cpython-312.pyc
T

178 lines
25 KiB
Plaintext
Raw Normal View History

Ë
jÖ]ãózdZddlmZmZmZmZddlZddlmZgd¢Z Gdd«Z
Gd „d
«Z Gd d e «Z y)
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ó¢eZdZdZdZddZdZdZdZdZ d „Z
d
Z d Z dd Z
d
ZdZdZdZdZdZdZdZdZdZdZdZdZdZy)r 
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óJ||_||_d|_d|_d|_y)Nrr
)ÚselfÚmanagerÚregionss úM/home/antigravity/intellecton/venv/lib/python3.12/site-packages/smmap/mman.pyÚ__init__zWindowCursor.__init__%s%؈Œ
؈Œ ؈Œ ؈Œ ؈
ócó$|j«y©Ú_destroy©rs rÚ__del__zWindowCursor.__del__,ó Ø
rcó|Sr©rs rÚ __enter__zWindowCursor.__enter__/s؈ rcó$|j«yrr)rÚexc_typeÚ exc_valueÚ tracebacks rÚ__exit__zWindowCursor.__exit__2r rcó|j«|jX t|j«dk(r>|jjj |jj
««yyy#ttf$rYywxYw)z&Destruction code to decrement countersNr) Ú unuse_regionrÚlenrÚ_fdictÚpopÚ
path_or_fdÚ TypeErrorÚKeyErrorrs rrzWindowCursor._destroy5sxà ×ÑÔà ;‰;Ð 
Üt—{‘{Ó# qÒ—MM×(×,¨T¯[©[×-CÑ-CÓ-EÕ #øôœxÐ
ñ
ð 
úsžAA6Á6BÂBcóp|j|_t|j«|j«|_|j|_|j|_|j
|_|jD]}|j
«Œ|j|jj
«yy)z>Copy all data from rhs into this instance, handles usage countN)rÚtyperrrrÚincrement_client_count)rÚrhsÚregions rÚ
_copy_fromzWindowCursor._copy_fromJs‰àŸ ˆŒ
Ø&”d˜3Ÿ:™:Ó& s§z¡zÓŒ Ø—{‘{ˆŒ Ø—H‘HˆŒ Ø—Y‘YˆŒ
à—k‘kò ,ˆFØ ×  <‰<Ð L‰L×  $rcóHt|««}|j|«|S)zcopy module interface)r2r6)rÚcpys rÚ__copy__zWindowCursor.__copy__Ys àŒd4‹j‹lˆØ ؈
rcóF|j«|j|«y)z™Assign rhs to this instance. This is required in order to get a real copy.
Alternatively, you can copy an existing instance using the copy moduleN)rr6)rr4s rÚassignzWindowCursor.assign`sð
ŒØ ˜Õrcó0d}|j}|jj«}t|xs||j «xs|«}|j
.|j
j
|«rd}n|j«||k\r|S|r>|j|j|||d«|_|j
j«||j
jz
|_ t||j
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 regionsTF)rrÚ 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ðˆ Øm‰mˆØ ×'ˆÜ4=˜5 #§/¡/Ó"3Ò"<°uÓ=ˆà <‰<Ð |‰|×Ô#‘ à×
UŠ?؈Kñ Ø×-¨d¯k©k¸6À4ÈÐPUÓVˆDŒLØ L‰L× ˜TŸ\™\Ÿ_™_ÑŒ ܘ˜tŸ|™|×Ñ?ˆŒ
àˆ rcó`|j|jjd«d|_y)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éÿÿÿÿ)rr3rs rr*zWindowCursor.unuse_regionŽs'ð <‰<Ð L‰L× Ô ˆ rcót|jj««|j|j|jzS)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Úbufferrrrs rrOzWindowCursor.bufferšs4ô˜$Ÿ,™,×·±¸4¿9¹9ÀTÇZÁZÑ;OÐPrcó6|jj«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Úmaprs rrQzWindowCursor.map¤sð
|‰|×ÑÓ!rcó|jduS)z2:return: True if we have a valid and usable regionN©rrs rÚis_validzWindowCursor.is_valid«sà|‰| 'rcó|jduS)z?:return: True if we are associated with a specific file alreadyN)rrs rÚ
is_associatedzWindowCursor.is_associated¯sà{‰{ &rcóH|jj|jzS)zh:return: offset to the first byte pointed to by our cursor
**Note:** only if is_valid() is True)rrBrrs rÚ ofs_beginzWindowCursor.ofs_begin³sð|‰| §¡Ñ*rcób|jj|jz|jzS)z3:return: offset to one past the last available byte©rrBrrrs rrCzWindowCursor.ofs_end¹s$ð|‰| §¡Ñ*¨T¯Z©ZÑ7rcó|jS)z$:return: amount of bytes we point to)rrs rrEzWindowCursor.size¾s àz‰zÐrcó|jS)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 debuggingrSrs rr5zWindowCursor.regionÂsð|‰|ÐrcóÀ|jj|jz|cxkxr3|jj|jz|jzkScS)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 rr@zWindowCursor.includes_ofsÇsHð   $§)¡)ÑÖ`°t·|±|·±ÈÏÉÑ7RÐUY×U_ÑU_Ñ7_Ñ`rcó6|jj«S)z$:return: size of the underlying file)rr=rs rr=zWindowCursor.file_sizeÏsà{‰{×&rcó6|jj«S)z>:return: path or file descriptor of the underlying mapped file)rr.rs rr.zWindowCursor.path_or_fdÓsà{‰{×'rcóœt|jj«t«r t d«|jj«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)Ú
isinstancerr.ÚintÚ
ValueErrorrs rÚpathzWindowCursor.path×s:ô d—k‘k×Ô Ð {‰{×'rcóœt|jj«t«r t d«|jj«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)rbrr.Ústrrdrs rÚfdzWindowCursor.fdßs:ô
d—k‘k×Ô Ð {‰{×'r)NN)rrr)Ú__name__Ú
__module__Ú __qualname__Ú__doc__Ú __slots__rrr#r(rr6r9r;rJr*rOrQrTrVrXrCrEr5r@r=r.rerhr"rrr r s„ñ€Ióòòòòò* òó &òPòQò 
òò
aò(rr cóeZdZdZgd¢ZeZeZe Z
e Z dZ
ddejfdZdZdZdZd „Zd
Zd Zd Zd
ZdZdZdZy)r Provides a manager which will produce single size cursors that are allowed
to always map the whole file.
Clients must be written to specifically know that they are accessing their data
through a StaticWindowMapManager, as they otherwise have to deal with their window size.
These clients would have to use a SlidingWindowMapBuffer to hide this fact.
This type will always use a maximum window size, and optimize certain methods to
accommodate this fact)r,Ú _window_sizeÚ_max_memory_sizeÚ_max_handle_countÚ _memory_sizeÚ
_handle_countircót«|_||_||_||_d|_d|_|dkr"d}t«rd}||jz|_|dk(r#d}t«rd}||jz|_yy)initialize the manager with the given parameters.
:param window_size: if -1, a default window size will be chosen depending on
the operating system's architecture. It will internally be quantified to a multiple of the page size
If 0, the window may have any size, which basically results in mapping the whole file at one
:param max_memory_size: maximum amount of memory we may map at once before releasing mapped regions.
If 0, a viable default will be set depending on the system's architecture.
It is a soft limit that is tried to be kept, but nothing bad happens if we have to over-allocate
:param max_open_handles: if not maxint, limit the amount of open file handles to the given number.
Otherwise the amount is only limited by the system itself. If a system or soft limit is hit,
the manager will free as many handles as possibleré@ii N) Údictr,rorprqrrrsrÚ _MB_in_bytes)rr?Úmax_memory_sizeÚmax_open_handlesÚcoeffs rrzStaticWindowMapManager.__init__ sô“fˆŒ ØÔØ /ˆÔØ!1ˆÔØˆÔØˆÔà ˜Š?؈EÜŒ{Øà %¨×(9Ñ(9Ñ 9ˆDÔ ð ˜aÒ ØˆEÜŒ{Øà$)¨D×,=Ñ,=Ñ$=ˆ rcód}|dk(s|j|z|jkDræd}d}|jj«D]=}|D]6}|j «dk(sŒ||j
|j
ksŒ3|}|}Œ8Œ?| |S|dz
}||j
|«=|jd«|xj|j«zc_|xjdzc_ |dk(rŒÉ|j|z|jkDrŒæ|S)amUnmap the region which was least-recently used and has no client
:param size: size of the region we want to map next (assuming its not already mapped partially or full
if 0, we try to free any available region
:return: Amount of freed regions
.. Note::
We don't raise exceptions anymore, in order to keep the system working, allowing temporary overallocation.
If the system runs out of memory, it will tell.
.. TODO::
implement a case where all unusued regions are discarded efficiently.
Currently its only brute force
rNrrL)
rrrpr,ÚvaluesÚ client_countÚ_ucÚindexr3rErs)rrEÚ num_foundÚ
lru_regionÚlru_listrr5s rÚ_collect_lru_regionz*StaticWindowMapManager._collect_lru_region/sðˆ ØqŠy˜d×/°$Ñ×9NÑ9NÒˆJ؈HØŸ;™;×
+Ø+×Ó/°6·:±:À
ÇÁÓ3NØ%+˜
Ø#*™ñ 
ÐðÐð
˜‰Nˆ˜Ÿ¨
Ó × -¨bÔ × Ò  §¡Ó!2Ñ Ø × Ò  !Ñ ð-q‹y˜d×/°$Ñ6¸×9NÑ9NÓNð0Ðrcó|j|z|jkDr|j|«d}|rt|«dk(sJ|d}nz |j |j «dt j|«}|xjdz
c_
|xj|j«z
c_|j|«|j|«sJ|S#t$r,|r|jd«|j||||d«cYSwxYw)z¸Utility to create a new region - for more information on the parameters,
see MapCursor.use_region.
:param a: A regions (a)rray
:return: The newly created regionNrrT)rrrpr+Ú MapRegionClsr.ÚsysÚmaxsizeÚ ExceptionrArsrEÚappendr@)rÚarDrErFÚ is_recursiveÚrs rrAz%StaticWindowMapManager._obtain_regionXsð
× Ñ ˜ # d×&;Ñ&;Ò × $  
ˆÙ Üq“6˜Q’;Ð !‘‰Að
IØ×% a§l¡l£n°a¼¿¹ÀeÓLð
× Ò  !Ñ Ø × Ò  §¡£Ñ Ø
H‰HQŒKð~‰~˜fÔˆøô)ò
Iñ
 ðà×Ô×*¨1¨f°d¸EÀ4Ó
IúsÁ 0CÃ2D Ä D có |jj|«}|€ |j|«}||j|<|j||«S)a
:return: a cursor pointing to the given path or file descriptor.
It can be used to map new regions of the file into memory
**Note:** if a file descriptor is given, it is assumed to be open and valid,
but may be closed afterwards. To refer to the same file, you may reuse
your existing file descriptor, but keep in mind that new windows can only
be mapped as long as it stays valid. This is why the using actual file paths
are preferred unless you plan to keep the file descriptor open.
**Note:** file descriptors are problematic as they are not necessarily unique, as two
different files opened and closed in succession might have the same file descriptor id.
**Note:** Using file descriptors directly is faster once new windows are mapped as it
prevents the file to be opened again just for the purpose of mapping it.)r,ÚgetÚMapRegionListClsÚWindowCursorCls)rr.rs rÚ make_cursorz"StaticWindowMapManager.make_cursorsLð —+‘+—/‘/ *ÓØ ˆ?Ø×+¨JÓ7ˆGØ&-ˆDK‰K˜
Ñ ×# D¨'Ó2rcó$|jd«S)z]Collect all available free-to-collect mapped regions
:return: Amount of freed handlesr)rs rÚcollectzStaticWindowMapManager.collect˜sð×Ó*rcó|jS)zO:return: amount of file handles in use. Each mapped region uses one file handle)rsrs rÚnum_file_handlesz'StaticWindowMapManager.num_file_handlessà×!rcó\tdd|jj«D«d«S)z$Amount of opened files in the systemcó ||zSrr")Úys rú<lambda>z7StaticWindowMapManager.num_open_files.<locals>.<lambda>£s
 1 q¡5€rc3ó>K|]}t|«dkDsŒdŒy­w)rrN)r+)Ú.0Úrlists rú <genexpr>z8StaticWindowMapManager.num_open_files.<locals>.<genexpr>£sèø€Ò*a°ÔRUÐV[ÓR\Ð_`ÓR`¬1Ñ*aùsr)rr,r|rs rÚnum_open_filesz%StaticWindowMapManager.num_open_files¡s&äÑ(Ñ*a¸$¿+¹+×:LÑ:LÓ:NÔ*aÐcdÓercó|jS)z8:return: size of each window when allocating new regions)rors rr?z"StaticWindowMapManager.window_size¥óà× Ñ Ð rcó|jS)z2:return: amount of bytes currently mapped in total)rrrs rÚmapped_memory_sizez)StaticWindowMapManager.mapped_memory_size©rcó|jS)z5:return: maximum amount of handles we may have opened)rqrs rÚmax_file_handlesz'StaticWindowMapManager.max_file_handles­sà×%rcó|jS)z1:return: maximum amount of memory we may allocate)rprs rÚmax_mapped_memory_sizez-StaticWindowMapManager.max_mapped_memory_size±sà×$rcóÒtjdk7ryd}|jj«D]3\}}|j |«sŒ|D]}|j «|dz
}ŒŒ5|S)a×ONLY AVAILABLE ON WINDOWS
On windows removing files is not allowed if anybody still has it opened.
If this process is ourselves, and if the whole process uses this memory