/usr/src/openvswitch-1.4.0/lib/sset.c is in openvswitch-datapath-dkms 1.4.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | /*
* Copyright (c) 2011 Nicira Networks.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#include "sset.h"
#include <assert.h>
#include "hash.h"
static uint32_t
hash_name__(const char *name, size_t length)
{
return hash_bytes(name, length, 0);
}
static uint32_t
hash_name(const char *name)
{
return hash_name__(name, strlen(name));
}
static struct sset_node *
sset_find__(const struct sset *set, const char *name, size_t hash)
{
struct sset_node *node;
HMAP_FOR_EACH_WITH_HASH (node, hmap_node, hash, &set->map) {
if (!strcmp(node->name, name)) {
return node;
}
}
return NULL;
}
static struct sset_node *
sset_add__(struct sset *set, const char *name, size_t length, size_t hash)
{
struct sset_node *node = xmalloc(length + sizeof *node);
memcpy(node->name, name, length + 1);
hmap_insert(&set->map, &node->hmap_node, hash);
return node;
}
/* Initializes 'set' as an empty set of strings. */
void
sset_init(struct sset *set)
{
hmap_init(&set->map);
}
/* Destroys 'sets'. */
void
sset_destroy(struct sset *set)
{
if (set) {
sset_clear(set);
hmap_destroy(&set->map);
}
}
/* Initializes 'set' to contain the same strings as 'orig'. */
void
sset_clone(struct sset *set, const struct sset *orig)
{
struct sset_node *node;
sset_init(set);
HMAP_FOR_EACH (node, hmap_node, &orig->map) {
sset_add__(set, node->name, strlen(node->name),
node->hmap_node.hash);
}
}
/* Exchanges the contents of 'a' and 'b'. */
void
sset_swap(struct sset *a, struct sset *b)
{
hmap_swap(&a->map, &b->map);
}
/* Adjusts 'set' so that it is still valid after it has been moved around in
* memory (e.g. due to realloc()). */
void
sset_moved(struct sset *set)
{
hmap_moved(&set->map);
}
/* Returns true if 'set' contains no strings, false if it contains at least one
* string. */
bool
sset_is_empty(const struct sset *set)
{
return hmap_is_empty(&set->map);
}
/* Returns the number of strings in 'set'. */
size_t
sset_count(const struct sset *set)
{
return hmap_count(&set->map);
}
/* Adds 'name' to 'set'. If 'name' is new, returns the new sset_node;
* otherwise (if a copy of 'name' already existed in 'set'), returns NULL. */
struct sset_node *
sset_add(struct sset *set, const char *name)
{
size_t length = strlen(name);
uint32_t hash = hash_name__(name, length);
return (sset_find__(set, name, hash)
? NULL
: sset_add__(set, name, length, hash));
}
/* Adds a copy of 'name' to 'set' and frees 'name'.
*
* If 'name' is new, returns the new sset_node; otherwise (if a copy of 'name'
* already existed in 'set'), returns NULL. */
struct sset_node *
sset_add_and_free(struct sset *set, char *name)
{
struct sset_node *node = sset_add(set, name);
free(name);
return node;
}
/* Adds 'name' to 'set'. Assert-fails if a copy of 'name' was already in
* 'set'. */
void
sset_add_assert(struct sset *set, const char *name)
{
bool added OVS_UNUSED = sset_add(set, name);
assert(added);
}
/* Adds a copy of each of the 'n' names in 'names' to 'set'. */
void
sset_add_array(struct sset *set, char **names, size_t n)
{
size_t i;
for (i = 0; i < n; i++) {
sset_add(set, names[i]);
}
}
/* Removes all of the strings from 'set'. */
void
sset_clear(struct sset *set)
{
const char *name, *next;
SSET_FOR_EACH_SAFE (name, next, set) {
sset_delete(set, SSET_NODE_FROM_NAME(name));
}
}
/* Deletes 'node' from 'set' and frees 'node'. */
void
sset_delete(struct sset *set, struct sset_node *node)
{
hmap_remove(&set->map, &node->hmap_node);
free(node);
}
/* Searches for 'name' in 'set'. If found, deletes it and returns true. If
* not found, returns false without modifying 'set'. */
bool
sset_find_and_delete(struct sset *set, const char *name)
{
struct sset_node *node = sset_find(set, name);
if (node) {
sset_delete(set, node);
}
return node != NULL;
}
/* Searches for 'name' in 'set' and deletes it. Assert-fails if 'name' is not
* in 'set'. */
void
sset_find_and_delete_assert(struct sset *set, const char *name)
{
bool deleted OVS_UNUSED = sset_find_and_delete(set, name);
assert(deleted);
}
/* Removes a string from 'set' and returns a copy of it. The caller must free
* the returned string (with free()).
*
* 'set' must not be empty.
*
* This is not a very good way to iterate through an sset: it copies each name
* and it takes O(n**2) time to remove all the names. Use SSET_FOR_EACH_SAFE
* instead, if you can. */
char *
sset_pop(struct sset *set)
{
const char *name = SSET_FIRST(set);
char *copy = xstrdup(name);
sset_delete(set, SSET_NODE_FROM_NAME(name));
return copy;
}
/* Searches for 'name' in 'set'. Returns its node, if found, otherwise a null
* pointer. */
struct sset_node *
sset_find(const struct sset *set, const char *name)
{
return sset_find__(set, name, hash_name(name));
}
/* Returns true if 'set' contains a copy of 'name', false otherwise. */
bool
sset_contains(const struct sset *set, const char *name)
{
return sset_find(set, name) != NULL;
}
/* Returns true if 'a' and 'b' contain the same strings, false otherwise. */
bool
sset_equals(const struct sset *a, const struct sset *b)
{
struct sset_node *node;
if (sset_count(a) != sset_count(b)) {
return false;
}
HMAP_FOR_EACH (node, hmap_node, &a->map) {
if (!sset_find__(b, node->name, node->hmap_node.hash)) {
return false;
}
}
return true;
}
|