-
Notifications
You must be signed in to change notification settings - Fork 28
/
wrapper.go
166 lines (134 loc) · 3.92 KB
/
wrapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2019 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package clone
import (
"encoding/binary"
"hash/crc64"
"reflect"
"sync"
"unsafe"
)
var (
sizeOfChecksum = unsafe.Sizeof(uint64(0))
crc64Table = crc64.MakeTable(crc64.ECMA)
cachedWrapperTypes sync.Map
)
// Wrap creates a wrapper of v, which must be a pointer.
// If v is not a pointer, Wrap simply returns v and do nothing.
//
// The wrapper is a deep clone of v's value. It holds a shadow copy to v internally.
//
// t := &T{Foo: 123}
// v := Wrap(t).(*T) // v is a clone of t.
// reflect.DeepEqual(t, v) == true // v equals t.
// v.Foo = 456 // v.Foo is changed, but t.Foo doesn't change.
// orig := Unwrap(v) // Use `Unwrap` to discard wrapper and return original value, which is t.
// orig.(*T) == t // orig and t is exactly the same.
// Undo(v) // Use `Undo` to discard any change on v.
// v.Foo == t.Foo // Now, the value of v and t are the same again.
func Wrap(v interface{}) interface{} {
if v == nil {
return v
}
val := reflect.ValueOf(v)
pt := val.Type()
if val.Kind() != reflect.Ptr {
return v
}
t := pt.Elem()
elem := val.Elem()
ptr := unsafe.Pointer(val.Pointer())
cache, ok := cachedWrapperTypes.Load(t)
if !ok {
cache = reflect.StructOf([]reflect.StructField{
{
Name: "T",
Type: t,
Anonymous: true,
},
{
Name: "Checksum",
Type: reflect.TypeOf(uint64(0)),
},
{
Name: "Origin",
Type: pt,
},
})
cachedWrapperTypes.Store(t, cache)
}
wrapperType := cache.(reflect.Type)
pw := defaultAllocator.New(wrapperType)
wrapperPtr := unsafe.Pointer(pw.Pointer())
wrapper := pw.Elem()
// Equivalent code: wrapper.T = Clone(v)
field := wrapper.Field(0)
field.Set(heapCloneState.clone(elem))
// Equivalent code: wrapper.Checksum = makeChecksum(v)
checksumPtr := unsafe.Pointer((uintptr(wrapperPtr) + t.Size()))
*(*uint64)(checksumPtr) = makeChecksum(t, uintptr(wrapperPtr), uintptr(ptr))
// Equivalent code: wrapper.Origin = v
originPtr := unsafe.Pointer((uintptr(wrapperPtr) + t.Size() + sizeOfChecksum))
*(*uintptr)(originPtr) = uintptr(ptr)
return field.Addr().Interface()
}
func validateChecksum(t reflect.Type, ptr unsafe.Pointer) bool {
pw := uintptr(ptr)
orig := uintptr(getOrigin(t, ptr))
checksum := *(*uint64)(unsafe.Pointer(uintptr(ptr) + t.Size()))
expected := makeChecksum(t, pw, orig)
return checksum == expected
}
func makeChecksum(t reflect.Type, pw uintptr, orig uintptr) uint64 {
var data [binary.MaxVarintLen64 * 2]byte
binary.PutUvarint(data[:binary.MaxVarintLen64], uint64(pw))
binary.PutUvarint(data[binary.MaxVarintLen64:], uint64(orig))
return crc64.Checksum(data[:], crc64Table)
}
func getOrigin(t reflect.Type, ptr unsafe.Pointer) unsafe.Pointer {
return *(*unsafe.Pointer)(unsafe.Pointer(uintptr(ptr) + t.Size() + sizeOfChecksum))
}
// Unwrap returns v's original value if v is a wrapped value.
// Otherwise, simply returns v itself.
func Unwrap(v interface{}) interface{} {
if v == nil {
return v
}
val := reflect.ValueOf(v)
if !isWrapped(val) {
return v
}
origVal := origin(val)
return origVal.Interface()
}
func origin(val reflect.Value) reflect.Value {
pt := val.Type()
t := pt.Elem()
ptr := unsafe.Pointer(val.Pointer())
orig := getOrigin(t, ptr)
origVal := reflect.NewAt(t, orig)
return origVal
}
// Undo discards any change made in wrapped value.
// If v is not a wrapped value, nothing happens.
func Undo(v interface{}) {
if v == nil {
return
}
val := reflect.ValueOf(v)
if !isWrapped(val) {
return
}
origVal := origin(val)
elem := val.Elem()
elem.Set(heapCloneState.clone(origVal.Elem()))
}
func isWrapped(val reflect.Value) bool {
pt := val.Type()
if pt.Kind() != reflect.Ptr {
return false
}
t := pt.Elem()
ptr := unsafe.Pointer(val.Pointer())
return validateChecksum(t, ptr)
}