Fix the inlining magic. (#157536, Jens Hatlak, and #149907, Morten
[platform/upstream/glib.git] / glib / gutils.h
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #ifndef __G_UTILS_H__
28 #define __G_UTILS_H__
29
30 #include <glib/gtypes.h>
31 #include <stdarg.h>
32
33 G_BEGIN_DECLS
34
35 #ifdef G_OS_WIN32
36
37 /* On native Win32, directory separator is the backslash, and search path
38  * separator is the semicolon.
39  */
40 #define G_DIR_SEPARATOR '\\'
41 #define G_DIR_SEPARATOR_S "\\"
42 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR || (c) == '/')
43 #define G_SEARCHPATH_SEPARATOR ';'
44 #define G_SEARCHPATH_SEPARATOR_S ";"
45
46 #else  /* !G_OS_WIN32 */
47
48 /* Unix */
49
50 #define G_DIR_SEPARATOR '/'
51 #define G_DIR_SEPARATOR_S "/"
52 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR)
53 #define G_SEARCHPATH_SEPARATOR ':'
54 #define G_SEARCHPATH_SEPARATOR_S ":"
55
56 #endif /* !G_OS_WIN32 */
57
58 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
59  * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
60  */
61 #if !defined (G_VA_COPY)
62 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
63 #    define G_VA_COPY(ap1, ap2)   (*(ap1) = *(ap2))
64 #  elif defined (G_VA_COPY_AS_ARRAY)
65 #    define G_VA_COPY(ap1, ap2)   g_memmove ((ap1), (ap2), sizeof (va_list))
66 #  else /* va_list is a pointer */
67 #    define G_VA_COPY(ap1, ap2)   ((ap1) = (ap2))
68 #  endif /* va_list is a pointer */
69 #endif /* !G_VA_COPY */
70
71 /* inlining hassle. for compilers that don't allow the `inline' keyword,
72  * mostly because of strict ANSI C compliance or dumbness, we try to fall
73  * back to either `__inline__' or `__inline'.
74  * G_CAN_INLINE is defined in glibconfig.h if the compiler seems to be 
75  * actually *capable* to do function inlining, in which case inline 
76  * function bodies do make sense. we also define G_INLINE_FUNC to properly 
77  * export the function prototypes if no inlining can be performed.
78  * inline function bodies have to be special cased with G_CAN_INLINE and a
79  * .c file specific macro to allow one compiled instance with extern linkage
80  * of the functions by defining G_IMPLEMENT_INLINES and the .c file macro.
81  */
82 #if defined (G_HAVE_INLINE) && defined (__GNUC__) && defined (__STRICT_ANSI__)
83 #  undef inline
84 #  define inline __inline__
85 #elif !defined (G_HAVE_INLINE)
86 #  undef inline
87 #  if defined (G_HAVE___INLINE__)
88 #    define inline __inline__
89 #  elif defined (G_HAVE___INLINE)
90 #    define inline __inline
91 #  else /* !inline && !__inline__ && !__inline */
92 #    define inline  /* don't inline, then */
93 #  endif
94 #endif
95 #ifdef G_IMPLEMENT_INLINES
96 #  define G_INLINE_FUNC
97 #elif defined (__GNUC__) && defined (__OPTIMIZE__)
98 #  define G_INLINE_FUNC extern inline
99 #elif defined (G_CAN_INLINE) && !defined (__GNUC__)
100 #  define G_INLINE_FUNC static inline
101 #else /* can't inline */
102 #  define G_INLINE_FUNC
103 #endif /* !G_INLINE_FUNC */
104
105 /* Retrive static string info
106  */
107 #ifdef G_OS_WIN32
108 #define g_get_user_name g_get_user_name_utf8
109 #define g_get_real_name g_get_real_name_utf8
110 #define g_get_home_dir g_get_home_dir_utf8
111 #define g_get_tmp_dir g_get_tmp_dir_utf8
112 #endif
113
114 G_CONST_RETURN gchar* g_get_user_name        (void);
115 G_CONST_RETURN gchar* g_get_real_name        (void);
116 G_CONST_RETURN gchar* g_get_home_dir         (void);
117 G_CONST_RETURN gchar* g_get_tmp_dir          (void);
118 gchar*                g_get_prgname          (void);
119 void                  g_set_prgname          (const gchar *prgname);
120 G_CONST_RETURN gchar* g_get_application_name (void);
121 void                  g_set_application_name (const gchar *application_name);
122
123 G_CONST_RETURN gchar*    g_get_user_data_dir      (void);
124 G_CONST_RETURN gchar*    g_get_user_config_dir    (void);
125 G_CONST_RETURN gchar*    g_get_user_cache_dir     (void);
126 G_CONST_RETURN gchar* G_CONST_RETURN * g_get_system_data_dirs   (void);
127 G_CONST_RETURN gchar* G_CONST_RETURN * g_get_system_config_dirs (void);
128
129 G_CONST_RETURN gchar* G_CONST_RETURN * g_get_language_names (void);
130
131 typedef struct _GDebugKey       GDebugKey;
132 struct _GDebugKey
133 {
134   gchar *key;
135   guint  value;
136 };
137
138 /* Miscellaneous utility functions
139  */
140 guint                 g_parse_debug_string (const gchar     *string,
141                                             const GDebugKey *keys,
142                                             guint            nkeys);
143
144 gint                  g_snprintf           (gchar       *string,
145                                             gulong       n,
146                                             gchar const *format,
147                                             ...) G_GNUC_PRINTF (3, 4);
148 gint                  g_vsnprintf          (gchar       *string,
149                                             gulong       n,
150                                             gchar const *format,
151                                             va_list      args);
152
153 /* Check if a file name is an absolute path */
154 gboolean              g_path_is_absolute   (const gchar *file_name);
155
156 /* In case of absolute paths, skip the root part */
157 G_CONST_RETURN gchar* g_path_skip_root     (const gchar *file_name);
158
159 #ifndef G_DISABLE_DEPRECATED
160
161 /* These two functions are deprecated and will be removed in the next
162  * major release of GLib. Use g_path_get_dirname/g_path_get_basename
163  * instead. Whatch out! The string returned by g_path_get_basename
164  * must be g_freed, while the string returned by g_basename must not.*/
165 G_CONST_RETURN gchar* g_basename           (const gchar *file_name);
166 #define g_dirname g_path_get_dirname
167
168 #endif /* G_DISABLE_DEPRECATED */
169
170 #ifdef G_OS_WIN32
171 #define g_get_current_dir g_get_current_dir_utf8
172 #endif
173
174 /* The returned strings are newly allocated with g_malloc() */
175 gchar*                g_get_current_dir    (void);
176 gchar*                g_path_get_basename  (const gchar *file_name) G_GNUC_MALLOC;
177 gchar*                g_path_get_dirname   (const gchar *file_name) G_GNUC_MALLOC;
178
179 /* Set the pointer at the specified location to NULL */
180 void                  g_nullify_pointer    (gpointer    *nullify_location);
181
182 /* return the environment string for the variable. The returned memory
183  * must not be freed. */
184 #ifdef G_OS_WIN32
185 #define g_getenv g_getenv_utf8
186 #define g_setenv g_setenv_utf8
187 #define g_unsetenv g_unsetenv_utf8
188 #define g_find_program_in_path g_find_program_in_path_utf8
189 #endif
190
191 G_CONST_RETURN gchar* g_getenv             (const gchar *variable);
192 gboolean              g_setenv             (const gchar *variable,
193                                             const gchar *value,
194                                             gboolean     overwrite);
195 void                  g_unsetenv           (const gchar *variable);
196
197
198 /* we try to provide a usefull equivalent for ATEXIT if it is
199  * not defined, but use is actually abandoned. people should
200  * use g_atexit() instead.
201  */
202 typedef void            (*GVoidFunc)            (void);
203 #ifndef ATEXIT
204 # define ATEXIT(proc)   g_ATEXIT(proc)
205 #else
206 # define G_NATIVE_ATEXIT
207 #endif /* ATEXIT */
208 /* we use a GLib function as a replacement for ATEXIT, so
209  * the programmer is not required to check the return value
210  * (if there is any in the implementation) and doesn't encounter
211  * missing include files.
212  */
213 void    g_atexit                (GVoidFunc    func);
214
215 /* Look for an executable in PATH, following execvp() rules */
216 gchar*  g_find_program_in_path  (const gchar *program);
217
218 /* Bit tests
219  */
220 G_INLINE_FUNC gint      g_bit_nth_lsf (gulong  mask,
221                                        gint    nth_bit);
222 G_INLINE_FUNC gint      g_bit_nth_msf (gulong  mask,
223                                        gint    nth_bit);
224 G_INLINE_FUNC guint     g_bit_storage (gulong  number);
225
226 /* Trash Stacks
227  * elements need to be >= sizeof (gpointer)
228  */
229 typedef struct _GTrashStack     GTrashStack;
230 struct _GTrashStack
231 {
232   GTrashStack *next;
233 };
234
235 G_INLINE_FUNC void      g_trash_stack_push      (GTrashStack **stack_p,
236                                                  gpointer      data_p);
237 G_INLINE_FUNC gpointer  g_trash_stack_pop       (GTrashStack **stack_p);
238 G_INLINE_FUNC gpointer  g_trash_stack_peek      (GTrashStack **stack_p);
239 G_INLINE_FUNC guint     g_trash_stack_height    (GTrashStack **stack_p);
240
241 /* inline function implementations
242  */
243 #if defined (G_CAN_INLINE) || defined (__G_UTILS_C__)
244 G_INLINE_FUNC gint
245 g_bit_nth_lsf (gulong mask,
246                gint   nth_bit)
247 {
248   do
249     {
250       nth_bit++;
251       if (mask & (1UL << nth_bit))
252         return nth_bit;
253     }
254   while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1));
255   return -1;
256 }
257 G_INLINE_FUNC gint
258 g_bit_nth_msf (gulong mask,
259                gint   nth_bit)
260 {
261   if (nth_bit < 0)
262     nth_bit = GLIB_SIZEOF_LONG * 8;
263   do
264     {
265       nth_bit--;
266       if (mask & (1UL << nth_bit))
267         return nth_bit;
268     }
269   while (nth_bit > 0);
270   return -1;
271 }
272 G_INLINE_FUNC guint
273 g_bit_storage (gulong number)
274 {
275   register guint n_bits = 0;
276   
277   do
278     {
279       n_bits++;
280       number >>= 1;
281     }
282   while (number);
283   return n_bits;
284 }
285 G_INLINE_FUNC void
286 g_trash_stack_push (GTrashStack **stack_p,
287                     gpointer      data_p)
288 {
289   GTrashStack *data = (GTrashStack *) data_p;
290
291   data->next = *stack_p;
292   *stack_p = data;
293 }
294 G_INLINE_FUNC gpointer
295 g_trash_stack_pop (GTrashStack **stack_p)
296 {
297   GTrashStack *data;
298
299   data = *stack_p;
300   if (data)
301     {
302       *stack_p = data->next;
303       /* NULLify private pointer here, most platforms store NULL as
304        * subsequent 0 bytes
305        */
306       data->next = NULL;
307     }
308
309   return data;
310 }
311 G_INLINE_FUNC gpointer
312 g_trash_stack_peek (GTrashStack **stack_p)
313 {
314   GTrashStack *data;
315
316   data = *stack_p;
317
318   return data;
319 }
320 G_INLINE_FUNC guint
321 g_trash_stack_height (GTrashStack **stack_p)
322 {
323   GTrashStack *data;
324   guint i = 0;
325
326   for (data = *stack_p; data; data = data->next)
327     i++;
328
329   return i;
330 }
331 #endif  /* G_CAN_INLINE || __G_UTILS_C__ */
332
333 /* Glib version.
334  * we prefix variable declarations so they can
335  * properly get exported in windows dlls.
336  */
337 GLIB_VAR const guint glib_major_version;
338 GLIB_VAR const guint glib_minor_version;
339 GLIB_VAR const guint glib_micro_version;
340 GLIB_VAR const guint glib_interface_age;
341 GLIB_VAR const guint glib_binary_age;
342
343 const gchar * glib_check_version (guint required_major,
344                                   guint required_minor,
345                                   guint required_micro);
346
347 #define GLIB_CHECK_VERSION(major,minor,micro)    \
348     (GLIB_MAJOR_VERSION > (major) || \
349      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
350      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
351       GLIB_MICRO_VERSION >= (micro)))
352
353 G_END_DECLS
354
355 /*
356  * On Windows, this macro defines a DllMain function that stores the
357  * actual DLL name that the code being compiled will be included in.
358  * STATIC should be empty or 'static'. DLL_NAME is the name of the
359  * (pointer to the) char array where the DLL name will be stored. If
360  * this is used, you must also include <windows.h>. If you need a more complex
361  * DLL entry point function, you cannot use this.
362  *
363  * On non-Windows platforms, expands to nothing.
364  */
365
366 #ifndef G_PLATFORM_WIN32
367 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
368 #else
369 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)                    \
370 static char *dll_name;                                                     \
371                                                                            \
372 BOOL WINAPI                                                                \
373 DllMain (HINSTANCE hinstDLL,                                               \
374          DWORD     fdwReason,                                              \
375          LPVOID    lpvReserved)                                            \
376 {                                                                          \
377   char bfr[1000];                                                          \
378   switch (fdwReason)                                                       \
379     {                                                                      \
380     case DLL_PROCESS_ATTACH:                                               \
381       GetModuleFileName ((HMODULE) hinstDLL, bfr, sizeof (bfr));           \
382       dll_name = g_path_get_basename (bfr);                                \
383       break;                                                               \
384     }                                                                      \
385                                                                            \
386   return TRUE;                                                             \
387 }
388 #endif /* G_PLATFORM_WIN32 */
389
390 #endif /* __G_UTILS_H__ */