Add struct example and decorator

This commit is contained in:
Pragyansh Chaturvedi
2025-09-21 03:01:13 +05:30
parent d01c7ad8ba
commit 8e231845ef
2 changed files with 16 additions and 1 deletions

View File

@ -1,9 +1,16 @@
from pythonbpf import bpf, map, section, bpfglobal, compile from pythonbpf import bpf, map, struct, section, bpfglobal, compile
from pythonbpf.helpers import ktime, pid from pythonbpf.helpers import ktime, pid
from pythonbpf.maps import PerfEventArray from pythonbpf.maps import PerfEventArray
from ctypes import c_void_p, c_int64, c_int32, c_uint64 from ctypes import c_void_p, c_int64, c_int32, c_uint64
@bpf
@struct
class data_t:
pid: c_uint64
ts: c_uint64
@bpf @bpf
@map @map
def events() -> PerfEventArray: def events() -> PerfEventArray:

View File

@ -9,11 +9,19 @@ def bpfglobal(func):
func._is_bpfglobal = True func._is_bpfglobal = True
return func return func
def map(func): def map(func):
"""Decorator to mark a function as a BPF map.""" """Decorator to mark a function as a BPF map."""
func._is_map = True func._is_map = True
return func return func
def struct(cls):
"""Decorator to mark a class as a BPF struct."""
cls._is_struct = True
return cls
def section(name: str): def section(name: str):
def wrapper(fn): def wrapper(fn):
fn._section = name fn._section = name