Skip to content

Commit 5dcf670

Browse files
committed
Add ScratchPoolManager and it's singleton
1 parent 6bce29b commit 5dcf670

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

pythonbpf/helper/helper_utils.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,44 @@ def has_handler(cls, helper_name):
3434
return helper_name in cls._handlers
3535

3636

37+
class ScratchPoolManager:
38+
"""Manage the temporary helper variables in local_sym_tab"""
39+
40+
def __init__(self):
41+
self._counter = 0
42+
43+
@property
44+
def counter(self):
45+
return self._counter
46+
47+
def reset(self):
48+
self._counter = 0
49+
logger.debug("Scratch pool counter reset to 0")
50+
51+
def get_next_temp(self, local_sym_tab):
52+
temp_name = f"__helper_temp_{self._counter}"
53+
self._counter += 1
54+
55+
if temp_name not in local_sym_tab:
56+
raise ValueError(
57+
f"Scratch pool exhausted or inadequate: {temp_name}. "
58+
f"Current counter: {self._counter}"
59+
)
60+
61+
62+
_temp_pool_manager = ScratchPoolManager() # Singleton instance
63+
64+
65+
def reset_scratch_pool():
66+
"""Reset the scratch pool counter"""
67+
_temp_pool_manager.reset()
68+
69+
70+
def get_next_scratch_temp(local_sym_tab):
71+
"""Get the next temporary variable name from the scratch pool"""
72+
return _temp_pool_manager.get_next_temp(local_sym_tab)
73+
74+
3775
def get_var_ptr_from_name(var_name, local_sym_tab):
3876
"""Get a pointer to a variable from the symbol table."""
3977
if local_sym_tab and var_name in local_sym_tab:

0 commit comments

Comments
 (0)