2010-08-17 03:33:07 +02:00
|
|
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
2009-11-18 00:42:52 +01:00
|
|
|
|
2012-07-18 19:07:51 +02:00
|
|
|
#pragma once
|
2009-11-18 00:42:52 +01:00
|
|
|
|
2010-02-03 13:03:47 +01:00
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2010 Lennart Poettering
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
2012-04-12 00:20:58 +02:00
|
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
2010-02-03 13:03:47 +01:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2012-04-12 00:20:58 +02:00
|
|
|
Lesser General Public License for more details.
|
2010-02-03 13:03:47 +01:00
|
|
|
|
2012-04-12 00:20:58 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2010-02-03 13:03:47 +01:00
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
2009-11-18 00:42:52 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2013-05-03 04:51:50 +02:00
|
|
|
#include "macro.h"
|
2013-12-18 02:49:03 +01:00
|
|
|
#include "util.h"
|
2013-05-03 04:51:50 +02:00
|
|
|
|
2009-11-18 00:42:52 +01:00
|
|
|
/* Pretty straightforward hash table implementation. As a minor
|
|
|
|
* optimization a NULL hashmap object will be treated as empty hashmap
|
|
|
|
* for all read operations. That way it is not necessary to
|
|
|
|
* instantiate an object for each Hashmap use. */
|
|
|
|
|
2013-12-22 19:59:12 +01:00
|
|
|
#define HASH_KEY_SIZE 16
|
|
|
|
|
2009-11-18 00:42:52 +01:00
|
|
|
typedef struct Hashmap Hashmap;
|
2014-10-13 18:11:16 +02:00
|
|
|
typedef struct OrderedHashmap OrderedHashmap;
|
2010-01-26 04:18:44 +01:00
|
|
|
typedef struct _IteratorStruct _IteratorStruct;
|
|
|
|
typedef _IteratorStruct* Iterator;
|
|
|
|
|
|
|
|
#define ITERATOR_FIRST ((Iterator) 0)
|
|
|
|
#define ITERATOR_LAST ((Iterator) -1)
|
2009-11-18 00:42:52 +01:00
|
|
|
|
2013-12-22 19:59:12 +01:00
|
|
|
typedef unsigned long (*hash_func_t)(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]);
|
2009-11-18 00:42:52 +01:00
|
|
|
typedef int (*compare_func_t)(const void *a, const void *b);
|
|
|
|
|
2014-08-13 01:00:18 +02:00
|
|
|
struct hash_ops {
|
|
|
|
hash_func_t hash;
|
|
|
|
compare_func_t compare;
|
|
|
|
};
|
|
|
|
|
2013-12-22 19:59:12 +01:00
|
|
|
unsigned long string_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) _pure_;
|
2013-05-03 04:51:50 +02:00
|
|
|
int string_compare_func(const void *a, const void *b) _pure_;
|
2014-08-13 01:00:18 +02:00
|
|
|
extern const struct hash_ops string_hash_ops;
|
2009-11-18 00:42:52 +01:00
|
|
|
|
2013-05-07 17:51:41 +02:00
|
|
|
/* This will compare the passed pointers directly, and will not
|
|
|
|
* dereference them. This is hence not useful for strings or
|
|
|
|
* suchlike. */
|
2013-12-22 19:59:12 +01:00
|
|
|
unsigned long trivial_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) _pure_;
|
2013-05-03 04:51:50 +02:00
|
|
|
int trivial_compare_func(const void *a, const void *b) _const_;
|
2014-08-13 01:00:18 +02:00
|
|
|
extern const struct hash_ops trivial_hash_ops;
|
2009-11-18 00:42:52 +01:00
|
|
|
|
2014-08-19 00:04:46 +02:00
|
|
|
/* 32bit values we can always just embedd in the pointer itself, but
|
|
|
|
* in order to support 32bit archs we need store 64bit values
|
|
|
|
* indirectly, since they don't fit in a pointer. */
|
2013-12-22 19:59:12 +01:00
|
|
|
unsigned long uint64_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) _pure_;
|
2013-05-03 04:51:50 +02:00
|
|
|
int uint64_compare_func(const void *a, const void *b) _pure_;
|
2014-08-13 01:00:18 +02:00
|
|
|
extern const struct hash_ops uint64_hash_ops;
|
2012-10-26 03:24:03 +02:00
|
|
|
|
2014-08-19 00:04:46 +02:00
|
|
|
/* On some archs dev_t is 32bit, and on others 64bit. And sometimes
|
|
|
|
* it's 64bit on 32bit archs, and sometimes 32bit on 64bit archs. Yuck! */
|
|
|
|
#if SIZEOF_DEV_T != 8
|
|
|
|
unsigned long devt_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) _pure_;
|
|
|
|
int devt_compare_func(const void *a, const void *b) _pure_;
|
2014-08-13 01:00:18 +02:00
|
|
|
extern const struct hash_ops devt_hash_ops = {
|
|
|
|
.hash = devt_hash_func,
|
|
|
|
.compare = devt_compare_func
|
|
|
|
};
|
2014-08-19 00:04:46 +02:00
|
|
|
#else
|
|
|
|
#define devt_hash_func uint64_hash_func
|
|
|
|
#define devt_compare_func uint64_compare_func
|
2014-08-13 01:00:18 +02:00
|
|
|
#define devt_hash_ops uint64_hash_ops
|
2014-08-19 00:04:46 +02:00
|
|
|
#endif
|
|
|
|
|
2014-08-13 01:00:18 +02:00
|
|
|
Hashmap *hashmap_new(const struct hash_ops *hash_ops);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline OrderedHashmap *ordered_hashmap_new(const struct hash_ops *hash_ops) {
|
|
|
|
return (OrderedHashmap*) hashmap_new(hash_ops);
|
|
|
|
}
|
2010-01-18 23:49:49 +01:00
|
|
|
void hashmap_free(Hashmap *h);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline void ordered_hashmap_free(OrderedHashmap *h) {
|
|
|
|
hashmap_free((Hashmap*) h);
|
|
|
|
}
|
2010-08-20 03:26:15 +02:00
|
|
|
void hashmap_free_free(Hashmap *h);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline void ordered_hashmap_free_free(OrderedHashmap *h) {
|
|
|
|
hashmap_free_free((Hashmap*) h);
|
|
|
|
}
|
2013-02-11 23:48:36 +01:00
|
|
|
void hashmap_free_free_free(Hashmap *h);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline void ordered_hashmap_free_free_free(OrderedHashmap *h) {
|
|
|
|
hashmap_free_free_free((Hashmap*) h);
|
|
|
|
}
|
2010-01-18 23:49:49 +01:00
|
|
|
Hashmap *hashmap_copy(Hashmap *h);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline OrderedHashmap *ordered_hashmap_copy(OrderedHashmap *h) {
|
|
|
|
return (OrderedHashmap*) hashmap_copy((Hashmap*) h);
|
|
|
|
}
|
2014-08-13 01:00:18 +02:00
|
|
|
int hashmap_ensure_allocated(Hashmap **h, const struct hash_ops *hash_ops);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline int ordered_hashmap_ensure_allocated(OrderedHashmap **h, const struct hash_ops *hash_ops) {
|
|
|
|
return hashmap_ensure_allocated((Hashmap**) h, hash_ops);
|
|
|
|
}
|
2009-11-18 00:42:52 +01:00
|
|
|
|
|
|
|
int hashmap_put(Hashmap *h, const void *key, void *value);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline int ordered_hashmap_put(OrderedHashmap *h, const void *key, void *value) {
|
|
|
|
return hashmap_put((Hashmap*) h, key, value);
|
|
|
|
}
|
2012-10-25 01:19:24 +02:00
|
|
|
int hashmap_update(Hashmap *h, const void *key, void *value);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline int ordered_hashmap_update(OrderedHashmap *h, const void *key, void *value) {
|
|
|
|
return hashmap_update((Hashmap*) h, key, value);
|
|
|
|
}
|
2010-01-20 02:12:12 +01:00
|
|
|
int hashmap_replace(Hashmap *h, const void *key, void *value);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline int ordered_hashmap_replace(OrderedHashmap *h, const void *key, void *value) {
|
|
|
|
return hashmap_replace((Hashmap*) h, key, value);
|
|
|
|
}
|
2013-04-26 18:40:08 +02:00
|
|
|
void *hashmap_get(Hashmap *h, const void *key);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline void *ordered_hashmap_get(OrderedHashmap *h, const void *key) {
|
|
|
|
return hashmap_get((Hashmap*) h, key);
|
|
|
|
}
|
2013-04-26 18:40:08 +02:00
|
|
|
void *hashmap_get2(Hashmap *h, const void *key, void **rkey);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline void *ordered_hashmap_get2(OrderedHashmap *h, const void *key, void **rkey) {
|
|
|
|
return hashmap_get2((Hashmap*) h, key, rkey);
|
|
|
|
}
|
2012-08-13 13:58:01 +02:00
|
|
|
bool hashmap_contains(Hashmap *h, const void *key);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline bool ordered_hashmap_contains(OrderedHashmap *h, const void *key) {
|
|
|
|
return hashmap_contains((Hashmap*) h, key);
|
|
|
|
}
|
2013-04-26 18:40:08 +02:00
|
|
|
void *hashmap_remove(Hashmap *h, const void *key);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline void *ordered_hashmap_remove(OrderedHashmap *h, const void *key) {
|
|
|
|
return hashmap_remove((Hashmap*) h, key);
|
|
|
|
}
|
2014-05-15 00:43:44 +02:00
|
|
|
void *hashmap_remove2(Hashmap *h, const void *key, void **rkey);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline void *ordered_hashmap_remove2(OrderedHashmap *h, const void *key, void **rkey) {
|
|
|
|
return hashmap_remove2((Hashmap*) h, key, rkey);
|
|
|
|
}
|
2013-04-26 18:40:08 +02:00
|
|
|
void *hashmap_remove_value(Hashmap *h, const void *key, void *value);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline void *ordered_hashmap_remove_value(OrderedHashmap *h, const void *key, void *value) {
|
|
|
|
return hashmap_remove_value((Hashmap*) h, key, value);
|
|
|
|
}
|
2010-04-06 02:38:43 +02:00
|
|
|
int hashmap_remove_and_put(Hashmap *h, const void *old_key, const void *new_key, void *value);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline int ordered_hashmap_remove_and_put(OrderedHashmap *h, const void *old_key, const void *new_key, void *value) {
|
|
|
|
return hashmap_remove_and_put((Hashmap*) h, old_key, new_key, value);
|
|
|
|
}
|
2010-07-20 20:33:19 +02:00
|
|
|
int hashmap_remove_and_replace(Hashmap *h, const void *old_key, const void *new_key, void *value);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline int ordered_hashmap_remove_and_replace(OrderedHashmap *h, const void *old_key, const void *new_key, void *value) {
|
|
|
|
return hashmap_remove_and_replace((Hashmap*) h, old_key, new_key, value);
|
|
|
|
}
|
2009-11-18 00:42:52 +01:00
|
|
|
|
2010-01-18 23:49:49 +01:00
|
|
|
int hashmap_merge(Hashmap *h, Hashmap *other);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline int ordered_hashmap_merge(OrderedHashmap *h, OrderedHashmap *other) {
|
|
|
|
return hashmap_merge((Hashmap*) h, (Hashmap*) other);
|
|
|
|
}
|
2014-10-14 23:35:24 +02:00
|
|
|
int hashmap_reserve(Hashmap *h, unsigned entries_add);
|
|
|
|
static inline int ordered_hashmap_reserve(OrderedHashmap *h, unsigned entries_add) {
|
|
|
|
return hashmap_reserve((Hashmap*) h, entries_add);
|
|
|
|
}
|
2014-10-15 00:17:51 +02:00
|
|
|
int hashmap_move(Hashmap *h, Hashmap *other);
|
|
|
|
static inline int ordered_hashmap_move(OrderedHashmap *h, OrderedHashmap *other) {
|
|
|
|
return hashmap_move((Hashmap*) h, (Hashmap*) other);
|
2014-10-13 18:11:16 +02:00
|
|
|
}
|
2010-04-06 02:38:43 +02:00
|
|
|
int hashmap_move_one(Hashmap *h, Hashmap *other, const void *key);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline int ordered_hashmap_move_one(OrderedHashmap *h, OrderedHashmap *other, const void *key) {
|
|
|
|
return hashmap_move_one((Hashmap*) h, (Hashmap*) other, key);
|
|
|
|
}
|
2010-01-18 23:49:49 +01:00
|
|
|
|
2013-05-03 04:51:50 +02:00
|
|
|
unsigned hashmap_size(Hashmap *h) _pure_;
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline unsigned ordered_hashmap_size(OrderedHashmap *h) {
|
|
|
|
return hashmap_size((Hashmap*) h);
|
|
|
|
}
|
2013-05-03 04:51:50 +02:00
|
|
|
bool hashmap_isempty(Hashmap *h) _pure_;
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline bool ordered_hashmap_isempty(OrderedHashmap *h) {
|
|
|
|
return hashmap_isempty((Hashmap*) h);
|
|
|
|
}
|
2013-10-01 00:13:18 +02:00
|
|
|
unsigned hashmap_buckets(Hashmap *h) _pure_;
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline unsigned ordered_hashmap_buckets(OrderedHashmap *h) {
|
|
|
|
return hashmap_buckets((Hashmap*) h);
|
|
|
|
}
|
2009-11-18 00:42:52 +01:00
|
|
|
|
2010-01-26 04:18:44 +01:00
|
|
|
void *hashmap_iterate(Hashmap *h, Iterator *i, const void **key);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline void *ordered_hashmap_iterate(OrderedHashmap *h, Iterator *i, const void **key) {
|
|
|
|
return hashmap_iterate((Hashmap*) h, i, key);
|
|
|
|
}
|
2009-11-18 00:42:52 +01:00
|
|
|
|
2010-01-19 04:15:20 +01:00
|
|
|
void hashmap_clear(Hashmap *h);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline void ordered_hashmap_clear(OrderedHashmap *h) {
|
|
|
|
hashmap_clear((Hashmap*) h);
|
|
|
|
}
|
2012-07-03 16:09:36 +02:00
|
|
|
void hashmap_clear_free(Hashmap *h);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline void ordered_hashmap_clear_free(OrderedHashmap *h) {
|
|
|
|
hashmap_clear_free((Hashmap*) h);
|
|
|
|
}
|
2013-02-11 23:48:36 +01:00
|
|
|
void hashmap_clear_free_free(Hashmap *h);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline void ordered_hashmap_clear_free_free(OrderedHashmap *h) {
|
|
|
|
hashmap_clear_free_free((Hashmap*) h);
|
|
|
|
}
|
2012-07-03 16:09:36 +02:00
|
|
|
|
2009-11-18 00:42:52 +01:00
|
|
|
void *hashmap_steal_first(Hashmap *h);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline void *ordered_hashmap_steal_first(OrderedHashmap *h) {
|
|
|
|
return hashmap_steal_first((Hashmap*) h);
|
|
|
|
}
|
2010-09-23 15:01:41 +02:00
|
|
|
void *hashmap_steal_first_key(Hashmap *h);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline void *ordered_hashmap_steal_first_key(OrderedHashmap *h) {
|
|
|
|
return hashmap_steal_first_key((Hashmap*) h);
|
|
|
|
}
|
2013-05-03 04:51:50 +02:00
|
|
|
void *hashmap_first(Hashmap *h) _pure_;
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline void *ordered_hashmap_first(OrderedHashmap *h) {
|
|
|
|
return hashmap_first((Hashmap*) h);
|
|
|
|
}
|
2013-05-03 04:51:50 +02:00
|
|
|
void *hashmap_first_key(Hashmap *h) _pure_;
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline void *ordered_hashmap_first_key(OrderedHashmap *h) {
|
|
|
|
return hashmap_first_key((Hashmap*) h);
|
|
|
|
}
|
2009-11-18 00:42:52 +01:00
|
|
|
|
2012-10-18 03:29:19 +02:00
|
|
|
void *hashmap_next(Hashmap *h, const void *key);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline void *ordered_hashmap_next(OrderedHashmap *h, const void *key) {
|
|
|
|
return hashmap_next((Hashmap*) h, key);
|
|
|
|
}
|
2012-10-18 03:29:19 +02:00
|
|
|
|
2011-04-25 20:41:47 +02:00
|
|
|
char **hashmap_get_strv(Hashmap *h);
|
2014-10-13 18:11:16 +02:00
|
|
|
static inline char **ordered_hashmap_get_strv(OrderedHashmap *h) {
|
|
|
|
return hashmap_get_strv((Hashmap*) h);
|
|
|
|
}
|
2011-04-25 20:41:47 +02:00
|
|
|
|
2010-01-26 04:18:44 +01:00
|
|
|
#define HASHMAP_FOREACH(e, h, i) \
|
2010-09-27 20:13:21 +02:00
|
|
|
for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), NULL); (e); (e) = hashmap_iterate((h), &(i), NULL))
|
2009-11-18 00:42:52 +01:00
|
|
|
|
2014-10-13 18:11:16 +02:00
|
|
|
#define ORDERED_HASHMAP_FOREACH(e, h, i) \
|
|
|
|
for ((i) = ITERATOR_FIRST, (e) = ordered_hashmap_iterate((h), &(i), NULL); \
|
|
|
|
(e); \
|
|
|
|
(e) = ordered_hashmap_iterate((h), &(i), NULL))
|
|
|
|
|
2010-01-26 04:18:44 +01:00
|
|
|
#define HASHMAP_FOREACH_KEY(e, k, h, i) \
|
2010-09-27 20:13:21 +02:00
|
|
|
for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), (const void**) &(k)); (e); (e) = hashmap_iterate((h), &(i), (const void**) &(k)))
|
2010-01-19 04:15:20 +01:00
|
|
|
|
2014-10-13 18:11:16 +02:00
|
|
|
#define ORDERED_HASHMAP_FOREACH_KEY(e, k, h, i) \
|
|
|
|
for ((i) = ITERATOR_FIRST, (e) = ordered_hashmap_iterate((h), &(i), (const void**) &(k)); \
|
|
|
|
(e); \
|
|
|
|
(e) = ordered_hashmap_iterate((h), &(i), (const void**) &(k)))
|
|
|
|
|
2013-12-18 02:49:03 +01:00
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free);
|
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free_free);
|
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free_free_free);
|
2014-10-13 18:11:16 +02:00
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedHashmap*, ordered_hashmap_free);
|
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedHashmap*, ordered_hashmap_free_free);
|
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedHashmap*, ordered_hashmap_free_free_free);
|
2013-12-18 02:49:03 +01:00
|
|
|
#define _cleanup_hashmap_free_ _cleanup_(hashmap_freep)
|
|
|
|
#define _cleanup_hashmap_free_free_ _cleanup_(hashmap_free_freep)
|
|
|
|
#define _cleanup_hashmap_free_free_free_ _cleanup_(hashmap_free_free_freep)
|
2014-10-13 18:11:16 +02:00
|
|
|
#define _cleanup_ordered_hashmap_free_ _cleanup_(ordered_hashmap_freep)
|
|
|
|
#define _cleanup_ordered_hashmap_free_free_ _cleanup_(ordered_hashmap_free_freep)
|
|
|
|
#define _cleanup_ordered_hashmap_free_free_free_ _cleanup_(ordered_hashmap_free_free_freep)
|