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@s,eZdZeZddZddZddZdS)ArenacCsx||_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__tempfileZ_RandomNameSequencerrr r"rrrrrsrc@s4eZdZejdkrdgZngZd ddZddZdS) rZlinuxz/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%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 r9cCst||Sr)rdetach)r Zdupfdrrrr7jsr7c@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) rNmaxrArPAGESIZE_DOUBLE_ARENA_SIZE_UNTILrinforrHappend)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_THANrGpoprDrErHremoverCrB)rrVrUblocksseqrrr_discard_arenas      zHeap._discard_arenac Cs|t|j|}|t|jkr&||S|j|}|j|}|}|sV|j|=|j|=|\}}}|j||f=|j||f=|Sr) bisectZ bisect_leftrBlenrWrCrYrDrE) rr rrUr\blockrVstartstoprrr_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) rEKeyError_absorbrDrCrTr^ZinsortrB) rr`rVrarbZ prev_block_Z next_blockrUrrr_add_free_blocks(   zHeap._add_free_blockcCs^|\}}}|j||f=|j||f=||}|j|}|||sV|j|=|j|||fSr)rDrErCrZrB)rr`rVrarbrUr\rrrres      z Heap._absorbcCs4|\}}}|j|}|||f|s0||dSr)rGrZr])rr`rVrarbr[rrr_remove_allocated_blocks   zHeap._remove_allocated_blockcCsBz|j}Wntk r&Yq>YnX||||qdSr)rIrY IndexErrorrgrhrr`rrr_free_pending_blockss   zHeap._free_pending_blockscCs~t|jkr$tdt|j|jds>|j|nr6formatr@acquirerIrTreleaserKrkrgrhrjrrrfree s   z Heap.freec Cs|dkrtd|tj|kr.td|t|jkrD||j |j d7_ | | t |d|j}||\}}}||}||kr||||f|j|||f|||fW5QRSQRXdS)NrSize {0:n} out of rangeSize {0:n} too larger)r6rlr4maxsize OverflowErrorrrr>rr@rJrkrNrP _alignmentrcrgrGadd)rr rVrarbZ real_stoprrrmalloc(s  z Heap.mallocN)r#r$r%rtrXrRrrQr staticmethodrNrWr]rcrgrerhrkrorvrrrrr;ss   r;c@s"eZdZeZddZddZdS)rcCs^|dkrtd|tj|kr.td|tj|}||f|_t j |tjj |fddS)Nrrprq)args) r6rlr4rrrsr_heaprvrrr,ro)rr r`rrrrFs   zBufferWrapper.__init__cCs&|j\\}}}}t|j|||Sr)r memoryviewr)rrVrarbr rrrcreate_memoryviewOszBufferWrapper.create_memoryviewN)r#r$r%r;ryrr{rrrrrBs )r^ collectionsrrrr4r&r?contextrrr__all__r5robjectrr9r7registerr;rrrrr s&   $! P