// MakeBufferBytes makes a Buffer encapsulating a byte slice.
425074
func (lib *Lib) MakeBufferBytes(data []byte) (*Buffer, error) {
	if len(data) == 0 {
		return lib.GSS_C_NO_BUFFER, nil
	}

	// have to allocate the memory in C land and copy
	b, err := lib.MakeBuffer(allocMalloc)
	if err != nil {
		return nil, err
	}

	l := C.size_t(len(data))
	c := C.malloc(l)
	if b == nil {
		return nil, ErrMallocFailed
	}
	C.memmove(c, (unsafe.Pointer)(&data[0]), l)

	b.C_gss_buffer_t.length = l
	b.C_gss_buffer_t.value = c
	b.alloc = allocMalloc

	return b, nil
}
105082
func BytesBuffer() *bytes.Buffer {
	buf := bytesBuffer.Get().(*bytes.Buffer)
	buf.Reset()
	return buf
}
1090046
func (b *Buffer) getOrCreateBuffer(keyspace, shard string) *shardBuffer {
	key := topoproto.KeyspaceShardString(keyspace, shard)
	b.mu.RLock()
	sb, ok := b.buffers[key]
	stopped := b.stopped
	b.mu.RUnlock()

	if stopped {
		return nil
	}
	if ok {
		return sb
	}

	b.mu.Lock()
	defer b.mu.Unlock()
	// Look it up again because it could have been created in the meantime.
	sb, ok = b.buffers[key]
	if !ok {
		sb = newShardBuffer(b.mode(keyspace, shard), keyspace, shard, b.now, b.bufferSizeSema)
		b.buffers[key] = sb
	}
	return sb
}
586302
func cloneBytes(v []byte) []byte {
	var clone = make([]byte, len(v))
	copy(clone, v)
	return clone
}
677944
func MarshalBytes(in interface{}) (out []byte, err error) {
	bufferString := bytes.NewBuffer(out)
	if err := Marshal(in, bufferString); err != nil {
		return nil, err
	}
	return bufferString.Bytes(), nil
}
216697
func (w *Writer) SetBuffer(raw []byte) {
	w.b = w.b[:0]
	w.b = append(w.b, raw...)
}
738003
func (be *BinaryEncoder) WriteBytes(x []byte) {
	be.WriteLong(int64(len(x)))
	_, _ = be.buffer.Write(x)
}
840459
func newBuffer() *buffer {
	e := new(element)
	b := &buffer{
		Cond: newCond(),
		head: e,
		tail: e,
	}
	return b
}
1179811
func NewBuffers(numbuffers, maxbuffers int) Buffers {
	return &buffers{
		Stats:      make(Stats, 0, NumSamples),
		bufs:       make([]*buffer, 0, numbuffers),
		numbuffers: numbuffers,
		maxbuffers: maxbuffers,
	}
}
1246720
func (bf *BufferFactory) ReturnBuffer(buf *bytes.Buffer) {
	bf.Return(buf.Bytes())
}
1078985
func (b *WriteAtBuffer) Bytes() []byte {
	b.m.Lock()
	defer b.m.Unlock()
	return b.buf
}
11955
func (d *Context) ResizeBuffer(index int, newSize int) unsafe.Pointer {
	return C.duk_resize_buffer(d.duk_context, C.duk_idx_t(index), C.duk_size_t(newSize))
}
655691
func writeLPBytes(buf []byte, b []byte, t Type) (int, error) {
	total, n := 0, len(b)

	if n > int(maxLPLength) {
		return 0, makeError(t, "length (%d) greater than %d bytes", n, maxLPLength)
	}

	if len(buf) < 2+n {
		return 0, makeError(t, "insufficient buffer size, expected %d, got %d", 2+n, len(buf))
	}

	binary.BigEndian.PutUint16(buf, uint16(n))
	total += 2

	copy(buf[total:], b)
	total += n

	return total, nil
}
959181
func (b *Buffer) AppendBytes(data []byte) {
	for len(data) > 0 {
		if cap(b.Buf) == len(b.Buf) { // EnsureSpace won't be inlined.
			b.EnsureSpace(1)
		}

		sz := cap(b.Buf) - len(b.Buf)
		if sz > len(data) {
			sz = len(data)
		}

		b.Buf = append(b.Buf, data[:sz]...)
		data = data[sz:]
	}
}
1288719
func (d *Context) PushBufferObject(bufferIdx, size, length int, flags uint) {
	C.duk_push_buffer_object(
		d.duk_context,
		C.duk_idx_t(bufferIdx),
		C.duk_size_t(size),
		C.duk_size_t(length),
		C.duk_uint_t(flags),
	)
}
1139962
func (t *Template) RenderBytes(context ...interface{}) ([]byte, error) {
	var b *bytes.Buffer
	err := t.Render(b, context...)
	return b.Bytes(), err
}
974577
func (t *Table) AsBuffer() *bytes.Buffer {
	var buffer bytes.Buffer

	writer := tabwriter.NewWriter(&buffer, 5, 0, 1, ' ', 0)
	template := strings.Repeat("%v\t", len(t.columns))

	// Header and separator.
	if !t.IsHeadless() {
		var colh []interface{}
		var cols []interface{}

		for _, col := range t.columns {
			colh = append(colh, col.title)
			cols = append(cols, strings.Repeat("-", col.width))
		}
		fmt.Fprintf(writer, template+"\n", colh...)
		fmt.Fprintf(writer, template+"\n", cols...)
	}

	// Body.
	for _, row := range t.rows {
		var rowi []interface{}
		for _, cell := range row {
			rowi = append(rowi, cell)
		}
		fmt.Fprintf(writer, template+"\n", rowi...)
	}

	writer.Flush()
	return &buffer
}