[apparmor] [PATCH 9/9] Rework the code so that update for nodes is now is now a function

John Johansen john.johansen at canonical.com
Wed Nov 10 22:02:30 GMT 2010


The other changes have made it so that using a macro really isn't justified
so rework the code to get rid of the hiddeous update_for_nodes macro.

Signed-off-by: John Johansen <john.johansen at canonical.com>
---
 parser/libapparmor_re/regexp.y |   59 ++++++++++++++++++++++-----------------
 1 files changed, 33 insertions(+), 26 deletions(-)

diff --git a/parser/libapparmor_re/regexp.y b/parser/libapparmor_re/regexp.y
index bf11733..70348c1 100644
--- a/parser/libapparmor_re/regexp.y
+++ b/parser/libapparmor_re/regexp.y
@@ -1449,6 +1449,8 @@ class DFA {
     void dump_node_to_dfa(void);
     State* add_new_state(NodeMap &nodemap, pair <unsigned long, NodeSet *> index, NodeSet *nodes, dfa_stats_t &stats);
     void update_state_transitions(NodeMap &nodemap, list <State *> &work_queue, State *state, dfa_stats_t &stats);
+    State *find_target_state(NodeMap &nodemap, list <State *> &work_queue,
+			     NodeSet *nodes, dfa_stats_t &stats);
 public:
     DFA(Node *root, dfaflags_t flags);
     virtual ~DFA();
@@ -1477,26 +1479,30 @@ State* DFA::add_new_state(NodeMap &nodemap, pair <unsigned long, NodeSet *> inde
 	return state;
 }
 
-/* macro to help out with DFA creation, not done as inlined fn as nearly
- * every line uses a different map or variable that would have to be passed
- */
-#define update_for_nodes(NODES, TARGET)	\
-do { \
-	pair <unsigned long, NodeSet *> index = make_pair(hash_NodeSet(NODES), NODES); \
-	map<pair <unsigned long, NodeSet *>, State *, deref_less_than>::iterator x = nodemap.find(index); \
-	if (x == nodemap.end()) { \
-		/* set of nodes isn't known so create new state, and nodes to \
-		 * state mapping \
-		 */ \
-		(TARGET) = add_new_state(nodemap, index, (NODES), stats); \
-		work_queue.push_back(TARGET);	  \
-	} else { \
-		/* set of nodes already has a mapping so free this one */ \
-		stats.duplicates++; \
-		delete (NODES);	    \
-		TARGET = x->second; \
-	} \
-} while (0)
+State *DFA::find_target_state(NodeMap &nodemap, list <State *> &work_queue,
+			      NodeSet *nodes, dfa_stats_t &stats)
+{
+	State *target;
+
+	pair <unsigned long, NodeSet *> index = make_pair(hash_NodeSet(nodes), nodes);
+
+	map<pair <unsigned long, NodeSet *>, State *, deref_less_than>::iterator x = nodemap.find(index);
+
+	if (x == nodemap.end()) {
+		/* set of nodes isn't known so create new state, and nodes to
+		 * state mapping
+		 */
+		target = add_new_state(nodemap, index, nodes, stats);
+		work_queue.push_back(target);
+	} else {
+		/* set of nodes already has a mapping so free this one */
+		stats.duplicates++;
+		delete (nodes);
+		target = x->second;
+	}
+
+	return target;
+}
 
 void DFA::update_state_transitions(NodeMap &nodemap,
 				   list <State *> &work_queue, State *state,
@@ -1519,18 +1525,19 @@ void DFA::update_state_transitions(NodeMap &nodemap,
 	 */
 
 	/* check the default transition first */
-	if (cases.otherwise) {
-		State *target;
-		update_for_nodes(cases.otherwise, target);
-		state->cases.otherwise = target;
-	}
+	if (cases.otherwise)
+		state->cases.otherwise = find_target_state(nodemap, work_queue,
+							   cases.otherwise,
+							   stats);;
 
 	/* For each transition from *from, check if the set of nodes it
 	 * transitions to already has been mapped to a state
 	 */
 	for (NodeCases::iterator j = cases.begin(); j != cases.end(); j++) {
 		State *target;
-		update_for_nodes(j->second, target);
+		target = find_target_state(nodemap, work_queue, j->second,
+					   stats);
+
 		/* Don't insert transition that the default transition
 		 * already covers
 		 */
-- 
1.7.1




More information about the AppArmor mailing list