+ minimask_destroy(&subtable->mask);
+ hmap_remove(&cls->subtables, &subtable->hmap_node);
+ hmap_destroy(&subtable->rules);
+ list_remove(&subtable->list_node);
+ free(subtable);
+}
+
+/* This function performs the following updates for 'subtable' in 'cls'
+ * following the addition of a new rule with priority 'new_priority' to
+ * 'subtable':
+ *
+ * - Update 'subtable->max_priority' and 'subtable->max_count' if necessary.
+ *
+ * - Update 'subtable''s position in 'cls->subtables_priority' if necessary.
+ *
+ * This function should only be called after adding a new rule, not after
+ * replacing a rule by an identical one or modifying a rule in-place. */
+static void
+update_subtables_after_insertion(struct classifier *cls,
+ struct cls_subtable *subtable,
+ unsigned int new_priority)
+{
+ if (new_priority == subtable->max_priority) {
+ ++subtable->max_count;
+ } else if (new_priority > subtable->max_priority) {
+ struct cls_subtable *iter;
+
+ subtable->max_priority = new_priority;
+ subtable->max_count = 1;
+
+ /* Possibly move 'subtable' earlier in the priority list. If we break
+ * out of the loop, then 'subtable' should be moved just after that
+ * 'iter'. If the loop terminates normally, then 'iter' will be the
+ * list head and we'll move subtable just after that (e.g. to the front
+ * of the list). */
+ iter = subtable;
+ LIST_FOR_EACH_REVERSE_CONTINUE (iter, list_node,
+ &cls->subtables_priority) {
+ if (iter->max_priority >= subtable->max_priority) {
+ break;
+ }
+ }
+
+ /* Move 'subtable' just after 'iter' (unless it's already there). */
+ if (iter->list_node.next != &subtable->list_node) {
+ list_splice(iter->list_node.next,
+ &subtable->list_node, subtable->list_node.next);
+ }
+ }
+}
+
+/* This function performs the following updates for 'subtable' in 'cls'
+ * following the deletion of a rule with priority 'del_priority' from
+ * 'subtable':
+ *
+ * - Update 'subtable->max_priority' and 'subtable->max_count' if necessary.
+ *
+ * - Update 'subtable''s position in 'cls->subtables_priority' if necessary.
+ *
+ * This function should only be called after removing a rule, not after
+ * replacing a rule by an identical one or modifying a rule in-place. */
+static void
+update_subtables_after_removal(struct classifier *cls,
+ struct cls_subtable *subtable,
+ unsigned int del_priority)
+{
+ struct cls_subtable *iter;
+
+ if (del_priority == subtable->max_priority && --subtable->max_count == 0) {
+ struct cls_rule *head;
+
+ subtable->max_priority = 0;
+ HMAP_FOR_EACH (head, hmap_node, &subtable->rules) {
+ if (head->priority > subtable->max_priority) {
+ subtable->max_priority = head->priority;
+ subtable->max_count = 1;
+ } else if (head->priority == subtable->max_priority) {
+ ++subtable->max_count;
+ }
+ }
+
+ /* Possibly move 'subtable' later in the priority list. If we break
+ * out of the loop, then 'subtable' should be moved just before that
+ * 'iter'. If the loop terminates normally, then 'iter' will be the
+ * list head and we'll move subtable just before that (e.g. to the back
+ * of the list). */
+ iter = subtable;
+ LIST_FOR_EACH_CONTINUE (iter, list_node, &cls->subtables_priority) {
+ if (iter->max_priority <= subtable->max_priority) {
+ break;
+ }
+ }
+
+ /* Move 'subtable' just before 'iter' (unless it's already there). */
+ if (iter->list_node.prev != &subtable->list_node) {
+ list_splice(&iter->list_node,
+ &subtable->list_node, subtable->list_node.next);
+ }
+ }