abyss_mallocvar.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef MALLOCVAR_INCLUDED
00011 #define MALLOCVAR_INCLUDED
00012
00013 #include <xmlrpc-c/config.h>
00014
00015 #include <limits.h>
00016 #include <stdlib.h>
00017
00018 static __inline__ void
00019 mallocProduct(void ** const resultP,
00020 unsigned int const factor1,
00021 unsigned int const factor2) {
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 if (factor1 == 0 || factor2 == 0)
00038 *resultP = malloc(1);
00039 else {
00040 if (UINT_MAX / factor2 < factor1)
00041 *resultP = NULL;
00042 else
00043 *resultP = malloc(factor1 * factor2);
00044 }
00045 }
00046
00047
00048
00049 static __inline__ void
00050 reallocProduct(void ** const blockP,
00051 unsigned int const factor1,
00052 unsigned int const factor2) {
00053
00054 if (UINT_MAX / factor2 < factor1)
00055 *blockP = NULL;
00056 else
00057 *blockP = realloc(*blockP, factor1 * factor2);
00058 }
00059
00060
00061
00062
00063
00064
00065 #define MALLOCARRAY(arrayName, nElements) do { \
00066 void * array; \
00067 mallocProduct(&array, nElements, sizeof(arrayName[0])); \
00068 arrayName = array; \
00069 } while (0)
00070
00071 #define REALLOCARRAY(arrayName, nElements) { \
00072 void * array = arrayName; \
00073 reallocProduct(&array, nElements, sizeof(arrayName[0])); \
00074 arrayName = array; \
00075 } while (0)
00076
00077
00078 #define MALLOCARRAY_NOFAIL(arrayName, nElements) \
00079 do { \
00080 MALLOCARRAY(arrayName, nElements); \
00081 if ((arrayName) == NULL) \
00082 abort(); \
00083 } while(0)
00084
00085 #define REALLOCARRAY_NOFAIL(arrayName, nElements) \
00086 do { \
00087 REALLOCARRAY(arrayName, nElements); \
00088 if ((arrayName) == NULL) \
00089 abort(); \
00090 } while(0)
00091
00092
00093 #define MALLOCVAR(varName) \
00094 varName = malloc(sizeof(*varName))
00095
00096 #define MALLOCVAR_NOFAIL(varName) \
00097 do {if ((varName = malloc(sizeof(*varName))) == NULL) abort();} while(0)
00098
00099 #endif
00100