WriteDirect cannot be mixed with WriteString or WriteBinary functions.
(extra []byte, remainLen int)
| 570 | |
| 571 | // WriteDirect cannot be mixed with WriteString or WriteBinary functions. |
| 572 | func (b *UnsafeLinkBuffer) WriteDirect(extra []byte, remainLen int) error { |
| 573 | n := len(extra) |
| 574 | if n == 0 || remainLen < 0 { |
| 575 | return nil |
| 576 | } |
| 577 | // find origin |
| 578 | origin := b.flush |
| 579 | malloc := b.mallocSize - remainLen // calculate the remaining malloc length |
| 580 | for t := origin.malloc - len(origin.buf); t < malloc; t = origin.malloc - len(origin.buf) { |
| 581 | malloc -= t |
| 582 | origin = origin.next |
| 583 | } |
| 584 | // Add the buf length of the original node |
| 585 | // `malloc` is the origin buffer offset that already malloced, the extra buffer should be inserted after that offset. |
| 586 | malloc += len(origin.buf) |
| 587 | |
| 588 | // Create dataNode and newNode and insert them into the chain |
| 589 | // dataNode wrap the user buffer extra, and newNode wrap the origin left netpoll buffer |
| 590 | // - originNode{buf=origin, off=0, malloc=malloc, readonly=true} : non-reusable |
| 591 | // - dataNode{buf=extra, off=0, malloc=len(extra), readonly=true} : non-reusable |
| 592 | // - newNode{buf=origin, off=malloc, malloc=origin.malloc, readonly=false} : reusable |
| 593 | dataNode := newLinkBufferNode(0) // zero node will be set by readonly mode |
| 594 | dataNode.buf, dataNode.malloc = extra[:0], n |
| 595 | |
| 596 | if remainLen > 0 { |
| 597 | // split a single buffer node to originNode and newNode |
| 598 | newNode := newLinkBufferNode(0) |
| 599 | newNode.off = malloc |
| 600 | newNode.buf = origin.buf[:malloc] |
| 601 | newNode.malloc = origin.malloc |
| 602 | newNode.unsetFlag(flagUnmanaged) |
| 603 | origin.malloc = malloc |
| 604 | origin.setFlag(flagUnmanaged) |
| 605 | |
| 606 | // link nodes |
| 607 | dataNode.next = newNode |
| 608 | newNode.next = origin.next |
| 609 | origin.next = dataNode |
| 610 | } else { |
| 611 | // link nodes |
| 612 | dataNode.next = origin.next |
| 613 | origin.next = dataNode |
| 614 | } |
| 615 | |
| 616 | // adjust b.write |
| 617 | for b.write.next != nil { |
| 618 | b.write = b.write.next |
| 619 | } |
| 620 | |
| 621 | b.mallocSize += n |
| 622 | return nil |
| 623 | } |
| 624 | |
| 625 | // WriteByte implements Writer. |
| 626 | func (b *UnsafeLinkBuffer) WriteByte(p byte) (err error) { |
nothing calls this directly
no test coverage detected