Skip to content

Commit 1d2a2ae

Browse files
committed
btf: move Builder.EnableDeduplication() to new btf.BuilderOptions
This commit adds an extra argument to NewBuilder to allow configuring the Builder during instantiation. This makes enabling duplication a bit more straightforward and harder to misuse. Signed-off-by: Timo Beckers <timo@isovalent.com>
1 parent a868488 commit 1d2a2ae

5 files changed

Lines changed: 24 additions & 27 deletions

File tree

btf/btf_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ func TestLoadSpecFromElf(t *testing.T) {
304304
}
305305

306306
func TestVerifierError(t *testing.T) {
307-
b, err := NewBuilder([]Type{&Int{Encoding: 255}})
307+
b, err := NewBuilder([]Type{&Int{Encoding: 255}}, nil)
308308
qt.Assert(t, qt.IsNil(err))
309309
_, err = NewHandle(b)
310310
testutils.SkipIfNotSupported(t, err)
@@ -391,7 +391,7 @@ func ExampleSpec_TypeByName() {
391391
func TestTypesIterator(t *testing.T) {
392392
types := []Type{(*Void)(nil), &Int{Size: 4}, &Int{Size: 2}}
393393

394-
b, err := NewBuilder(types[1:])
394+
b, err := NewBuilder(types[1:], nil)
395395
if err != nil {
396396
t.Fatal(err)
397397
}

btf/feature.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ var haveEnum64 = internal.NewFeatureTest("ENUM64", func() error {
135135
}, "6.0")
136136

137137
func probeBTF(typ Type) error {
138-
b, err := NewBuilder([]Type{typ})
138+
b, err := NewBuilder([]Type{typ}, nil)
139139
if err != nil {
140140
return err
141141
}

btf/marshal.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,32 @@ type Builder struct {
8282
deduper *deduper
8383
}
8484

85+
type BuilderOptions struct {
86+
// Deduplicate enables type deduplication.
87+
Deduplicate bool
88+
}
89+
8590
// NewBuilder creates a Builder from a list of types.
8691
//
8792
// It is more efficient than calling [Add] individually.
8893
//
8994
// Returns an error if adding any of the types fails.
90-
func NewBuilder(types []Type) (*Builder, error) {
95+
func NewBuilder(types []Type, opts *BuilderOptions) (*Builder, error) {
96+
if opts == nil {
97+
opts = &BuilderOptions{}
98+
}
99+
91100
b := &Builder{
92101
make([]Type, 0, len(types)),
93102
make(map[Type]TypeID, len(types)),
94103
nil,
95104
nil,
96105
}
97106

107+
if opts.Deduplicate {
108+
b.deduper = newDeduper()
109+
}
110+
98111
for _, typ := range types {
99112
_, err := b.Add(typ)
100113
if err != nil {
@@ -110,22 +123,6 @@ func (b *Builder) Empty() bool {
110123
return len(b.types) == 0 && (b.strings == nil || b.strings.Length() == 0)
111124
}
112125

113-
// EnableDeduplication enables type deduplication for subsequently added types.
114-
// Type deduplication cannot be enabled after types have been added and cannot
115-
// be disabled once enabled.
116-
func (b *Builder) EnableDeduplication() error {
117-
if b.deduper != nil {
118-
return errors.New("deduplication already enabled")
119-
}
120-
121-
if len(b.stableIDs) > 0 {
122-
return errors.New("cannot enable deduplication after adding types")
123-
}
124-
125-
b.deduper = newDeduper()
126-
return nil
127-
}
128-
129126
// Add a Type and allocate a stable ID for it.
130127
//
131128
// Adding the identical Type multiple times is valid and will return the same ID.

btf/marshal_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestBuilderMarshal(t *testing.T) {
2525
&Typedef{"baz", typ, nil},
2626
}
2727

28-
b, err := NewBuilder(want)
28+
b, err := NewBuilder(want, nil)
2929
qt.Assert(t, qt.IsNil(err))
3030

3131
cpy := *b
@@ -91,7 +91,7 @@ limitTypes:
9191
}
9292
}
9393

94-
b, err := NewBuilder(types)
94+
b, err := NewBuilder(types, nil)
9595
qt.Assert(t, qt.IsNil(err))
9696
buf, err := b.Marshal(nil, KernelMarshalOptions())
9797
qt.Assert(t, qt.IsNil(err))
@@ -120,7 +120,7 @@ func TestMarshalEnum64(t *testing.T) {
120120
},
121121
}
122122

123-
b, err := NewBuilder([]Type{enum})
123+
b, err := NewBuilder([]Type{enum}, nil)
124124
qt.Assert(t, qt.IsNil(err))
125125
buf, err := b.Marshal(nil, &MarshalOptions{
126126
Order: internal.NativeEndian,
@@ -156,7 +156,7 @@ func TestMarshalDeclTags(t *testing.T) {
156156
},
157157
}
158158

159-
b, err := NewBuilder(types)
159+
b, err := NewBuilder(types, nil)
160160
qt.Assert(t, qt.IsNil(err))
161161
buf, err := b.Marshal(nil, &MarshalOptions{
162162
Order: internal.NativeEndian,
@@ -187,7 +187,7 @@ func TestMarshalTypeTags(t *testing.T) {
187187
},
188188
}
189189

190-
b, err := NewBuilder(types)
190+
b, err := NewBuilder(types, nil)
191191
qt.Assert(t, qt.IsNil(err))
192192
buf, err := b.Marshal(nil, &MarshalOptions{
193193
Order: internal.NativeEndian,
@@ -237,7 +237,7 @@ func BenchmarkBuildVmlinux(b *testing.B) {
237237
func marshalNativeEndian(tb testing.TB, types []Type) []byte {
238238
tb.Helper()
239239

240-
b, err := NewBuilder(types)
240+
b, err := NewBuilder(types, nil)
241241
qt.Assert(tb, qt.IsNil(err))
242242
buf, err := b.Marshal(nil, nil)
243243
qt.Assert(tb, qt.IsNil(err))

btf/workarounds_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestDatasecResolveWorkaround(t *testing.T) {
4949
},
5050
}
5151

52-
b, err := NewBuilder([]Type{ds})
52+
b, err := NewBuilder([]Type{ds}, nil)
5353
if err != nil {
5454
t.Fatal(err)
5555
}

0 commit comments

Comments
 (0)