dbus-hash.h 7.76 KB
Newer Older
1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
John Palmieri's avatar
John Palmieri committed
2
/* dbus-hash.h Generic hash table utility (internal to D-Bus implementation)
3
 * 
4 5
 * Copyright (C) 2002  Red Hat, Inc.
 *
6
 * Licensed under the Academic Free License version 2.1
7 8 9 10 11 12 13 14 15 16 17 18 19
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21 22 23 24 25 26
 *
 */

#ifndef DBUS_HASH_H
#define DBUS_HASH_H

27 28
#ifdef HAVE_STDINT_H
#include <stdint.h>
29 30 31 32 33 34
#endif

#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif

35 36
#include <dbus/dbus-memory.h>
#include <dbus/dbus-types.h>
37
#include <dbus/dbus-sysdeps.h>
38

39
DBUS_BEGIN_DECLS
40

41 42 43 44 45
/**
 * @addtogroup DBusHashTable
 * @{
 */

46 47
/** Hash iterator object. The iterator is on the stack, but its real
 * fields are hidden privately.
48 49 50
 */
struct DBusHashIter
{
51 52 53 54 55 56
  void *dummy1; /**< Do not use. */
  void *dummy2; /**< Do not use. */
  void *dummy3; /**< Do not use. */
  void *dummy4; /**< Do not use. */
  int   dummy5; /**< Do not use. */
  int   dummy6; /**< Do not use. */
57 58
};

59 60 61
typedef struct DBusHashTable DBusHashTable;
typedef struct DBusHashIter  DBusHashIter;

62
/* Allowing an arbitrary function as with GLib
63 64 65
 * would be nicer for a public API, but for
 * an internal API this saves typing, we can add
 * more whenever we feel like it.
66 67 68
 */
typedef enum
{
69 70
  DBUS_HASH_STRING,        /**< Hash keys are strings. */
  DBUS_HASH_INT,           /**< Hash keys are integers. */
71
  DBUS_HASH_UINTPTR        /**< Hash keys are integer capable to hold a pointer. */
72
} DBusHashType;
73

74
DBUS_PRIVATE_EXPORT
75 76 77
DBusHashTable* _dbus_hash_table_new                (DBusHashType      type,
                                                    DBusFreeFunction  key_free_function,
                                                    DBusFreeFunction  value_free_function);
78
DBusHashTable* _dbus_hash_table_ref                (DBusHashTable    *table);
79
DBUS_PRIVATE_EXPORT
80
void           _dbus_hash_table_unref              (DBusHashTable    *table);
81
void           _dbus_hash_table_remove_all         (DBusHashTable    *table);
82
DBUS_PRIVATE_EXPORT
83 84
void           _dbus_hash_iter_init                (DBusHashTable    *table,
                                                    DBusHashIter     *iter);
85
DBUS_PRIVATE_EXPORT
86
dbus_bool_t    _dbus_hash_iter_next                (DBusHashIter     *iter);
87
DBUS_PRIVATE_EXPORT
88
void           _dbus_hash_iter_remove_entry        (DBusHashIter     *iter);
89
DBUS_PRIVATE_EXPORT
90 91 92
void*          _dbus_hash_iter_get_value           (DBusHashIter     *iter);
void           _dbus_hash_iter_set_value           (DBusHashIter     *iter,
                                                    void             *value);
93
DBUS_PRIVATE_EXPORT
94
int            _dbus_hash_iter_get_int_key         (DBusHashIter     *iter);
95
DBUS_PRIVATE_EXPORT
96
const char*    _dbus_hash_iter_get_string_key      (DBusHashIter     *iter);
97
DBUS_PRIVATE_EXPORT
98
uintptr_t      _dbus_hash_iter_get_uintptr_key     (DBusHashIter     *iter);
99 100 101 102
dbus_bool_t    _dbus_hash_iter_lookup              (DBusHashTable    *table,
                                                    void             *key,
                                                    dbus_bool_t       create_if_not_found,
                                                    DBusHashIter     *iter);
103
DBUS_PRIVATE_EXPORT
104 105
void*          _dbus_hash_table_lookup_string      (DBusHashTable    *table,
                                                    const char       *key);
106
DBUS_PRIVATE_EXPORT
107 108
void*          _dbus_hash_table_lookup_int         (DBusHashTable    *table,
                                                    int               key);
