U AT^j-@sddlZddlmZddlZddlZddlZddlZddlZddlm Z m Z ddl m Z dgZ ejdkrddlZGdd d eZn,Gd d d eZd d Zd dZe eeGdddeZGdddeZdS)N) defaultdict) reductionassert_spawning)util BufferWrapperwin32c@s0eZdZdZeZddZddZddZ dS) ArenazL A shared memory area backed by anonymous memory (Windows). cCsx||_tdD]B}dtt|jf}tjd||d}tdkrHqZ| qt d||_ ||_ |j|j f|_ dS)Ndz pym-%d-%sZtagnamerzCannot find name for new mmap)sizerangeosgetpidnext_randmmap_winapiZ GetLastErrorcloseFileExistsErrornamebuffer_state)selfr irbufr,/usr/lib64/python3.8/multiprocessing/heap.py__init__&s   Arena.__init__cCst||jSN)rr)rrrr __getstate__5szArena.__getstate__cCs,|\|_|_|_tjd|j|jd|_dS)Nr r )r rrrr)rstaterrr __setstate__9szArena.__setstate__N) __name__ __module__ __qualname____doc__tempfile_RandomNameSequencerrr"r$rrrrr s r c@s8eZdZdZejdkrdgZngZd ddZddZd S) r zJ A shared memory area backed by a temporary file (POSIX). linuxz/dev/shmr cCsx||_||_|dkrbtjdt||d\|_}t|t |tj |jft |j|t |j|j|_ dS)Nr zpym-%d-)prefixdir)r fdr)mkstemprr _choose_dirunlinkrFinalizer ftruncaterr)rr r.rrrrrMs   r cCs6|jD]&}t|}|j|j|kr|SqtSr!)_dir_candidatesrstatvfsf_bavailf_frsizer get_temp_dir)rr dstrrrr0[s    zArena._choose_dirN)r ) r%r&r'r(sysplatformr4rr0rrrrr Cs   cCs(|jdkrtdt|jt|jffS)Nr zDArena is unpicklable because forking was enabled when it was created)r. ValueError rebuild_arenar rDupFd)arrr reduce_arenads rAcCst||Sr!)r detach)r Zdupfdrrrr>jsr>c@szeZdZdZdZdZejfddZe ddZ ddZ d d Z d d Z d dZddZddZddZddZddZdS)Heapi@cCsXt|_t|_||_g|_i|_i|_ i|_ t t |_ g|_g|_d|_d|_dSNr)rr_lastpid threadingLock_lock_size_lengths _len_to_seq_start_to_block_stop_to_blockrset_allocated_blocks_arenas_pending_free_blocks _n_mallocs_n_frees)rr rrrr{s   z Heap.__init__cCs|d}|||@S)Nrr)nZ alignmentmaskrrr_roundupsz Heap._roundupcCsZ|t|j|tj}|j|jkr0|jd9_td|t|}|j ||d|fS)Nz"allocating a new mmap of length %dr) rWmaxrJrPAGESIZE_DOUBLE_ARENA_SIZE_UNTILrinfor rQappend)rr lengtharenarrr _new_arenas   zHeap._new_arenacCsz|j}||jkrdS|j|}|j|df=|j||f=|j||j|}||d|f|sv|j|=|j |dSrE) r _DISCARD_FREE_SPACE_LARGER_THANrPpoprMrNrQremoverLrK)rr_r^blocksseqrrr_discard_arenas      zHeap._discard_arenac Cs|t|j|}|t|jkr&||S|j|}|j|}|}|sV|j|=|j|=|\}}}|j||f=|j||f=|Sr!) bisect bisect_leftrKlenr`rLrbrMrN) rr rr^reblockr_startstoprrr_mallocs      z Heap._mallocc Cs|\}}}z|j||f}Wntk r0YnX||\}}z|j||f}Wntk rfYnX||\}}|||f}||}z|j||Wn.tk r|g|j|<t|j|YnX||j||f<||j||f<dSr!) rNKeyError_absorbrMrLr]rginsortrK) rrjr_rkrlZ prev_block_Z next_blockr^rrr_add_free_blocks(   zHeap._add_free_blockcCs^|\}}}|j||f=|j||f=||}|j|}|||sV|j|=|j|||fSr!)rMrNrLrcrK)rrjr_rkrlr^rerrrros      z Heap._absorbcCs4|\}}}|j|}|||f|s0||dSr!)rPrcrf)rrjr_rkrlrdrrr_remove_allocated_blocks   zHeap._remove_allocated_blockcCsBz|j}Wntk r&Yq>YnX||||qdSr!)rRrb IndexErrorrrrsrrjrrr_free_pending_blockss   zHeap._free_pending_blockscCs~t|jkr$tdt|j|jds>|j|nregisterrCrrrrr s&   $! P