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 // 13 // TODO(mknyszek): Add a faster SIMD-based implementation. 14 func FilterNil(bufp *uintptr, n int32) int32 { 15 buf := unsafe.Slice(bufp, int(n)) 16 lo := 0 17 hi := len(buf) - 1 18 for lo < hi { 19 for lo < hi && buf[hi] == 0 { 20 hi-- 21 } 22 for lo < hi && buf[lo] != 0 { 23 lo++ 24 } 25 if lo >= hi { 26 break 27 } 28 buf[lo] = buf[hi] 29 hi-- 30 } 31 if hi >= 0 && buf[hi] == 0 { 32 hi-- 33 } 34 return int32(hi) + 1 35 } 36