-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unsafeslice: add OfString and AsString to convert between strings and…
… slices These functions, unlike most other variations of the same logic I have seen in the wild, make a serious attempt to detect and report erroneous mutations, especially when the race detector is available. (Go programs can and do assume that strings are immutable, and not even the unsafeslice package should undermine that invariant.)
- Loading branch information
Bryan C. Mills
committed
Apr 25, 2020
1 parent
f44e66b
commit 8c4dfe9
Showing
7 changed files
with
326 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2020 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build !go1.14 | ||
|
||
package unsafeslice | ||
|
||
import ( | ||
"hash" | ||
"hash/fnv" | ||
) | ||
|
||
func newHash() hash.Hash64 { | ||
return fnv.New64a() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2020 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// Package eventually enables the use of finalizers whose registration can be | ||
// blocked until an arbitrary point in the program. | ||
package eventually | ||
|
||
import ( | ||
"runtime" | ||
) | ||
|
||
var unblocked = make(chan struct{}) | ||
|
||
func init() { | ||
close(unblocked) | ||
} | ||
|
||
// Block delays finalizer registration until unblock is called. | ||
func Block() (unblock func()) { | ||
c := make(chan struct{}) | ||
unblocked = c | ||
return func() { close(c) } | ||
} | ||
|
||
// SetFinalizer sets a finalizer f for pointer p. | ||
// | ||
// If registration is currently blocked, SetFinalizer registers it in a | ||
// background goroutine that first waits for registration to be unblocked. | ||
func SetFinalizer(p, f interface{}) { | ||
select { | ||
case <-unblocked: | ||
runtime.SetFinalizer(p, f) | ||
default: | ||
go func() { | ||
<-unblocked | ||
runtime.SetFinalizer(p, f) | ||
}() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2020 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build go1.14 | ||
|
||
package unsafeslice | ||
|
||
import ( | ||
"hash/maphash" | ||
"sync" | ||
) | ||
|
||
var seed struct { | ||
once sync.Once | ||
seed maphash.Seed | ||
} | ||
|
||
func newHash() *maphash.Hash { | ||
seed.once.Do(func() { | ||
seed.seed = maphash.MakeSeed() | ||
}) | ||
|
||
h := new(maphash.Hash) | ||
h.SetSeed(seed.seed) | ||
return h | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright 2020 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build !race | ||
|
||
package unsafeslice | ||
|
||
const raceEnabled = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright 2020 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build race | ||
|
||
package unsafeslice | ||
|
||
const raceEnabled = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters