Skip to content

Commit 41bc3c0

Browse files
committed
lib: merge branch 'cpaasch-oai:t/resizeable-hashtable'
#434
2 parents 6ddf23d + 97069f4 commit 41bc3c0

7 files changed

Lines changed: 293 additions & 64 deletions

File tree

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ noinst_HEADERS = \
335335
include/nl-aux-xfrm/nl-xfrm.h \
336336
include/nl-default.h \
337337
include/nl-priv-dynamic-core/cache-api.h \
338+
lib/hashtable-api.h \
338339
include/nl-priv-dynamic-core/nl-core.h \
339340
include/nl-priv-dynamic-core/object-api.h \
340341
include/nl-priv-dynamic-route/nl-priv-dynamic-route.h \

include/netlink/hashtable.h

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,30 @@ extern "C" {
1414
#endif
1515

1616
typedef struct nl_hash_node {
17-
uint32_t key;
18-
uint32_t key_size;
19-
struct nl_object * obj;
20-
struct nl_hash_node * next;
17+
uint32_t key;
18+
uint32_t key_size;
19+
struct nl_object *obj;
20+
struct nl_hash_node *next;
2121
} nl_hash_node_t;
2222

2323
typedef struct nl_hash_table {
24-
int size;
25-
nl_hash_node_t ** nodes;
24+
int size;
25+
nl_hash_node_t **nodes;
2626
} nl_hash_table_t;
2727

2828
/* Default hash table size */
2929
#define NL_MAX_HASH_ENTRIES 1024
3030

3131
/* Access Functions */
32-
extern nl_hash_table_t * nl_hash_table_alloc(int size);
33-
extern void nl_hash_table_free(nl_hash_table_t *ht);
34-
35-
extern int nl_hash_table_add(nl_hash_table_t *ht,
36-
struct nl_object *obj);
37-
extern int nl_hash_table_del(nl_hash_table_t *ht,
38-
struct nl_object *obj);
39-
40-
extern struct nl_object * nl_hash_table_lookup(nl_hash_table_t *ht,
41-
struct nl_object *obj);
42-
extern uint32_t nl_hash(void *k, size_t length,
43-
uint32_t initval);
32+
extern nl_hash_table_t *nl_hash_table_alloc(int size);
33+
extern void nl_hash_table_free(nl_hash_table_t *ht);
34+
35+
extern int nl_hash_table_add(nl_hash_table_t *ht, struct nl_object *obj);
36+
extern int nl_hash_table_del(nl_hash_table_t *ht, struct nl_object *obj);
37+
38+
extern struct nl_object *nl_hash_table_lookup(nl_hash_table_t *ht,
39+
struct nl_object *obj);
40+
extern uint32_t nl_hash(void *k, size_t length, uint32_t initval);
4441

4542
#ifdef __cplusplus
4643
}

include/nl-priv-dynamic-core/cache-api.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,6 @@ struct nl_cache_ops
163163
/** Netlink protocol */
164164
int co_protocol;
165165

166-
/** cache object hash size **/
167-
int co_hash_size;
168-
169166
/** cache flags */
170167
unsigned int co_flags;
171168

@@ -261,7 +258,7 @@ struct nl_cache {
261258
int c_iarg2;
262259
int c_refcnt;
263260
unsigned int c_flags;
264-
struct nl_hash_table *hashtable;
261+
struct nl_rhash_table *hashtable;
265262
struct nl_cache_ops *c_ops;
266263
};
267264

lib/cache.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "nl-priv-dynamic-core/nl-core.h"
5656
#include "nl-priv-dynamic-core/object-api.h"
5757
#include "nl-priv-dynamic-core/cache-api.h"
58+
#include "hashtable-api.h"
5859
#include "nl-aux-core/nl-core.h"
5960

6061
/**
@@ -200,14 +201,7 @@ struct nl_cache *nl_cache_alloc(struct nl_cache_ops *ops)
200201
* cache objects for faster lookups
201202
*/
202203
if (ops->co_obj_ops->oo_keygen) {
203-
int hashtable_size;
204-
205-
if (ops->co_hash_size)
206-
hashtable_size = ops->co_hash_size;
207-
else
208-
hashtable_size = NL_MAX_HASH_ENTRIES;
209-
210-
cache->hashtable = nl_hash_table_alloc(hashtable_size);
204+
cache->hashtable = nl_rhash_table_alloc();
211205
}
212206

213207
NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));
@@ -379,7 +373,7 @@ static void __nl_cache_free(struct nl_cache *cache)
379373
nl_cache_clear(cache);
380374

381375
if (cache->hashtable)
382-
nl_hash_table_free(cache->hashtable);
376+
nl_rhash_table_free(cache->hashtable);
383377

384378
NL_DBG(2, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
385379
free(cache);
@@ -439,7 +433,7 @@ static int __cache_add(struct nl_cache *cache, struct nl_object *obj)
439433
obj->ce_cache = cache;
440434

441435
if (cache->hashtable) {
442-
ret = nl_hash_table_add(cache->hashtable, obj);
436+
ret = nl_rhash_table_add(cache->hashtable, obj);
443437
if (ret < 0) {
444438
obj->ce_cache = NULL;
445439
return ret;
@@ -558,7 +552,7 @@ void nl_cache_remove(struct nl_object *obj)
558552
return;
559553

560554
if (cache->hashtable) {
561-
ret = nl_hash_table_del(cache->hashtable, obj);
555+
ret = nl_rhash_table_del(cache->hashtable, obj);
562556
if (ret < 0)
563557
NL_DBG(2, "Failed to delete %p from cache %p <%s>.\n",
564558
obj, cache, nl_cache_name(cache));
@@ -1095,7 +1089,7 @@ static struct nl_object *__cache_fast_lookup(struct nl_cache *cache,
10951089
{
10961090
struct nl_object *obj;
10971091

1098-
obj = nl_hash_table_lookup(cache->hashtable, needle);
1092+
obj = nl_rhash_table_lookup(cache->hashtable, needle);
10991093
if (obj) {
11001094
nl_object_get(obj);
11011095
return obj;

lib/hashtable-api.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* SPDX-License-Identifier: LGPL-2.1-only */
2+
/*
3+
* Private resizable hash table API for libnl.
4+
*/
5+
6+
#ifndef NETLINK_HASHTABLE_API_H_
7+
#define NETLINK_HASHTABLE_API_H_
8+
9+
#include "nl-priv-dynamic-core/object-api.h"
10+
11+
/* Opaque resizable hash table handle. */
12+
typedef struct nl_rhash_table nl_rhash_table_t;
13+
14+
/* Allocation / Deletion */
15+
nl_rhash_table_t *nl_rhash_table_alloc(void);
16+
void nl_rhash_table_free(nl_rhash_table_t *ht);
17+
18+
/* Access helpers */
19+
struct nl_object *nl_rhash_table_lookup(nl_rhash_table_t *ht,
20+
struct nl_object *obj);
21+
int nl_rhash_table_add(nl_rhash_table_t *ht, struct nl_object *obj);
22+
int nl_rhash_table_del(nl_rhash_table_t *ht, struct nl_object *obj);
23+
24+
#endif /* NETLINK_HASHTABLE_API_H_ */

0 commit comments

Comments
 (0)