mirror of
https://github.com/lkmio/lkm.git
synced 2025-10-16 04:00:36 +08:00
修复rtmp内存池缓存失败问题
This commit is contained in:
48
stream/queue.go
Normal file
48
stream/queue.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package stream
|
||||
|
||||
import "github.com/yangjiechina/avformat/utils"
|
||||
|
||||
type Queue struct {
|
||||
*ringBuffer
|
||||
}
|
||||
|
||||
func NewQueue(capacity int) *Queue {
|
||||
utils.Assert(capacity > 0)
|
||||
|
||||
return &Queue{ringBuffer: &ringBuffer{
|
||||
data: make([]interface{}, capacity),
|
||||
head: 0,
|
||||
tail: 0,
|
||||
size: 0,
|
||||
}}
|
||||
}
|
||||
|
||||
func (q *Queue) Push(value interface{}) {
|
||||
if q.ringBuffer.IsFull() {
|
||||
newArray := make([]interface{}, q.ringBuffer.Size()*2)
|
||||
head, tail := q.ringBuffer.All()
|
||||
copy(newArray, head)
|
||||
if tail != nil {
|
||||
copy(newArray[len(head):], tail)
|
||||
}
|
||||
|
||||
q.data = newArray
|
||||
q.head = 0
|
||||
q.tail = q.size
|
||||
}
|
||||
|
||||
q.data[q.tail] = value
|
||||
q.tail = (q.tail + 1) % cap(q.data)
|
||||
|
||||
q.size++
|
||||
}
|
||||
|
||||
func (q *Queue) PopBack() interface{} {
|
||||
utils.Assert(q.size > 0)
|
||||
|
||||
value := q.ringBuffer.Tail()
|
||||
q.size--
|
||||
q.tail = utils.MaxInt(0, q.tail-1)
|
||||
|
||||
return value
|
||||
}
|
Reference in New Issue
Block a user