Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["buffer_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
)
go_library(
name = "go_default_library",
srcs = [
"buffer.go",
"writer.go",
],
importpath = "go-common/app/service/main/broadcast/libs/bytes",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,86 @@
package bytes
import (
"sync"
)
// Buffer buffer.
type Buffer struct {
buf []byte
next *Buffer // next free buffer
}
// Bytes bytes.
func (b *Buffer) Bytes() []byte {
return b.buf
}
// Pool is a buffer pool.
type Pool struct {
lock sync.Mutex
free *Buffer
max int
num int
size int
}
// NewPool new a memory buffer pool struct.
func NewPool(num, size int) (p *Pool) {
p = new(Pool)
p.init(num, size)
return
}
// Init init the memory buffer.
func (p *Pool) Init(num, size int) {
p.init(num, size)
}
// init init the memory buffer.
func (p *Pool) init(num, size int) {
p.num = num
p.size = size
p.max = num * size
p.grow()
}
// grow grow the memory buffer size, and update free pointer.
func (p *Pool) grow() {
var (
i int
b *Buffer
bs []Buffer
buf []byte
)
buf = make([]byte, p.max)
bs = make([]Buffer, p.num)
p.free = &bs[0]
b = p.free
for i = 1; i < p.num; i++ {
b.buf = buf[(i-1)*p.size : i*p.size]
b.next = &bs[i]
b = b.next
}
b.buf = buf[(i-1)*p.size : i*p.size]
b.next = nil
}
// Get get a free memory buffer.
func (p *Pool) Get() (b *Buffer) {
p.lock.Lock()
if b = p.free; b == nil {
p.grow()
b = p.free
}
p.free = b.next
p.lock.Unlock()
return
}
// Put put back a memory buffer to free.
func (p *Pool) Put(b *Buffer) {
p.lock.Lock()
b.next = p.free
p.free = b
p.lock.Unlock()
}

View File

@@ -0,0 +1,21 @@
package bytes
import (
"testing"
)
func TestBuffer(t *testing.T) {
p := NewPool(2, 10)
b := p.Get()
if b.Bytes() == nil || len(b.Bytes()) == 0 {
t.FailNow()
}
b = p.Get()
if b.Bytes() == nil || len(b.Bytes()) == 0 {
t.FailNow()
}
b = p.Get()
if b.Bytes() == nil || len(b.Bytes()) == 0 {
t.FailNow()
}
}

View File

@@ -0,0 +1,57 @@
package bytes
// Writer writer.
type Writer struct {
n int
buf []byte
}
// NewWriterSize new a writer with size.
func NewWriterSize(n int) *Writer {
return &Writer{buf: make([]byte, n)}
}
// Len buff len.
func (w *Writer) Len() int {
return w.n
}
// Size buff cap.
func (w *Writer) Size() int {
return len(w.buf)
}
// Reset reset the buff.
func (w *Writer) Reset() {
w.n = 0
}
// Buffer return buff.
func (w *Writer) Buffer() []byte {
return w.buf[:w.n]
}
// Peek peek a buf.
func (w *Writer) Peek(n int) []byte {
var buf []byte
w.grow(n)
buf = w.buf[w.n : w.n+n]
w.n += n
return buf
}
// Write write a buff.
func (w *Writer) Write(p []byte) {
w.grow(len(p))
w.n += copy(w.buf[w.n:], p)
}
func (w *Writer) grow(n int) {
var buf []byte
if w.n+n < len(w.buf) {
return
}
buf = make([]byte, 2*len(w.buf)+n)
copy(buf, w.buf[:w.n])
w.buf = buf
}