github.com/database64128/shadowsocks-go@v1.7.0/zerocopy/zerocopy.go (about) 1 // Package zerocopy defines interfaces and helper functions for zero-copy read/write operations. 2 package zerocopy 3 4 // Headroom reports the amount of extra space required in read/write buffers besides the payload. 5 type Headroom struct { 6 // Front is the minimum space required at the beginning of the buffer before payload. 7 Front int 8 9 // Rear is the minimum space required at the end of the buffer after payload. 10 Rear int 11 } 12 13 // MaxHeadroom returns the maximum front and rear headroom of the two headroom pairs. 14 func MaxHeadroom(first, second Headroom) Headroom { 15 if first.Front < second.Front { 16 first.Front = second.Front 17 } 18 if first.Rear < second.Rear { 19 first.Rear = second.Rear 20 } 21 return first 22 } 23 24 // UDPRelayHeadroom returns the packer headroom subtracted by the unpacker headroom. 25 func UDPRelayHeadroom(packerHeadroom, unpackerHeadroom Headroom) Headroom { 26 packerHeadroom.Front -= unpackerHeadroom.Front 27 if packerHeadroom.Front < 0 { 28 packerHeadroom.Front = 0 29 } 30 packerHeadroom.Rear -= unpackerHeadroom.Rear 31 if packerHeadroom.Rear < 0 { 32 packerHeadroom.Rear = 0 33 } 34 return packerHeadroom 35 } 36 37 // tester allows us to write test functions outside _test.go files without importing the testing package. 38 type tester interface { 39 Error(args ...any) 40 Fatal(args ...any) 41 Errorf(format string, args ...any) 42 Fatalf(format string, args ...any) 43 }