Skip to content

Commit 08ff076

Browse files
Add map decorator and simplify type conversion logic
1 parent 83937dc commit 08ff076

4 files changed

Lines changed: 16 additions & 13 deletions

File tree

examples/execve2.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from pythonbpf.decorators import bpf, bpfglobal, section
1+
from pythonbpf.decorators import bpf, map, section, bpfglobal
22
from ctypes import c_void_p, c_int64, c_int32, c_uint64
33
from pythonbpf.helpers import bpf_ktime_get_ns
44
from pythonbpf.maps import HashMap
55

66

77
@bpf
8-
@bpfglobal
9-
def last():
8+
@map
9+
def last() -> HashMap:
1010
return HashMap(key_type=c_uint64, value_type=c_uint64, max_entries=1)
1111

1212

@@ -25,5 +25,9 @@ def hello_again(ctx: c_void_p) -> c_int64:
2525
ts = bpf_ktime_get_ns()
2626
return c_int64(0)
2727

28+
# @bpf
29+
# @bpfglobal
30+
# def LICENSE() -> str:
31+
# return "GPL"
2832

2933
LICENSE = "GPL"

pythonbpf/decorators.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ def bpfglobal(func):
99
func._is_bpfglobal = True
1010
return func
1111

12+
def map(func):
13+
"""Decorator to mark a function as a BPF map."""
14+
func._is_map = True
15+
return func
1216

1317
def section(name: str):
1418
def wrapper(fn):

pythonbpf/functions_pass.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,11 @@ def process_bpf_chunk(func_node, module, return_type):
9090
def create_bpf_map(module, map_name, map_params):
9191
"""Create a BPF map in the module with the given parameters"""
9292

93-
type_mapping = {
94-
'c_uint32': ir.IntType(32),
95-
'c_uint64': ir.IntType(64),
96-
'c_int32': ir.IntType(32),
97-
'c_int64': ir.IntType(64),
98-
# Add more mappings as needed
99-
}
100-
10193
key_type_str = map_params.get('key_type', 'c_uint32')
10294
value_type_str = map_params.get('value_type', 'c_uint32')
10395

104-
key_type = type_mapping.get(key_type_str, ir.IntType(32))
105-
value_type = type_mapping.get(value_type_str, ir.IntType(32))
96+
key_type = ctypes_to_ir(key_type_str)
97+
value_type = ctypes_to_ir(value_type_str)
10698

10799
map_struct_type = ir.LiteralStructType([
108100
ir.PointerType(), # type

pythonbpf/globals_pass.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,8 @@ def globals_processing(tree, module: ir.Module):
4242

4343
elif isinstance(dec, ast.Name) and dec.id == "bpfglobal":
4444
collected.append(node.name)
45+
46+
elif isinstance(dec, ast.Name) and dec.id == "map":
47+
collected.append(node.name)
4548

4649
emit_globals(module, collected)

0 commit comments

Comments
 (0)