Skip to content

Commit

Permalink
backport of 2820 (vitessio#2845)
Browse files Browse the repository at this point in the history
Signed-off-by: Dirkjan Bussink <[email protected]>
  • Loading branch information
planetscale-actions-bot authored Aug 2, 2023
1 parent 0684dd4 commit 31ceeee
Show file tree
Hide file tree
Showing 45 changed files with 2,313 additions and 268 deletions.
6 changes: 0 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,6 @@ endif
-ldflags "$(shell tools/build_version_flags.sh)" \
-o ${VTROOTBIN} ./go/...

# build vtboost with CGO, because it depends on libc
CGO_ENABLED=1 go build \
-trimpath $(EXTRA_BUILD_FLAGS) $(VT_GO_PARALLEL) \
-ldflags "$(shell tools/build_version_flags.sh)" \
-o ${VTROOTBIN} ./go/boost/cmd/vtboost/...

# cross-build can be used to cross-compile Vitess client binaries
# Outside of select client binaries (namely vtctlclient & vtexplain), cross-compiled Vitess Binaries are not recommended for production deployments
# Usage: GOOS=darwin GOARCH=amd64 make cross-build
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ require (
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090
golang.org/x/sync v0.1.0
gopkg.in/src-d/go-errors.v1 v1.0.0
modernc.org/mathutil v1.5.0
modernc.org/sqlite v1.21.0
storj.io/drpc v0.0.33
)
Expand Down Expand Up @@ -202,8 +203,7 @@ require (
lukechampine.com/uint128 v1.2.0 // indirect
modernc.org/cc/v3 v3.40.0 // indirect
modernc.org/ccgo/v3 v3.16.13 // indirect
modernc.org/libc v1.22.3 // indirect
modernc.org/mathutil v1.5.0 // indirect
modernc.org/libc v1.22.5 // indirect
modernc.org/memory v1.5.0 // indirect
modernc.org/opt v0.1.3 // indirect
modernc.org/strutil v1.1.3 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1112,8 +1112,8 @@ modernc.org/ccgo/v3 v3.16.13 h1:Mkgdzl46i5F/CNR/Kj80Ri59hC8TKAhZrYSaqvkwzUw=
modernc.org/ccgo/v3 v3.16.13/go.mod h1:2Quk+5YgpImhPjv2Qsob1DnZ/4som1lJTodubIcoUkY=
modernc.org/ccorpus v1.11.6 h1:J16RXiiqiCgua6+ZvQot4yUuUy8zxgqbqEEUuGPlISk=
modernc.org/httpfs v1.0.6 h1:AAgIpFZRXuYnkjftxTAZwMIiwEqAfk8aVB2/oA6nAeM=
modernc.org/libc v1.22.3 h1:D/g6O5ftAfavceqlLOFwaZuA5KYafKwmr30A6iSqoyY=
modernc.org/libc v1.22.3/go.mod h1:MQrloYP209xa2zHome2a8HLiLm6k0UT8CoHpV74tOFw=
modernc.org/libc v1.22.5 h1:91BNch/e5B0uPbJFgqbxXuOnxBQjlS//icfQEGmvyjE=
modernc.org/libc v1.22.5/go.mod h1:jj+Z7dTNX8fBScMVNRAYZ/jF91K8fdT2hYMThc3YjBY=
modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ=
modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
//go:build cgo

package offheap

/*
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"runtime"
"sync"
"unsafe"
)

var DefaultAllocator cmalloc
"vitess.io/vitess/go/boost/common/rowstore/offheap/memory"
)

const LeakCheck = false

// cmalloc tracks each individual allocation for the
// Allocator tracks each individual allocation for the
// leak checker.
// The lock is used to prevent concurrent access.
type cmalloc struct {
type Allocator struct {
allocator memory.Allocator
lock sync.Mutex
allocated map[uintptr]*allocation
}

// alloc allocates a new piece of memory of the given size.
// If the leak checker is active, we store the backtrace
// of our caller.
func (m *cmalloc) alloc(size uintptr) unsafe.Pointer {
ptr := unsafe.Pointer(C.malloc(C.size_t(size)))
func (m *Allocator) alloc(size int) unsafe.Pointer {
ptr, err := m.allocator.UnsafeMalloc(int(size))
if err != nil {
panic(err)
}
if LeakCheck {
m.lock.Lock()
defer m.lock.Unlock()
Expand All @@ -51,7 +49,7 @@ func (m *cmalloc) alloc(size uintptr) unsafe.Pointer {
// free frees a given piece of memory. If the leak checker
// is active, it validates that the pointer was also allocated
// and that we're not doing a double free.
func (m *cmalloc) free(ptr unsafe.Pointer) {
func (m *Allocator) free(ptr unsafe.Pointer) {
if ptr == nil {
return
}
Expand All @@ -71,26 +69,31 @@ func (m *cmalloc) free(ptr unsafe.Pointer) {
var stacklen = runtime.Callers(1, stack[:])
alloc.free = stack[:stacklen]
}
C.free(ptr)
err := m.allocator.UnsafeFree(ptr)
if err != nil {
panic(err)
}
}

// EnsureNoLeaks can be used in tests to validate no
// memory has leaked. It depends on the leak checker
// being enabled.
func (m *cmalloc) EnsureNoLeaks() {
m.lock.Lock()
defer m.lock.Unlock()
func (m *Allocator) EnsureNoLeaks() {
if LeakCheck {
m.lock.Lock()
defer m.lock.Unlock()

for ptr, alloc := range m.allocated {
if alloc.free == nil {
panic(fmt.Errorf("did not free pointer %#x\nALLOC: %s", ptr, alloc.AllocStack()))
for ptr, alloc := range m.allocated {
if alloc.free == nil {
panic(fmt.Errorf("did not free pointer %#x\nALLOC: %s", ptr, alloc.AllocStack()))
}
}
}
}

// IsAllocated checks if a pointer is allocated. Only
// works if the leak checker is enabled.
func (m *cmalloc) IsAllocated(ptr unsafe.Pointer) bool {
func (m *Allocator) IsAllocated(ptr unsafe.Pointer) bool {
m.lock.Lock()
defer m.lock.Unlock()

Expand Down
37 changes: 0 additions & 37 deletions go/boost/common/rowstore/offheap/allocator_go.go

This file was deleted.

31 changes: 0 additions & 31 deletions go/boost/common/rowstore/offheap/allocator_go_test.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build cgo

package offheap

import (
Expand All @@ -14,24 +12,24 @@ func Test_cmallocPanics(t *testing.T) {

tests := []struct {
name string
op func(m *cmalloc)
op func(m *Allocator)
}{
{
name: "leak",
op: func(m *cmalloc) {
op: func(m *Allocator) {
m.alloc(8)
},
},
{
name: "free unallocated",
op: func(m *cmalloc) {
op: func(m *Allocator) {
var i int
m.free(unsafe.Pointer(&i))
},
},
{
name: "double free",
op: func(m *cmalloc) {
op: func(m *Allocator) {
p := m.alloc(8)
m.free(p)
m.free(p)
Expand All @@ -50,7 +48,7 @@ func Test_cmallocPanics(t *testing.T) {
t.Logf("panic: %v", r)
}()

var m cmalloc
var m Allocator
tt.op(&m)
m.EnsureNoLeaks()
})
Expand Down
Loading

0 comments on commit 31ceeee

Please sign in to comment.