U ÙAT^j-c@sÒddlZddlmZddlZddlZddlZddlZddlZddlm Z m Z ddl m Z dgZ ejdkr‚ddlZGdd „d eƒZn,Gd d „d eƒZd d „Zd d„Ze  ee¡Gdd„deƒZGdd„deƒZdS)éN)Ú defaultdicté)Ú reductionÚassert_spawning)ÚutilÚ BufferWrapperZwin32c@s0eZdZdZe ¡Zdd„Zdd„Zdd„Z dS) ÚArenazL A shared memory area backed by anonymous memory (Windows). cCsx||_tdƒD]B}dt ¡t|jƒf}tjd||d}t ¡dkrHqZ|  ¡qt dƒ‚||_ ||_ |j|j f|_ dS)Nidz pym-%d-%séÿÿÿÿ©ZtagnamerzCannot find name for new mmap)ÚsizeZrangeÚosÚgetpidZnextÚ_randÚmmapÚ_winapiZ GetLastErrorÚcloseZFileExistsErrorÚnameÚbufferÚ_state)Úselfr ÚirZbuf©rú,/usr/lib64/python3.8/multiprocessing/heap.pyÚ__init__&s   úArena.__init__cCst|ƒ|jS©N)rr)rrrrÚ __getstate__5szArena.__getstate__cCs,|\|_|_|_tjd|j|jd|_dS)Nrr )r rrrr)rZstaterrrÚ __setstate__9szArena.__setstate__N) Ú__name__Ú __module__Ú __qualname__Ú__doc__ÚtempfileZ_RandomNameSequencer rrrrrrrrs rc@s8eZdZdZejdkrdgZngZd dd„Zdd„Zd S) rzJ A shared memory area backed by a temporary file (POSIX). Zlinuxz/dev/shmrcCsx||_||_|dkrbtjdt ¡| |¡d\|_}t |¡t  |tj |jf¡t  |j|¡t   |j|j¡|_ dS)Nrzpym-%d-)ZprefixZdir)r Úfdr!Zmkstempr r Ú _choose_dirZunlinkrÚFinalizerZ ftruncaterr)rr r"rrrrrMs þ  rcCs6|jD]&}t |¡}|j|j|kr|Sqt ¡Sr)Ú_dir_candidatesr ZstatvfsZf_bavailZf_frsizerZ get_temp_dir)rr ZdZstrrrr#[s    zArena._choose_dirN)r) rrrr ÚsysÚplatformr%rr#rrrrrCs   cCs(|jdkrtdƒ‚t|jt |j¡ffS)NrzDArena is unpicklable because forking was enabled when it was created)r"Ú ValueErrorÚ rebuild_arenar rZDupFd)ZarrrÚ reduce_arenads r*cCst|| ¡ƒSr)rZdetach)r Zdupfdrrrr)jsr)c@szeZdZdZdZdZejfdd„Ze dd„ƒZ dd„Z d d „Z d d „Z d d„Zdd„Zdd„Zdd„Zdd„Zdd„ZdS)ÚHeapii@cCsXt ¡|_t ¡|_||_g|_i|_i|_ i|_ t t ƒ|_ g|_g|_d|_d|_dS©Nr)r r Ú_lastpidÚ threadingZLockÚ_lockÚ_sizeÚ_lengthsÚ _len_to_seqÚ_start_to_blockÚ_stop_to_blockrZsetÚ_allocated_blocksÚ_arenasÚ_pending_free_blocksÚ _n_mallocsÚ_n_frees)rr rrrr{s   z Heap.__init__cCs|d}|||@S)Nrr)ZnZ alignmentZmaskrrrÚ_roundup˜sz Heap._roundupcCsZ| t|j|ƒtj¡}|j|jkr0|jd9_t d|¡t|ƒ}|j   |¡|d|fS)Niz"allocating a new mmap of length %dr) r:Úmaxr0rÚPAGESIZEÚ_DOUBLE_ARENA_SIZE_UNTILrZinforr6Úappend)rr ÚlengthÚarenarrrÚ _new_arenažs   zHeap._new_arenacCsz|j}||jkrdS|j |¡}|j|df=|j||f=|j |¡|j|}| |d|f¡|sv|j|=|j  |¡dSr,) r Ú_DISCARD_FREE_SPACE_LARGER_THANr5Úpopr3r4r6Úremover2r1)rr@r?ÚblocksÚseqrrrÚ_discard_arenaªs      zHeap._discard_arenac Cs|t |j|¡}|t|jƒkr&| |¡S|j|}|j|}| ¡}|sV|j|=|j|=|\}}}|j||f=|j||f=|Sr) ÚbisectZ bisect_leftr1ZlenrAr2rCr3r4) rr rr?rFÚblockr@ÚstartÚstoprrrÚ_malloc¼s      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) r4ZKeyErrorÚ_absorbr3r2r>rHZinsortr1) rrIr@rJrKZ prev_blockZ_Z next_blockr?rrrÚ_add_free_blockÍs(   zHeap._add_free_blockcCs^|\}}}|j||f=|j||f=||}|j|}| |¡|sV|j|=|j |¡||fSr)r3r4r2rDr1)rrIr@rJrKr?rFrrrrMës      z Heap._absorbcCs4|\}}}|j|}| ||f¡|s0| |¡dSr)r5rDrG)rrIr@rJrKrErrrÚ_remove_allocated_blockús   zHeap._remove_allocated_blockcCsBz|j ¡}Wntk r&Yq>YnX| |¡| |¡qdSr)r7rCZ IndexErrorrNrO©rrIrrrÚ_free_pending_blockss   zHeap._free_pending_blockscCs~t ¡|jkr$td t ¡|j¡ƒ‚|j d¡s>|j |¡nZreleaser9rQrNrOrPrrrÚfree s ÿÿ  z Heap.freec CsÚ|dkrtd |¡ƒ‚tj|kr.td |¡ƒ‚t ¡|jkrD| ¡|j †|j d7_ |  ¡|  t |dƒ|j¡}| |¡\}}}||}||kr¦| |||f¡|j| ||f¡|||fW5QR£SQRXdS)NrúSize {0:n} out of rangeúSize {0:n} too larger)r(rRr&ÚmaxsizeÚ OverflowErrorr r r-rr/r8rQr:r;Ú _alignmentrLrNr5Zadd)rr r@rJrKZ real_stoprrrÚmalloc(s  z Heap.mallocN)rrrrXrBr=rr<rZ staticmethodr:rArGrLrNrMrOrQrSrYrrrrr+ss   r+c@s"eZdZeƒZdd„Zdd„ZdS)rcCs^|dkrtd |¡ƒ‚tj|kr.td |¡ƒ‚tj |¡}||f|_t j |tjj |fddS)NrrTrU)Zargs) r(rRr&rVrWrÚ_heaprYrrr$rS)rr rIrrrrFs   zBufferWrapper.__init__cCs&|j\\}}}}t|jƒ|||…Sr)rZ memoryviewr)rr@rJrKr rrrÚcreate_memoryviewOszBufferWrapper.create_memoryviewN)rrrr+rZrr[rrrrrBs )rHZ collectionsrrr r&r!r.ZcontextrrZrZ__all__r'rZobjectrr*r)Zregisterr+rrrrrZ s&   $! P