@@ -542,31 +542,33 @@ _dispatch_alloc_maybe_madvise_page(dispatch_continuation_t c)
542542 }
543543 // They are all unallocated, so we could madvise the page. Try to
544544 // take ownership of them all.
545- int last_locked = 0 ;
546- do {
547- if (!os_atomic_cmpxchg (& page_bitmaps [last_locked ], BITMAP_C (0 ),
545+ for (i = 0 ; i < BITMAPS_PER_PAGE ; i ++ ) {
546+ if (!os_atomic_cmpxchg (& page_bitmaps [i ], BITMAP_C (0 ),
548547 BITMAP_ALL_ONES , relaxed )) {
549548 // We didn't get one; since there is a cont allocated in
550549 // the page, we can't madvise. Give up and unlock all.
551- goto unlock ;
550+ break ;
552551 }
553- } while (++ last_locked < (signed )BITMAPS_PER_PAGE );
552+ }
553+
554+ if (i >= BITMAPS_PER_PAGE ) {
554555#if DISPATCH_DEBUG
555- // fprintf(stderr, "%s: madvised page %p for cont %p (next = %p), "
556- // "[%u+1]=%u bitmaps at %p\n", __func__, page, c, c->do_next,
557- // last_locked-1, BITMAPS_PER_PAGE, &page_bitmaps[0]);
558- // Scribble to expose use-after-free bugs
559- // madvise (syscall) flushes these stores
560- memset (page , DISPATCH_ALLOCATOR_SCRIBBLE , DISPATCH_ALLOCATOR_PAGE_SIZE );
556+ // fprintf(stderr, "%s: madvised page %p for cont %p (next = %p), "
557+ // "[%u+1]=%u bitmaps at %p\n", __func__, page, c, c->do_next,
558+ // last_locked-1, BITMAPS_PER_PAGE, &page_bitmaps[0]);
559+ // Scribble to expose use-after-free bugs
560+ // madvise (syscall) flushes these stores
561+ memset (page , DISPATCH_ALLOCATOR_SCRIBBLE , DISPATCH_ALLOCATOR_PAGE_SIZE );
561562#endif
562- (void )dispatch_assume_zero (madvise (page , DISPATCH_ALLOCATOR_PAGE_SIZE ,
563- MADV_FREE ));
563+ // madvise the page
564+ (void )dispatch_assume_zero (madvise (page , DISPATCH_ALLOCATOR_PAGE_SIZE ,
565+ MADV_FREE ));
566+ }
564567
565- unlock :
566- while (last_locked > 1 ) {
567- page_bitmaps [-- last_locked ] = BITMAP_C (0 );
568+ while (i > 1 ) {
569+ page_bitmaps [-- i ] = BITMAP_C (0 );
568570 }
569- if (last_locked ) {
571+ if (i ) {
570572 os_atomic_store (& page_bitmaps [0 ], BITMAP_C (0 ), relaxed );
571573 }
572574 return ;
0 commit comments