2 * Copyright 2008, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * A hash table for pthreads library
41 static uint32_t hash_get_bucket_id(struct NaClHashTable
*table
,
44 return id
% table
->number_of_buckets
;
47 int __nacl_hash_init(struct NaClHashTable
*table
)
51 table
->number_of_buckets
= sizeof(table
->buckets
) / sizeof(table
->buckets
[0]);
52 for (i
= 0; i
< table
->number_of_buckets
; ++i
) {
53 LIST_INIT(&table
->buckets
[i
]);
58 void __nacl_hash_destroy(struct NaClHashTable
*table
)
61 * Nothing to free - everything is static
62 * TODO: what do we do when there
63 * are still some entries in the table?
67 int __nacl_hash_insert_mu(struct NaClHashTable
*table
,
68 nc_hash_entry_t
*entry
)
70 uint32_t bucket_id
= hash_get_bucket_id(table
, entry
->id_function(entry
));
71 LIST_INSERT_HEAD(&table
->buckets
[bucket_id
], entry
, list_entries
);
75 nc_hash_entry_t
*__nacl_hash_find_id_mu(struct NaClHashTable
*table
,
78 uint32_t bucket_id
= hash_get_bucket_id(table
, id
);
79 nc_hash_entry_t
*iter
;
80 LIST_FOREACH(iter
, &table
->buckets
[bucket_id
], list_entries
) {
81 if (iter
->id_function(iter
) == id
) {
88 int __nacl_hash_id_exists_mu(struct NaClHashTable
*table
,
91 if (NULL
== __nacl_hash_find_id_mu(table
, id
)) {
97 nc_hash_entry_t
*__nacl_hash_remove_mu(struct NaClHashTable
*table
,
100 uint32_t bucket_id
= hash_get_bucket_id(table
, id
);
101 nc_hash_entry_t
*iter
;
102 LIST_FOREACH(iter
, &table
->buckets
[bucket_id
], list_entries
) {
103 if (iter
->id_function(iter
) == id
) {
104 LIST_REMOVE(iter
, list_entries
);