alloc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*!
  2. * \file fis/alloc.c
  3. *
  4. * \brief GIS Library - Memory allocation routines.
  5. *
  6. * (C) 2001-2009 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 Original author CERL
  12. */
  13. #include <stdlib.h>
  14. #include <grass/gis.h>
  15. #include <grass/glocale.h>
  16. /*!
  17. * \brief Memory allocation.
  18. *
  19. * Allocates a block of memory at least <i>n</i> bytes which is
  20. * aligned properly for all data types. A pointer to the aligned block
  21. * is returned.
  22. *
  23. * Dies with error message on memory allocation fail.
  24. *
  25. * \param file file name
  26. * \param line line number
  27. * \param t number of elements
  28. */
  29. void *G__malloc(const char *file, int line, 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. G_fatal_error(_("G_malloc: unable to allocate %lu bytes of memory at %s:%d"),
  37. (unsigned long) n, file, line);
  38. return buf;
  39. }
  40. /*!
  41. * \brief Memory allocation.
  42. *
  43. * Allocates a properly aligned block of memory <i>n</i>*<i>m</i>
  44. * bytes in length, initializes the allocated memory to zero, and
  45. * returns a pointer to the allocated block of memory.
  46. *
  47. * Dies with error message on memory allocation fail.
  48. *
  49. * <b>Note:</b> Allocating memory for reading and writing raster maps
  50. * is discussed in \ref Allocating_Raster_I_O_Buffers.
  51. *
  52. * \param file fine name
  53. * \param line line number
  54. * \param m element size
  55. * \param n number of elements
  56. */
  57. void *G__calloc(const char *file, int line, size_t m, size_t n)
  58. {
  59. void *buf;
  60. if (m <= 0)
  61. m = 1; /* make sure we get a valid requests */
  62. if (n <= 0)
  63. n = 1;
  64. buf = calloc(m, n);
  65. if (!buf)
  66. G_fatal_error(_("G_calloc: unable to allocate %lu * %lu bytes of memory at %s:%d"),
  67. (unsigned long) m, (unsigned long) n, file, line);
  68. return buf;
  69. }
  70. /*!
  71. * \brief Memory reallocation.
  72. *
  73. * Changes the <i>size</i> of a previously allocated block of memory
  74. * at <i>ptr</i> and returns a pointer to the new block of memory. The
  75. * <i>size</i> may be larger or smaller than the original size. If the
  76. * original block cannot be extended "in place", then a new block is
  77. * allocated and the original block copied to the new block.
  78. *
  79. * <b>Note:</b> If <i>buf</i> is NULL, then this routine simply
  80. * allocates a block of <i>n</i> bytes else <i>buf</i> must point to
  81. * memory that has been dynamically allocated by G_malloc(),
  82. * G_calloc(), G_realloc(), malloc(3), alloc(3), or realloc(3).. This
  83. * routine works around broken realloc() routines, which do not
  84. * handle a NULL <i>buf</i>.
  85. *
  86. * \param file file name
  87. * \param line line number
  88. * \param[in,out] buf buffer holding original data
  89. * \param[in] n array size
  90. */
  91. void *G__realloc(const char *file, int line, void *buf, size_t n)
  92. {
  93. if (n <= 0)
  94. n = 1; /* make sure we get a valid request */
  95. if (!buf)
  96. buf = malloc(n);
  97. else
  98. buf = realloc(buf, n);
  99. if (!buf)
  100. G_fatal_error(_("G_realloc: unable to allocate %lu bytes of memory at %s:%d"),
  101. (unsigned long) n, file, line);
  102. return buf;
  103. }
  104. /*!
  105. * \brief Free allocated memory.
  106. *
  107. * \param[in,out] buf buffer holding original data
  108. */
  109. void G_free(void *buf)
  110. {
  111. free(buf);
  112. }
  113. /*!
  114. * \brief Advance void pointer
  115. *
  116. * Advances void pointer by <i>size</i> bytes. Returns new pointer
  117. * value.
  118. *
  119. * Useful in raster row processing loops, substitutes
  120. *
  121. \code
  122. CELL *cell;
  123. cell += n;
  124. \endcode
  125. *
  126. * Now
  127. \code
  128. rast = G_incr_void_ptr(rast, Rast_cell_size(data_type))
  129. \endcode
  130. *
  131. * (where rast is void* and <i>data_type</i> is RASTER_MAP_TYPE can be
  132. * used instead of rast++.)
  133. *
  134. * Very useful to generalize the row processing - loop i.e.
  135. * \code
  136. * void * buf_ptr += Rast_cell_size(data_type)
  137. * \endcode
  138. *
  139. * \param ptr pointer
  140. * \param size buffer size
  141. *
  142. * \return pointer to the data
  143. */
  144. #ifndef G_incr_void_ptr
  145. void *G_incr_void_ptr(const void *ptr, size_t size)
  146. {
  147. /* assuming that the size of unsigned char is 1 */
  148. return (void *)((const unsigned char *)ptr + size);
  149. }
  150. #endif