chunk_map: Store key, value pair in entry_t struct
[strongswan.git] / src / charon-tkm / src / tkm / tkm_chunk_map.c
1 /*
2 * Copyright (C) 2012 Reto Buerki
3 * Copyright (C) 2012 Adrian-Ken Rueegsegger
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <collections/hashtable.h>
18 #include <threading/rwlock.h>
19 #include <utils/chunk.h>
20 #include <utils/debug.h>
21
22 #include "tkm_chunk_map.h"
23
24 typedef struct private_tkm_chunk_map_t private_tkm_chunk_map_t;
25
26 /**
27 * Private data of tkm chunk map.
28 */
29 struct private_tkm_chunk_map_t {
30
31 /**
32 * public functions
33 */
34 tkm_chunk_map_t public;
35
36 /**
37 * Hashtable to store mappings.
38 */
39 hashtable_t *mappings;
40
41 /**
42 * rwlock for table.
43 */
44 rwlock_t *lock;
45
46 };
47
48 /**
49 * Entry for hashtables
50 */
51 typedef struct {
52 /** Key chunk */
53 chunk_t key;
54 /** Entry value */
55 uint64_t value;
56 } entry_t;
57
58 /**
59 * Destroy a hashtable entry
60 */
61 static void entry_destroy(entry_t *this)
62 {
63 chunk_free(&this->key);
64 free(this);
65 }
66
67 METHOD(tkm_chunk_map_t, insert, void,
68 private_tkm_chunk_map_t * const this, const chunk_t * const data,
69 const uint64_t id)
70 {
71 entry_t *entry;
72 INIT(entry,
73 .key = chunk_clone(*data),
74 .value = id
75 );
76
77 this->lock->write_lock(this->lock);
78 entry = this->mappings->put(this->mappings, (void*)&entry->key, entry);
79 this->lock->unlock(this->lock);
80
81 if (entry)
82 {
83 entry_destroy(entry);
84 }
85 }
86
87 METHOD(tkm_chunk_map_t, get_id, uint64_t,
88 private_tkm_chunk_map_t * const this, chunk_t *data)
89 {
90 entry_t *entry;
91 this->lock->read_lock(this->lock);
92 entry = this->mappings->get(this->mappings, data);
93 this->lock->unlock(this->lock);
94
95 if (!entry)
96 {
97 return 0;
98 }
99
100 return entry->value;
101 }
102
103 METHOD(tkm_chunk_map_t, remove_, bool,
104 private_tkm_chunk_map_t * const this, chunk_t *data)
105 {
106 entry_t *entry;
107
108 this->lock->write_lock(this->lock);
109 entry = this->mappings->remove(this->mappings, data);
110 this->lock->unlock(this->lock);
111
112 if (entry)
113 {
114 entry_destroy(entry);
115 return TRUE;
116 }
117 else
118 {
119 return FALSE;
120 }
121 }
122
123 METHOD(tkm_chunk_map_t, destroy, void,
124 private_tkm_chunk_map_t *this)
125 {
126 entry_t *entry;
127 enumerator_t *enumerator;
128
129 this->lock->write_lock(this->lock);
130 enumerator = this->mappings->create_enumerator(this->mappings);
131 while (enumerator->enumerate(enumerator, NULL, &entry))
132 {
133 entry_destroy(entry);
134 }
135 enumerator->destroy(enumerator);
136 this->lock->unlock(this->lock);
137
138 this->mappings->destroy(this->mappings);
139 this->lock->destroy(this->lock);
140 free(this);
141 }
142
143 /**
144 * Hashtable hash function.
145 */
146 static u_int hash(chunk_t *key)
147 {
148 return chunk_hash(*key);
149 }
150
151 /*
152 * see header file
153 */
154 tkm_chunk_map_t *tkm_chunk_map_create()
155 {
156 private_tkm_chunk_map_t *this;
157
158 INIT(this,
159 .public = {
160 .insert = _insert,
161 .get_id = _get_id,
162 .remove = _remove_,
163 .destroy = _destroy,
164 },
165 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
166 .mappings = hashtable_create((hashtable_hash_t)hash,
167 (hashtable_equals_t)chunk_equals_ptr, 32),
168 );
169
170 return &this->public;
171 }