2 // Copyright (c) 2010-2017 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 #include <rte_hash_crc.h>
20 #include "prox_malloc.h"
21 #include "prox_assert.h"
24 #define HASH_SET_ALLOC_CHUNCK 1024
25 #define HASH_SET_ALLOC_CHUNCK_MEM (sizeof(struct hash_set_entry) * 1024)
27 struct hash_set_entry {
31 struct hash_set_entry *next;
37 struct hash_set_entry *alloc;
39 struct hash_set_entry *mem[0];
42 static struct hash_set_entry *hash_set_alloc_entry(struct hash_set *hs)
44 struct hash_set_entry *ret;
46 if (hs->alloc_count == 0) {
47 size_t mem_size = HASH_SET_ALLOC_CHUNCK *
48 sizeof(struct hash_set_entry);
50 hs->alloc = prox_zmalloc(mem_size, hs->socket_id);
51 hs->alloc_count = HASH_SET_ALLOC_CHUNCK;
60 struct hash_set *hash_set_create(uint32_t n_buckets, int socket_id)
63 size_t mem_size = sizeof(*ret) + sizeof(ret->mem[0]) * n_buckets;
65 ret = prox_zmalloc(mem_size, socket_id);
66 ret->n_buckets = n_buckets;
67 ret->socket_id = socket_id;
72 void *hash_set_find(struct hash_set *hs, void *data, size_t len)
74 uint32_t crc = rte_hash_crc(data, len, 0);
76 struct hash_set_entry *entry = hs->mem[crc % hs->n_buckets];
79 if (entry->crc == crc && entry->len == len &&
80 memcmp(entry->data, data, len) == 0)
87 void hash_set_add(struct hash_set *hs, void *data, size_t len)
89 uint32_t crc = rte_hash_crc(data, len, 0);
90 struct hash_set_entry *new = hash_set_alloc_entry(hs);
96 if (hs->mem[crc % hs->n_buckets]) {
97 struct hash_set_entry *entry = hs->mem[crc % hs->n_buckets];
103 hs->mem[crc % hs->n_buckets] = new;