alloc.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <grass/imagery.h>
  4. void *I_malloc(size_t n)
  5. {
  6. void *b;
  7. b = G_malloc(n);
  8. return b;
  9. }
  10. void *I_realloc(void *b, size_t n)
  11. {
  12. b = G_realloc(b, n);
  13. return b;
  14. }
  15. int I_free(void *b)
  16. {
  17. if ((char *)b != NULL)
  18. G_free(b);
  19. return 0;
  20. }
  21. double **I_alloc_double2(int a, int b)
  22. {
  23. double **x;
  24. int i;
  25. x = (double **)I_malloc((a + 1) * sizeof(double *));
  26. for (i = 0; i < a; i++) {
  27. int n;
  28. x[i] = (double *)I_malloc(b * sizeof(double));
  29. for (n = 0; n < b; n++)
  30. x[i][n] = 0;
  31. }
  32. x[a] = NULL;
  33. return x;
  34. }
  35. int *I_alloc_int(int a)
  36. {
  37. int *x;
  38. int i;
  39. x = (int *)I_malloc(a * sizeof(int));
  40. for (i = 0; i < a; i++)
  41. x[i] = 0;
  42. return x;
  43. }
  44. int **I_alloc_int2(int a, int b)
  45. {
  46. int **x;
  47. int i, n;
  48. x = (int **)I_malloc((a + 1) * sizeof(int *));
  49. for (i = 0; i < a; i++) {
  50. x[i] = (int *)I_malloc(b * sizeof(int));
  51. for (n = 0; n < b; n++)
  52. x[i][n] = 0;
  53. }
  54. x[a] = NULL;
  55. return x;
  56. }
  57. int I_free_int2(int **x)
  58. {
  59. int i;
  60. if (x != NULL) {
  61. for (i = 0; x[i] != NULL; i++)
  62. G_free(x[i]);
  63. G_free(x);
  64. }
  65. return 0;
  66. }
  67. int I_free_double2(double **x)
  68. {
  69. int i;
  70. if (x != NULL) {
  71. for (i = 0; x[i] != NULL; i++)
  72. G_free(x[i]);
  73. G_free(x);
  74. }
  75. return 0;
  76. }
  77. double ***I_alloc_double3(int a, int b, int c)
  78. {
  79. double ***x;
  80. int i, n;
  81. x = (double ***)G_malloc((a + 1) * sizeof(double **));
  82. for (i = 0; i < a; i++) {
  83. x[i] = I_alloc_double2(b, c);
  84. if (x[i] == NULL) {
  85. for (n = 0; n < i; n++)
  86. G_free(x[n]);
  87. G_free(x);
  88. return (double ***)NULL;
  89. }
  90. }
  91. x[a] = NULL;
  92. return x;
  93. }
  94. int I_free_double3(double ***x)
  95. {
  96. int i;
  97. if (x != NULL) {
  98. for (i = 0; x[i] != NULL; i++)
  99. I_free_double2(x[i]);
  100. G_free(x);
  101. }
  102. return 0;
  103. }