-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathringbuffer_compat.go
More file actions
50 lines (44 loc) · 1.1 KB
/
ringbuffer_compat.go
File metadata and controls
50 lines (44 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package zlog
// Entry for compatibility with old tests
type Entry = LogEntry
// OldRingBuffer wraps the new generic RingBuffer for compatibility
type OldRingBuffer struct {
rb *RingBuffer[Entry]
pool *Pool[*Entry]
}
// NewCompatRingBuffer creates a ring buffer (compatibility wrapper)
func NewCompatRingBuffer(size int) *OldRingBuffer {
// Keep old behavior - panic if not power of 2
if size&(size-1) != 0 {
panic("ring buffer size must be power of 2")
}
pool := NewPool(func() *Entry {
return &Entry{}
})
return &OldRingBuffer{
rb: NewRingBuffer[Entry](size, pool),
pool: pool,
}
}
// Put wrapper for old interface
func (orb *OldRingBuffer) Put(data []byte) bool {
entry := orb.pool.Get()
if cap(entry.data) < len(data) {
entry.data = make([]byte, len(data))
} else {
entry.data = entry.data[:len(data)]
}
copy(entry.data, data)
return orb.rb.Put(entry)
}
// Get wrapper for old interface
func (orb *OldRingBuffer) Get() ([]byte, bool) {
entry, ok := orb.rb.Get()
if !ok {
return nil, false
}
data := entry.data
entry.data = entry.data[:0]
orb.pool.Put(entry)
return data, true
}