alloc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * \file alloc.c
  3. *
  4. * \brief GIS Library - Memory allocation routines.
  5. *
  6. * (C) 2001-2008 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author GRASS GIS Development Team
  12. *
  13. * \date 1999-2008
  14. */
  15. #include <stdlib.h>
  16. #include <grass/gis.h>
  17. #include <grass/glocale.h>
  18. /**
  19. * \brief Memory allocation.
  20. *
  21. * Allocates a block of
  22. * memory at least <b>n</b> bytes which is aligned properly for all data
  23. * types. A pointer to the aligned block is returned.<br>
  24. * Dies with error message on memory allocation fail.
  25. *
  26. * \param[in] n
  27. * \return void *
  28. */
  29. void *G_malloc(size_t n)
  30. {
  31. void *buf;
  32. if (n <= 0)
  33. n = 1; /* make sure we get a valid request */
  34. buf = malloc(n);
  35. if (buf)
  36. return buf;
  37. G_fatal_error(_("G_malloc: out of memory"));
  38. return NULL;
  39. }
  40. /**
  41. * \brief Memory allocation.
  42. *
  43. * Allocates a
  44. * properly aligned block of memory <b>n</b>*<b>m</b> bytes in length,
  45. * initializes the allocated memory to zero, and returns a pointer to the
  46. * allocated block of memory.<br>
  47. * Dies with error message on memory allocation fail.<br>
  48. * <b>Note:</b> Allocating memory for reading and writing raster maps is
  49. * discussed in Allocating_Raster_I_O_Buffers.
  50. *
  51. * \param[in] n number of elements
  52. * \param[in] m element size
  53. * \return void *
  54. */
  55. void *G_calloc(size_t m, size_t n)
  56. {
  57. void *buf;
  58. if (m <= 0)
  59. m = 1; /* make sure we get a valid requests */
  60. if (n <= 0)
  61. n = 1;
  62. buf = calloc(m, n);
  63. if (buf)
  64. return buf;
  65. G_fatal_error(_("G_calloc: out of memory"));
  66. return NULL;
  67. }
  68. /**
  69. * \brief Memory reallocation.
  70. *
  71. * Changes the
  72. * <b>size</b> of a previously allocated block of memory at <b>ptr</b> and
  73. * returns a pointer to the new block of memory. The <b>size</b> may be larger
  74. * or smaller than the original size. If the original block cannot be extended
  75. * "in place", then a new block is allocated and the original block copied to the
  76. * new block.<br>
  77. * <b>Note:</b> If <b>buf</b> is NULL, then this routine simply allocates a
  78. * block of <b>n</b> bytes else <b>buf</b> must point to memory that has been dynamically
  79. * allocated by <i>G_malloc()</i>, <i>G_calloc()</i>, <i>G_realloc()</i>,
  80. * malloc(3), alloc(3), or realloc(3).. This routine works around broken realloc( )
  81. * routines, which do not handle a NULL <b>buf</b>.
  82. *
  83. * \param[in,out] buf buffer holding original data
  84. * \param[in] n array size
  85. * \return void *
  86. */
  87. void *G_realloc(void *buf, size_t n)
  88. {
  89. if (n <= 0)
  90. n = 1; /* make sure we get a valid request */
  91. if (!buf)
  92. buf = malloc(n);
  93. else
  94. buf = realloc(buf, n);
  95. if (buf)
  96. return buf;
  97. G_fatal_error(_("G_realloc: out of memory"));
  98. return NULL;
  99. }
  100. /**
  101. * \brief Free allocated memory.
  102. *
  103. * \param[in,out] buf buffer holding original data
  104. */
  105. void G_free(void *buf)
  106. {
  107. free(buf);
  108. }