From 021063521dd460c1e53ae5d530d50ff78aded84f Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Tue, 2 Aug 2022 22:12:16 +0200 Subject: [PATCH] use a generic linked list --- conn_id_manager.go | 15 +- frame_sorter.go | 22 +- frame_sorter_test.go | 107 +++++---- internal/ackhandler/gen.go | 3 - internal/ackhandler/packet_linkedlist.go | 217 ------------------ .../ackhandler/received_packet_history.go | 18 +- .../received_packet_history_test.go | 62 ++--- internal/ackhandler/sent_packet_history.go | 19 +- internal/utils/byteinterval_linkedlist.go | 217 ------------------ internal/utils/gen.go | 5 - internal/utils/linkedlist/README.md | 9 +- internal/utils/linkedlist/linkedlist.go | 125 +++++----- internal/utils/new_connection_id.go | 12 - internal/utils/newconnectionid_linkedlist.go | 217 ------------------ internal/utils/packet_interval.go | 9 - internal/utils/packetinterval_linkedlist.go | 217 ------------------ internal/utils/streamframe_interval.go | 9 - 17 files changed, 203 insertions(+), 1080 deletions(-) delete mode 100644 internal/ackhandler/gen.go delete mode 100644 internal/ackhandler/packet_linkedlist.go delete mode 100644 internal/utils/byteinterval_linkedlist.go delete mode 100644 internal/utils/gen.go delete mode 100644 internal/utils/new_connection_id.go delete mode 100644 internal/utils/newconnectionid_linkedlist.go delete mode 100644 internal/utils/packet_interval.go delete mode 100644 internal/utils/packetinterval_linkedlist.go delete mode 100644 internal/utils/streamframe_interval.go diff --git a/conn_id_manager.go b/conn_id_manager.go index e1b025a985f..1d8e5a0c481 100644 --- a/conn_id_manager.go +++ b/conn_id_manager.go @@ -6,11 +6,18 @@ import ( "github.com/lucas-clemente/quic-go/internal/protocol" "github.com/lucas-clemente/quic-go/internal/qerr" "github.com/lucas-clemente/quic-go/internal/utils" + list "github.com/lucas-clemente/quic-go/internal/utils/linkedlist" "github.com/lucas-clemente/quic-go/internal/wire" ) +type newConnID struct { + SequenceNumber uint64 + ConnectionID protocol.ConnectionID + StatelessResetToken protocol.StatelessResetToken +} + type connIDManager struct { - queue utils.NewConnectionIDList + queue list.List[newConnID] handshakeComplete bool activeSequenceNumber uint64 @@ -71,7 +78,7 @@ func (h *connIDManager) add(f *wire.NewConnectionIDFrame) error { // Retire elements in the queue. // Doesn't retire the active connection ID. if f.RetirePriorTo > h.highestRetired { - var next *utils.NewConnectionIDElement + var next *list.Element[newConnID] for el := h.queue.Front(); el != nil; el = next { if el.Value.SequenceNumber >= f.RetirePriorTo { break @@ -104,7 +111,7 @@ func (h *connIDManager) add(f *wire.NewConnectionIDFrame) error { func (h *connIDManager) addConnectionID(seq uint64, connID protocol.ConnectionID, resetToken protocol.StatelessResetToken) error { // insert a new element at the end if h.queue.Len() == 0 || h.queue.Back().Value.SequenceNumber < seq { - h.queue.PushBack(utils.NewConnectionID{ + h.queue.PushBack(newConnID{ SequenceNumber: seq, ConnectionID: connID, StatelessResetToken: resetToken, @@ -123,7 +130,7 @@ func (h *connIDManager) addConnectionID(seq uint64, connID protocol.ConnectionID break } if el.Value.SequenceNumber > seq { - h.queue.InsertBefore(utils.NewConnectionID{ + h.queue.InsertBefore(newConnID{ SequenceNumber: seq, ConnectionID: connID, StatelessResetToken: resetToken, diff --git a/frame_sorter.go b/frame_sorter.go index aeafa7d4209..0573ade938c 100644 --- a/frame_sorter.go +++ b/frame_sorter.go @@ -4,9 +4,15 @@ import ( "errors" "github.com/lucas-clemente/quic-go/internal/protocol" - "github.com/lucas-clemente/quic-go/internal/utils" + list "github.com/lucas-clemente/quic-go/internal/utils/linkedlist" ) +// byteInterval is an interval from one ByteCount to the other +type byteInterval struct { + Start protocol.ByteCount + End protocol.ByteCount +} + type frameSorterEntry struct { Data []byte DoneCb func() @@ -15,17 +21,17 @@ type frameSorterEntry struct { type frameSorter struct { queue map[protocol.ByteCount]frameSorterEntry readPos protocol.ByteCount - gaps *utils.ByteIntervalList + gaps *list.List[byteInterval] } var errDuplicateStreamData = errors.New("duplicate stream data") func newFrameSorter() *frameSorter { s := frameSorter{ - gaps: utils.NewByteIntervalList(), + gaps: list.New[byteInterval](), queue: make(map[protocol.ByteCount]frameSorterEntry), } - s.gaps.PushFront(utils.ByteInterval{Start: 0, End: protocol.MaxByteCount}) + s.gaps.PushFront(byteInterval{Start: 0, End: protocol.MaxByteCount}) return &s } @@ -118,7 +124,7 @@ func (s *frameSorter) push(data []byte, offset protocol.ByteCount, doneCb func() if !startGapEqualsEndGap { s.deleteConsecutive(startGapEnd) - var nextGap *utils.ByteIntervalElement + var nextGap *list.Element[byteInterval] for gap := startGapNext; gap.Value.End < endGapStart; gap = nextGap { nextGap = gap.Next() s.deleteConsecutive(gap.Value.End) @@ -140,7 +146,7 @@ func (s *frameSorter) push(data []byte, offset protocol.ByteCount, doneCb func() } else { if startGapEqualsEndGap && adjustedStartGapEnd { // The frame split the existing gap into two. - s.gaps.InsertAfter(utils.ByteInterval{Start: end, End: startGapEnd}, startGap) + s.gaps.InsertAfter(byteInterval{Start: end, End: startGapEnd}, startGap) } else if !startGapEqualsEndGap { endGap.Value.Start = end } @@ -164,7 +170,7 @@ func (s *frameSorter) push(data []byte, offset protocol.ByteCount, doneCb func() return nil } -func (s *frameSorter) findStartGap(offset protocol.ByteCount) (*utils.ByteIntervalElement, bool) { +func (s *frameSorter) findStartGap(offset protocol.ByteCount) (*list.Element[byteInterval], bool) { for gap := s.gaps.Front(); gap != nil; gap = gap.Next() { if offset >= gap.Value.Start && offset <= gap.Value.End { return gap, true @@ -176,7 +182,7 @@ func (s *frameSorter) findStartGap(offset protocol.ByteCount) (*utils.ByteInterv panic("no gap found") } -func (s *frameSorter) findEndGap(startGap *utils.ByteIntervalElement, offset protocol.ByteCount) (*utils.ByteIntervalElement, bool) { +func (s *frameSorter) findEndGap(startGap *list.Element[byteInterval], offset protocol.ByteCount) (*list.Element[byteInterval], bool) { for gap := startGap; gap != nil; gap = gap.Next() { if offset >= gap.Value.Start && offset < gap.Value.End { return gap, true diff --git a/frame_sorter_test.go b/frame_sorter_test.go index f0100d68361..520a91c79c1 100644 --- a/frame_sorter_test.go +++ b/frame_sorter_test.go @@ -8,7 +8,6 @@ import ( "time" "github.com/lucas-clemente/quic-go/internal/protocol" - "github.com/lucas-clemente/quic-go/internal/utils" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) @@ -16,7 +15,7 @@ import ( var _ = Describe("frame sorter", func() { var s *frameSorter - checkGaps := func(expectedGaps []utils.ByteInterval) { + checkGaps := func(expectedGaps []byteInterval) { if s.gaps.Len() != len(expectedGaps) { fmt.Println("Gaps:") for gap := s.gaps.Front(); gap != nil; gap = gap.Next() { @@ -158,7 +157,7 @@ var _ = Describe("frame sorter", func() { 3: f1, 6: f2, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 11, End: protocol.MaxByteCount}, }) @@ -181,7 +180,7 @@ var _ = Describe("frame sorter", func() { 3: f1, 10: f2, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 6, End: 10}, {Start: 15, End: protocol.MaxByteCount}, @@ -209,7 +208,7 @@ var _ = Describe("frame sorter", func() { 6: f2, 10: f3, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 15, End: protocol.MaxByteCount}, }) @@ -233,7 +232,7 @@ var _ = Describe("frame sorter", func() { 3: f1, 7: f2[2:], }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 9, End: protocol.MaxByteCount}, }) @@ -253,7 +252,7 @@ var _ = Describe("frame sorter", func() { 3 * mult: f1, 7 * mult: f2[2*mult:], }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3 * mult}, {Start: 9 * mult, End: protocol.MaxByteCount}, }) @@ -276,7 +275,7 @@ var _ = Describe("frame sorter", func() { 0: f1, 4: f2[1:], }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 7, End: protocol.MaxByteCount}, }) checkCallbackNotCalled(t1) @@ -295,7 +294,7 @@ var _ = Describe("frame sorter", func() { 0: f1, 4 * mult: f2[mult:], }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 7 * mult, End: protocol.MaxByteCount}, }) checkCallbackNotCalled(t1) @@ -317,7 +316,7 @@ var _ = Describe("frame sorter", func() { 3: f2[:2], 5: f1, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 9, End: protocol.MaxByteCount}, }) @@ -337,7 +336,7 @@ var _ = Describe("frame sorter", func() { 3 * mult: f2[:2*mult], 5 * mult: f1, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3 * mult}, {Start: 9 * mult, End: protocol.MaxByteCount}, }) @@ -364,7 +363,7 @@ var _ = Describe("frame sorter", func() { 6: f2, 10: f3, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 8, End: 10}, {Start: 15, End: protocol.MaxByteCount}, @@ -393,7 +392,7 @@ var _ = Describe("frame sorter", func() { 10: f2, 15: f3, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 6, End: 10}, {Start: 12, End: 15}, @@ -423,7 +422,7 @@ var _ = Describe("frame sorter", func() { 8: f2, 10: f3, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 6, End: 8}, {Start: 15, End: protocol.MaxByteCount}, @@ -452,7 +451,7 @@ var _ = Describe("frame sorter", func() { 6: f3[1:5], 10: f2, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 15, End: protocol.MaxByteCount}, }) @@ -477,7 +476,7 @@ var _ = Describe("frame sorter", func() { 6 * mult: f3[mult : 5*mult], 10 * mult: f2, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3 * mult}, {Start: 15 * mult, End: protocol.MaxByteCount}, }) @@ -505,7 +504,7 @@ var _ = Describe("frame sorter", func() { 7: f3[2:], 10: f2, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 15, End: protocol.MaxByteCount}, }) @@ -534,7 +533,7 @@ var _ = Describe("frame sorter", func() { 7 * mult: f3[2*mult:], 10 * mult: f2, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3 * mult}, {Start: 15 * mult, End: protocol.MaxByteCount}, }) @@ -557,7 +556,7 @@ var _ = Describe("frame sorter", func() { checkQueue(map[protocol.ByteCount][]byte{ 3: f2, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 10, End: protocol.MaxByteCount}, }) @@ -582,7 +581,7 @@ var _ = Describe("frame sorter", func() { checkQueue(map[protocol.ByteCount][]byte{ 3: f3, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 10, End: protocol.MaxByteCount}, }) @@ -609,7 +608,7 @@ var _ = Describe("frame sorter", func() { 3: f3[:3], 6: f2, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 10, End: protocol.MaxByteCount}, }) @@ -633,7 +632,7 @@ var _ = Describe("frame sorter", func() { 3 * mult: f3[:3*mult], 6 * mult: f2, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3 * mult}, {Start: 10 * mult, End: protocol.MaxByteCount}, }) @@ -659,7 +658,7 @@ var _ = Describe("frame sorter", func() { checkQueue(map[protocol.ByteCount][]byte{ 3: f3, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 9, End: protocol.MaxByteCount}, }) @@ -682,7 +681,7 @@ var _ = Describe("frame sorter", func() { checkQueue(map[protocol.ByteCount][]byte{ 5: f1, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 5}, {Start: 9, End: protocol.MaxByteCount}, }) @@ -708,7 +707,7 @@ var _ = Describe("frame sorter", func() { 3: f1, 6: f2, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 9, End: protocol.MaxByteCount}, }) @@ -731,7 +730,7 @@ var _ = Describe("frame sorter", func() { checkQueue(map[protocol.ByteCount][]byte{ 5: f1, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 5}, {Start: 9, End: protocol.MaxByteCount}, }) @@ -756,7 +755,7 @@ var _ = Describe("frame sorter", func() { checkQueue(map[protocol.ByteCount][]byte{ 5: f1, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 5}, {Start: 10, End: protocol.MaxByteCount}, }) @@ -778,7 +777,7 @@ var _ = Describe("frame sorter", func() { checkQueue(map[protocol.ByteCount][]byte{ 0: f1, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 10, End: protocol.MaxByteCount}, }) checkCallbackNotCalled(t1) @@ -796,7 +795,7 @@ var _ = Describe("frame sorter", func() { cb2, t2 := getCallback() Expect(s.Push(f1, 5, cb1)).To(Succeed()) // 5 - 10 Expect(s.Push(f2, 7, cb2)).To(Succeed()) // 7 - 10 - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 5}, {Start: 10, End: protocol.MaxByteCount}, }) @@ -821,7 +820,7 @@ var _ = Describe("frame sorter", func() { checkQueue(map[protocol.ByteCount][]byte{ 3: f2, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 8, End: protocol.MaxByteCount}, }) @@ -846,7 +845,7 @@ var _ = Describe("frame sorter", func() { checkQueue(map[protocol.ByteCount][]byte{ 3: f3, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 11, End: protocol.MaxByteCount}, }) @@ -873,7 +872,7 @@ var _ = Describe("frame sorter", func() { 3: f1, 6: f3, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 12, End: protocol.MaxByteCount}, }) @@ -904,7 +903,7 @@ var _ = Describe("frame sorter", func() { 6: f4, 15: f3, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 18, End: protocol.MaxByteCount}, }) @@ -928,7 +927,7 @@ var _ = Describe("frame sorter", func() { checkQueue(map[protocol.ByteCount][]byte{ 3: f2, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 13, End: protocol.MaxByteCount}, }) @@ -955,7 +954,7 @@ var _ = Describe("frame sorter", func() { 3: f1, 6: f2, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 2}, {Start: 10, End: protocol.MaxByteCount}, }) @@ -980,7 +979,7 @@ var _ = Describe("frame sorter", func() { 3 * mult: f1, 6 * mult: f2, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 2 * mult}, {Start: 10 * mult, End: protocol.MaxByteCount}, }) @@ -1008,7 +1007,7 @@ var _ = Describe("frame sorter", func() { 3: f1, 6: f2, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 2}, {Start: 10, End: protocol.MaxByteCount}, }) @@ -1033,7 +1032,7 @@ var _ = Describe("frame sorter", func() { 3 * mult: f1, 6 * mult: f2, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 2 * mult}, {Start: 10 * mult, End: protocol.MaxByteCount}, }) @@ -1060,7 +1059,7 @@ var _ = Describe("frame sorter", func() { 3: f1, 6: f3, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 11, End: protocol.MaxByteCount}, }) @@ -1088,7 +1087,7 @@ var _ = Describe("frame sorter", func() { 6: f2, 9: f3[4:], }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 11, End: protocol.MaxByteCount}, }) @@ -1113,7 +1112,7 @@ var _ = Describe("frame sorter", func() { 6 * mult: f2, 9 * mult: f3[4*mult:], }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3 * mult}, {Start: 11 * mult, End: protocol.MaxByteCount}, }) @@ -1140,7 +1139,7 @@ var _ = Describe("frame sorter", func() { 3: f1, 6: f3[1:], }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 15, End: protocol.MaxByteCount}, }) @@ -1164,7 +1163,7 @@ var _ = Describe("frame sorter", func() { 3 * mult: f1, 6 * mult: f3[mult:], }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3 * mult}, {Start: 15 * mult, End: protocol.MaxByteCount}, }) @@ -1190,7 +1189,7 @@ var _ = Describe("frame sorter", func() { checkQueue(map[protocol.ByteCount][]byte{ 3: f3, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 12, End: protocol.MaxByteCount}, }) @@ -1220,7 +1219,7 @@ var _ = Describe("frame sorter", func() { 3: f1, 6: f4[1:], }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 17, End: protocol.MaxByteCount}, }) @@ -1248,7 +1247,7 @@ var _ = Describe("frame sorter", func() { 3 * mult: f1, 6 * mult: f4[mult:], }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3 * mult}, {Start: 17 * mult, End: protocol.MaxByteCount}, }) @@ -1280,7 +1279,7 @@ var _ = Describe("frame sorter", func() { 10: f3, 20: f4, }) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 5}, {Start: 25, End: protocol.MaxByteCount}, }) @@ -1304,7 +1303,7 @@ var _ = Describe("frame sorter", func() { Expect(s.Push(f1, 3, cb1)).To(Succeed()) // 3 - 6 Expect(s.Push(f2, 9, cb2)).To(Succeed()) // 9 - 13 Expect(s.Push(f3, 3, cb3)).To(Succeed()) // 3 - 11 - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3}, {Start: 13, End: protocol.MaxByteCount}, }) @@ -1328,7 +1327,7 @@ var _ = Describe("frame sorter", func() { Expect(s.Push(f1, 3*mult, cb1)).To(Succeed()) // 3 - 6 Expect(s.Push(f2, 9*mult, cb2)).To(Succeed()) // 9 - 13 Expect(s.Push(f3, 3*mult, cb3)).To(Succeed()) // 3 - 11 - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 0, End: 3 * mult}, {Start: 13 * mult, End: protocol.MaxByteCount}, }) @@ -1350,7 +1349,7 @@ var _ = Describe("frame sorter", func() { // now receive the duplicate Expect(s.Push([]byte("foobar"), 0, nil)).To(Succeed()) Expect(s.queue).To(BeEmpty()) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 6, End: protocol.MaxByteCount}, }) }) @@ -1366,7 +1365,7 @@ var _ = Describe("frame sorter", func() { Expect(offset).To(Equal(protocol.ByteCount(3))) Expect(data).To(Equal([]byte("bar"))) Expect(s.queue).To(BeEmpty()) - checkGaps([]utils.ByteInterval{ + checkGaps([]byteInterval{ {Start: 6, End: protocol.MaxByteCount}, }) }) @@ -1475,7 +1474,7 @@ var _ = Describe("frame sorter", func() { for _, f := range frames { push(f.data, f.offset) } - checkGaps([]utils.ByteInterval{{Start: num * dataLen, End: protocol.MaxByteCount}}) + checkGaps([]byteInterval{{Start: num * dataLen, End: protocol.MaxByteCount}}) Expect(getData()).To(Equal(data)) Expect(s.queue).To(BeEmpty()) @@ -1492,7 +1491,7 @@ var _ = Describe("frame sorter", func() { push(df.data, df.offset) } } - checkGaps([]utils.ByteInterval{{Start: num * dataLen, End: protocol.MaxByteCount}}) + checkGaps([]byteInterval{{Start: num * dataLen, End: protocol.MaxByteCount}}) Expect(getData()).To(Equal(data)) Expect(s.queue).To(BeEmpty()) @@ -1515,7 +1514,7 @@ var _ = Describe("frame sorter", func() { push(b, offset) } } - checkGaps([]utils.ByteInterval{{Start: num * dataLen, End: protocol.MaxByteCount}}) + checkGaps([]byteInterval{{Start: num * dataLen, End: protocol.MaxByteCount}}) Expect(getData()).To(Equal(data)) Expect(s.queue).To(BeEmpty()) diff --git a/internal/ackhandler/gen.go b/internal/ackhandler/gen.go deleted file mode 100644 index 32235f81ab1..00000000000 --- a/internal/ackhandler/gen.go +++ /dev/null @@ -1,3 +0,0 @@ -package ackhandler - -//go:generate genny -pkg ackhandler -in ../utils/linkedlist/linkedlist.go -out packet_linkedlist.go gen Item=Packet diff --git a/internal/ackhandler/packet_linkedlist.go b/internal/ackhandler/packet_linkedlist.go deleted file mode 100644 index bb74f4ef933..00000000000 --- a/internal/ackhandler/packet_linkedlist.go +++ /dev/null @@ -1,217 +0,0 @@ -// This file was automatically generated by genny. -// Any changes will be lost if this file is regenerated. -// see https://github.com/cheekybits/genny - -package ackhandler - -// Linked list implementation from the Go standard library. - -// PacketElement is an element of a linked list. -type PacketElement struct { - // Next and previous pointers in the doubly-linked list of elements. - // To simplify the implementation, internally a list l is implemented - // as a ring, such that &l.root is both the next element of the last - // list element (l.Back()) and the previous element of the first list - // element (l.Front()). - next, prev *PacketElement - - // The list to which this element belongs. - list *PacketList - - // The value stored with this element. - Value Packet -} - -// Next returns the next list element or nil. -func (e *PacketElement) Next() *PacketElement { - if p := e.next; e.list != nil && p != &e.list.root { - return p - } - return nil -} - -// Prev returns the previous list element or nil. -func (e *PacketElement) Prev() *PacketElement { - if p := e.prev; e.list != nil && p != &e.list.root { - return p - } - return nil -} - -// PacketList is a linked list of Packets. -type PacketList struct { - root PacketElement // sentinel list element, only &root, root.prev, and root.next are used - len int // current list length excluding (this) sentinel element -} - -// Init initializes or clears list l. -func (l *PacketList) Init() *PacketList { - l.root.next = &l.root - l.root.prev = &l.root - l.len = 0 - return l -} - -// NewPacketList returns an initialized list. -func NewPacketList() *PacketList { return new(PacketList).Init() } - -// Len returns the number of elements of list l. -// The complexity is O(1). -func (l *PacketList) Len() int { return l.len } - -// Front returns the first element of list l or nil if the list is empty. -func (l *PacketList) Front() *PacketElement { - if l.len == 0 { - return nil - } - return l.root.next -} - -// Back returns the last element of list l or nil if the list is empty. -func (l *PacketList) Back() *PacketElement { - if l.len == 0 { - return nil - } - return l.root.prev -} - -// lazyInit lazily initializes a zero List value. -func (l *PacketList) lazyInit() { - if l.root.next == nil { - l.Init() - } -} - -// insert inserts e after at, increments l.len, and returns e. -func (l *PacketList) insert(e, at *PacketElement) *PacketElement { - n := at.next - at.next = e - e.prev = at - e.next = n - n.prev = e - e.list = l - l.len++ - return e -} - -// insertValue is a convenience wrapper for insert(&Element{Value: v}, at). -func (l *PacketList) insertValue(v Packet, at *PacketElement) *PacketElement { - return l.insert(&PacketElement{Value: v}, at) -} - -// remove removes e from its list, decrements l.len, and returns e. -func (l *PacketList) remove(e *PacketElement) *PacketElement { - e.prev.next = e.next - e.next.prev = e.prev - e.next = nil // avoid memory leaks - e.prev = nil // avoid memory leaks - e.list = nil - l.len-- - return e -} - -// Remove removes e from l if e is an element of list l. -// It returns the element value e.Value. -// The element must not be nil. -func (l *PacketList) Remove(e *PacketElement) Packet { - if e.list == l { - // if e.list == l, l must have been initialized when e was inserted - // in l or l == nil (e is a zero Element) and l.remove will crash - l.remove(e) - } - return e.Value -} - -// PushFront inserts a new element e with value v at the front of list l and returns e. -func (l *PacketList) PushFront(v Packet) *PacketElement { - l.lazyInit() - return l.insertValue(v, &l.root) -} - -// PushBack inserts a new element e with value v at the back of list l and returns e. -func (l *PacketList) PushBack(v Packet) *PacketElement { - l.lazyInit() - return l.insertValue(v, l.root.prev) -} - -// InsertBefore inserts a new element e with value v immediately before mark and returns e. -// If mark is not an element of l, the list is not modified. -// The mark must not be nil. -func (l *PacketList) InsertBefore(v Packet, mark *PacketElement) *PacketElement { - if mark.list != l { - return nil - } - // see comment in List.Remove about initialization of l - return l.insertValue(v, mark.prev) -} - -// InsertAfter inserts a new element e with value v immediately after mark and returns e. -// If mark is not an element of l, the list is not modified. -// The mark must not be nil. -func (l *PacketList) InsertAfter(v Packet, mark *PacketElement) *PacketElement { - if mark.list != l { - return nil - } - // see comment in List.Remove about initialization of l - return l.insertValue(v, mark) -} - -// MoveToFront moves element e to the front of list l. -// If e is not an element of l, the list is not modified. -// The element must not be nil. -func (l *PacketList) MoveToFront(e *PacketElement) { - if e.list != l || l.root.next == e { - return - } - // see comment in List.Remove about initialization of l - l.insert(l.remove(e), &l.root) -} - -// MoveToBack moves element e to the back of list l. -// If e is not an element of l, the list is not modified. -// The element must not be nil. -func (l *PacketList) MoveToBack(e *PacketElement) { - if e.list != l || l.root.prev == e { - return - } - // see comment in List.Remove about initialization of l - l.insert(l.remove(e), l.root.prev) -} - -// MoveBefore moves element e to its new position before mark. -// If e or mark is not an element of l, or e == mark, the list is not modified. -// The element and mark must not be nil. -func (l *PacketList) MoveBefore(e, mark *PacketElement) { - if e.list != l || e == mark || mark.list != l { - return - } - l.insert(l.remove(e), mark.prev) -} - -// MoveAfter moves element e to its new position after mark. -// If e or mark is not an element of l, or e == mark, the list is not modified. -// The element and mark must not be nil. -func (l *PacketList) MoveAfter(e, mark *PacketElement) { - if e.list != l || e == mark || mark.list != l { - return - } - l.insert(l.remove(e), mark) -} - -// PushBackList inserts a copy of an other list at the back of list l. -// The lists l and other may be the same. They must not be nil. -func (l *PacketList) PushBackList(other *PacketList) { - l.lazyInit() - for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() { - l.insertValue(e.Value, l.root.prev) - } -} - -// PushFrontList inserts a copy of an other list at the front of list l. -// The lists l and other may be the same. They must not be nil. -func (l *PacketList) PushFrontList(other *PacketList) { - l.lazyInit() - for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() { - l.insertValue(e.Value, &l.root) - } -} diff --git a/internal/ackhandler/received_packet_history.go b/internal/ackhandler/received_packet_history.go index 5a0391ef2c0..ca0b475b23d 100644 --- a/internal/ackhandler/received_packet_history.go +++ b/internal/ackhandler/received_packet_history.go @@ -2,22 +2,28 @@ package ackhandler import ( "github.com/lucas-clemente/quic-go/internal/protocol" - "github.com/lucas-clemente/quic-go/internal/utils" + list "github.com/lucas-clemente/quic-go/internal/utils/linkedlist" "github.com/lucas-clemente/quic-go/internal/wire" ) +// interval is an interval from one PacketNumber to the other +type interval struct { + Start protocol.PacketNumber + End protocol.PacketNumber +} + // The receivedPacketHistory stores if a packet number has already been received. // It generates ACK ranges which can be used to assemble an ACK frame. // It does not store packet contents. type receivedPacketHistory struct { - ranges *utils.PacketIntervalList + ranges *list.List[interval] deletedBelow protocol.PacketNumber } func newReceivedPacketHistory() *receivedPacketHistory { return &receivedPacketHistory{ - ranges: utils.NewPacketIntervalList(), + ranges: list.New[interval](), } } @@ -34,7 +40,7 @@ func (h *receivedPacketHistory) ReceivedPacket(p protocol.PacketNumber) bool /* func (h *receivedPacketHistory) addToRanges(p protocol.PacketNumber) bool /* is a new packet (and not a duplicate / delayed packet) */ { if h.ranges.Len() == 0 { - h.ranges.PushBack(utils.PacketInterval{Start: p, End: p}) + h.ranges.PushBack(interval{Start: p, End: p}) return true } @@ -61,13 +67,13 @@ func (h *receivedPacketHistory) addToRanges(p protocol.PacketNumber) bool /* is // create a new range at the end if p > el.Value.End { - h.ranges.InsertAfter(utils.PacketInterval{Start: p, End: p}, el) + h.ranges.InsertAfter(interval{Start: p, End: p}, el) return true } } // create a new range at the beginning - h.ranges.InsertBefore(utils.PacketInterval{Start: p, End: p}, h.ranges.Front()) + h.ranges.InsertBefore(interval{Start: p, End: p}, h.ranges.Front()) return true } diff --git a/internal/ackhandler/received_packet_history_test.go b/internal/ackhandler/received_packet_history_test.go index bc9e1a207c1..c7640d9557a 100644 --- a/internal/ackhandler/received_packet_history_test.go +++ b/internal/ackhandler/received_packet_history_test.go @@ -6,8 +6,8 @@ import ( "sort" "github.com/lucas-clemente/quic-go/internal/protocol" - "github.com/lucas-clemente/quic-go/internal/utils" "github.com/lucas-clemente/quic-go/internal/wire" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) @@ -23,14 +23,14 @@ var _ = Describe("receivedPacketHistory", func() { It("adds the first packet", func() { Expect(hist.ReceivedPacket(4)).To(BeTrue()) Expect(hist.ranges.Len()).To(Equal(1)) - Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 4})) + Expect(hist.ranges.Front().Value).To(Equal(interval{Start: 4, End: 4})) }) It("doesn't care about duplicate packets", func() { Expect(hist.ReceivedPacket(4)).To(BeTrue()) Expect(hist.ReceivedPacket(4)).To(BeFalse()) Expect(hist.ranges.Len()).To(Equal(1)) - Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 4})) + Expect(hist.ranges.Front().Value).To(Equal(interval{Start: 4, End: 4})) }) It("adds a few consecutive packets", func() { @@ -38,7 +38,7 @@ var _ = Describe("receivedPacketHistory", func() { Expect(hist.ReceivedPacket(5)).To(BeTrue()) Expect(hist.ReceivedPacket(6)).To(BeTrue()) Expect(hist.ranges.Len()).To(Equal(1)) - Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 6})) + Expect(hist.ranges.Front().Value).To(Equal(interval{Start: 4, End: 6})) }) It("doesn't care about a duplicate packet contained in an existing range", func() { @@ -47,22 +47,22 @@ var _ = Describe("receivedPacketHistory", func() { Expect(hist.ReceivedPacket(6)).To(BeTrue()) Expect(hist.ReceivedPacket(5)).To(BeFalse()) Expect(hist.ranges.Len()).To(Equal(1)) - Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 6})) + Expect(hist.ranges.Front().Value).To(Equal(interval{Start: 4, End: 6})) }) It("extends a range at the front", func() { Expect(hist.ReceivedPacket(4)).To(BeTrue()) Expect(hist.ReceivedPacket(3)).To(BeTrue()) Expect(hist.ranges.Len()).To(Equal(1)) - Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 3, End: 4})) + Expect(hist.ranges.Front().Value).To(Equal(interval{Start: 3, End: 4})) }) It("creates a new range when a packet is lost", func() { Expect(hist.ReceivedPacket(4)).To(BeTrue()) Expect(hist.ReceivedPacket(6)).To(BeTrue()) Expect(hist.ranges.Len()).To(Equal(2)) - Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 4})) - Expect(hist.ranges.Back().Value).To(Equal(utils.PacketInterval{Start: 6, End: 6})) + Expect(hist.ranges.Front().Value).To(Equal(interval{Start: 4, End: 4})) + Expect(hist.ranges.Back().Value).To(Equal(interval{Start: 6, End: 6})) }) It("creates a new range in between two ranges", func() { @@ -71,17 +71,17 @@ var _ = Describe("receivedPacketHistory", func() { Expect(hist.ranges.Len()).To(Equal(2)) Expect(hist.ReceivedPacket(7)).To(BeTrue()) Expect(hist.ranges.Len()).To(Equal(3)) - Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 4})) - Expect(hist.ranges.Front().Next().Value).To(Equal(utils.PacketInterval{Start: 7, End: 7})) - Expect(hist.ranges.Back().Value).To(Equal(utils.PacketInterval{Start: 10, End: 10})) + Expect(hist.ranges.Front().Value).To(Equal(interval{Start: 4, End: 4})) + Expect(hist.ranges.Front().Next().Value).To(Equal(interval{Start: 7, End: 7})) + Expect(hist.ranges.Back().Value).To(Equal(interval{Start: 10, End: 10})) }) It("creates a new range before an existing range for a belated packet", func() { Expect(hist.ReceivedPacket(6)).To(BeTrue()) Expect(hist.ReceivedPacket(4)).To(BeTrue()) Expect(hist.ranges.Len()).To(Equal(2)) - Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 4})) - Expect(hist.ranges.Back().Value).To(Equal(utils.PacketInterval{Start: 6, End: 6})) + Expect(hist.ranges.Front().Value).To(Equal(interval{Start: 4, End: 4})) + Expect(hist.ranges.Back().Value).To(Equal(interval{Start: 6, End: 6})) }) It("extends a previous range at the end", func() { @@ -89,8 +89,8 @@ var _ = Describe("receivedPacketHistory", func() { Expect(hist.ReceivedPacket(7)).To(BeTrue()) Expect(hist.ReceivedPacket(5)).To(BeTrue()) Expect(hist.ranges.Len()).To(Equal(2)) - Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 5})) - Expect(hist.ranges.Back().Value).To(Equal(utils.PacketInterval{Start: 7, End: 7})) + Expect(hist.ranges.Front().Value).To(Equal(interval{Start: 4, End: 5})) + Expect(hist.ranges.Back().Value).To(Equal(interval{Start: 7, End: 7})) }) It("extends a range at the front", func() { @@ -98,8 +98,8 @@ var _ = Describe("receivedPacketHistory", func() { Expect(hist.ReceivedPacket(7)).To(BeTrue()) Expect(hist.ReceivedPacket(6)).To(BeTrue()) Expect(hist.ranges.Len()).To(Equal(2)) - Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 4})) - Expect(hist.ranges.Back().Value).To(Equal(utils.PacketInterval{Start: 6, End: 7})) + Expect(hist.ranges.Front().Value).To(Equal(interval{Start: 4, End: 4})) + Expect(hist.ranges.Back().Value).To(Equal(interval{Start: 6, End: 7})) }) It("closes a range", func() { @@ -108,7 +108,7 @@ var _ = Describe("receivedPacketHistory", func() { Expect(hist.ranges.Len()).To(Equal(2)) Expect(hist.ReceivedPacket(5)).To(BeTrue()) Expect(hist.ranges.Len()).To(Equal(1)) - Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 6})) + Expect(hist.ranges.Front().Value).To(Equal(interval{Start: 4, End: 6})) }) It("closes a range in the middle", func() { @@ -119,9 +119,9 @@ var _ = Describe("receivedPacketHistory", func() { Expect(hist.ranges.Len()).To(Equal(4)) Expect(hist.ReceivedPacket(5)).To(BeTrue()) Expect(hist.ranges.Len()).To(Equal(3)) - Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 1, End: 1})) - Expect(hist.ranges.Front().Next().Value).To(Equal(utils.PacketInterval{Start: 4, End: 6})) - Expect(hist.ranges.Back().Value).To(Equal(utils.PacketInterval{Start: 10, End: 10})) + Expect(hist.ranges.Front().Value).To(Equal(interval{Start: 1, End: 1})) + Expect(hist.ranges.Front().Next().Value).To(Equal(interval{Start: 4, End: 6})) + Expect(hist.ranges.Back().Value).To(Equal(interval{Start: 10, End: 10})) }) }) @@ -137,7 +137,7 @@ var _ = Describe("receivedPacketHistory", func() { Expect(hist.ReceivedPacket(10)).To(BeTrue()) hist.DeleteBelow(6) Expect(hist.ranges.Len()).To(Equal(1)) - Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 10, End: 10})) + Expect(hist.ranges.Front().Value).To(Equal(interval{Start: 10, End: 10})) }) It("deletes multiple ranges", func() { @@ -146,7 +146,7 @@ var _ = Describe("receivedPacketHistory", func() { Expect(hist.ReceivedPacket(10)).To(BeTrue()) hist.DeleteBelow(8) Expect(hist.ranges.Len()).To(Equal(1)) - Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 10, End: 10})) + Expect(hist.ranges.Front().Value).To(Equal(interval{Start: 10, End: 10})) }) It("adjusts a range, if packets are delete from an existing range", func() { @@ -157,7 +157,7 @@ var _ = Describe("receivedPacketHistory", func() { Expect(hist.ReceivedPacket(7)).To(BeTrue()) hist.DeleteBelow(5) Expect(hist.ranges.Len()).To(Equal(1)) - Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 5, End: 7})) + Expect(hist.ranges.Front().Value).To(Equal(interval{Start: 5, End: 7})) }) It("adjusts a range, if only one packet remains in the range", func() { @@ -166,15 +166,15 @@ var _ = Describe("receivedPacketHistory", func() { Expect(hist.ReceivedPacket(10)).To(BeTrue()) hist.DeleteBelow(5) Expect(hist.ranges.Len()).To(Equal(2)) - Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 5, End: 5})) - Expect(hist.ranges.Back().Value).To(Equal(utils.PacketInterval{Start: 10, End: 10})) + Expect(hist.ranges.Front().Value).To(Equal(interval{Start: 5, End: 5})) + Expect(hist.ranges.Back().Value).To(Equal(interval{Start: 10, End: 10})) }) It("keeps a one-packet range, if deleting up to the packet directly below", func() { Expect(hist.ReceivedPacket(4)).To(BeTrue()) hist.DeleteBelow(4) Expect(hist.ranges.Len()).To(Equal(1)) - Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 4, End: 4})) + Expect(hist.ranges.Front().Value).To(Equal(interval{Start: 4, End: 4})) }) It("doesn't add delayed packets below deleted ranges", func() { @@ -183,10 +183,10 @@ var _ = Describe("receivedPacketHistory", func() { Expect(hist.ReceivedPacket(6)).To(BeTrue()) hist.DeleteBelow(5) Expect(hist.ranges.Len()).To(Equal(1)) - Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 5, End: 6})) + Expect(hist.ranges.Front().Value).To(Equal(interval{Start: 5, End: 6})) Expect(hist.ReceivedPacket(2)).To(BeFalse()) Expect(hist.ranges.Len()).To(Equal(1)) - Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 5, End: 6})) + Expect(hist.ranges.Front().Value).To(Equal(interval{Start: 5, End: 6})) }) It("doesn't create more than MaxNumAckRanges ranges", func() { @@ -194,11 +194,11 @@ var _ = Describe("receivedPacketHistory", func() { Expect(hist.ReceivedPacket(2 * i)).To(BeTrue()) } Expect(hist.ranges.Len()).To(Equal(protocol.MaxNumAckRanges)) - Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 0, End: 0})) + Expect(hist.ranges.Front().Value).To(Equal(interval{Start: 0, End: 0})) hist.ReceivedPacket(2*protocol.MaxNumAckRanges + 1000) // check that the oldest ACK range was deleted Expect(hist.ranges.Len()).To(Equal(protocol.MaxNumAckRanges)) - Expect(hist.ranges.Front().Value).To(Equal(utils.PacketInterval{Start: 2, End: 2})) + Expect(hist.ranges.Front().Value).To(Equal(interval{Start: 2, End: 2})) }) }) diff --git a/internal/ackhandler/sent_packet_history.go b/internal/ackhandler/sent_packet_history.go index d5704dd2c75..fca26911512 100644 --- a/internal/ackhandler/sent_packet_history.go +++ b/internal/ackhandler/sent_packet_history.go @@ -6,22 +6,23 @@ import ( "github.com/lucas-clemente/quic-go/internal/protocol" "github.com/lucas-clemente/quic-go/internal/utils" + list "github.com/lucas-clemente/quic-go/internal/utils/linkedlist" ) type sentPacketHistory struct { rttStats *utils.RTTStats - outstandingPacketList *PacketList - etcPacketList *PacketList - packetMap map[protocol.PacketNumber]*PacketElement + outstandingPacketList *list.List[Packet] + etcPacketList *list.List[Packet] + packetMap map[protocol.PacketNumber]*list.Element[Packet] highestSent protocol.PacketNumber } func newSentPacketHistory(rttStats *utils.RTTStats) *sentPacketHistory { return &sentPacketHistory{ rttStats: rttStats, - outstandingPacketList: NewPacketList(), - etcPacketList: NewPacketList(), - packetMap: make(map[protocol.PacketNumber]*PacketElement), + outstandingPacketList: list.New[Packet](), + etcPacketList: list.New[Packet](), + packetMap: make(map[protocol.PacketNumber]*list.Element[Packet]), highestSent: protocol.InvalidPacketNumber, } } @@ -43,7 +44,7 @@ func (h *sentPacketHistory) SentPacket(p *Packet, isAckEliciting bool) { h.highestSent = p.PacketNumber if isAckEliciting { - var el *PacketElement + var el *list.Element[Packet] if p.outstanding() { el = h.outstandingPacketList.PushBack(*p) } else { @@ -58,7 +59,7 @@ func (h *sentPacketHistory) Iterate(cb func(*Packet) (cont bool, err error)) err cont := true outstandingEl := h.outstandingPacketList.Front() etcEl := h.etcPacketList.Front() - var el *PacketElement + var el *list.Element[Packet] // whichever has the next packet number is returned first for cont { if outstandingEl == nil || (etcEl != nil && etcEl.Value.PacketNumber < outstandingEl.Value.PacketNumber) { @@ -113,7 +114,7 @@ func (h *sentPacketHistory) HasOutstandingPackets() bool { func (h *sentPacketHistory) DeleteOldPackets(now time.Time) { maxAge := 3 * h.rttStats.PTO(false) - var nextEl *PacketElement + var nextEl *list.Element[Packet] // we don't iterate outstandingPacketList, as we should not delete outstanding packets. // being outstanding for more than 3*PTO should only happen in the case of drastic RTT changes. for el := h.etcPacketList.Front(); el != nil; el = nextEl { diff --git a/internal/utils/byteinterval_linkedlist.go b/internal/utils/byteinterval_linkedlist.go deleted file mode 100644 index 096023ef28d..00000000000 --- a/internal/utils/byteinterval_linkedlist.go +++ /dev/null @@ -1,217 +0,0 @@ -// This file was automatically generated by genny. -// Any changes will be lost if this file is regenerated. -// see https://github.com/cheekybits/genny - -package utils - -// Linked list implementation from the Go standard library. - -// ByteIntervalElement is an element of a linked list. -type ByteIntervalElement struct { - // Next and previous pointers in the doubly-linked list of elements. - // To simplify the implementation, internally a list l is implemented - // as a ring, such that &l.root is both the next element of the last - // list element (l.Back()) and the previous element of the first list - // element (l.Front()). - next, prev *ByteIntervalElement - - // The list to which this element belongs. - list *ByteIntervalList - - // The value stored with this element. - Value ByteInterval -} - -// Next returns the next list element or nil. -func (e *ByteIntervalElement) Next() *ByteIntervalElement { - if p := e.next; e.list != nil && p != &e.list.root { - return p - } - return nil -} - -// Prev returns the previous list element or nil. -func (e *ByteIntervalElement) Prev() *ByteIntervalElement { - if p := e.prev; e.list != nil && p != &e.list.root { - return p - } - return nil -} - -// ByteIntervalList is a linked list of ByteIntervals. -type ByteIntervalList struct { - root ByteIntervalElement // sentinel list element, only &root, root.prev, and root.next are used - len int // current list length excluding (this) sentinel element -} - -// Init initializes or clears list l. -func (l *ByteIntervalList) Init() *ByteIntervalList { - l.root.next = &l.root - l.root.prev = &l.root - l.len = 0 - return l -} - -// NewByteIntervalList returns an initialized list. -func NewByteIntervalList() *ByteIntervalList { return new(ByteIntervalList).Init() } - -// Len returns the number of elements of list l. -// The complexity is O(1). -func (l *ByteIntervalList) Len() int { return l.len } - -// Front returns the first element of list l or nil if the list is empty. -func (l *ByteIntervalList) Front() *ByteIntervalElement { - if l.len == 0 { - return nil - } - return l.root.next -} - -// Back returns the last element of list l or nil if the list is empty. -func (l *ByteIntervalList) Back() *ByteIntervalElement { - if l.len == 0 { - return nil - } - return l.root.prev -} - -// lazyInit lazily initializes a zero List value. -func (l *ByteIntervalList) lazyInit() { - if l.root.next == nil { - l.Init() - } -} - -// insert inserts e after at, increments l.len, and returns e. -func (l *ByteIntervalList) insert(e, at *ByteIntervalElement) *ByteIntervalElement { - n := at.next - at.next = e - e.prev = at - e.next = n - n.prev = e - e.list = l - l.len++ - return e -} - -// insertValue is a convenience wrapper for insert(&Element{Value: v}, at). -func (l *ByteIntervalList) insertValue(v ByteInterval, at *ByteIntervalElement) *ByteIntervalElement { - return l.insert(&ByteIntervalElement{Value: v}, at) -} - -// remove removes e from its list, decrements l.len, and returns e. -func (l *ByteIntervalList) remove(e *ByteIntervalElement) *ByteIntervalElement { - e.prev.next = e.next - e.next.prev = e.prev - e.next = nil // avoid memory leaks - e.prev = nil // avoid memory leaks - e.list = nil - l.len-- - return e -} - -// Remove removes e from l if e is an element of list l. -// It returns the element value e.Value. -// The element must not be nil. -func (l *ByteIntervalList) Remove(e *ByteIntervalElement) ByteInterval { - if e.list == l { - // if e.list == l, l must have been initialized when e was inserted - // in l or l == nil (e is a zero Element) and l.remove will crash - l.remove(e) - } - return e.Value -} - -// PushFront inserts a new element e with value v at the front of list l and returns e. -func (l *ByteIntervalList) PushFront(v ByteInterval) *ByteIntervalElement { - l.lazyInit() - return l.insertValue(v, &l.root) -} - -// PushBack inserts a new element e with value v at the back of list l and returns e. -func (l *ByteIntervalList) PushBack(v ByteInterval) *ByteIntervalElement { - l.lazyInit() - return l.insertValue(v, l.root.prev) -} - -// InsertBefore inserts a new element e with value v immediately before mark and returns e. -// If mark is not an element of l, the list is not modified. -// The mark must not be nil. -func (l *ByteIntervalList) InsertBefore(v ByteInterval, mark *ByteIntervalElement) *ByteIntervalElement { - if mark.list != l { - return nil - } - // see comment in List.Remove about initialization of l - return l.insertValue(v, mark.prev) -} - -// InsertAfter inserts a new element e with value v immediately after mark and returns e. -// If mark is not an element of l, the list is not modified. -// The mark must not be nil. -func (l *ByteIntervalList) InsertAfter(v ByteInterval, mark *ByteIntervalElement) *ByteIntervalElement { - if mark.list != l { - return nil - } - // see comment in List.Remove about initialization of l - return l.insertValue(v, mark) -} - -// MoveToFront moves element e to the front of list l. -// If e is not an element of l, the list is not modified. -// The element must not be nil. -func (l *ByteIntervalList) MoveToFront(e *ByteIntervalElement) { - if e.list != l || l.root.next == e { - return - } - // see comment in List.Remove about initialization of l - l.insert(l.remove(e), &l.root) -} - -// MoveToBack moves element e to the back of list l. -// If e is not an element of l, the list is not modified. -// The element must not be nil. -func (l *ByteIntervalList) MoveToBack(e *ByteIntervalElement) { - if e.list != l || l.root.prev == e { - return - } - // see comment in List.Remove about initialization of l - l.insert(l.remove(e), l.root.prev) -} - -// MoveBefore moves element e to its new position before mark. -// If e or mark is not an element of l, or e == mark, the list is not modified. -// The element and mark must not be nil. -func (l *ByteIntervalList) MoveBefore(e, mark *ByteIntervalElement) { - if e.list != l || e == mark || mark.list != l { - return - } - l.insert(l.remove(e), mark.prev) -} - -// MoveAfter moves element e to its new position after mark. -// If e or mark is not an element of l, or e == mark, the list is not modified. -// The element and mark must not be nil. -func (l *ByteIntervalList) MoveAfter(e, mark *ByteIntervalElement) { - if e.list != l || e == mark || mark.list != l { - return - } - l.insert(l.remove(e), mark) -} - -// PushBackList inserts a copy of an other list at the back of list l. -// The lists l and other may be the same. They must not be nil. -func (l *ByteIntervalList) PushBackList(other *ByteIntervalList) { - l.lazyInit() - for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() { - l.insertValue(e.Value, l.root.prev) - } -} - -// PushFrontList inserts a copy of an other list at the front of list l. -// The lists l and other may be the same. They must not be nil. -func (l *ByteIntervalList) PushFrontList(other *ByteIntervalList) { - l.lazyInit() - for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() { - l.insertValue(e.Value, &l.root) - } -} diff --git a/internal/utils/gen.go b/internal/utils/gen.go deleted file mode 100644 index 8a63e95891e..00000000000 --- a/internal/utils/gen.go +++ /dev/null @@ -1,5 +0,0 @@ -package utils - -//go:generate genny -pkg utils -in linkedlist/linkedlist.go -out byteinterval_linkedlist.go gen Item=ByteInterval -//go:generate genny -pkg utils -in linkedlist/linkedlist.go -out packetinterval_linkedlist.go gen Item=PacketInterval -//go:generate genny -pkg utils -in linkedlist/linkedlist.go -out newconnectionid_linkedlist.go gen Item=NewConnectionID diff --git a/internal/utils/linkedlist/README.md b/internal/utils/linkedlist/README.md index 15b46dce5dd..2915d6013da 100644 --- a/internal/utils/linkedlist/README.md +++ b/internal/utils/linkedlist/README.md @@ -1,11 +1,4 @@ # Usage This is the Go standard library implementation of a linked list -(https://golang.org/src/container/list/list.go), modified such that genny -(https://github.com/cheekybits/genny) can be used to generate a typed linked -list. - -To generate, run -``` -genny -pkg $PACKAGE -in linkedlist.go -out $OUTFILE gen Item=$TYPE -``` +(https://golang.org/src/container/list/list.go), modified to use Go generics. diff --git a/internal/utils/linkedlist/linkedlist.go b/internal/utils/linkedlist/linkedlist.go index 74b815a81b0..217b21efb11 100644 --- a/internal/utils/linkedlist/linkedlist.go +++ b/internal/utils/linkedlist/linkedlist.go @@ -1,30 +1,34 @@ -package linkedlist - -import "github.com/cheekybits/genny/generic" - -// Linked list implementation from the Go standard library. - -// Item is a generic type. -type Item generic.Type - -// ItemElement is an element of a linked list. -type ItemElement struct { +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package list implements a doubly linked list. +// +// To iterate over a list (where l is a *List[T]): +// +// for e := l.Front(); e != nil; e = e.Next() { +// // do something with e.Value +// } +package list + +// Element is an element of a linked list. +type Element[T any] struct { // Next and previous pointers in the doubly-linked list of elements. // To simplify the implementation, internally a list l is implemented // as a ring, such that &l.root is both the next element of the last // list element (l.Back()) and the previous element of the first list // element (l.Front()). - next, prev *ItemElement + next, prev *Element[T] // The list to which this element belongs. - list *ItemList + list *List[T] // The value stored with this element. - Value Item + Value T } // Next returns the next list element or nil. -func (e *ItemElement) Next() *ItemElement { +func (e *Element[T]) Next() *Element[T] { if p := e.next; e.list != nil && p != &e.list.root { return p } @@ -32,36 +36,37 @@ func (e *ItemElement) Next() *ItemElement { } // Prev returns the previous list element or nil. -func (e *ItemElement) Prev() *ItemElement { +func (e *Element[T]) Prev() *Element[T] { if p := e.prev; e.list != nil && p != &e.list.root { return p } return nil } -// ItemList is a linked list of Items. -type ItemList struct { - root ItemElement // sentinel list element, only &root, root.prev, and root.next are used - len int // current list length excluding (this) sentinel element +// List represents a doubly linked list. +// The zero value for List is an empty list ready to use. +type List[T any] struct { + root Element[T] // sentinel list element, only &root, root.prev, and root.next are used + len int // current list length excluding (this) sentinel element } // Init initializes or clears list l. -func (l *ItemList) Init() *ItemList { +func (l *List[T]) Init() *List[T] { l.root.next = &l.root l.root.prev = &l.root l.len = 0 return l } -// NewItemList returns an initialized list. -func NewItemList() *ItemList { return new(ItemList).Init() } +// New returns an initialized list. +func New[T any]() *List[T] { return new(List[T]).Init() } // Len returns the number of elements of list l. // The complexity is O(1). -func (l *ItemList) Len() int { return l.len } +func (l *List[T]) Len() int { return l.len } // Front returns the first element of list l or nil if the list is empty. -func (l *ItemList) Front() *ItemElement { +func (l *List[T]) Front() *Element[T] { if l.len == 0 { return nil } @@ -69,7 +74,7 @@ func (l *ItemList) Front() *ItemElement { } // Back returns the last element of list l or nil if the list is empty. -func (l *ItemList) Back() *ItemElement { +func (l *List[T]) Back() *Element[T] { if l.len == 0 { return nil } @@ -77,44 +82,56 @@ func (l *ItemList) Back() *ItemElement { } // lazyInit lazily initializes a zero List value. -func (l *ItemList) lazyInit() { +func (l *List[T]) lazyInit() { if l.root.next == nil { l.Init() } } // insert inserts e after at, increments l.len, and returns e. -func (l *ItemList) insert(e, at *ItemElement) *ItemElement { - n := at.next - at.next = e +func (l *List[T]) insert(e, at *Element[T]) *Element[T] { e.prev = at - e.next = n - n.prev = e + e.next = at.next + e.prev.next = e + e.next.prev = e e.list = l l.len++ return e } // insertValue is a convenience wrapper for insert(&Element{Value: v}, at). -func (l *ItemList) insertValue(v Item, at *ItemElement) *ItemElement { - return l.insert(&ItemElement{Value: v}, at) +func (l *List[T]) insertValue(v T, at *Element[T]) *Element[T] { + return l.insert(&Element[T]{Value: v}, at) } -// remove removes e from its list, decrements l.len, and returns e. -func (l *ItemList) remove(e *ItemElement) *ItemElement { +// remove removes e from its list, decrements l.len +func (l *List[T]) remove(e *Element[T]) { e.prev.next = e.next e.next.prev = e.prev e.next = nil // avoid memory leaks e.prev = nil // avoid memory leaks e.list = nil l.len-- - return e +} + +// move moves e to next to at. +func (l *List[T]) move(e, at *Element[T]) { + if e == at { + return + } + e.prev.next = e.next + e.next.prev = e.prev + + e.prev = at + e.next = at.next + e.prev.next = e + e.next.prev = e } // Remove removes e from l if e is an element of list l. // It returns the element value e.Value. // The element must not be nil. -func (l *ItemList) Remove(e *ItemElement) Item { +func (l *List[T]) Remove(e *Element[T]) T { if e.list == l { // if e.list == l, l must have been initialized when e was inserted // in l or l == nil (e is a zero Element) and l.remove will crash @@ -124,13 +141,13 @@ func (l *ItemList) Remove(e *ItemElement) Item { } // PushFront inserts a new element e with value v at the front of list l and returns e. -func (l *ItemList) PushFront(v Item) *ItemElement { +func (l *List[T]) PushFront(v T) *Element[T] { l.lazyInit() return l.insertValue(v, &l.root) } // PushBack inserts a new element e with value v at the back of list l and returns e. -func (l *ItemList) PushBack(v Item) *ItemElement { +func (l *List[T]) PushBack(v T) *Element[T] { l.lazyInit() return l.insertValue(v, l.root.prev) } @@ -138,7 +155,7 @@ func (l *ItemList) PushBack(v Item) *ItemElement { // InsertBefore inserts a new element e with value v immediately before mark and returns e. // If mark is not an element of l, the list is not modified. // The mark must not be nil. -func (l *ItemList) InsertBefore(v Item, mark *ItemElement) *ItemElement { +func (l *List[T]) InsertBefore(v T, mark *Element[T]) *Element[T] { if mark.list != l { return nil } @@ -149,7 +166,7 @@ func (l *ItemList) InsertBefore(v Item, mark *ItemElement) *ItemElement { // InsertAfter inserts a new element e with value v immediately after mark and returns e. // If mark is not an element of l, the list is not modified. // The mark must not be nil. -func (l *ItemList) InsertAfter(v Item, mark *ItemElement) *ItemElement { +func (l *List[T]) InsertAfter(v T, mark *Element[T]) *Element[T] { if mark.list != l { return nil } @@ -160,57 +177,57 @@ func (l *ItemList) InsertAfter(v Item, mark *ItemElement) *ItemElement { // MoveToFront moves element e to the front of list l. // If e is not an element of l, the list is not modified. // The element must not be nil. -func (l *ItemList) MoveToFront(e *ItemElement) { +func (l *List[T]) MoveToFront(e *Element[T]) { if e.list != l || l.root.next == e { return } // see comment in List.Remove about initialization of l - l.insert(l.remove(e), &l.root) + l.move(e, &l.root) } // MoveToBack moves element e to the back of list l. // If e is not an element of l, the list is not modified. // The element must not be nil. -func (l *ItemList) MoveToBack(e *ItemElement) { +func (l *List[T]) MoveToBack(e *Element[T]) { if e.list != l || l.root.prev == e { return } // see comment in List.Remove about initialization of l - l.insert(l.remove(e), l.root.prev) + l.move(e, l.root.prev) } // MoveBefore moves element e to its new position before mark. // If e or mark is not an element of l, or e == mark, the list is not modified. // The element and mark must not be nil. -func (l *ItemList) MoveBefore(e, mark *ItemElement) { +func (l *List[T]) MoveBefore(e, mark *Element[T]) { if e.list != l || e == mark || mark.list != l { return } - l.insert(l.remove(e), mark.prev) + l.move(e, mark.prev) } // MoveAfter moves element e to its new position after mark. // If e or mark is not an element of l, or e == mark, the list is not modified. // The element and mark must not be nil. -func (l *ItemList) MoveAfter(e, mark *ItemElement) { +func (l *List[T]) MoveAfter(e, mark *Element[T]) { if e.list != l || e == mark || mark.list != l { return } - l.insert(l.remove(e), mark) + l.move(e, mark) } -// PushBackList inserts a copy of an other list at the back of list l. +// PushBackList inserts a copy of another list at the back of list l. // The lists l and other may be the same. They must not be nil. -func (l *ItemList) PushBackList(other *ItemList) { +func (l *List[T]) PushBackList(other *List[T]) { l.lazyInit() for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() { l.insertValue(e.Value, l.root.prev) } } -// PushFrontList inserts a copy of an other list at the front of list l. +// PushFrontList inserts a copy of another list at the front of list l. // The lists l and other may be the same. They must not be nil. -func (l *ItemList) PushFrontList(other *ItemList) { +func (l *List[T]) PushFrontList(other *List[T]) { l.lazyInit() for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() { l.insertValue(e.Value, &l.root) diff --git a/internal/utils/new_connection_id.go b/internal/utils/new_connection_id.go deleted file mode 100644 index 694ee7aaf2c..00000000000 --- a/internal/utils/new_connection_id.go +++ /dev/null @@ -1,12 +0,0 @@ -package utils - -import ( - "github.com/lucas-clemente/quic-go/internal/protocol" -) - -// NewConnectionID is a new connection ID -type NewConnectionID struct { - SequenceNumber uint64 - ConnectionID protocol.ConnectionID - StatelessResetToken protocol.StatelessResetToken -} diff --git a/internal/utils/newconnectionid_linkedlist.go b/internal/utils/newconnectionid_linkedlist.go deleted file mode 100644 index d59562e536e..00000000000 --- a/internal/utils/newconnectionid_linkedlist.go +++ /dev/null @@ -1,217 +0,0 @@ -// This file was automatically generated by genny. -// Any changes will be lost if this file is regenerated. -// see https://github.com/cheekybits/genny - -package utils - -// Linked list implementation from the Go standard library. - -// NewConnectionIDElement is an element of a linked list. -type NewConnectionIDElement struct { - // Next and previous pointers in the doubly-linked list of elements. - // To simplify the implementation, internally a list l is implemented - // as a ring, such that &l.root is both the next element of the last - // list element (l.Back()) and the previous element of the first list - // element (l.Front()). - next, prev *NewConnectionIDElement - - // The list to which this element belongs. - list *NewConnectionIDList - - // The value stored with this element. - Value NewConnectionID -} - -// Next returns the next list element or nil. -func (e *NewConnectionIDElement) Next() *NewConnectionIDElement { - if p := e.next; e.list != nil && p != &e.list.root { - return p - } - return nil -} - -// Prev returns the previous list element or nil. -func (e *NewConnectionIDElement) Prev() *NewConnectionIDElement { - if p := e.prev; e.list != nil && p != &e.list.root { - return p - } - return nil -} - -// NewConnectionIDList is a linked list of NewConnectionIDs. -type NewConnectionIDList struct { - root NewConnectionIDElement // sentinel list element, only &root, root.prev, and root.next are used - len int // current list length excluding (this) sentinel element -} - -// Init initializes or clears list l. -func (l *NewConnectionIDList) Init() *NewConnectionIDList { - l.root.next = &l.root - l.root.prev = &l.root - l.len = 0 - return l -} - -// NewNewConnectionIDList returns an initialized list. -func NewNewConnectionIDList() *NewConnectionIDList { return new(NewConnectionIDList).Init() } - -// Len returns the number of elements of list l. -// The complexity is O(1). -func (l *NewConnectionIDList) Len() int { return l.len } - -// Front returns the first element of list l or nil if the list is empty. -func (l *NewConnectionIDList) Front() *NewConnectionIDElement { - if l.len == 0 { - return nil - } - return l.root.next -} - -// Back returns the last element of list l or nil if the list is empty. -func (l *NewConnectionIDList) Back() *NewConnectionIDElement { - if l.len == 0 { - return nil - } - return l.root.prev -} - -// lazyInit lazily initializes a zero List value. -func (l *NewConnectionIDList) lazyInit() { - if l.root.next == nil { - l.Init() - } -} - -// insert inserts e after at, increments l.len, and returns e. -func (l *NewConnectionIDList) insert(e, at *NewConnectionIDElement) *NewConnectionIDElement { - n := at.next - at.next = e - e.prev = at - e.next = n - n.prev = e - e.list = l - l.len++ - return e -} - -// insertValue is a convenience wrapper for insert(&Element{Value: v}, at). -func (l *NewConnectionIDList) insertValue(v NewConnectionID, at *NewConnectionIDElement) *NewConnectionIDElement { - return l.insert(&NewConnectionIDElement{Value: v}, at) -} - -// remove removes e from its list, decrements l.len, and returns e. -func (l *NewConnectionIDList) remove(e *NewConnectionIDElement) *NewConnectionIDElement { - e.prev.next = e.next - e.next.prev = e.prev - e.next = nil // avoid memory leaks - e.prev = nil // avoid memory leaks - e.list = nil - l.len-- - return e -} - -// Remove removes e from l if e is an element of list l. -// It returns the element value e.Value. -// The element must not be nil. -func (l *NewConnectionIDList) Remove(e *NewConnectionIDElement) NewConnectionID { - if e.list == l { - // if e.list == l, l must have been initialized when e was inserted - // in l or l == nil (e is a zero Element) and l.remove will crash - l.remove(e) - } - return e.Value -} - -// PushFront inserts a new element e with value v at the front of list l and returns e. -func (l *NewConnectionIDList) PushFront(v NewConnectionID) *NewConnectionIDElement { - l.lazyInit() - return l.insertValue(v, &l.root) -} - -// PushBack inserts a new element e with value v at the back of list l and returns e. -func (l *NewConnectionIDList) PushBack(v NewConnectionID) *NewConnectionIDElement { - l.lazyInit() - return l.insertValue(v, l.root.prev) -} - -// InsertBefore inserts a new element e with value v immediately before mark and returns e. -// If mark is not an element of l, the list is not modified. -// The mark must not be nil. -func (l *NewConnectionIDList) InsertBefore(v NewConnectionID, mark *NewConnectionIDElement) *NewConnectionIDElement { - if mark.list != l { - return nil - } - // see comment in List.Remove about initialization of l - return l.insertValue(v, mark.prev) -} - -// InsertAfter inserts a new element e with value v immediately after mark and returns e. -// If mark is not an element of l, the list is not modified. -// The mark must not be nil. -func (l *NewConnectionIDList) InsertAfter(v NewConnectionID, mark *NewConnectionIDElement) *NewConnectionIDElement { - if mark.list != l { - return nil - } - // see comment in List.Remove about initialization of l - return l.insertValue(v, mark) -} - -// MoveToFront moves element e to the front of list l. -// If e is not an element of l, the list is not modified. -// The element must not be nil. -func (l *NewConnectionIDList) MoveToFront(e *NewConnectionIDElement) { - if e.list != l || l.root.next == e { - return - } - // see comment in List.Remove about initialization of l - l.insert(l.remove(e), &l.root) -} - -// MoveToBack moves element e to the back of list l. -// If e is not an element of l, the list is not modified. -// The element must not be nil. -func (l *NewConnectionIDList) MoveToBack(e *NewConnectionIDElement) { - if e.list != l || l.root.prev == e { - return - } - // see comment in List.Remove about initialization of l - l.insert(l.remove(e), l.root.prev) -} - -// MoveBefore moves element e to its new position before mark. -// If e or mark is not an element of l, or e == mark, the list is not modified. -// The element and mark must not be nil. -func (l *NewConnectionIDList) MoveBefore(e, mark *NewConnectionIDElement) { - if e.list != l || e == mark || mark.list != l { - return - } - l.insert(l.remove(e), mark.prev) -} - -// MoveAfter moves element e to its new position after mark. -// If e or mark is not an element of l, or e == mark, the list is not modified. -// The element and mark must not be nil. -func (l *NewConnectionIDList) MoveAfter(e, mark *NewConnectionIDElement) { - if e.list != l || e == mark || mark.list != l { - return - } - l.insert(l.remove(e), mark) -} - -// PushBackList inserts a copy of an other list at the back of list l. -// The lists l and other may be the same. They must not be nil. -func (l *NewConnectionIDList) PushBackList(other *NewConnectionIDList) { - l.lazyInit() - for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() { - l.insertValue(e.Value, l.root.prev) - } -} - -// PushFrontList inserts a copy of an other list at the front of list l. -// The lists l and other may be the same. They must not be nil. -func (l *NewConnectionIDList) PushFrontList(other *NewConnectionIDList) { - l.lazyInit() - for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() { - l.insertValue(e.Value, &l.root) - } -} diff --git a/internal/utils/packet_interval.go b/internal/utils/packet_interval.go deleted file mode 100644 index 62cc8b9cb31..00000000000 --- a/internal/utils/packet_interval.go +++ /dev/null @@ -1,9 +0,0 @@ -package utils - -import "github.com/lucas-clemente/quic-go/internal/protocol" - -// PacketInterval is an interval from one PacketNumber to the other -type PacketInterval struct { - Start protocol.PacketNumber - End protocol.PacketNumber -} diff --git a/internal/utils/packetinterval_linkedlist.go b/internal/utils/packetinterval_linkedlist.go deleted file mode 100644 index b461e85a96e..00000000000 --- a/internal/utils/packetinterval_linkedlist.go +++ /dev/null @@ -1,217 +0,0 @@ -// This file was automatically generated by genny. -// Any changes will be lost if this file is regenerated. -// see https://github.com/cheekybits/genny - -package utils - -// Linked list implementation from the Go standard library. - -// PacketIntervalElement is an element of a linked list. -type PacketIntervalElement struct { - // Next and previous pointers in the doubly-linked list of elements. - // To simplify the implementation, internally a list l is implemented - // as a ring, such that &l.root is both the next element of the last - // list element (l.Back()) and the previous element of the first list - // element (l.Front()). - next, prev *PacketIntervalElement - - // The list to which this element belongs. - list *PacketIntervalList - - // The value stored with this element. - Value PacketInterval -} - -// Next returns the next list element or nil. -func (e *PacketIntervalElement) Next() *PacketIntervalElement { - if p := e.next; e.list != nil && p != &e.list.root { - return p - } - return nil -} - -// Prev returns the previous list element or nil. -func (e *PacketIntervalElement) Prev() *PacketIntervalElement { - if p := e.prev; e.list != nil && p != &e.list.root { - return p - } - return nil -} - -// PacketIntervalList is a linked list of PacketIntervals. -type PacketIntervalList struct { - root PacketIntervalElement // sentinel list element, only &root, root.prev, and root.next are used - len int // current list length excluding (this) sentinel element -} - -// Init initializes or clears list l. -func (l *PacketIntervalList) Init() *PacketIntervalList { - l.root.next = &l.root - l.root.prev = &l.root - l.len = 0 - return l -} - -// NewPacketIntervalList returns an initialized list. -func NewPacketIntervalList() *PacketIntervalList { return new(PacketIntervalList).Init() } - -// Len returns the number of elements of list l. -// The complexity is O(1). -func (l *PacketIntervalList) Len() int { return l.len } - -// Front returns the first element of list l or nil if the list is empty. -func (l *PacketIntervalList) Front() *PacketIntervalElement { - if l.len == 0 { - return nil - } - return l.root.next -} - -// Back returns the last element of list l or nil if the list is empty. -func (l *PacketIntervalList) Back() *PacketIntervalElement { - if l.len == 0 { - return nil - } - return l.root.prev -} - -// lazyInit lazily initializes a zero List value. -func (l *PacketIntervalList) lazyInit() { - if l.root.next == nil { - l.Init() - } -} - -// insert inserts e after at, increments l.len, and returns e. -func (l *PacketIntervalList) insert(e, at *PacketIntervalElement) *PacketIntervalElement { - n := at.next - at.next = e - e.prev = at - e.next = n - n.prev = e - e.list = l - l.len++ - return e -} - -// insertValue is a convenience wrapper for insert(&Element{Value: v}, at). -func (l *PacketIntervalList) insertValue(v PacketInterval, at *PacketIntervalElement) *PacketIntervalElement { - return l.insert(&PacketIntervalElement{Value: v}, at) -} - -// remove removes e from its list, decrements l.len, and returns e. -func (l *PacketIntervalList) remove(e *PacketIntervalElement) *PacketIntervalElement { - e.prev.next = e.next - e.next.prev = e.prev - e.next = nil // avoid memory leaks - e.prev = nil // avoid memory leaks - e.list = nil - l.len-- - return e -} - -// Remove removes e from l if e is an element of list l. -// It returns the element value e.Value. -// The element must not be nil. -func (l *PacketIntervalList) Remove(e *PacketIntervalElement) PacketInterval { - if e.list == l { - // if e.list == l, l must have been initialized when e was inserted - // in l or l == nil (e is a zero Element) and l.remove will crash - l.remove(e) - } - return e.Value -} - -// PushFront inserts a new element e with value v at the front of list l and returns e. -func (l *PacketIntervalList) PushFront(v PacketInterval) *PacketIntervalElement { - l.lazyInit() - return l.insertValue(v, &l.root) -} - -// PushBack inserts a new element e with value v at the back of list l and returns e. -func (l *PacketIntervalList) PushBack(v PacketInterval) *PacketIntervalElement { - l.lazyInit() - return l.insertValue(v, l.root.prev) -} - -// InsertBefore inserts a new element e with value v immediately before mark and returns e. -// If mark is not an element of l, the list is not modified. -// The mark must not be nil. -func (l *PacketIntervalList) InsertBefore(v PacketInterval, mark *PacketIntervalElement) *PacketIntervalElement { - if mark.list != l { - return nil - } - // see comment in List.Remove about initialization of l - return l.insertValue(v, mark.prev) -} - -// InsertAfter inserts a new element e with value v immediately after mark and returns e. -// If mark is not an element of l, the list is not modified. -// The mark must not be nil. -func (l *PacketIntervalList) InsertAfter(v PacketInterval, mark *PacketIntervalElement) *PacketIntervalElement { - if mark.list != l { - return nil - } - // see comment in List.Remove about initialization of l - return l.insertValue(v, mark) -} - -// MoveToFront moves element e to the front of list l. -// If e is not an element of l, the list is not modified. -// The element must not be nil. -func (l *PacketIntervalList) MoveToFront(e *PacketIntervalElement) { - if e.list != l || l.root.next == e { - return - } - // see comment in List.Remove about initialization of l - l.insert(l.remove(e), &l.root) -} - -// MoveToBack moves element e to the back of list l. -// If e is not an element of l, the list is not modified. -// The element must not be nil. -func (l *PacketIntervalList) MoveToBack(e *PacketIntervalElement) { - if e.list != l || l.root.prev == e { - return - } - // see comment in List.Remove about initialization of l - l.insert(l.remove(e), l.root.prev) -} - -// MoveBefore moves element e to its new position before mark. -// If e or mark is not an element of l, or e == mark, the list is not modified. -// The element and mark must not be nil. -func (l *PacketIntervalList) MoveBefore(e, mark *PacketIntervalElement) { - if e.list != l || e == mark || mark.list != l { - return - } - l.insert(l.remove(e), mark.prev) -} - -// MoveAfter moves element e to its new position after mark. -// If e or mark is not an element of l, or e == mark, the list is not modified. -// The element and mark must not be nil. -func (l *PacketIntervalList) MoveAfter(e, mark *PacketIntervalElement) { - if e.list != l || e == mark || mark.list != l { - return - } - l.insert(l.remove(e), mark) -} - -// PushBackList inserts a copy of an other list at the back of list l. -// The lists l and other may be the same. They must not be nil. -func (l *PacketIntervalList) PushBackList(other *PacketIntervalList) { - l.lazyInit() - for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() { - l.insertValue(e.Value, l.root.prev) - } -} - -// PushFrontList inserts a copy of an other list at the front of list l. -// The lists l and other may be the same. They must not be nil. -func (l *PacketIntervalList) PushFrontList(other *PacketIntervalList) { - l.lazyInit() - for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() { - l.insertValue(e.Value, &l.root) - } -} diff --git a/internal/utils/streamframe_interval.go b/internal/utils/streamframe_interval.go deleted file mode 100644 index ec16d251b89..00000000000 --- a/internal/utils/streamframe_interval.go +++ /dev/null @@ -1,9 +0,0 @@ -package utils - -import "github.com/lucas-clemente/quic-go/internal/protocol" - -// ByteInterval is an interval from one ByteCount to the other -type ByteInterval struct { - Start protocol.ByteCount - End protocol.ByteCount -}