diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index d690cc0..72b1f3a 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -4,7 +4,7 @@ "Deps": [ { "ImportPath": "github.com/chihaya/bencode", - "Rev": "e60878f635e1a61315c413492e133dd39769b1d1" + "Rev": "3c485a8d166ff6a79baba90c2c2da01c8348e930" }, { "ImportPath": "github.com/golang/glog", @@ -16,7 +16,7 @@ }, { "ImportPath": "github.com/pushrax/bufferpool", - "Rev": "c8962db3ad1e65fe9a83e7298dcd213938cf02d5" + "Rev": "7d6e1653dee10a165d1f357f3a57bc8031e9621b" }, { "ImportPath": "github.com/pushrax/faststats", diff --git a/Godeps/_workspace/src/github.com/chihaya/bencode/encoder.go b/Godeps/_workspace/src/github.com/chihaya/bencode/encoder.go index 8b33fd9..da6c84b 100644 --- a/Godeps/_workspace/src/github.com/chihaya/bencode/encoder.go +++ b/Godeps/_workspace/src/github.com/chihaya/bencode/encoder.go @@ -62,6 +62,12 @@ func marshal(w io.Writer, data interface{}) error { case uint: marshalUint(w, uint64(v)) + case int16: + marshalInt(w, int64(v)) + + case uint16: + marshalUint(w, uint64(v)) + case int64: marshalInt(w, v) diff --git a/Godeps/_workspace/src/github.com/chihaya/bencode/encoder_test.go b/Godeps/_workspace/src/github.com/chihaya/bencode/encoder_test.go index 6b3f432..0cf2aae 100644 --- a/Godeps/_workspace/src/github.com/chihaya/bencode/encoder_test.go +++ b/Godeps/_workspace/src/github.com/chihaya/bencode/encoder_test.go @@ -19,6 +19,8 @@ var marshalTests = []struct { {uint(43), "i43e"}, {int64(44), "i44e"}, {uint64(45), "i45e"}, + {int16(44), "i44e"}, + {uint16(45), "i45e"}, {"example", "7:example"}, {[]byte("example"), "7:example"}, diff --git a/Godeps/_workspace/src/github.com/pushrax/bufferpool/bufferpool.go b/Godeps/_workspace/src/github.com/pushrax/bufferpool/bufferpool.go index b83919d..e3e71fc 100644 --- a/Godeps/_workspace/src/github.com/pushrax/bufferpool/bufferpool.go +++ b/Godeps/_workspace/src/github.com/pushrax/bufferpool/bufferpool.go @@ -29,7 +29,7 @@ func New(poolSize, bufferSize int) *BufferPool { // Take is used to obtain a new zeroed buffer. This will allocate a new buffer // if the pool was empty. func (pool *BufferPool) Take() *bytes.Buffer { - return bytes.NewBuffer(pool.TakeSlice()) + return bytes.NewBuffer(pool.TakeSlice()[:0]) } // TakeSlice is used to obtain a new slice. This will allocate a new slice @@ -38,7 +38,7 @@ func (pool *BufferPool) TakeSlice() (slice []byte) { select { case slice = <-pool.pool: default: - slice = make([]byte, 0, pool.bufferSize) + slice = make([]byte, pool.bufferSize) } return } @@ -46,12 +46,13 @@ func (pool *BufferPool) TakeSlice() (slice []byte) { // Give is used to attempt to return a buffer to the pool. It may not // be added to the pool if it was already full. func (pool *BufferPool) Give(buf *bytes.Buffer) error { - if buf.Len() != pool.bufferSize { + buf.Reset() + slice := buf.Bytes() + + if cap(slice) < pool.bufferSize { return errors.New("Gave an incorrectly sized buffer to the pool.") } - buf.Reset() - slice := buf.Bytes() return pool.GiveSlice(slice[:buf.Len()]) } diff --git a/Godeps/_workspace/src/github.com/pushrax/bufferpool/bufferpool_test.go b/Godeps/_workspace/src/github.com/pushrax/bufferpool/bufferpool_test.go index 809c806..5727f08 100644 --- a/Godeps/_workspace/src/github.com/pushrax/bufferpool/bufferpool_test.go +++ b/Godeps/_workspace/src/github.com/pushrax/bufferpool/bufferpool_test.go @@ -12,6 +12,21 @@ import ( "github.com/pushrax/bufferpool" ) +func ExampleNew() { + bp := bufferpool.New(10, 255) + + dogBuffer := bp.Take() + dogBuffer.WriteString("Dog!") + bp.Give(dogBuffer) + + catBuffer := bp.Take() // dogBuffer is reused and reset. + catBuffer.WriteString("Cat!") + + fmt.Println(catBuffer) + // Output: + // Cat! +} + func TestTakeFromEmpty(t *testing.T) { bp := bufferpool.New(1, 1) poolBuf := bp.Take() @@ -37,17 +52,16 @@ func TestTakeFromFilled(t *testing.T) { } } -func ExampleNew() { - bp := bufferpool.New(10, 255) +func TestSliceSemantics(t *testing.T) { + bp := bufferpool.New(1, 8) - dogBuffer := bp.Take() - dogBuffer.WriteString("Dog!") - bp.Give(dogBuffer) + buf := bp.Take() + buf.WriteString("abc") + bp.Give(buf) - catBuffer := bp.Take() // dogBuffer is reused and reset. - catBuffer.WriteString("Cat!") + buf2 := bp.TakeSlice() - fmt.Println(catBuffer) - // Output: - // Cat! + if !bytes.Equal(buf2[:3], []byte("abc")) { + t.Fatalf("Buffer from filled bufferpool was recycled incorrectly.") + } }