alloc.c 4.5 KB

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