B /g^"c@sddlZddlZddlZddlZddlZddlZddlmZmZddl m Z dgZ ej dkrvddl Z GdddeZn,Gd ddeZd d Zd d ZeeeGdddeZGdddeZdS)N) reductionassert_spawning)util BufferWrapperZwin32c@s,eZdZeZddZddZddZdS)ArenacCsz||_xTtdD]@}dtt|jf}tjd||d}tdkrHP| qWt d||_ ||_ |j|j f|_ dS)Nidz pym-%d-%sZtagnamerzCannot find name for new mmap)sizeZrangeosgetpidZnext_randmmap_winapiZ GetLastErrorcloseZFileExistsErrornamebuffer_state)selfr irZbufr,/usr/lib64/python3.7/multiprocessing/heap.py__init__"s  Arena.__init__cCst||jSN)rr)rrrr __getstate__1szArena.__getstate__cCs,|\|_|_|_tjd|j|jd|_dS)Nrr)r rrr r)rZstaterrr __setstate__5szArena.__setstate__N) __name__ __module__ __qualname__tempfileZ_RandomNameSequencer rrrrrrrrsrc@s4eZdZejdkrdgZngZd ddZddZdS) rZlinuxz/dev/shmrcCsx||_||_|dkrbtjdt||d\|_}t|t |tj |jft |j|t |j|j|_ dS)Nrzpym-%d-)ZprefixZdir)r fdrZmkstempr r _choose_dirZunlinkrFinalizerZ ftruncater r)rr r rrrrrDs  rcCs6x,|jD]"}t|}|j|j|kr|SqWtSr)_dir_candidatesr ZstatvfsZf_bavailZf_frsizerZ get_temp_dir)rr ZdZstrrrr!Ps   zArena._choose_dirN)r)rrrsysplatformr#rr!rrrrr>s   cCs(|jdkrtdt|jt|jffS)NrzDArena is unpicklable because forking was enabled when it was created)r ValueError rebuild_arenar rZDupFd)Zarrr reduce_arenaYs r(cCst||Sr)rZdetach)r Zdupfdrrrr'_sr'c@sZeZdZdZejfddZeddZddZ dd Z d d Z d d Z ddZ ddZdS)HeapicCsJt|_t|_||_g|_i|_i|_ i|_ t |_ g|_ g|_dSr)r r _lastpid threadingZLock_lock_size_lengths _len_to_seq_start_to_block_stop_to_blockZset_allocated_blocks_arenas_pending_free_blocks)rr rrrrls  z Heap.__init__cCs|d}|||@S)Nrr)ZnZ alignmentZmaskrrr_roundupysz Heap._roundupc Cst|j|}|t|jkrj|t|j|tj}|jd9_t d|t |}|j ||d|fS|j|}|j|}|}|s|j|=|j|=|\}}}|j||f=|j||f=|S)Niz"allocating a new mmap of length %dr)bisectZ bisect_leftr.Zlenr5maxr-r PAGESIZErZinforr3appendr/popr0r1) rr rlengtharenaseqblockstartstoprrr_mallocs"        z Heap._mallocc Cs|\}}}y|j||f}Wntk r0YnX||\}}y|j||f}Wntk rfYnX||\}}|||f}||}y|j||Wn.tk r|g|j|<t|j|YnX||j||f<||j||f<dSr) r1ZKeyError_absorbr0r/r9r6Zinsortr.) rr>r<r?r@Z prev_blockZ_Z next_blockr;rrr_frees(   z Heap._freecCs^|\}}}|j||f=|j||f=||}|j|}|||sV|j|=|j|||fSr)r0r1r/remover.)rr>r<r?r@r;r=rrrrBs      z Heap._absorbcCsFx@y|j}Wntk r&PYnX|j|||qWdSr)r4r:Z IndexErrorr2rDrCrr>rrr_free_pending_blockss zHeap._free_pending_blockscCsrt|jkr$tdt|j|jds>|j|n0z"| |j || |Wd|j XdS)Nz$My pid ({0:n}) is not last pid {1:n}F)r r r*r&formatr,Zacquirer4r9rFr2rDrCZreleaserErrrfrees   z Heap.freec Cs|dkrtd|tj|kr.td|t|jkrD||j h| | t |d|j }||\}}}||}||kr||||f|||f}|j||SQRXdS)NrSize {0:n} out of rangeSize {0:n} too larger)r&rGr$maxsize OverflowErrorr r r*rr,rFr5r7 _alignmentrArCr2Zadd)rr r<r?r@Znew_stopr>rrrmallocs    z Heap.mallocN)rrrrMr r8rZ staticmethodr5rArCrBrFrHrNrrrrr)hs  r)c@s"eZdZeZddZddZdS)rcCs^|dkrtd|tj|kr.td|tj|}||f|_t j |tjj |fddS)NrrIrJ)Zargs) r&rGr$rKrLr_heaprNrrr"rH)rr r>rrrrs   zBufferWrapper.__init__cCs&|j\\}}}}t|j|||Sr)rZ memoryviewr)rr<r?r@r rrrcreate_memoryview szBufferWrapper.create_memoryviewN)rrrr)rOrrPrrrrrs )r6r r r$rr+ZcontextrrZrZ__all__r%rZobjectrr(r'Zregisterr)rrrrrZ s$