diff options
author | Cherry Zhang <cherryyz@google.com> | 2020-10-28 09:12:20 -0400 |
---|---|---|
committer | Cherry Zhang <cherryyz@google.com> | 2020-10-28 09:12:20 -0400 |
commit | a16e30d162c1c7408db7821e7b9513cefa09c6ca (patch) | |
tree | af752ba9ba44c547df39bb0af9bff79f610ba9d5 /src/hash/crc32/crc32.go | |
parent | 91e4d2d57bc341dd82c98247117114c851380aef (diff) | |
parent | cf6cfba4d5358404dd890f6025e573a4b2156543 (diff) | |
download | go-a16e30d162c1c7408db7821e7b9513cefa09c6ca.tar.gz go-a16e30d162c1c7408db7821e7b9513cefa09c6ca.zip |
[dev.link] all: merge branch 'master' into dev.linkdev.link
Clean merge.
Change-Id: Ia7b2808bc649790198d34c226a61d9e569084dc5
Diffstat (limited to 'src/hash/crc32/crc32.go')
-rw-r--r-- | src/hash/crc32/crc32.go | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/src/hash/crc32/crc32.go b/src/hash/crc32/crc32.go index 908b84adcb..f330fdb77a 100644 --- a/src/hash/crc32/crc32.go +++ b/src/hash/crc32/crc32.go @@ -16,6 +16,7 @@ import ( "errors" "hash" "sync" + "sync/atomic" ) // The size of a CRC-32 checksum in bytes. @@ -78,6 +79,7 @@ var castagnoliTable8 *slicing8Table var castagnoliArchImpl bool var updateCastagnoli func(crc uint32, p []byte) uint32 var castagnoliOnce sync.Once +var haveCastagnoli uint32 func castagnoliInit() { castagnoliTable = simpleMakeTable(Castagnoli) @@ -93,6 +95,8 @@ func castagnoliInit() { return slicingUpdate(crc, castagnoliTable8, p) } } + + atomic.StoreUint32(&haveCastagnoli, 1) } // IEEETable is the table for the IEEE polynomial. @@ -208,10 +212,10 @@ func readUint32(b []byte) uint32 { // Update returns the result of adding the bytes in p to the crc. func Update(crc uint32, tab *Table, p []byte) uint32 { - switch tab { - case castagnoliTable: + switch { + case atomic.LoadUint32(&haveCastagnoli) != 0 && tab == castagnoliTable: return updateCastagnoli(crc, p) - case IEEETable: + case tab == IEEETable: // Unfortunately, because IEEETable is exported, IEEE may be used without a // call to MakeTable. We have to make sure it gets initialized in that case. ieeeOnce.Do(ieeeInit) @@ -222,10 +226,10 @@ func Update(crc uint32, tab *Table, p []byte) uint32 { } func (d *digest) Write(p []byte) (n int, err error) { - switch d.tab { - case castagnoliTable: + switch { + case atomic.LoadUint32(&haveCastagnoli) != 0 && d.tab == castagnoliTable: d.crc = updateCastagnoli(d.crc, p) - case IEEETable: + case d.tab == IEEETable: // We only create digest objects through New() which takes care of // initialization in this case. d.crc = updateIEEE(d.crc, p) |