summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/autotrace/xstd.h
blob: 18e4d5bf14520a70a05ba271bcc780bca9de1405 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* xstd.h: Wrappers for functions in C standard library
 Was: xmem, xfile */

/* These call the corresponding function in the standard library, and
   abort if those routines fail. */

#ifndef XSTD_H
#define XSTD_H

#include "types.h"
#include <assert.h>
#include <stdlib.h>

/*
 * XMEM
 */
#ifndef __cplusplus
#define XMALLOC(new_mem, size)			\
do						\
  {						\
    new_mem = (gpointer) malloc (size);	\
    assert(new_mem);				\
  } while (0)

#define XCALLOC(new_mem, size)			\
do						\
  {						\
    new_mem = (gpointer) calloc (size, 1);	\
    assert(new_mem);				\
  } while (0)

#define XREALLOC(old_ptr, size)				\
do							\
  {							\
    gpointer new_mem;					\
							\
    if (old_ptr == NULL)				\
      XMALLOC (new_mem, size);				\
    else						\
      {							\
        new_mem = (gpointer) realloc (old_ptr, size);	\
        assert(new_mem);				\
      }							\
							\
    old_ptr = new_mem;					\
} while (0)

#else
/* Use templates if Cplusplus... */
#define XMALLOC(new_mem, size)					\
do								\
  {								\
    (gpointer&)(new_mem) = (gpointer) malloc (size);	\
     assert(new_mem);						\
  } while (0)

#define XCALLOC(new_mem, sizex)					\
do								\
  {								\
    (gpointer&)(new_mem) = (void *) calloc (sizex, 1);	\
    assert(new_mem);						\
  } while (0)

#define XREALLOC(old_ptr, size)						  \
do									  \
  {									  \
    gpointer new_mem;							  \
									  \
    if (old_ptr == NULL)						  \
      XMALLOC (new_mem, (size));					  \
    else								  \
      {									  \
        (gpointer&) new_mem = (gpointer) realloc ((old_ptr), (size)); \
        assert(new_mem);						  \
      }									  \
									  \
    (gpointer&)old_ptr = new_mem;					  \
  } while (0)
#endif

#endif /* Not XSTD_H */