Source file src/internal/runtime/gc/scan/filter.go

     1  // Copyright 2025 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package scan
     6  
     7  import "unsafe"
     8  
     9  // FilterNil packs non-nil (non-zero) values in bufp together
    10  // at the beginning of bufp, returning the length of the
    11  // packed buffer. It treats bufp as an array of size n.
    12  func FilterNil(bufp *uintptr, n int32) int32 {
    13  	buf := unsafe.Slice(bufp, int(n))
    14  	lo := 0
    15  	hi := len(buf) - 1
    16  	for lo < hi {
    17  		for lo < hi && buf[hi] == 0 {
    18  			hi--
    19  		}
    20  		for lo < hi && buf[lo] != 0 {
    21  			lo++
    22  		}
    23  		if lo >= hi {
    24  			break
    25  		}
    26  		buf[lo] = buf[hi]
    27  		hi--
    28  	}
    29  	if hi >= 0 && buf[hi] == 0 {
    30  		hi--
    31  	}
    32  	return int32(hi) + 1
    33  }
    34  

View as plain text