Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

atoms.h

Go to the documentation of this file.
00001 /*
00002  * $Id: atoms.h 13946 2007-06-24 00:43:12Z cbiere $
00003  *
00004  * Copyright (c) 2002-2003, Raphael Manfredi
00005  *
00006  *----------------------------------------------------------------------
00007  * This file is part of gtk-gnutella.
00008  *
00009  *  gtk-gnutella is free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  gtk-gnutella is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU General Public License
00020  *  along with gtk-gnutella; if not, write to the Free Software
00021  *  Foundation, Inc.:
00022  *      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *----------------------------------------------------------------------
00024  */
00025 
00036 #ifndef _atoms_h_
00037 #define _atoms_h_
00038 
00039 #include "common.h"
00040 
00041 /*
00042  * Atom types.
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  * Convenience macros.
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   /* !TRACK_ATOMS */
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  /* TRACK_ATOMS */
00167 
00168 /*
00169  * Public interface.
00170  */
00171 
00172 void atoms_init(void);
00173 void atoms_close(void);
00174 
00175 /*
00176  * Hash functions and equality checks
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  /* _atoms_h_ */
00247 
00248 /* vi: set ts=4 sw=4 cindent: */
00249 

Generated on Sat Jun 30 17:53:22 2007 for gtk-gnutella by  doxygen 1.3.9.1