pointers that reference addresses of the internal circular buffer should not escape

This commit is contained in:
James DeFelice 2015-04-04 12:39:09 +00:00
parent 318252a107
commit b173a1e44c

View File

@ -23,7 +23,7 @@ import (
// A circular buffer for ContainerStats. // A circular buffer for ContainerStats.
type StatsBuffer struct { type StatsBuffer struct {
buffer []info.ContainerStats buffer []*info.ContainerStats
size int size int
index int index int
} }
@ -31,7 +31,7 @@ type StatsBuffer struct {
// Returns a new thread-compatible StatsBuffer. // Returns a new thread-compatible StatsBuffer.
func NewStatsBuffer(size int) *StatsBuffer { func NewStatsBuffer(size int) *StatsBuffer {
return &StatsBuffer{ return &StatsBuffer{
buffer: make([]info.ContainerStats, size), buffer: make([]*info.ContainerStats, size),
size: 0, size: 0,
index: size - 1, index: size - 1,
} }
@ -43,7 +43,8 @@ func (self *StatsBuffer) Add(item *info.ContainerStats) {
self.size++ self.size++
} }
self.index = (self.index + 1) % len(self.buffer) self.index = (self.index + 1) % len(self.buffer)
self.buffer[self.index] = *item copied := *item
self.buffer[self.index] = &copied
} }
// Returns up to maxResult elements in the specified time period (inclusive). // Returns up to maxResult elements in the specified time period (inclusive).
@ -131,7 +132,7 @@ func (self *StatsBuffer) FirstN(n int) []*info.ContainerStats {
res := make([]*info.ContainerStats, n) res := make([]*info.ContainerStats, n)
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
index := (start + i) % len(self.buffer) index := (start + i) % len(self.buffer)
res[i] = &self.buffer[index] res[i] = self.buffer[index]
} }
return res return res
} }
@ -142,7 +143,7 @@ func (self *StatsBuffer) Get(index int) *info.ContainerStats {
if calculatedIndex < 0 { if calculatedIndex < 0 {
calculatedIndex += len(self.buffer) calculatedIndex += len(self.buffer)
} }
return &self.buffer[calculatedIndex] return self.buffer[calculatedIndex]
} }
func (self *StatsBuffer) Size() int { func (self *StatsBuffer) Size() int {