2
0

mallocvar.h 3.1 KB

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