initial load of upstream version 1.06.32
[xmlrpc-c] / lib / util / include / mallocvar.h
1 /* These are some dynamic memory allocation facilities.  They are essentially
2    an extension to C, as they do allocations with a cognizance of C 
3    variables.  You can use them to make C read more like a high level
4    language.
5
6    Before including this, you must define an __inline__ macro if your
7    compiler doesn't recognize it as a keyword.
8 */
9
10 #ifndef MALLOCVAR_INCLUDED
11 #define MALLOCVAR_INCLUDED
12
13 #include "xmlrpc_config.h"
14
15 #include <limits.h>
16 #include <stdlib.h>
17
18 static __inline__ void
19 mallocProduct(void **      const resultP, 
20               unsigned int const factor1,
21               unsigned int const factor2) {
22 /*----------------------------------------------------------------------------
23    malloc a space whose size in bytes is the product of 'factor1' and
24    'factor2'.  But if that size cannot be represented as an unsigned int,
25    return NULL without allocating anything.  Also return NULL if the malloc
26    fails.
27
28    If either factor is zero, malloc a single byte.
29
30    Note that malloc() actually takes a size_t size argument, so the
31    proper test would be whether the size can be represented by size_t,
32    not unsigned int.  But there is no reliable indication available to
33    us, like UINT_MAX, of what the limitations of size_t are.  We
34    assume size_t is at least as expressive as unsigned int and that
35    nobody really needs to allocate more than 4GB of memory.
36 -----------------------------------------------------------------------------*/
37     if (factor1 == 0 || factor2 == 0)
38         *resultP = malloc(1);
39     else {
40         if (UINT_MAX / factor2 < factor1) 
41             *resultP = NULL;
42         else 
43             *resultP = malloc(factor1 * factor2); 
44     }
45 }
46
47
48
49 static __inline__ void
50 reallocProduct(void **      const blockP,
51                unsigned int const factor1,
52                unsigned int const factor2) {
53     
54     if (UINT_MAX / factor2 < factor1) 
55         *blockP = NULL;
56     else 
57         *blockP = realloc(*blockP, factor1 * factor2); 
58 }
59
60
61 /* IMPLEMENTATION NOTE:  There are huge strict aliasing pitfalls here
62    if you cast pointers, e.g. (void **)
63 */
64
65 #define MALLOCARRAY(arrayName, nElements) do { \
66     void * array; \
67     mallocProduct(&array, nElements, sizeof(arrayName[0])); \
68     arrayName = array; \
69 } while (0)
70
71 #define REALLOCARRAY(arrayName, nElements) { \
72     void * array = arrayName; \
73     reallocProduct(&array, nElements, sizeof(arrayName[0])); \
74     arrayName = array; \
75 } while (0)
76
77
78 #define MALLOCARRAY_NOFAIL(arrayName, nElements) \
79 do { \
80     MALLOCARRAY(arrayName, nElements); \
81     if ((arrayName) == NULL) \
82         abort(); \
83 } while(0)
84
85 #define REALLOCARRAY_NOFAIL(arrayName, nElements) \
86 do { \
87     REALLOCARRAY(arrayName, nElements); \
88     if ((arrayName) == NULL) \
89         abort(); \
90 } while(0)
91
92
93 #define MALLOCVAR(varName) \
94     varName = malloc(sizeof(*varName))
95
96 #define MALLOCVAR_NOFAIL(varName) \
97     do {if ((varName = malloc(sizeof(*varName))) == NULL) abort();} while(0)
98
99 #endif
100