|
| 1 | +// Copyright 2024 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 unique |
| 6 | + |
| 7 | +import ( |
| 8 | + "internal/abi" |
| 9 | + "unsafe" |
| 10 | +) |
| 11 | + |
| 12 | +// clone makes a copy of value, and may update string values found in value |
| 13 | +// with a cloned version of those strings. The purpose of explicitly cloning |
| 14 | +// strings is to avoid accidentally giving a large string a long lifetime. |
| 15 | +// |
| 16 | +// Note that this will clone strings in structs and arrays found in value, |
| 17 | +// and will clone value if it itself is a string. It will not, however, clone |
| 18 | +// strings if value is of interface or slice type (that is, found via an |
| 19 | +// indirection). |
| 20 | +func clone[T comparable](value T, seq *cloneSeq) T { |
| 21 | + for _, offset := range seq.stringOffsets { |
| 22 | + ps := (*string)(unsafe.Pointer(uintptr(unsafe.Pointer(&value)) + offset)) |
| 23 | + *ps = cloneString(*ps) |
| 24 | + } |
| 25 | + return value |
| 26 | +} |
| 27 | + |
| 28 | +// singleStringClone describes how to clone a single string. |
| 29 | +var singleStringClone = cloneSeq{stringOffsets: []uintptr{0}} |
| 30 | + |
| 31 | +// cloneSeq describes how to clone a value of a particular type. |
| 32 | +type cloneSeq struct { |
| 33 | + stringOffsets []uintptr |
| 34 | +} |
| 35 | + |
| 36 | +// makeCloneSeq creates a cloneSeq for a type. |
| 37 | +func makeCloneSeq(typ *abi.Type) cloneSeq { |
| 38 | + if typ == nil { |
| 39 | + return cloneSeq{} |
| 40 | + } |
| 41 | + if typ.Kind() == abi.String { |
| 42 | + return singleStringClone |
| 43 | + } |
| 44 | + var seq cloneSeq |
| 45 | + switch typ.Kind() { |
| 46 | + case abi.Struct: |
| 47 | + buildStructCloneSeq(typ, &seq, 0) |
| 48 | + case abi.Array: |
| 49 | + buildArrayCloneSeq(typ, &seq, 0) |
| 50 | + } |
| 51 | + return seq |
| 52 | +} |
| 53 | + |
| 54 | +// buildStructCloneSeq populates a cloneSeq for an abi.Type that has Kind abi.Struct. |
| 55 | +func buildStructCloneSeq(typ *abi.Type, seq *cloneSeq, baseOffset uintptr) { |
| 56 | + styp := typ.StructType() |
| 57 | + for i := range styp.Fields { |
| 58 | + f := &styp.Fields[i] |
| 59 | + switch f.Typ.Kind() { |
| 60 | + case abi.String: |
| 61 | + seq.stringOffsets = append(seq.stringOffsets, baseOffset+f.Offset) |
| 62 | + case abi.Struct: |
| 63 | + buildStructCloneSeq(f.Typ, seq, baseOffset+f.Offset) |
| 64 | + case abi.Array: |
| 65 | + buildArrayCloneSeq(f.Typ, seq, baseOffset+f.Offset) |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// buildArrayCloneSeq populates a cloneSeq for an abi.Type that has Kind abi.Array. |
| 71 | +func buildArrayCloneSeq(typ *abi.Type, seq *cloneSeq, baseOffset uintptr) { |
| 72 | + atyp := typ.ArrayType() |
| 73 | + etyp := atyp.Elem |
| 74 | + offset := baseOffset |
| 75 | + for range atyp.Len { |
| 76 | + switch etyp.Kind() { |
| 77 | + case abi.String: |
| 78 | + seq.stringOffsets = append(seq.stringOffsets, offset) |
| 79 | + case abi.Struct: |
| 80 | + buildStructCloneSeq(etyp, seq, offset) |
| 81 | + case abi.Array: |
| 82 | + buildArrayCloneSeq(etyp, seq, offset) |
| 83 | + } |
| 84 | + offset += etyp.Size() |
| 85 | + align := uintptr(etyp.FieldAlign()) |
| 86 | + offset = (offset + align - 1) &^ (align - 1) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// cloneString is a copy of strings.Clone, because we can't depend on the strings |
| 91 | +// package here. Several packages that might make use of unique, like net, explicitly |
| 92 | +// forbid depending on unicode, which strings depends on. |
| 93 | +func cloneString(s string) string { |
| 94 | + if len(s) == 0 { |
| 95 | + return "" |
| 96 | + } |
| 97 | + b := make([]byte, len(s)) |
| 98 | + copy(b, s) |
| 99 | + return unsafe.String(&b[0], len(b)) |
| 100 | +} |
0 commit comments