aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/encoding/binary
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2012-02-08 14:09:20 +1100
committerRob Pike <r@golang.org>2012-02-08 14:09:20 +1100
commit52ebadd3569b31ce423d4868ac9aa54a373aa1ad (patch)
tree321aca3f752539902795d92075331419eb0ebe7e /src/pkg/encoding/binary
parentf23a6dba5e7a477b15bb10c5f630df01b5f0ea88 (diff)
downloadgo-52ebadd3569b31ce423d4868ac9aa54a373aa1ad.tar.xz
encoding/binary: hide TotalSize
The function has a bizarre signature: it was the only public function there that exposed the reflect package. Also, its definition is peculiar and hard to explain. It doesn't merit being exported. This is an API change but really, it should never have been exported and it's certain very few programs will depend on it: it's too weird. Fixes #2846. R=golang-dev, gri, bradfitz CC=golang-dev https://golang.org/cl/5639054
Diffstat (limited to 'src/pkg/encoding/binary')
-rw-r--r--src/pkg/encoding/binary/binary.go10
-rw-r--r--src/pkg/encoding/binary/binary_test.go2
2 files changed, 8 insertions, 4 deletions
diff --git a/src/pkg/encoding/binary/binary.go b/src/pkg/encoding/binary/binary.go
index d2f8b1e624..4be83f53bd 100644
--- a/src/pkg/encoding/binary/binary.go
+++ b/src/pkg/encoding/binary/binary.go
@@ -163,7 +163,7 @@ func Read(r io.Reader, order ByteOrder, data interface{}) error {
default:
return errors.New("binary.Read: invalid type " + d.Type().String())
}
- size := TotalSize(v)
+ size := dataSize(v)
if size < 0 {
return errors.New("binary.Read: invalid type " + v.Type().String())
}
@@ -242,7 +242,7 @@ func Write(w io.Writer, order ByteOrder, data interface{}) error {
return err
}
v := reflect.Indirect(reflect.ValueOf(data))
- size := TotalSize(v)
+ size := dataSize(v)
if size < 0 {
return errors.New("binary.Write: invalid type " + v.Type().String())
}
@@ -253,7 +253,11 @@ func Write(w io.Writer, order ByteOrder, data interface{}) error {
return err
}
-func TotalSize(v reflect.Value) int {
+// dataSize returns the number of bytes the actual data represented by v occupies in memory.
+// For compound structures, it sums the sizes of the elements. Thus, for instance, for a slice
+// it returns the length of the slice times the element size and does not count the memory
+// occupied by the header.
+func dataSize(v reflect.Value) int {
if v.Kind() == reflect.Slice {
elem := sizeof(v.Type().Elem())
if elem < 0 {
diff --git a/src/pkg/encoding/binary/binary_test.go b/src/pkg/encoding/binary/binary_test.go
index 3e7057ea22..ff361b7e37 100644
--- a/src/pkg/encoding/binary/binary_test.go
+++ b/src/pkg/encoding/binary/binary_test.go
@@ -187,7 +187,7 @@ func BenchmarkReadStruct(b *testing.B) {
bsr := &byteSliceReader{}
var buf bytes.Buffer
Write(&buf, BigEndian, &s)
- n := TotalSize(reflect.ValueOf(s))
+ n := dataSize(reflect.ValueOf(s))
b.SetBytes(int64(n))
t := s
b.ResetTimer()