Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mempool: add std allocator #382

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,16 @@ func (mp *MemPool) Free(buf []byte) {
mp.pool.Put(&buf)
}

// NativeAllocator definition.
type NativeAllocator struct{}
// stdAllocator .
type stdAllocator struct{}

// Malloc .
func (a *NativeAllocator) Malloc(size int) []byte {
func (a *stdAllocator) Malloc(size int) []byte {
return make([]byte, size)
}

// Realloc .
func (a *NativeAllocator) Realloc(buf []byte, size int) []byte {
func (a *stdAllocator) Realloc(buf []byte, size int) []byte {
if size <= cap(buf) {
return buf[:size]
}
Expand All @@ -129,7 +129,19 @@ func (a *NativeAllocator) Realloc(buf []byte, size int) []byte {
}

// Free .
func (a *NativeAllocator) Free(buf []byte) {
func (a *stdAllocator) Free(buf []byte) {
}

func (a *stdAllocator) Append(buf []byte, more ...byte) []byte {
return append(buf, more...)
}

func (a *stdAllocator) AppendString(buf []byte, more string) []byte {
return append(buf, more...)
}

func NewSTD() Allocator {
return &stdAllocator{}
}

// Malloc exports default package method.
Expand Down
Loading