WriteBuffer will not submit(e.g. Flush) data to ensure normal use of MallocLen. you must actively submit before read the data. The argument buf can't be used after calling WriteBuffer. (set it to nil)
(buf *LinkBuffer)
| 500 | // you must actively submit before read the data. |
| 501 | // The argument buf can't be used after calling WriteBuffer. (set it to nil) |
| 502 | func (b *UnsafeLinkBuffer) WriteBuffer(buf *LinkBuffer) (err error) { |
| 503 | if buf == nil { |
| 504 | return |
| 505 | } |
| 506 | bufLen, bufMallocLen := buf.Len(), buf.MallocLen() |
| 507 | if bufLen+bufMallocLen <= 0 { |
| 508 | return nil |
| 509 | } |
| 510 | b.write.next = buf.read |
| 511 | b.write = buf.write |
| 512 | |
| 513 | // close buf, prevents reuse. |
| 514 | for buf.head != buf.read { |
| 515 | nd := buf.head |
| 516 | buf.head = buf.head.next |
| 517 | nd.Release() |
| 518 | } |
| 519 | for buf.write = buf.write.next; buf.write != nil; { |
| 520 | nd := buf.write |
| 521 | buf.write = buf.write.next |
| 522 | nd.Release() |
| 523 | } |
| 524 | buf.length, buf.mallocSize, buf.head, buf.read, buf.flush, buf.write = 0, 0, nil, nil, nil, nil |
| 525 | |
| 526 | // DON'T MODIFY THE CODE BELOW UNLESS YOU KNOW WHAT YOU ARE DOING ! |
| 527 | // |
| 528 | // You may encounter a chain of bugs and not be able to |
| 529 | // find out within a week that they are caused by modifications here. |
| 530 | // |
| 531 | // After release buf, continue to adjust b. |
| 532 | b.write.next = nil |
| 533 | if bufLen > 0 { |
| 534 | b.recalLen(bufLen) |
| 535 | } |
| 536 | b.mallocSize += bufMallocLen |
| 537 | return nil |
| 538 | } |
| 539 | |
| 540 | // WriteString implements Writer. |
| 541 | func (b *UnsafeLinkBuffer) WriteString(s string) (n int, err error) { |