U e5dj-@sddlZddlmZddlZddlZddlZddlZddlZddlm Z m Z ddl m Z dgZ ejdkrddlZGdd d eZn,Gd d d eZd d Zd dZe eeGdddeZGdddeZdS)N) defaultdict) reductionassert_spawning)util BufferWrapperZwin32c@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 irZbufr,/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__tempfileZ_RandomNameSequencerrr r"rrrrrs rc@s8eZdZdZejdkrdgZngZd ddZddZd S) rzJ A shared memory area backed by a temporary file (POSIX). Zlinuxz/dev/shmr cCsx||_||_|dkrbtjdt||d\|_}t|t |tj |jft |j|t |j|j|_ dS)Nr zpym-%d-)prefixdir)r fdr'Zmkstemprr _choose_dirunlinkrFinalizer ftruncaterr)rr r*rrrrrMs   rcCs6|jD]&}t|}|j|j|kr|SqtSr)_dir_candidatesrstatvfsf_bavailf_frsizerZ get_temp_dir)rr dstrrrr+[s    zArena._choose_dirN)r ) r#r$r%r&sysplatformr/rr+rrrrrCs   cCs(|jdkrtdt|jt|jffS)Nr zDArena is unpicklable because forking was enabled when it was created)r* ValueError rebuild_arenar rZDupFd)arrr reduce_arenads r:cCst||Sr)rdetach)r Zdupfdrrrr8jsr8c@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 threadingZLock_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) rOmaxrBrPAGESIZE_DOUBLE_ARENA_SIZE_UNTILrinforrIappend)rr lengtharenarrr _new_arenas   zHeap._new_arenacCsz|j}||jkrdS|j|}|j|df=|j||f=|j||j|}||d|f|sv|j|=|j |dSr>) r _DISCARD_FREE_SPACE_LARGER_THANrHpoprErFrIremoverDrC)rrWrVblocksseqrrr_discard_arenas      zHeap._discard_arenac Cs|t|j|}|t|jkr&||S|j|}|j|}|}|sV|j|=|j|=|\}}}|j||f=|j||f=|Sr) bisectZ bisect_leftrClenrXrDrZrErF) rr rrVr]blockrWstartstoprrr_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) rFKeyError_absorbrErDrUr_ZinsortrC) rrarWrbrcZ prev_block_Z next_blockrVrrr_add_free_blocks(   zHeap._add_free_blockcCs^|\}}}|j||f=|j||f=||}|j|}|||sV|j|=|j|||fSr)rErFrDr[rC)rrarWrbrcrVr]rrrrfs      z Heap._absorbcCs4|\}}}|j|}|||f|s0||dSr)rHr[r^)rrarWrbrcr\rrr_remove_allocated_blocks   zHeap._remove_allocated_blockcCsBz|j}Wntk r&Yq>YnX||||qdSr)rJrZ IndexErrorrhrirrarrr_free_pending_blockss   zHeap._free_pending_blockscCs~t|jkr$tdt|j|jds>|j|n s&   $! P