buffer.go 732 B

123456789101112131415161718192021222324252627282930313233343536
  1. package fuse
  2. import "unsafe"
  3. // buffer provides a mechanism for constructing a message from
  4. // multiple segments.
  5. type buffer []byte
  6. // alloc allocates size bytes and returns a pointer to the new
  7. // segment.
  8. func (w *buffer) alloc(size uintptr) unsafe.Pointer {
  9. s := int(size)
  10. if len(*w)+s > cap(*w) {
  11. old := *w
  12. *w = make([]byte, len(*w), 2*cap(*w)+s)
  13. copy(*w, old)
  14. }
  15. l := len(*w)
  16. *w = (*w)[:l+s]
  17. return unsafe.Pointer(&(*w)[l])
  18. }
  19. // reset clears out the contents of the buffer.
  20. func (w *buffer) reset() {
  21. for i := range (*w)[:cap(*w)] {
  22. (*w)[i] = 0
  23. }
  24. *w = (*w)[:0]
  25. }
  26. func newBuffer(extra uintptr) buffer {
  27. const hdrSize = unsafe.Sizeof(outHeader{})
  28. buf := make(buffer, hdrSize, hdrSize+extra)
  29. return buf
  30. }