109
DBUS_PRIVATE_EXPORT
110 111
void*          _dbus_hash_table_lookup_uintptr     (DBusHashTable    *table,
                                                    uintptr_t         key);
112
DBUS_PRIVATE_EXPORT
113 114
dbus_bool_t    _dbus_hash_table_remove_string      (DBusHashTable    *table,
                                                    const char       *key);
115
DBUS_PRIVATE_EXPORT
116 117
dbus_bool_t    _dbus_hash_table_remove_int         (DBusHashTable    *table,
                                                    int               key);
118
DBUS_PRIVATE_EXPORT
119 120
dbus_bool_t    _dbus_hash_table_remove_uintptr     (DBusHashTable    *table,
                                                    uintptr_t         key);
121
DBUS_PRIVATE_EXPORT
122 123 124
dbus_bool_t    _dbus_hash_table_insert_string      (DBusHashTable    *table,
                                                    char             *key,
                                                    void             *value);
125
DBUS_PRIVATE_EXPORT
126 127 128
dbus_bool_t    _dbus_hash_table_insert_int         (DBusHashTable    *table,
                                                    int               key,
                                                    void             *value);
129
DBUS_PRIVATE_EXPORT
130 131
dbus_bool_t    _dbus_hash_table_insert_uintptr     (DBusHashTable    *table,
                                                    uintptr_t         key,
132
                                                    void             *value);
133
DBUS_PRIVATE_EXPORT
134
int            _dbus_hash_table_get_n_entries      (DBusHashTable    *table);
135

136
/* Preallocation */
137 138

/** A preallocated hash entry */
139 140
typedef struct DBusPreallocatedHash DBusPreallocatedHash;

141
DBUS_PRIVATE_EXPORT
142
DBusPreallocatedHash *_dbus_hash_table_preallocate_entry          (DBusHashTable        *table);
143
DBUS_PRIVATE_EXPORT
144 145
void                  _dbus_hash_table_free_preallocated_entry    (DBusHashTable        *table,
                                                                   DBusPreallocatedHash *preallocated);
146
DBUS_PRIVATE_EXPORT
147 148 149 150 151
void                  _dbus_hash_table_insert_string_preallocated (DBusHashTable        *table,
                                                                   DBusPreallocatedHash *preallocated,
                                                                   char                 *key,
                                                                   void                 *value);

152 153 154 155 156 157 158 159 160 161
#ifdef DBUS_WIN
# define DBUS_HASH_POLLABLE DBUS_HASH_UINTPTR
#else
# define DBUS_HASH_POLLABLE DBUS_HASH_INT
#endif

static inline DBusPollable
_dbus_hash_iter_get_pollable_key (DBusHashIter *iter)
{
#ifdef DBUS_WIN
162 163 164 165
  DBusSocket s;

  s.sock = _dbus_hash_iter_get_uintptr_key (iter);
  return s;
166 167 168 169 170 171 172 173 174 175
#else
  return _dbus_hash_iter_get_int_key (iter);
#endif
}

static inline void *
_dbus_hash_table_lookup_pollable (DBusHashTable *table,
                                  DBusPollable   key)
{
#ifdef DBUS_WIN
176
  return _dbus_hash_table_lookup_uintptr (table, key.sock);
177 178 179 180 181 182 183 184 185 186
#else
  return _dbus_hash_table_lookup_int (table, key);
#endif
}

static inline dbus_bool_t
_dbus_hash_table_remove_pollable (DBusHashTable *table,
                                  DBusPollable   key)
{
#ifdef DBUS_WIN
187
  return _dbus_hash_table_remove_uintptr (table, key.sock);
188 189 190 191 192 193 194 195 196 197 198
#else
  return _dbus_hash_table_remove_int (table, key);
#endif
}

static inline dbus_bool_t
_dbus_hash_table_insert_pollable (DBusHashTable *table,
                                  DBusPollable   key,
                                  void          *value)
{
#ifdef DBUS_WIN
199
  return _dbus_hash_table_insert_uintptr (table, key.sock, value);
200 201 202 203 204
#else
  return _dbus_hash_table_insert_int (table, key, value);
#endif
}

205
/** @} */
206

207
DBUS_END_DECLS
208 209

#endif /* DBUS_HASH_H */