Skip to content

Commit

Permalink
pktque.Buf: bugfix: release pkt.Data
Browse files Browse the repository at this point in the history
  • Loading branch information
nareix committed Aug 26, 2016
1 parent 3937f7e commit 46d5df0
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions av/pktque/buf.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@

package pktque

import (
"github.com/nareix/joy4/av"
)

type Buf struct {
Head, Tail BufPos
pkts []av.Packet
Head, Tail BufPos
pkts []av.Packet
size, maxsize int
count int
}

func NewBuf() *Buf {
return &Buf{
pkts: make([]av.Packet, 64),
maxsize: 1024*512,
pkts: make([]av.Packet, 64),
maxsize: 1024 * 512,
}
}

Expand All @@ -24,10 +24,13 @@ func (self *Buf) SetMaxSize(size int) {
}

func (self *Buf) shrink() {
for self.size > self.maxsize && self.Head-self.Tail > 1 {
pkt := self.pkts[int(self.Head)&(len(self.pkts)-1)]
for self.size > self.maxsize && self.count > 1 {
i := int(self.Head) & (len(self.pkts) - 1)
pkt := self.pkts[i]
self.pkts[i] = av.Packet{}
self.size -= len(pkt.Data)
self.Head++
self.count--
}
}

Expand All @@ -40,11 +43,12 @@ func (self *Buf) grow() {
}

func (self *Buf) Push(pkt av.Packet) {
if int(self.Tail-self.Head) == len(self.pkts) {
if self.count == len(self.pkts) {
self.grow()
}
self.pkts[int(self.Tail)&(len(self.pkts)-1)] = pkt
self.Tail++
self.count++
self.size += len(pkt.Data)
self.shrink()
}
Expand All @@ -60,14 +64,13 @@ func (self *Buf) IsValidPos(pos BufPos) bool {
type BufPos int

func (self BufPos) LT(pos BufPos) bool {
return self - pos < 0
return self-pos < 0
}

func (self BufPos) GE(pos BufPos) bool {
return self - pos >= 0
return self-pos >= 0
}

func (self BufPos) GT(pos BufPos) bool {
return self - pos > 0
return self-pos > 0
}

0 comments on commit 46d5df0

Please sign in to comment.