Skip to content

Commit

Permalink
filterx-eval: extract context begin/end code as macros
Browse files Browse the repository at this point in the history
Although this code has a single user, it is relatively complex, relies
on arcane mechanics of FilterXScope and FilterXEvalContext. Hide it
and delegate it to show where it belongs.

These have become macros, as we need to use the caller's stack frame to
allocate the scope.


Signed-off-by: Balazs Scheidler <balazs.scheidler@axoflow.com>
  • Loading branch information
bazsi committed Jan 24, 2025
1 parent eb2b716 commit 0138d38
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 53 deletions.
25 changes: 25 additions & 0 deletions lib/filterx/filterx-eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,30 @@ filterx_eval_store_weak_ref(FilterXObject *object)
}
}

#define FILTERX_EVAL_BEGIN_CONTEXT(eval_context, previous_context) \
do { \
FilterXScope *scope = NULL; \
gboolean local_scope = FALSE; \
\
if (previous_context) \
scope = filterx_scope_reuse(previous_context->scope); \
\
if (!scope) \
{ \
gsize alloc_size = filterx_scope_get_alloc_size(); \
scope = g_alloca(alloc_size); \
filterx_scope_init_instance(scope, alloc_size, path_options->filterx_context ? path_options->filterx_context->scope : NULL); \
local_scope = TRUE; \
} \
filterx_eval_init_context(&eval_context, path_options->filterx_context, scope, msg); \
do


#define FILTERX_EVAL_END_CONTEXT(eval_context) \
while(0); \
filterx_eval_deinit_context(&eval_context); \
if (local_scope) \
filterx_scope_clear(scope); \
} while(0)

#endif
94 changes: 41 additions & 53 deletions lib/filterx/filterx-pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,63 +64,51 @@ log_filterx_pipe_queue(LogPipe *s, LogMessage *msg, const LogPathOptions *path_o
{
LogFilterXPipe *self = (LogFilterXPipe *) s;
FilterXEvalContext eval_context;
LogPathOptions local_path_options;
FilterXEvalResult eval_res;
gboolean local_scope = FALSE;

path_options = log_path_options_chain(&local_path_options, path_options);

FilterXScope *scope = NULL;
if (path_options->filterx_context)
scope = filterx_scope_reuse(path_options->filterx_context->scope);

if (!scope)
{
gsize alloc_size = filterx_scope_get_alloc_size();
scope = g_alloca(alloc_size);
filterx_scope_init_instance(scope, alloc_size, path_options->filterx_context ? path_options->filterx_context->scope : NULL);
local_scope = TRUE;
}
filterx_eval_init_context(&eval_context, path_options->filterx_context, scope, msg);

msg_trace(">>>>>> filterx rule evaluation begin",
evt_tag_str("rule", self->name),
log_pipe_location_tag(s),
evt_tag_msg_reference(msg));
FilterXEvalContext *previous_context = path_options->filterx_context;

NVTable *payload = nv_table_ref(msg->payload);
eval_res = filterx_eval_exec(&eval_context, self->block);

msg_trace("<<<<<< filterx rule evaluation result",
filterx_format_eval_result(eval_res),
evt_tag_str("rule", self->name),
log_pipe_location_tag(s),
evt_tag_int("dirty", filterx_scope_is_dirty(eval_context.scope)),
evt_tag_msg_reference(msg));

local_path_options.filterx_context = &eval_context;
switch (eval_res)
FILTERX_EVAL_BEGIN_CONTEXT(eval_context, previous_context)
{
case FXE_SUCCESS:
log_pipe_forward_msg(s, msg, path_options);
break;

case FXE_FAILURE:
if (path_options->matched)
(*path_options->matched) = FALSE;
/* FALLTHROUGH */
case FXE_DROP:
log_msg_drop(msg, path_options, AT_PROCESSED);
break;

default:
g_assert_not_reached();
break;
FilterXEvalResult eval_res;
LogPathOptions local_path_options;

path_options = log_path_options_chain(&local_path_options, path_options);

msg_trace(">>>>>> filterx rule evaluation begin",
evt_tag_str("rule", self->name),
log_pipe_location_tag(s),
evt_tag_msg_reference(msg));

eval_res = filterx_eval_exec(&eval_context, self->block);

msg_trace("<<<<<< filterx rule evaluation result",
filterx_format_eval_result(eval_res),
evt_tag_str("rule", self->name),
log_pipe_location_tag(s),
evt_tag_int("dirty", filterx_scope_is_dirty(eval_context.scope)),
evt_tag_msg_reference(msg));

local_path_options.filterx_context = &eval_context;
switch (eval_res)
{
case FXE_SUCCESS:
log_pipe_forward_msg(s, msg, path_options);
break;

case FXE_FAILURE:
if (path_options->matched)
(*path_options->matched) = FALSE;
/* FALLTHROUGH */
case FXE_DROP:
log_msg_drop(msg, path_options, AT_PROCESSED);
break;

default:
g_assert_not_reached();
break;
}
}

filterx_eval_deinit_context(&eval_context);
if (local_scope)
filterx_scope_clear(scope);
FILTERX_EVAL_END_CONTEXT(eval_context);
nv_table_unref(payload);
}

Expand Down

0 comments on commit 0138d38

Please sign in to comment.