Skip to content

Commit 644fc57

Browse files
kwakubineylmb
authored andcommitted
internal: replace internal memoize with sync.OnceValues
Signed-off-by: kwakubiney <[email protected]>
1 parent 77874d8 commit 644fc57

File tree

7 files changed

+12
-35
lines changed

7 files changed

+12
-35
lines changed

btf/btf_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io"
99
"os"
10+
"sync"
1011
"testing"
1112

1213
"github.com/go-quicktest/qt"
@@ -37,7 +38,7 @@ type specAndRawBTF struct {
3738
spec *Spec
3839
}
3940

40-
var vmlinuxTestdata = internal.Memoize(func() (specAndRawBTF, error) {
41+
var vmlinuxTestdata = sync.OnceValues(func() (specAndRawBTF, error) {
4142
b, err := internal.ReadAllCompressed("testdata/vmlinux.btf.gz")
4243
if err != nil {
4344
return specAndRawBTF{}, err

cpu.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import (
44
"fmt"
55
"os"
66
"strings"
7-
8-
"github.com/cilium/ebpf/internal"
7+
"sync"
98
)
109

11-
var possibleCPU = internal.Memoize(func() (int, error) {
10+
var possibleCPU = sync.OnceValues(func() (int, error) {
1211
return parseCPUsFromFile("/sys/devices/system/cpu/possible")
1312
})
1413

internal/memoize.go

-26
This file was deleted.

internal/testutils/cgroup.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"errors"
55
"os"
66
"strings"
7+
"sync"
78
"testing"
89

9-
"github.com/cilium/ebpf/internal"
1010
"github.com/cilium/ebpf/internal/unix"
1111
)
1212

13-
var cgroup2Path = internal.Memoize(func() (string, error) {
13+
var cgroup2Path = sync.OnceValues(func() (string, error) {
1414
mounts, err := os.ReadFile("/proc/mounts")
1515
if err != nil {
1616
return "", err

internal/tracefs/kprobe.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"runtime"
1010
"strings"
11+
"sync"
1112
"syscall"
1213

1314
"github.com/cilium/ebpf/internal"
@@ -110,7 +111,7 @@ func sanitizeTracefsPath(path ...string) (string, error) {
110111
// Since kernel 4.1 tracefs should be mounted by default at /sys/kernel/tracing,
111112
// but may be also be available at /sys/kernel/debug/tracing if debugfs is mounted.
112113
// The available tracefs paths will depends on distribution choices.
113-
var getTracefsPath = internal.Memoize(func() (string, error) {
114+
var getTracefsPath = sync.OnceValues(func() (string, error) {
114115
for _, p := range []struct {
115116
path string
116117
fsType int64

internal/version.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package internal
22

33
import (
44
"fmt"
5+
"sync"
56

67
"github.com/cilium/ebpf/internal/unix"
78
)
@@ -79,7 +80,7 @@ func (v Version) Kernel() uint32 {
7980
}
8081

8182
// KernelVersion returns the version of the currently running kernel.
82-
var KernelVersion = Memoize(func() (Version, error) {
83+
var KernelVersion = sync.OnceValues(func() (Version, error) {
8384
return detectKernelVersion()
8485
})
8586

map.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"path/filepath"
1111
"reflect"
1212
"strings"
13+
"sync"
1314
"time"
1415
"unsafe"
1516

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

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

0 commit comments

Comments
 (0)