B /g^"@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)Ndz pym-%d-%s)tagnamerzCannot find name for new mmap)sizerangeosgetpidnext_randmmap_winapiZ GetLastErrorcloseFileExistsErrornamebuffer_state)selfr irZbufr,/usr/lib64/python3.7/multiprocessing/heap.py__init__"s  zArena.__init__cCst||jS)N)rr)rrrr __getstate__1szArena.__getstate__cCs,|\|_|_|_tjd|j|jd|_dS)Nr )r )r rrrr)rstaterrr __setstate__5szArena.__setstate__N) __name__ __module__ __qualname__tempfileZ_RandomNameSequencerrrrrrrrrsrc@s4eZdZejdkrdgZngZd ddZddZdS) rZlinuxz/dev/shmr cCsx||_||_|dkrbtjdt||d\|_}t|t |tj |jft |j|t |j|j|_ dS)Nr zpym-%d-)prefixdir)r fdr#Zmkstempr r _choose_dirunlinkrFinalizer ftruncaterr)rr r&rrrrrDs  zArena.__init__cCs6x,|jD]"}t|}|j|j|kr|SqWtS)N)_dir_candidatesr statvfsf_bavailf_frsizerZ get_temp_dir)rr dstrrrr'Ps   zArena._choose_dirN)r )r r!r"sysplatformr+rr'rrrrr>s   cCs(|jdkrtdt|jt|jffS)Nr zDArena is unpicklable because forking was enabled when it was created)r& ValueError rebuild_arenar rZDupFd)arrr reduce_arenaYs r6cCst||S)N)rdetach)r Zdupfdrrrr4_sr4c@sZeZdZdZejfddZeddZddZ dd Z d d Z d d Z ddZ ddZdS)HeapcCsJt|_t|_||_g|_i|_i|_ i|_ t |_ g|_ g|_dS)N)r r_lastpid threadingZLock_lock_size_lengths _len_to_seq_start_to_block_stop_to_blockset_allocated_blocks_arenas_pending_free_blocks)rr rrrrls  z Heap.__init__cCs|d}|||@S)Nrr)nZ alignmentmaskrrr_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)Nz"allocating a new mmap of length %dr)bisectZ bisect_leftr>lenrHmaxr=rPAGESIZErinforrDappendr?popr@rA) 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<dS)N) rAKeyError_absorbr@r?rOrJZinsortr>) rrTrRrUrVZ prev_block_Z next_blockrQrrr_frees(   z Heap._freecCs^|\}}}|j||f=|j||f=||}|j|}|||sV|j|=|j|||fS)N)r@rAr?remover>)rrTrRrUrVrQrSrrrrYs      z Heap._absorbcCsFx@y|j}Wntk r&PYnX|j|||qWdS)N)rErP IndexErrorrCr\r[)rrTrrr_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 rr:r3formatr<acquirerErOr^rCr\r[release)rrTrrrfrees   z Heap.freec Cs|dkrtd|tj|kr.td|t|jkrD||j h| | t |d|j }||\}}}||}||kr||||f|||f}|j||SQRXdS)NrzSize {0:n} out of rangezSize {0:n} too larger)r3r_r1maxsize OverflowErrorr rr:rr<r^rHrL _alignmentrWr[rCadd)rr rRrUrVZnew_stoprTrrrmallocs    z Heap.mallocN)r r!r"rerrMr staticmethodrHrWr[rYr^rbrgrrrrr8hs  r8c@s"eZdZeZddZddZdS)rcCs^|dkrtd|tj|kr.td|tj|}||f|_t j |tjj |fddS)NrzSize {0:n} out of rangezSize {0:n} too large)args) r3r_r1rcrdr_heaprgrrr)rb)rr rTrrrrs   zBufferWrapper.__init__cCs&|j\\}}}}t|j|||S)N)r memoryviewr)rrRrUrVr rrrcreate_memoryview szBufferWrapper.create_memoryviewN)r r!r"r8rjrrlrrrrrs )rJrr r1r#r;contextrrr__all__r2robjectrr6r4registerr8rrrrr s$