Commit 0f96f944 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Implement packetcache.GetLast.

parent da97560c
......@@ -135,6 +135,21 @@ func (cache *Cache) Get(seqno uint16, result []byte) uint16 {
return 0
}
func (cache *Cache) GetLast(result []byte) uint16 {
cache.mu.Lock()
defer cache.mu.Unlock()
i := cache.tail - 1
if i >= uint16(len(cache.entries)) {
i = 0
}
return uint16(copy(
result[:cache.entries[i].length],
cache.entries[i].buf[:]),
)
}
func (cache *Cache) GetAt(seqno uint16, index uint16, result []byte) uint16 {
cache.mu.Lock()
defer cache.mu.Unlock()
......
......@@ -33,6 +33,10 @@ func TestCache(t *testing.T) {
if !bytes.Equal(buf[:l], buf1) {
t.Errorf("Couldn't get 13 at %v", i1)
}
l = cache.GetLast(buf)
if !bytes.Equal(buf[:l], buf2) {
t.Errorf("Couldn't get last")
}
l = cache.Get(17, buf)
if !bytes.Equal(buf[:l], buf2) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment