-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
128 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2024-present Datadog, Inc. | ||
|
||
//go:build linux | ||
|
||
package ebpfless | ||
|
||
// WriteMapWithSizeLimit updates a map via m[key] = val. | ||
// However, if the map would overflow sizeLimit, it returns false instead. | ||
func WriteMapWithSizeLimit[Key comparable, Val any](m map[Key]Val, key Key, val Val, sizeLimit int) bool { | ||
_, exists := m[key] | ||
if !exists && len(m) >= sizeLimit { | ||
return false | ||
} | ||
m[key] = val | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2024-present Datadog, Inc. | ||
|
||
//go:build linux | ||
|
||
package ebpfless | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestWriteMapWithSizeLimit(t *testing.T) { | ||
m := map[string]int{} | ||
|
||
// not full: any write should work | ||
ok := WriteMapWithSizeLimit(m, "foo", 123, 1) | ||
require.True(t, ok) | ||
|
||
expectedFoo := map[string]int{ | ||
"foo": 123, | ||
} | ||
require.Equal(t, expectedFoo, m) | ||
|
||
// full: shouldn't write a new key | ||
ok = WriteMapWithSizeLimit(m, "bar", 456, 1) | ||
require.False(t, ok) | ||
require.Equal(t, expectedFoo, m) | ||
|
||
// full: replacing key should still work | ||
ok = WriteMapWithSizeLimit(m, "foo", 789, 1) | ||
require.True(t, ok) | ||
require.Equal(t, map[string]int{ | ||
"foo": 789, | ||
}, m) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters