Skip to content

Commit

Permalink
internal: replace internal memoize with sync.OnceValues
Browse files Browse the repository at this point in the history
Signed-off-by: kwakubiney <kebiney@hotmail.com>
  • Loading branch information
kwakubiney committed Nov 28, 2023
1 parent c079f51 commit 3f2d0b4
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 35 deletions.
3 changes: 2 additions & 1 deletion btf/btf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"os"
"sync"
"testing"

"github.com/cilium/ebpf/internal"
Expand Down Expand Up @@ -36,7 +37,7 @@ type specAndRawBTF struct {
spec *Spec
}

var vmlinuxTestdata = internal.Memoize(func() (specAndRawBTF, error) {
var vmlinuxTestdata = sync.OnceValues(func() (specAndRawBTF, error) {
b, err := internal.ReadAllCompressed("testdata/vmlinux.btf.gz")
if err != nil {
return specAndRawBTF{}, err
Expand Down
5 changes: 2 additions & 3 deletions cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"fmt"
"os"
"strings"

"github.com/cilium/ebpf/internal"
"sync"
)

var possibleCPU = internal.Memoize(func() (int, error) {
var possibleCPU = sync.OnceValues(func() (int, error) {
return parseCPUsFromFile("/sys/devices/system/cpu/possible")
})

Expand Down
26 changes: 0 additions & 26 deletions internal/memoize.go

This file was deleted.

4 changes: 2 additions & 2 deletions internal/testutils/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"errors"
"os"
"strings"
"sync"
"testing"

"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/unix"
)

var cgroup2Path = internal.Memoize(func() (string, error) {
var cgroup2Path = sync.OnceValues(func() (string, error) {
mounts, err := os.ReadFile("/proc/mounts")
if err != nil {
return "", err
Expand Down
3 changes: 2 additions & 1 deletion internal/tracefs/kprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"runtime"
"strings"
"sync"
"syscall"

"github.com/cilium/ebpf/internal"
Expand Down Expand Up @@ -110,7 +111,7 @@ func sanitizeTracefsPath(path ...string) (string, error) {
// Since kernel 4.1 tracefs should be mounted by default at /sys/kernel/tracing,
// but may be also be available at /sys/kernel/debug/tracing if debugfs is mounted.
// The available tracefs paths will depends on distribution choices.
var getTracefsPath = internal.Memoize(func() (string, error) {
var getTracefsPath = sync.OnceValues(func() (string, error) {
for _, p := range []struct {
path string
fsType int64
Expand Down
3 changes: 2 additions & 1 deletion internal/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package internal

import (
"fmt"
"sync"

"github.com/cilium/ebpf/internal/unix"
)
Expand Down Expand Up @@ -79,7 +80,7 @@ func (v Version) Kernel() uint32 {
}

// KernelVersion returns the version of the currently running kernel.
var KernelVersion = Memoize(func() (Version, error) {
var KernelVersion = sync.OnceValues(func() (Version, error) {

Check failure on line 83 in internal/version.go

View workflow job for this annotation

GitHub Actions / Run tests on previous stable Go

undefined: sync.OnceValues

Check failure on line 83 in internal/version.go

View workflow job for this annotation

GitHub Actions / Run tests on previous stable Go

undefined: sync.OnceValues

Check failure on line 83 in internal/version.go

View workflow job for this annotation

GitHub Actions / Run tests on pre-built kernel (6.1)

undefined: sync.OnceValues

Check failure on line 83 in internal/version.go

View workflow job for this annotation

GitHub Actions / Run tests on pre-built kernel (6.1)

undefined: sync.OnceValues

Check failure on line 83 in internal/version.go

View workflow job for this annotation

GitHub Actions / Run tests on pre-built kernel (5.4)

undefined: sync.OnceValues

Check failure on line 83 in internal/version.go

View workflow job for this annotation

GitHub Actions / Run tests on pre-built kernel (5.4)

undefined: sync.OnceValues

Check failure on line 83 in internal/version.go

View workflow job for this annotation

GitHub Actions / Run tests on pre-built kernel (4.14)

undefined: sync.OnceValues

Check failure on line 83 in internal/version.go

View workflow job for this annotation

GitHub Actions / Run tests on pre-built kernel (4.14)

undefined: sync.OnceValues

Check failure on line 83 in internal/version.go

View workflow job for this annotation

GitHub Actions / Run tests on pre-built kernel (4.9)

undefined: sync.OnceValues

Check failure on line 83 in internal/version.go

View workflow job for this annotation

GitHub Actions / Run tests on pre-built kernel (4.9)

undefined: sync.OnceValues
return detectKernelVersion()
})

Expand Down
3 changes: 2 additions & 1 deletion map.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"reflect"
"strings"
"sync"
"time"
"unsafe"

Expand Down Expand Up @@ -907,7 +908,7 @@ func (m *Map) nextKey(key interface{}, nextKeyOut sys.Pointer) error {
return nil
}

var mmapProtectedPage = internal.Memoize(func() ([]byte, error) {
var mmapProtectedPage = sync.OnceValues(func() ([]byte, error) {
return unix.Mmap(-1, 0, os.Getpagesize(), unix.PROT_NONE, unix.MAP_ANON|unix.MAP_SHARED)
})

Expand Down

0 comments on commit 3f2d0b4

Please sign in to comment.