2 * Copyright (C) 2012 Reto Buerki
3 * Copyright (C) 2012 Adrian-Ken Rueegsegger
4 * Hochschule fuer Technik Rapperswil
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>.
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
17 #include <collections/hashtable.h>
18 #include <threading/rwlock.h>
19 #include <utils/chunk.h>
20 #include <utils/debug.h>
22 #include "tkm_chunk_map.h"
24 typedef struct private_tkm_chunk_map_t private_tkm_chunk_map_t
;
27 * Private data of tkm chunk map.
29 struct private_tkm_chunk_map_t
{
34 tkm_chunk_map_t
public;
37 * Hashtable to store mappings.
39 hashtable_t
*mappings
;
48 METHOD(tkm_chunk_map_t
, insert
, void,
49 private_tkm_chunk_map_t
* const this, const chunk_t
* const data
,
52 uint64_t *value
= malloc_thing(uint64_t);
55 this->lock
->write_lock(this->lock
);
56 value
= this->mappings
->put(this->mappings
, (void*)data
, value
);
57 this->lock
->unlock(this->lock
);
65 METHOD(tkm_chunk_map_t
, get_id
, uint64_t,
66 private_tkm_chunk_map_t
* const this, chunk_t
*data
)
69 this->lock
->read_lock(this->lock
);
70 value
= this->mappings
->get(this->mappings
, data
);
71 this->lock
->unlock(this->lock
);
73 return value
== NULL ?
0 : *value
;
76 METHOD(tkm_chunk_map_t
, remove_
, bool,
77 private_tkm_chunk_map_t
* const this, chunk_t
*data
)
79 this->lock
->write_lock(this->lock
);
80 uint64_t *value
= this->mappings
->remove(this->mappings
, data
);
81 this->lock
->unlock(this->lock
);
94 METHOD(tkm_chunk_map_t
, destroy
, void,
95 private_tkm_chunk_map_t
*this)
98 enumerator_t
*enumerator
;
100 this->lock
->write_lock(this->lock
);
101 enumerator
= this->mappings
->create_enumerator(this->mappings
);
102 while (enumerator
->enumerate(enumerator
, NULL
, &value
))
104 this->mappings
->remove_at(this->mappings
, enumerator
);
107 enumerator
->destroy(enumerator
);
108 this->lock
->unlock(this->lock
);
110 this->lock
->destroy(this->lock
);
111 this->mappings
->destroy(this->mappings
);
116 * Hashtable hash function.
118 static u_int
hash(chunk_t
*key
)
120 return chunk_hash(*key
);
126 tkm_chunk_map_t
*tkm_chunk_map_create()
128 private_tkm_chunk_map_t
*this;
137 .lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
),
138 .mappings
= hashtable_create((hashtable_hash_t
)hash
,
139 (hashtable_equals_t
)chunk_equals_ptr
, 32),
142 return &this->public;