Skip to content

Commit

Permalink
feat(map): make CalcMapValueSize a public helper
Browse files Browse the repository at this point in the history
Signed-off-by: Geyslan Gregório <[email protected]>
  • Loading branch information
geyslan committed Apr 6, 2024
1 parent d1d119c commit 7371781
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
8 changes: 2 additions & 6 deletions map-common.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,9 @@ func GetMapInfoByFD(fd int) (*BPFMapInfo, error) {
}, nil
}

//
// Map misc internal
//

// calcMapValueSize calculates the size of the value for a map.
// CalcMapValueSize calculates the size of the value for a map.
// For per-CPU maps, it is calculated based on the number of possible CPUs.
func calcMapValueSize(valueSize int, mapType MapType) (int, error) {
func CalcMapValueSize(valueSize int, mapType MapType) (int, error) {
if valueSize <= 0 {
return 0, fmt.Errorf("value size must be greater than 0")
}
Expand Down
10 changes: 5 additions & 5 deletions map-low.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (m *BPFMapLow) GetValue(key unsafe.Pointer) ([]byte, error) {
}

func (m *BPFMapLow) GetValueFlags(key unsafe.Pointer, flags MapFlag) ([]byte, error) {
valueSize, err := calcMapValueSize(m.ValueSize(), m.Type())
valueSize, err := CalcMapValueSize(m.ValueSize(), m.Type())
if err != nil {
return nil, fmt.Errorf("map %s %w", m.Name(), err)
}
Expand Down Expand Up @@ -314,7 +314,7 @@ func (m *BPFMapLow) LookupAndDeleteElemFlags(
}

func (m *BPFMapLow) GetValueAndDeleteKey(key unsafe.Pointer) ([]byte, error) {
valueSize, err := calcMapValueSize(m.ValueSize(), m.Type())
valueSize, err := CalcMapValueSize(m.ValueSize(), m.Type())
if err != nil {
return nil, fmt.Errorf("map %s %w", m.Name(), err)
}
Expand All @@ -332,7 +332,7 @@ func (m *BPFMapLow) GetValueAndDeleteKey(key unsafe.Pointer) ([]byte, error) {
}

func (m *BPFMapLow) GetValueAndDeleteKeyFlags(key unsafe.Pointer, flags MapFlag) ([]byte, error) {
valueSize, err := calcMapValueSize(m.ValueSize(), m.Type())
valueSize, err := CalcMapValueSize(m.ValueSize(), m.Type())
if err != nil {
return nil, fmt.Errorf("map %s %w", m.Name(), err)
}
Expand Down Expand Up @@ -397,7 +397,7 @@ func (m *BPFMapLow) GetNextKey(key unsafe.Pointer, nextKey unsafe.Pointer) error
// GetValueBatch gets the values with the given keys from the map.
// It returns the values and the number of read elements.
func (m *BPFMapLow) GetValueBatch(keys, startKey, nextKey unsafe.Pointer, count uint32) ([][]byte, uint32, error) {
valueSize, err := calcMapValueSize(m.ValueSize(), m.Type())
valueSize, err := CalcMapValueSize(m.ValueSize(), m.Type())
if err != nil {
return nil, 0, fmt.Errorf("map %s %w", m.Name(), err)
}
Expand Down Expand Up @@ -444,7 +444,7 @@ func (m *BPFMapLow) GetValueBatch(keys, startKey, nextKey unsafe.Pointer, count
// deletes them.
// It returns the values and the number of deleted elements.
func (m *BPFMapLow) GetValueAndDeleteBatch(keys, startKey, nextKey unsafe.Pointer, count uint32) ([][]byte, uint32, error) {
valueSize, err := calcMapValueSize(m.ValueSize(), m.Type())
valueSize, err := CalcMapValueSize(m.ValueSize(), m.Type())
if err != nil {
return nil, 0, fmt.Errorf("map %s %w", m.Name(), err)
}
Expand Down
10 changes: 5 additions & 5 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (m *BPFMap) MapExtra() uint64 {
// }

func (m *BPFMap) InitialValue() ([]byte, error) {
valueSize, err := calcMapValueSize(m.ValueSize(), m.Type())
valueSize, err := CalcMapValueSize(m.ValueSize(), m.Type())
if err != nil {
return nil, fmt.Errorf("map %s %w", m.Name(), err)
}
Expand All @@ -227,7 +227,7 @@ func (m *BPFMap) InitialValue() ([]byte, error) {
}

func (m *BPFMap) SetInitialValue(value unsafe.Pointer) error {
valueSize, err := calcMapValueSize(m.ValueSize(), m.Type())
valueSize, err := CalcMapValueSize(m.ValueSize(), m.Type())
if err != nil {
return fmt.Errorf("map %s %w", m.Name(), err)
}
Expand Down Expand Up @@ -384,7 +384,7 @@ func (m *BPFMap) GetValue(key unsafe.Pointer) ([]byte, error) {
}

func (m *BPFMap) GetValueFlags(key unsafe.Pointer, flags MapFlag) ([]byte, error) {
valueSize, err := calcMapValueSize(m.ValueSize(), m.Type())
valueSize, err := CalcMapValueSize(m.ValueSize(), m.Type())
if err != nil {
return nil, fmt.Errorf("map %s %w", m.Name(), err)
}
Expand Down Expand Up @@ -439,7 +439,7 @@ func (m *BPFMap) GetValueAndDeleteKey(key unsafe.Pointer) ([]byte, error) {
// and delete the key in the BPFMap, with the specified flags.
// It returns the value as a slice of bytes.
func (m *BPFMap) GetValueAndDeleteKeyFlags(key unsafe.Pointer, flags MapFlag) ([]byte, error) {
valueSize, err := calcMapValueSize(m.ValueSize(), m.Type())
valueSize, err := CalcMapValueSize(m.ValueSize(), m.Type())
if err != nil {
return nil, fmt.Errorf("map %s %w", m.Name(), err)
}
Expand Down Expand Up @@ -486,7 +486,7 @@ func (m *BPFMap) Update(key, value unsafe.Pointer) error {
}

func (m *BPFMap) UpdateValueFlags(key, value unsafe.Pointer, flags MapFlag) error {
valueSize, err := calcMapValueSize(m.ValueSize(), m.Type())
valueSize, err := CalcMapValueSize(m.ValueSize(), m.Type())
if err != nil {
return fmt.Errorf("map %s %w", m.Name(), err)
}
Expand Down

0 comments on commit 7371781

Please sign in to comment.