classifier: Constify fields.
authorJarno Rajahalme <jrajahalme@nicira.com>
Mon, 27 Oct 2014 17:57:28 +0000 (10:57 -0700)
committerJarno Rajahalme <jrajahalme@nicira.com>
Thu, 6 Nov 2014 23:02:12 +0000 (15:02 -0800)
Some struct cls_match and cls_subtable fields were already documented
of being const.  Make them const and use CONST_CAST where appropriate
to initialize them.

This will help catch future errors modifying those fields after
initialization.

Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
lib/classifier-private.h
lib/classifier.c

index 24d277e..a00c001 100644 (file)
@@ -33,22 +33,22 @@ struct cls_subtable {
     /* The fields are only used by writers. */
     int n_rules OVS_GUARDED;                /* Number of rules, including
                                              * duplicates. */
-    int max_priority OVS_GUARDED; /* Max priority of any rule in subtable. */
+    int max_priority OVS_GUARDED;  /* Max priority of any rule in subtable. */
     unsigned int max_count OVS_GUARDED;     /* Count of max_priority rules. */
 
     /* These fields are accessed by readers who care about wildcarding. */
-    tag_type tag;       /* Tag generated from mask for partitioning (const). */
-    uint8_t n_indices;                   /* How many indices to use (const). */
-    uint8_t index_ofs[CLS_MAX_INDICES];   /* u32 segment boundaries (const). */
+    const tag_type tag;       /* Tag generated from mask for partitioning. */
+    const uint8_t n_indices;                   /* How many indices to use. */
+    const uint8_t index_ofs[CLS_MAX_INDICES];  /* u32 segment boundaries. */
     unsigned int trie_plen[CLS_MAX_TRIES];  /* Trie prefix length in 'mask'
                                              * (runtime configurable). */
-    int ports_mask_len;                     /* (const) */
+    const int ports_mask_len;
     struct cmap indices[CLS_MAX_INDICES];   /* Staged lookup indices. */
     rcu_trie_ptr ports_trie;                /* NULL if none. */
 
     /* These fields are accessed by all readers. */
-    struct cmap rules;                      /* Contains "struct cls_rule"s. */
-    struct minimask mask;                   /* Wildcards for fields (const). */
+    struct cmap rules;                      /* Contains 'cls_match'es. */
+    const struct minimask mask;             /* Wildcards for fields. */
     /* 'mask' must be the last field. */
 };
 
@@ -64,20 +64,20 @@ struct cls_partition {
 
 /* Internal representation of a rule in a "struct cls_subtable". */
 struct cls_match {
-    /* Accessed only by writers and iterators. */
+    /* Accessed by everybody. */
     struct rculist list OVS_GUARDED; /* Identical, lower-priority rules. */
 
     /* Accessed only by writers. */
     struct cls_partition *partition OVS_GUARDED;
 
     /* Accessed by readers interested in wildcarding. */
-    int priority;               /* Larger numbers are higher priorities. */
+    const int priority;         /* Larger numbers are higher priorities. */
     struct cmap_node index_nodes[CLS_MAX_INDICES]; /* Within subtable's
                                                     * 'indices'. */
     /* Accessed by all readers. */
     struct cmap_node cmap_node; /* Within struct cls_subtable 'rules'. */
-    struct cls_rule *cls_rule;
-    struct miniflow flow;       /* Matching rule. Mask is in the subtable. */
+    const struct cls_rule *cls_rule;
+    const struct miniflow flow; /* Matching rule. Mask is in the subtable. */
     /* 'flow' must be the last field. */
 };
 
index 251885d..64d0786 100644 (file)
@@ -44,9 +44,10 @@ cls_match_alloc(struct cls_rule *rule)
         = xmalloc(sizeof *cls_match - sizeof cls_match->flow.inline_values
                   + MINIFLOW_VALUES_SIZE(count));
 
-    cls_match->cls_rule = rule;
-    miniflow_clone_inline(&cls_match->flow, &rule->match.flow, count);
-    cls_match->priority = rule->priority;
+    *CONST_CAST(const struct cls_rule **, &cls_match->cls_rule) = rule;
+    *CONST_CAST(int *, &cls_match->priority) = rule->priority;
+    miniflow_clone_inline(CONST_CAST(struct miniflow *, &cls_match->flow),
+                          &rule->match.flow, count);
     rule->cls_match = cls_match;
 
     return cls_match;
@@ -1027,7 +1028,8 @@ insert_subtable(struct classifier *cls, const struct minimask *mask)
     subtable = xzalloc(sizeof *subtable - sizeof mask->masks.inline_values
                        + MINIFLOW_VALUES_SIZE(count));
     cmap_init(&subtable->rules);
-    miniflow_clone_inline(&subtable->mask.masks, &mask->masks, count);
+    miniflow_clone_inline(CONST_CAST(struct miniflow *, &subtable->mask.masks),
+                          &mask->masks, count);
 
     /* Init indices for segmented lookup, if any. */
     flow_wildcards_init_catchall(&new);
@@ -1039,7 +1041,8 @@ insert_subtable(struct classifier *cls, const struct minimask *mask)
         /* Add an index if it adds mask bits. */
         if (!flow_wildcards_equal(&new, &old)) {
             cmap_init(&subtable->indices[index]);
-            subtable->index_ofs[index] = cls->flow_segments[i];
+            *CONST_CAST(uint8_t *, &subtable->index_ofs[index])
+                = cls->flow_segments[i];
             index++;
             old = new;
         }
@@ -1051,15 +1054,16 @@ insert_subtable(struct classifier *cls, const struct minimask *mask)
         flow_wildcards_fold_minimask_range(&new, mask, prev, FLOW_U32S);
         if (flow_wildcards_equal(&new, &old)) {
             --index;
-            subtable->index_ofs[index] = 0;
+            *CONST_CAST(uint8_t *, &subtable->index_ofs[index]) = 0;
             cmap_destroy(&subtable->indices[index]);
         }
     }
-    subtable->n_indices = index;
+    *CONST_CAST(uint8_t *, &subtable->n_indices) = index;
 
-    subtable->tag = (minimask_get_metadata_mask(mask) == OVS_BE64_MAX
-                     ? tag_create_deterministic(hash)
-                     : TAG_ALL);
+    *CONST_CAST(tag_type *, &subtable->tag) =
+        (minimask_get_metadata_mask(mask) == OVS_BE64_MAX
+         ? tag_create_deterministic(hash)
+         : TAG_ALL);
 
     for (i = 0; i < cls->n_tries; i++) {
         subtable->trie_plen[i] = minimask_get_prefix_len(mask,
@@ -1068,7 +1072,7 @@ insert_subtable(struct classifier *cls, const struct minimask *mask)
 
     /* Ports trie. */
     ovsrcu_set_hidden(&subtable->ports_trie, NULL);
-    subtable->ports_mask_len
+    *CONST_CAST(int *, &subtable->ports_mask_len)
         = 32 - ctz32(ntohl(MINIFLOW_GET_BE32(&mask->masks, tp_src)));
 
     cmap_insert(&cls->subtables_map, &subtable->cmap_node, hash);
@@ -1090,7 +1094,6 @@ destroy_subtable(struct classifier *cls, struct cls_subtable *subtable)
     }
     cmap_remove(&cls->subtables_map, &subtable->cmap_node,
                 minimask_hash(&subtable->mask, 0));
-    minimask_destroy(&subtable->mask);
     cmap_destroy(&subtable->rules);
     ovsrcu_postpone(free, subtable);
 }