1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-hash.h Generic hash table utility (internal to D-Bus implementation)
4 * Copyright (C) 2002 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 #ifdef HAVE_INTTYPES_H
35 #include <dbus/dbus-memory.h>
36 #include <dbus/dbus-types.h>
37 #include <dbus/dbus-sysdeps.h>
42 * @addtogroup DBusHashTable
46 /** Hash iterator object. The iterator is on the stack, but its real
47 * fields are hidden privately.
51 void *dummy1; /**< Do not use. */
52 void *dummy2; /**< Do not use. */
53 void *dummy3; /**< Do not use. */
54 void *dummy4; /**< Do not use. */
55 int dummy5; /**< Do not use. */
56 int dummy6; /**< Do not use. */
59 typedef struct DBusHashTable DBusHashTable;
60 typedef struct DBusHashIter DBusHashIter;
62 /* Allowing an arbitrary function as with GLib
63 * would be nicer for a public API, but for
64 * an internal API this saves typing, we can add
65 * more whenever we feel like it.
69 DBUS_HASH_STRING, /**< Hash keys are strings. */
70 DBUS_HASH_INT, /**< Hash keys are integers. */
71 DBUS_HASH_UINTPTR /**< Hash keys are integer capable to hold a pointer. */
75 DBusHashTable* _dbus_hash_table_new (DBusHashType type,
76 DBusFreeFunction key_free_function,
77 DBusFreeFunction value_free_function);
78 DBusHashTable* _dbus_hash_table_ref (DBusHashTable *table);
80 void _dbus_hash_table_unref (DBusHashTable *table);
81 void _dbus_hash_table_remove_all (DBusHashTable *table);
83 void _dbus_hash_iter_init (DBusHashTable *table,
86 dbus_bool_t _dbus_hash_iter_next (DBusHashIter *iter);
88 void _dbus_hash_iter_remove_entry (DBusHashIter *iter);
90 void* _dbus_hash_iter_get_value (DBusHashIter *iter);
91 void _dbus_hash_iter_set_value (DBusHashIter *iter,
94 int _dbus_hash_iter_get_int_key (DBusHashIter *iter);
96 const char* _dbus_hash_iter_get_string_key (DBusHashIter *iter);
98 uintptr_t _dbus_hash_iter_get_uintptr_key (DBusHashIter *iter);
99 dbus_bool_t _dbus_hash_iter_lookup (DBusHashTable *table,
101 dbus_bool_t create_if_not_found,
104 void* _dbus_hash_table_lookup_string (DBusHashTable *table,
107 void* _dbus_hash_table_lookup_int (DBusHashTable *table,
110 void* _dbus_hash_table_lookup_uintptr (DBusHashTable *table,
113 dbus_bool_t _dbus_hash_table_remove_string (DBusHashTable *table,
116 dbus_bool_t _dbus_hash_table_remove_int (DBusHashTable *table,
119 dbus_bool_t _dbus_hash_table_remove_uintptr (DBusHashTable *table,
122 dbus_bool_t _dbus_hash_table_insert_string (DBusHashTable *table,
126 dbus_bool_t _dbus_hash_table_insert_int (DBusHashTable *table,
130 dbus_bool_t _dbus_hash_table_insert_uintptr (DBusHashTable *table,
134 int _dbus_hash_table_get_n_entries (DBusHashTable *table);
137 char ** _dbus_hash_table_to_array (DBusHashTable *table,
140 dbus_bool_t _dbus_hash_table_from_array (DBusHashTable *table,
146 /** A preallocated hash entry */
147 typedef struct DBusPreallocatedHash DBusPreallocatedHash;
150 DBusPreallocatedHash *_dbus_hash_table_preallocate_entry (DBusHashTable *table);
152 void _dbus_hash_table_free_preallocated_entry (DBusHashTable *table,
153 DBusPreallocatedHash *preallocated);
155 void _dbus_hash_table_insert_string_preallocated (DBusHashTable *table,
156 DBusPreallocatedHash *preallocated,
161 # define DBUS_HASH_POLLABLE DBUS_HASH_UINTPTR
163 # define DBUS_HASH_POLLABLE DBUS_HASH_INT
166 static inline DBusPollable
167 _dbus_hash_iter_get_pollable_key (DBusHashIter *iter)
172 s.sock = _dbus_hash_iter_get_uintptr_key (iter);
175 return _dbus_hash_iter_get_int_key (iter);
180 _dbus_hash_table_lookup_pollable (DBusHashTable *table,
184 return _dbus_hash_table_lookup_uintptr (table, key.sock);
186 return _dbus_hash_table_lookup_int (table, key);
190 static inline dbus_bool_t
191 _dbus_hash_table_remove_pollable (DBusHashTable *table,
195 return _dbus_hash_table_remove_uintptr (table, key.sock);
197 return _dbus_hash_table_remove_int (table, key);
201 static inline dbus_bool_t
202 _dbus_hash_table_insert_pollable (DBusHashTable *table,
207 return _dbus_hash_table_insert_uintptr (table, key.sock, value);
209 return _dbus_hash_table_insert_int (table, key, value);
214 _dbus_clear_hash_table (DBusHashTable **table_p)
216 _dbus_clear_pointer_impl (DBusHashTable, table_p, _dbus_hash_table_unref);
223 #endif /* DBUS_HASH_H */