Skip to content

Commit 2e005f6

Browse files
committed
Create MapProcessorRegistry to add more maps
1 parent cbc6b93 commit 2e005f6

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

pythonbpf/maps/maps_pass.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pythonbpf.type_deducer import ctypes_to_ir
44
from pythonbpf import dwarf_constants as dc
55
from enum import Enum
6+
from .maps_utils import MapProcessorRegistry
67

78
map_sym_tab = {}
89

@@ -183,6 +184,7 @@ def create_map_debug_info(module, map_global, map_name, map_params):
183184
return global_var_expr
184185

185186

187+
@MapProcessorRegistry.register("HashMap")
186188
def process_hash_map(map_name, rval, module):
187189
print(f"Creating HashMap map: {map_name}")
188190
map_params = {"type": BPFMapType.HASH}
@@ -211,6 +213,7 @@ def process_hash_map(map_name, rval, module):
211213
return create_bpf_map(module, map_name, map_params)
212214

213215

216+
@MapProcessorRegistry.register("PerfEventArray")
214217
def process_perf_event_map(map_name, rval, module):
215218
print(f"Creating PerfEventArray map: {map_name}")
216219
map_params = {"type": BPFMapType.PERF_EVENT_ARRAY}
@@ -235,10 +238,6 @@ def process_bpf_map(func_node, module):
235238
map_name = func_node.name
236239
print(f"Processing BPF map: {map_name}")
237240

238-
BPF_MAP_TYPES = {"HashMap": process_hash_map, # BPF_MAP_TYPE_HASH
239-
"PerfEventArray": process_perf_event_map, # BPF_MAP_TYPE_PERF_EVENT_ARRAY
240-
}
241-
242241
# For now, assume single return statement
243242
return_stmt = None
244243
for stmt in func_node.body:
@@ -252,8 +251,8 @@ def process_bpf_map(func_node, module):
252251

253252
# Handle only HashMap maps
254253
if isinstance(rval, ast.Call) and isinstance(rval.func, ast.Name):
255-
if rval.func.id in BPF_MAP_TYPES:
256-
handler = BPF_MAP_TYPES[rval.func.id]
254+
handler = MapProcessorRegistry.get(rval.func.id)
255+
if handler:
257256
handler(map_name, rval, module)
258257
else:
259258
print(f"Unknown map type {rval.func.id}, defaulting to HashMap")

pythonbpf/maps/maps_utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class MapProcessorRegistry:
2+
"""Registry for map processor functions"""
3+
_processors = {}
4+
5+
@classmethod
6+
def register(cls, map_type_name):
7+
"""Decorator to register a processor function for a map type"""
8+
def decorator(func):
9+
cls._processors[map_type_name] = func
10+
return func
11+
return decorator
12+
13+
@classmethod
14+
def get_processor(cls, map_type_name):
15+
"""Get the processor function for a map type"""
16+
return cls._processors.get(map_type_name)

0 commit comments

Comments
 (0)