| 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 "pm_config.h" |
| 14 |
|
| 15 |
#include <limits.h> |
| 16 |
#include <stdlib.h> |
| 17 |
|
| 18 |
#ifdef __cplusplus |
| 19 |
extern "C" { |
| 20 |
#endif |
| 21 |
#if 0 |
| 22 |
} /* to fake out automatic code indenters */ |
| 23 |
#endif |
| 24 |
|
| 25 |
static __inline__ void |
| 26 |
mallocProduct(void ** const resultP, |
| 27 |
size_t const factor1, |
| 28 |
unsigned int const factor2) { |
| 29 |
/*---------------------------------------------------------------------------- |
| 30 |
malloc a space whose size in bytes is the product of 'factor1' and |
| 31 |
'factor2'. But if the malloc fails, or that size is too large even to |
| 32 |
request from malloc, return NULL without allocating anything. |
| 33 |
|
| 34 |
If either factor is zero, malloc a single byte. |
| 35 |
-----------------------------------------------------------------------------*/ |
| 36 |
/* C99 introduces SIZE_MAX, the maximum size_t value. |
| 37 |
|
| 38 |
Pre-C99, we do the best we can, assuming conventional encoding of |
| 39 |
numbers and that size_t is unsigned. |
| 40 |
*/ |
| 41 |
size_t const sizeMax = |
| 42 |
#if defined(SIZE_MAX) |
| 43 |
SIZE_MAX |
| 44 |
#else |
| 45 |
~((size_t)0) |
| 46 |
#endif |
| 47 |
; |
| 48 |
|
| 49 |
if (factor1 == 0 || factor2 == 0) |
| 50 |
*resultP = malloc(1); |
| 51 |
else { |
| 52 |
/* N.B. The type of malloc's argument is size_t */ |
| 53 |
if ((size_t)factor2 != factor2) |
| 54 |
*resultP = NULL; |
| 55 |
else { |
| 56 |
if (sizeMax / factor2 < factor1) |
| 57 |
*resultP = NULL; |
| 58 |
else |
| 59 |
*resultP = malloc(factor1 * factor2); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
static __inline__ void |
| 67 |
reallocProduct(void ** const blockP, |
| 68 |
size_t const factor1, |
| 69 |
unsigned int const factor2) { |
| 70 |
/*---------------------------------------------------------------------------- |
| 71 |
Reallocate the block at *blockPP to size 'factor1' * 'factor2'. |
| 72 |
(Reallocate means make *blockPP point to a block of that size that |
| 73 |
contains the same data as the block to which *blockP points now, with |
| 74 |
additional space as needed at the end). |
| 75 |
|
| 76 |
If *blockPP is null, make *blockPP point to a new block of memory of |
| 77 |
the desired size with no contents. |
| 78 |
|
| 79 |
If the reallocation fails, release any memory pointed by *blockP and |
| 80 |
make *blockP null. |
| 81 |
-----------------------------------------------------------------------------*/ |
| 82 |
size_t const sizeMax = |
| 83 |
#if defined(SIZE_MAX) |
| 84 |
SIZE_MAX |
| 85 |
#else |
| 86 |
~((size_t)0) |
| 87 |
#endif |
| 88 |
; |
| 89 |
|
| 90 |
void * const oldBlockP = *blockP; |
| 91 |
|
| 92 |
void * newBlockP; |
| 93 |
|
| 94 |
/* N.B. The type of realloc's argument is size_t */ |
| 95 |
if ((size_t)factor2 != factor2) |
| 96 |
newBlockP = NULL; |
| 97 |
else { |
| 98 |
if (sizeMax / factor2 < factor1) |
| 99 |
newBlockP = NULL; |
| 100 |
else |
| 101 |
newBlockP = realloc(oldBlockP, factor1 * factor2); |
| 102 |
} |
| 103 |
if (newBlockP) |
| 104 |
*blockP = newBlockP; |
| 105 |
else { |
| 106 |
free(oldBlockP); |
| 107 |
*blockP = NULL; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
#define MALLOCARRAY(arrayName, nElements) do { \ |
| 114 |
void * array; \ |
| 115 |
mallocProduct(&array, nElements, sizeof(arrayName[0])); \ |
| 116 |
arrayName = array; \ |
| 117 |
} while (0) |
| 118 |
|
| 119 |
#define REALLOCARRAY(arrayName, nElements) do { \ |
| 120 |
void * array; \ |
| 121 |
array = arrayName; \ |
| 122 |
reallocProduct(&array, nElements, sizeof(arrayName[0])); \ |
| 123 |
arrayName = array; \ |
| 124 |
} while (0) |
| 125 |
|
| 126 |
|
| 127 |
#define MALLOCARRAY_NOFAIL(arrayName, nElements) \ |
| 128 |
do { \ |
| 129 |
MALLOCARRAY(arrayName, nElements); \ |
| 130 |
if ((arrayName) == NULL) \ |
| 131 |
abort(); \ |
| 132 |
} while(0) |
| 133 |
|
| 134 |
#define REALLOCARRAY_NOFAIL(arrayName, nElements) \ |
| 135 |
do { \ |
| 136 |
REALLOCARRAY(arrayName, nElements); \ |
| 137 |
if ((arrayName) == NULL) \ |
| 138 |
abort(); \ |
| 139 |
} while(0) |
| 140 |
|
| 141 |
#define MALLOCARRAY2(arrayName, nRows, nCols) do { \ |
| 142 |
void * array; \ |
| 143 |
pm_mallocarray2(&array, nRows, nCols, sizeof(arrayName[0][0])); \ |
| 144 |
arrayName = array; \ |
| 145 |
} while (0) |
| 146 |
|
| 147 |
#define MALLOCARRAY2_NOFAIL(arrayName, nRows, nCols) do { \ |
| 148 |
MALLOCARRAY2(arrayName, nRows, nCols); \ |
| 149 |
if ((arrayName) == NULL) \ |
| 150 |
abort(); \ |
| 151 |
} while (0) |
| 152 |
|
| 153 |
void |
| 154 |
pm_freearray2(void ** const rowIndex); |
| 155 |
|
| 156 |
|
| 157 |
#define MALLOCVAR(varName) \ |
| 158 |
varName = malloc(sizeof(*varName)) |
| 159 |
|
| 160 |
#define MALLOCVAR_NOFAIL(varName) \ |
| 161 |
do {if ((varName = malloc(sizeof(*varName))) == NULL) abort();} while(0) |
| 162 |
|
| 163 |
void |
| 164 |
pm_mallocarray2(void ** const resultP, |
| 165 |
unsigned int const cols, |
| 166 |
unsigned int const rows, |
| 167 |
unsigned int const elementSize); |
| 168 |
|
| 169 |
#ifdef __cplusplus |
| 170 |
} |
| 171 |
#endif |
| 172 |
#endif |