00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00036 #ifndef _atoms_h_
00037 #define _atoms_h_
00038
00039 #include "common.h"
00040
00041
00042
00043
00044
00045 enum atom_type {
00046 ATOM_STRING,
00047 ATOM_GUID,
00048 ATOM_SHA1,
00049 ATOM_TTH,
00050 ATOM_UINT64,
00051 ATOM_FILESIZE,
00053 NUM_ATOM_TYPES
00054 };
00055
00056 #if !defined(TRACK_ATOMS) || defined(ATOMS_SOURCE)
00057 gconstpointer atom_get(enum atom_type type, gconstpointer key);
00058 void atom_free(enum atom_type type, gconstpointer key);
00059 #endif
00060
00061
00062
00063
00064
00065 #ifdef TRACK_ATOMS
00066
00067 #define atom_str_get(k) atom_get_track(ATOM_STRING, (k), _WHERE_, __LINE__)
00068 #define atom_str_free(k) atom_free_track(ATOM_STRING, (k), _WHERE_, __LINE__)
00069
00070 #define atom_guid_get(k) atom_get_track(ATOM_GUID, (k), _WHERE_, __LINE__)
00071 #define atom_guid_free(k) atom_free_track(ATOM_GUID, (k), _WHERE_, __LINE__)
00072
00073 #define atom_sha1_get(k) atom_get_track(ATOM_SHA1, (k), _WHERE_, __LINE__)
00074 #define atom_sha1_free(k) atom_free_track(ATOM_SHA1, (k), _WHERE_, __LINE__)
00075
00076 #define atom_tth_get(k) atom_get_track(ATOM_TTH, (k), _WHERE_, __LINE__)
00077 #define atom_tth_free(k) atom_free_track(ATOM_TTH, (k), _WHERE_, __LINE__)
00078
00079 #define atom_uint64_get(k) atom_get_track(ATOM_UINT64, (k), _WHERE_, __LINE__)
00080 #define atom_uint64_free(k) atom_free_track(ATOM_UINT64, (k), _WHERE_, __LINE__)
00081
00082 #define atom_filesize_get(k) \
00083 atom_get_track(ATOM_FILESIZE, (k), _WHERE_, __LINE__)
00084 #define atom_filesize_free(k) \
00085 atom_free_track(ATOM_FILESIZE, (k), _WHERE_, __LINE__)
00086
00087 #ifndef ATOMS_SOURCE
00088 #define atom_get(t,k) atom_get_track(t, (k), _WHERE_, __LINE__)
00089 #define atom_free(t,k) atom_free_track(t, (k), _WHERE_, __LINE__)
00090 #endif
00091
00092 #else
00093
00094 static inline const gchar *
00095 atom_str_get(const gchar *k)
00096 {
00097 return atom_get(ATOM_STRING, k);
00098 }
00099
00100 static inline void
00101 atom_str_free(const gchar *k)
00102 {
00103 return atom_free(ATOM_STRING, k);
00104 }
00105
00106 static inline const gchar *
00107 atom_guid_get(const gchar *k)
00108 {
00109 return atom_get(ATOM_GUID, k);
00110 }
00111
00112 static inline void
00113 atom_guid_free(const gchar *k)
00114 {
00115 return atom_free(ATOM_GUID, k);
00116 }
00117
00118 static inline const struct sha1 *
00119 atom_sha1_get(const struct sha1 *k)
00120 {
00121 return atom_get(ATOM_SHA1, k);
00122 }
00123
00124 static inline void
00125 atom_sha1_free(const struct sha1 *k)
00126 {
00127 return atom_free(ATOM_SHA1, k);
00128 }
00129
00130 static inline const struct tth *
00131 atom_tth_get(const struct tth *k)
00132 {
00133 return atom_get(ATOM_TTH, k);
00134 }
00135
00136 static inline void
00137 atom_tth_free(const struct tth *k)
00138 {
00139 return atom_free(ATOM_TTH, k);
00140 }
00141
00142 static inline const guint64 *
00143 atom_uint64_get(const guint64 *k)
00144 {
00145 return atom_get(ATOM_UINT64, k);
00146 }
00147
00148 static inline void
00149 atom_uint64_free(const guint64 *k)
00150 {
00151 return atom_free(ATOM_UINT64, k);
00152 }
00153
00154 static inline const filesize_t *
00155 atom_filesize_get(const filesize_t *k)
00156 {
00157 return atom_get(ATOM_FILESIZE, k);
00158 }
00159
00160 static inline void
00161 atom_filesize_free(const filesize_t *k)
00162 {
00163 return atom_free(ATOM_FILESIZE, k);
00164 }
00165
00166 #endif
00167
00168
00169
00170
00171
00172 void atoms_init(void);
00173 void atoms_close(void);
00174
00175
00176
00177
00178 guint filesize_hash(gconstpointer key);
00179 gint filesize_eq(gconstpointer a, gconstpointer b);
00180 guint sha1_hash(gconstpointer key);
00181 gint sha1_eq(gconstpointer a, gconstpointer b);
00182 guint tth_hash(gconstpointer key);
00183 gint tth_eq(gconstpointer a, gconstpointer b);
00184 guint guid_hash(gconstpointer key);
00185 gint guid_eq(gconstpointer a, gconstpointer b);
00186 guint uint64_hash(gconstpointer key);
00187 gint uint64_eq(gconstpointer a, gconstpointer b);
00188 guint binary_hash(const guchar *key, guint len);
00189
00190 #ifdef TRACK_ATOMS
00191 gconstpointer atom_get_track(enum atom_type, gconstpointer key,
00192 gchar *file, gint line);
00193 void atom_free_track(enum atom_type, gconstpointer key, gchar *file, gint line);
00194 #endif
00195
00196
00202 #define GENERATE_ATOM_FREE_NULL(name, type) \
00203 static inline void \
00204 atom_ ## name ## _free_null(const type *k_ptr) \
00205 { \
00206 if (*k_ptr) { \
00207 atom_ ## name ## _free(*k_ptr); \
00208 *k_ptr = NULL; \
00209 } \
00210 }
00211
00212 struct sha1;
00213 struct tth;
00214
00215 GENERATE_ATOM_FREE_NULL(filesize, filesize_t *)
00216 GENERATE_ATOM_FREE_NULL(guid, gchar *)
00217 GENERATE_ATOM_FREE_NULL(sha1, struct sha1 *)
00218 GENERATE_ATOM_FREE_NULL(str, gchar *)
00219 GENERATE_ATOM_FREE_NULL(tth, struct tth *)
00220 GENERATE_ATOM_FREE_NULL(uint64, guint64 *)
00221 #undef GENERATE_ATOM_FREE_NULL
00222
00229 #define GENERATE_ATOM_CHANGE(name, type) \
00230 static inline void \
00231 atom_ ## name ## _change(const type *atom_ptr, const type value) \
00232 { \
00233 const void *atom = value ? atom_ ## name ## _get(value) : NULL; \
00234 atom_ ## name ## _free_null(atom_ptr); \
00235 *atom_ptr = atom; \
00236 }
00237
00238 GENERATE_ATOM_CHANGE(filesize, filesize_t *)
00239 GENERATE_ATOM_CHANGE(guid, gchar *)
00240 GENERATE_ATOM_CHANGE(sha1, struct sha1 *)
00241 GENERATE_ATOM_CHANGE(str, gchar *)
00242 GENERATE_ATOM_CHANGE(tth, struct tth *)
00243 GENERATE_ATOM_CHANGE(uint64, guint64 *)
00244 #undef GENERATE_ATOM_CHANGE
00245
00246 #endif
00247
00248
00249