3 \ @sddlZddlZddlZddlZddlZddlZddlmZmZddl m Z dgZ ej dkrvddl Z GdddeZn,Gd ddeZd d Zd d ZejeeGdddeZGdddeZdS)N) reductionassert_spawning)util BufferWrapperZwin32c@s,eZdZejZddZddZddZdS)ArenacCsz||_xTtdD]@}dtjt|jf}tjd||d}tjdkrHP|j qWt d||_ ||_ |j|j f|_ dS)Ndz pym-%d-%sr)tagnamerzCannot find name for new mmap)sizerangeosgetpidnext_randmmap_winapiZ GetLastErrorcloseFileExistsErrornamebuffer_state)selfr irZbufr/usr/lib64/python3.6/heap.py__init__"s  zArena.__init__cCst||jS)N)rr)rrrr __getstate__1szArena.__getstate__cCs,|\|_|_|_tjd|j|jd|_dS)Nr)r r )r rrrr)rstaterrr __setstate__5szArena.__setstate__N) __name__ __module__ __qualname__tempfileZ_RandomNameSequencerrrrrrrrrsrc@seZdZdddZdS)rrc Cs||_||_|d krtjdtjtjd\|_}tj|tj |tj |jft |jddd^}d }||krd|}xt ||D]}|j |qW~|j d|||j|kstWdQRXtj|j|j|_dS) Nrzpym-%d-)prefixdirwbF)closefdir i)r fdr#Zmkstempr rrZ get_temp_dirunlinkFinalizeropenr writetellAssertionErrorrr)rr r)rfZbsZzeros_rrrr@s" zArena.__init__Nr )r )r r!r"rrrrrr>scCs(|jdkrtdt|jtj|jffS)NrzDArena is unpicklable because forking was enabled when it was createdr )r) ValueError rebuild_arenar rZDupFd)arrr reduce_arenaSs r5cCst||jS)N)rdetach)r Zdupfdrrrr3Ysr3c@sZeZdZdZejfddZeddZddZ dd Z d d Z d d Z ddZ ddZdS)HeapcCsJtj|_tj|_||_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 rrrrfs  z Heap.__init__cCs|d}|||@S)Nrr)nZ alignmentmaskrrr_roundupssz Heap._roundupc Cstj|j|}|t|jkrj|jt|j|tj}|jd9_t j d|t |}|j j ||d|fS|j|}|j|}|j}|s|j|=|j|=|\}}}|j||f=|j||f=|S)Nz"allocating a new mmap of length %dr)bisectZ bisect_leftr=lenrGmaxr<rPAGESIZErinforrCappendr>popr?r@) rr rlengtharenaseqblockstartstoprrr_mallocys"        z Heap._mallocc Cs|\}}}y|j||f}Wntk r0YnX|j|\}}y|j||f}Wntk rfYnX|j|\}}|||f}||}y|j|j|Wn.tk r|g|j|<tj|j|YnX||j||f<||j||f<dS)N) r@KeyError_absorbr?r>rNrIZinsortr=) rrSrQrTrUZ prev_blockr1Z next_blockrPrrr_frees(   z Heap._freecCs^|\}}}|j||f=|j||f=||}|j|}|j||sV|j|=|jj|||fS)N)r?r@r>remover=)rrSrQrTrUrPrRrrrrXs      z Heap._absorbc CsFx@y|jj}Wntk r&PYnX|jj||j|qWdS)N)rDrO IndexErrorrBrZrY)rrSrrr_free_pending_blockss zHeap._free_pending_blocksc Cs`tj|jkst|jjds,|jj|n0z"|j|j j ||j |Wd|jj XdS)NF) r rr9r/r;acquirerDrNr\rBrZrYrelease)rrSrrrfrees   z Heap.freec Csd|kotjknsttj|jkr4|j|jh|j|j t |d|j }|j |\}}}||}||kr|j |||f|||f}|jj||SQRXdS)Nrr)sysmaxsizer/r rr9rr;r\rGrK _alignmentrVrYrBadd)rr rQrTrUZnew_stoprSrrrmallocs  z Heap.mallocN)r r!r"rbrrLr staticmethodrGrVrYrXr\r_rdrrrrr7bs  r7c@s"eZdZeZddZddZdS)rcCsNd|kotjknsttjj|}||f|_tj|tjj |fddS)Nr)args) r`rar/r_heaprdrrr+r_)rr rSrrrrs  zBufferWrapper.__init__cCs&|j\\}}}}t|j|||S)N)r memoryviewr)rrQrTrUr rrrcreate_memoryviewszBufferWrapper.create_memoryviewN)r r!r"r7rgrrirrrrrs)rIrr r`r#r:contextrrr__all__platformrobjectrr5r3registerr7rrrrr s$