mirror of
https://github.com/clearlinux/libnetwork.git
synced 2026-08-19 04:17:48 +00:00
Add serialize/deserialize for sequence list
Signed-off-by: Alessandro Boch <aboch@docker.com>
This commit is contained in:
@@ -5,6 +5,8 @@ package bitseq
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/docker/libnetwork/netutils"
|
||||
)
|
||||
|
||||
// Block Sequence constants
|
||||
@@ -74,6 +76,45 @@ func (s *Sequence) Equal(o *Sequence) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// ToByteArray converts the sequence into a byte array
|
||||
// TODO (aboch): manage network/host order stuff
|
||||
func (s *Sequence) ToByteArray() ([]byte, error) {
|
||||
var bb []byte
|
||||
|
||||
p := s
|
||||
for p != nil {
|
||||
bb = append(bb, netutils.U32ToA(p.Block)...)
|
||||
bb = append(bb, netutils.U32ToA(p.Count)...)
|
||||
p = p.Next
|
||||
}
|
||||
|
||||
return bb, nil
|
||||
}
|
||||
|
||||
// FromByteArray construct the sequence from the byte array
|
||||
// TODO (aboch): manage network/host order stuff
|
||||
func (s *Sequence) FromByteArray(data []byte) error {
|
||||
l := len(data)
|
||||
if l%8 != 0 {
|
||||
return fmt.Errorf("cannot deserialize byte sequence of lenght %d", l)
|
||||
}
|
||||
|
||||
p := s
|
||||
i := 0
|
||||
for {
|
||||
p.Block = netutils.ATo32(data[i : i+4])
|
||||
p.Count = netutils.ATo32(data[i+4 : i+8])
|
||||
i += 8
|
||||
if i == l {
|
||||
break
|
||||
}
|
||||
p.Next = &Sequence{}
|
||||
p = p.Next
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetFirstAvailable looks for the first unset bit in passed mask
|
||||
func GetFirstAvailable(head *Sequence) (int, int) {
|
||||
byteIndex := 0
|
||||
|
||||
@@ -331,3 +331,45 @@ func TestPushReservation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSerializeDeserialize(t *testing.T) {
|
||||
s := &Sequence{
|
||||
Block: 0xffffffff,
|
||||
Count: 1,
|
||||
Next: &Sequence{
|
||||
Block: 0xFF000000,
|
||||
Count: 1,
|
||||
Next: &Sequence{
|
||||
Block: 0xffffffff,
|
||||
Count: 6,
|
||||
Next: &Sequence{
|
||||
Block: 0xffffffff,
|
||||
Count: 1,
|
||||
Next: &Sequence{
|
||||
Block: 0xFF800000,
|
||||
Count: 1,
|
||||
Next: &Sequence{
|
||||
Block: 0xffffffff,
|
||||
Count: 6,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
data, err := s.ToByteArray()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r := &Sequence{}
|
||||
err = r.FromByteArray(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !s.Equal(r) {
|
||||
t.Fatalf("Sequences are different: \n%v\n%v", s, r)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user