Coverity fixes from Nelson
[sxemacs] / src / sxe-utils.h
1 /* Utility definitions for C code of SXEmacs 
2
3    Copyright (C) 1985-1987, 1992-1995 Free Software Foundation, Inc.
4    Copyright (C) 1993-1996 Richard Mlynarik.
5    Copyright (C) 1995, 1996, 2000 Ben Wing.
6    Copyright (C) 2004 Steve Youngs.
7    Copyright (C) 2011 Nelson Ferreira.
8
9 This file is part of SXEmacs
10
11 SXEmacs is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 SXEmacs is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
23
24 /* NOT synched with FSF */
25 #ifndef INCLUDED_sxe_utils_h_
26 #define INCLUDED_sxe_utils_h_
27
28 /* ------------------------ include files ------------------- */
29
30 /* We include the following generally useful header files so that you
31    don't have to worry about prototypes when using the standard C
32    library functions and macros.  These files shouldn't be excessively
33    large so they shouldn't cause that much of a slowdown. */
34
35 #include <stdlib.h>
36 #include <string.h>             /* primarily for memcpy, etc. */
37 #include <stdio.h>              /* NULL, etc. */
38 #include <ctype.h>
39 #include <stdarg.h>
40 #include <stddef.h>             /* offsetof */
41 #include <sys/types.h>
42 #include <limits.h>
43 #if defined HAVE_STDBOOL_H
44 # include <stdbool.h>
45 #endif
46
47 \f
48 /* goodies */
49 #ifdef SXE_UNUSED
50 #elif defined(__GNUC__)
51 # define SXE_UNUSED(x) UNUSED_ ## x __attribute__((unused))
52 #elif defined(__LCLINT__)
53 # define SXE_UNUSED(x) /*@unused@*/ x
54 #else
55 # define SXE_UNUSED(x) x
56 #endif
57 #ifdef UNUSED
58 #undef UNUSED
59 #define UNUSED(x) SXE_UNUSED(x)
60 #endif
61
62 #ifdef WEAK_EXTERN
63 #elif defined(__GNUC__)
64 # define WEAK_EXTERN    extern __attribute__((weak))
65 #else
66 # error "Grrr, bloody 'ell, can't figure out how to create weak symbols"
67 #endif
68
69 #ifdef WEAK
70 #elif defined(__GNUC__)
71 # define WEAK           __attribute__((weak))
72 #else
73 # error "Grrr, bloody 'ell, can't figure out how to create weak symbols"
74 #endif
75
76
77 #ifdef LIKELY
78 #else
79 #define LIKELY(_x)      __builtin_expect(!!(_x), 1)
80 #endif
81 #ifdef UNLIKELY
82 #else
83 #define UNLIKELY(_x)    __builtin_expect(!!(_x), 0)
84 #endif
85
86 \f
87
88 #if defined SXE_STATIC_EXTERN_INLINE
89 # define extern_inline  static inline
90 #elif !defined __GNUC_STDC_INLINE__ && !defined SXE_INLINE_NO_EXTERN
91 # define extern_inline  extern inline
92 #else
93 # define extern_inline  inline
94 #endif
95
96 #ifdef ALL_DEBUG_FLAGS
97 #undef SXE_DEBUG_FLAG
98 #define SXE_DEBUG_FLAG
99 #endif
100
101 #define __SXE_DEBUG__(args...)          fprintf(stderr, "SXE " args)
102 #ifndef SXE_DEBUG_FLAG
103 #define SXE_DEBUG(args...)
104 #else
105 #define SXE_DEBUG(args...)              __SXE_DEBUG__(args)
106 #endif
107 #define SXE_DEBUG_PT(args...)           SXE_DEBUG("[pthread]: " args)
108 #define SXE_CRITICAL(args...)           __SXE_DEBUG__("CRITICAL: " args)
109
110
111
112 /* We define assert iff USE_ASSERTIONS or DEBUG_SXEMACS is defined.
113    Otherwise we define it to be empty.  Quantify has shown that the
114    time the assert checks take is measurable so let's not include them
115    in production binaries. */
116
117 #ifdef USE_ASSERTIONS
118 /* Highly dubious kludge */
119 /*   (thanks, Jamie, I feel better now -- ben) */
120 void assert_failed(const char *, int, const char *);
121 # define abort() (assert_failed (__FILE__, __LINE__, "abort()"))
122 # define assert(x) ((x) ? (void) 0 : assert_failed (__FILE__, __LINE__, #x))
123 #else
124 # ifdef DEBUG_SXEMACS
125 #  define assert(x) ((x) ? (void) 0 : (void) abort ())
126 # else
127 #  define assert(x)
128 # endif
129 #endif
130
131 /* from c.ac */
132 #ifndef BITS_PER_CHAR
133 #define BITS_PER_CHAR 8
134 #endif
135 #define SXE_SHORTBITS (SIZEOF_SHORT * BITS_PER_CHAR)
136 #define SXE_INTBITS (SIZEOF_INT * BITS_PER_CHAR)
137 #define SXE_LONGBITS (SIZEOF_LONG * BITS_PER_CHAR)
138 #define SXE_LONG_LONG_BITS (SIZEOF_LONG_LONG_INT * BITS_PER_CHAR)
139 #define SXE_VOID_P_BITS (SIZEOF_VOID_P * BITS_PER_CHAR)
140
141 /* Also define min() and max(). (Some compilers put them in strange
142    places that won't be referenced by the above include files, such
143    as 'macros.h' under Solaris.) */
144
145 #ifndef min
146 #define min(a,b) (((a) <= (b)) ? (a) : (b))
147 #endif
148 #ifndef max
149 #define max(a,b) (((a) > (b)) ? (a) : (b))
150 #endif
151
152 \f
153 /* generally useful */
154 #define countof(x) ((int) (sizeof(x)/sizeof((x)[0])))
155 #define xnew(type) ((type *) xmalloc (sizeof (type)))
156 #define xnew_atomic(type) ((type *) xmalloc_atomic (sizeof (type)))
157 #define xnew_array(type, len) ((type *) xmalloc ((len) * sizeof (type)))
158 #define xnew_atomic_array(type, len)                    \
159         ((type*)xmalloc_atomic((len) * sizeof(type)))
160 #define xnew_and_zero(type) ((type *) xmalloc_and_zero (sizeof (type)))
161 #define xzero(lvalue) ((void) memset (&(lvalue), '\0', sizeof (lvalue)))
162 #define xnew_array_and_zero(type, len)                          \
163         ((type*)xmalloc_and_zero ((len) * sizeof (type)))
164 #define xrealloc_array(ptr, type, len)                          \
165         ((void) (ptr = (type *) xrealloc (ptr, (len) * sizeof (type))))
166 #define XREALLOC_ARRAY          xrealloc_array
167 #define alloca_array(type, len) ((type *) alloca ((len) * sizeof (type)))
168
169 #if !defined HAVE_DECL_STRDUP
170 extern char *strdup(const char *s);
171 #endif  /* HAVE_DECL_STRDUP */
172
173 \f
174 #ifndef PRINTF_ARGS
175 # if defined (__GNUC__) && (__GNUC__ >= 2)
176 #  define PRINTF_ARGS(string_index,first_to_check) \
177           __attribute__ ((format (printf, string_index, first_to_check)))
178 # else
179 #  define PRINTF_ARGS(string_index,first_to_check)
180 # endif                         /* GNUC */
181 #endif
182
183 #ifndef DOESNT_RETURN
184 # if defined __GNUC__
185 #  if ((__GNUC__ > 2) || (__GNUC__ == 2) && (__GNUC_MINOR__ >= 5))
186 #   define DOESNT_RETURN void
187 #   define DECLARE_DOESNT_RETURN(decl) \
188            extern void decl __attribute__ ((noreturn))
189 #   define DECLARE_DOESNT_RETURN_GCC_ATTRIBUTE_SYNTAX_SUCKS(decl,str,idx) \
190      /* Should be able to state multiple independent __attribute__s, but  \
191         the losing syntax doesn't work that way, and screws losing cpp */ \
192            extern void decl \
193                   __attribute__ ((noreturn, format (printf, str, idx)))
194 #  else
195 #   define DOESNT_RETURN void volatile
196 #   define DECLARE_DOESNT_RETURN(decl) extern void volatile decl
197 #   define DECLARE_DOESNT_RETURN_GCC_ATTRIBUTE_SYNTAX_SUCKS(decl,str,idx) \
198            extern void volatile decl PRINTF_ARGS(str,idx)
199 #  endif                        /* GNUC 2.5 */
200 # else
201 #  define DOESNT_RETURN void
202 #  define DECLARE_DOESNT_RETURN(decl) extern void decl
203 #  define DECLARE_DOESNT_RETURN_GCC_ATTRIBUTE_SYNTAX_SUCKS(decl,str,idx) \
204           extern void decl PRINTF_ARGS(str,idx)
205 # endif                         /* GNUC */
206 #endif
207
208 \f
209 /* No type has a greater alignment requirement than max_align_t.
210    (except perhaps for types we don't use, like long double) */
211 typedef union {
212         struct {
213                 long l;
214         } l;
215         struct {
216                 void *p;
217         } p;
218         struct {
219                 void (*f) (void);
220         } f;
221         struct {
222                 double d;
223         } d;
224 } max_align_t;
225
226 #ifndef ALIGNOF
227 # if defined (__GNUC__) && (__GNUC__ >= 2)
228 /* gcc has an extension that gives us exactly what we want. */
229 #  define ALIGNOF(type) __alignof__ (type)
230 # elif ! defined (__cplusplus)
231 /* The following is mostly portable, except that:
232    - it doesn't work for inside out declarations like void (*) (void).
233      (so just call ALIGNOF with a typedef'ed name)
234    - it doesn't work with C++.  The C++ committee has decided,
235      in its infinite wisdom, that:
236      "Types must be declared in declarations, not in expressions." */
237 #  define ALIGNOF(type) offsetof (struct { char c; type member; }, member)
238 # else
239 /* C++ is annoying, but it has a big bag of tricks.
240    The following doesn't have the "inside out" declaration bug C does. */
241 template < typename T > struct alignment_trick {
242         char c;
243         T member;
244 };
245 #  define ALIGNOF(type) offsetof (alignment_trick<type>, member)
246 # endif
247 #endif                          /* ALIGNOF */
248
249 #define ALIGN_SIZE(len, unit) \
250   ((((len) + (unit) - 1) / (unit)) * (unit))
251
252 /* #### Yuck, this is kind of evil */
253 #define ALIGN_PTR(ptr, unit) \
254   ((void *) ALIGN_SIZE ((size_t) (ptr), unit))
255
256 #ifndef DO_NOTHING
257 #define DO_NOTHING do {} while (0)
258 #endif
259
260 #ifndef DECLARE_NOTHING
261 #define DECLARE_NOTHING struct nosuchstruct
262 #endif
263
264 \f
265 /************************************************************************/
266 /*                                Memory                                */
267 /************************************************************************/
268
269 #ifdef ALL_DEBUG_FLAGS
270 #undef GC_DEBUG_FLAG
271 #define GC_DEBUG_FLAG
272 #endif
273
274 #if !defined GC_DEBUG_FLAG
275 # define SXE_DEBUG_GC(args...)
276 #else
277 # define SXE_DEBUG_GC(args...)          __SXE_DEBUG__("[gc] " args)
278 #endif
279 #define SXE_DEBUG_GC_PT(args...)        SXE_DEBUG_GC("[pthread]: " args)
280 #define SXE_CRITICAL_GC(args...)        __SXE_DEBUG__("[gc] CRITICAL: " args)
281
282 void malloc_warning(const char *);
283
284 #if defined HAVE_BDWGC && defined EF_USE_BDWGC
285 # if defined HAVE_THREADS
286 #  if !defined GC_PTHREADS
287 #   define GC_PTHREADS  1
288 #  endif  /* !GC_PTHREADS */
289 #  if !defined GC_THREADS
290 #   define GC_THREADS   1
291 #  endif  /* !GC_THREADS */
292 # endif  /* HAVE_THREADS */
293
294 # undef GC_DEBUG
295 # if defined GC_DEBUG_FLAG
296 #  define GC_DEBUG      1
297 # endif  /* GC_DEBUG_FLAG */
298 # if defined HAVE_GC_GC_H
299 #  include <gc/gc.h>
300 # elif defined HAVE_GC_H
301 #  include <gc.h>
302 # else
303 #  error "Take less of those pills!"
304 # endif
305
306 # if defined GC_DEBUG_FLAG
307 #  define zmalloc               GC_MALLOC_IGNORE_OFF_PAGE
308 #  define zcalloc(n, m) GC_MALLOC((n)*(m))
309 #  define zmalloc_atomic        GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE
310 #  define zmalloc_and_zero      GC_MALLOC
311 #  define zrealloc              GC_REALLOC
312 #  define zstrdup               GC_STRDUP
313 #  undef zfree
314 #  define zfree(x)              GC_FREE(x)
315 # else  /* !GC_DEBUG_FLAG */
316 #  define zmalloc               GC_malloc_ignore_off_page
317 #  define zcalloc(n, m)         GC_malloc((n)*(m))
318 #  define zmalloc_atomic        GC_malloc_atomic_ignore_off_page
319 #  define zmalloc_and_zero      GC_malloc
320 #  define zrealloc              GC_realloc
321 #  define zstrdup               GC_strdup
322 #  undef zfree
323 #  define zfree(x)
324 # endif /* GC_DEBUG_FLAG */
325
326 #else  /* !BDWGC */
327 #define zmalloc         F&^!
328 #define zcalloc         F&^!
329 #define zmalloc_atomic  F&^!
330 #define zmalloc_and_zero        F&^!
331 #define zrealloc        F&^!
332 #define zstrdrup        F&^!
333 #endif  /* BDWGC */
334
335 /* also define libc mem funs */
336 #define ymalloc         malloc
337 #define ycalloc(n, m)   calloc(n, m)
338 #define ymalloc_atomic(n)       ycalloc(1, n)
339 #define ymalloc_and_zero(x)     ycalloc(1, x)
340 #define yrealloc        realloc
341 #define ystrdup         strdup
342 #define yfree(x)        free(x)
343 /* and their convenience companions */
344 #define ynew(type)              ((type*)ymalloc(sizeof(type)))
345 #define ynew_array(type, len)   ((type*)ymalloc((len) * sizeof(type)))
346 #define ynew_and_zero(type)     ((type*)ymalloc_and_zero(sizeof(type)))
347 #define ynew_array_and_zero(type, len)                  \
348         ((type*)ymalloc_and_zero((len) * sizeof(type)))
349 #define YREALLOC_ARRAY(ptr, type, len)                                  \
350         ((void)(ptr = (type *)yrealloc(ptr, (len) * sizeof (type))))
351
352 #if defined HAVE_BDWGC && defined EF_USE_BDWGC
353 /* and now the x* series */
354 # define xmalloc                zmalloc
355 # define xcalloc                zcalloc
356 # define xmalloc_atomic         zmalloc_atomic
357 # define xmalloc_and_zero       zmalloc_and_zero
358 # define xrealloc               zrealloc
359 # define xstrdup                zstrdup
360 # if defined ERROR_CHECK_MALLOC
361 #  define xfree(args...)        zfree(args)
362 # else  /* !ERROR_CHECK_MALLOC */
363 #  define xfree(args...)
364 # endif  /* ERROR_CHECK_MALLOC */
365
366 #else  /* !BDWGC */
367 void *xmalloc(size_t size);
368 void *xmalloc_atomic(size_t size);
369 void *xmalloc_and_zero(size_t size);
370 void *xrealloc(void *, size_t size);
371 char *xstrdup(const char *);
372 # if defined ERROR_CHECK_MALLOC
373 #  if SIZEOF_VOID_P == 4
374 #   define xfree(lvalue)                                        \
375         do {                                                    \
376                 void *volatile *xfree_ptr =                     \
377                         (void *volatile*)                       \
378                         ((volatile void*)&(lvalue));            \
379                 assert(*xfree_ptr != (void*)0xB00BB4BE);        \
380                 yfree(*xfree_ptr);                              \
381                 *xfree_ptr = (void*)0xB00BB4BE;                 \
382         } while (0)
383 #  elif SIZEOF_VOID_P == 8
384 #   define xfree(lvalue)                                                        \
385         do {                                                            \
386                 void *volatile *xfree_ptr =                             \
387                         (void *volatile*)                               \
388                         ((volatile void*)&(lvalue));                    \
389                 assert(*xfree_ptr != (void*)0xCAFEBABEDEADBEEF);        \
390                 yfree(*xfree_ptr);                                      \
391                 *xfree_ptr = (void*)0xCAFEBABEDEADBEEF;                 \
392         } while (0)
393 #  else  /* huh? */
394 #   error "Strange-arse system detected.  Watch a movie, it\'s more fun!"
395 #  endif
396 # else  /* !ERROR_CHECK_MALLOC */
397 #  define xfree(args...)        yfree(args)
398 # endif  /* ERROR_CHECK_MALLOC */
399 #endif  /* BDWGC */
400
401 #endif