heap-allocated.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* GCC-StarPU
  2. Copyright (C) 2011 Institut National de Recherche en Informatique et Automatique
  3. GCC-StarPU is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. GCC-StarPU is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with GCC-StarPU. If not, see <http://www.gnu.org/licenses/>. */
  13. #undef NDEBUG
  14. #include <mocks.h>
  15. static void
  16. foo (size_t size)
  17. {
  18. /* See ISO C99, Section 6.7.5.2 ("Array Declarators") and Section 6.7.6
  19. ("Type names"). */
  20. float *test_array_parm (float m[size][23], int x, int y)
  21. {
  22. return &m[x][y];
  23. }
  24. size_t minus_one = size - 1;
  25. float *test_array_static_parm (float m[static minus_one][23], int x, int y)
  26. {
  27. return &m[x][y];
  28. }
  29. float *test_pointer_parm (float *m, int x, int y)
  30. {
  31. return &m[23 * x + y];
  32. }
  33. expected_malloc_argument = size * 23 * sizeof (float);
  34. /* The idea is that this code should be compilable both with and without
  35. the attribute. */
  36. float m[size][23] __attribute__ ((heap_allocated));
  37. assert (malloc_calls == 1);
  38. /* Make sure "array arithmetic" works. */
  39. assert ((char *) &m[0][1] - (char *) &m[0][0] == sizeof m[0][0]);
  40. assert ((char *) &m[1][0] - (char *) &m[0][22] == sizeof m[0][0]);
  41. unsigned int x, y;
  42. for (x = 0; x < size; x++)
  43. for (y = 0; y < 23; y++)
  44. {
  45. assert (&m[x][y] == test_array_parm (m, x, y));
  46. assert (&m[x][y] == test_array_static_parm (m, x, y));
  47. assert (&m[x][y] == test_pointer_parm ((float *) m, x, y));
  48. assert (&m[x][y] == test_pointer_parm ((float *) &m, x, y));
  49. }
  50. /* Freed when going out of scope. */
  51. expected_free_argument = m;
  52. }
  53. int
  54. main (int argc, char *argv[])
  55. {
  56. #pragma starpu initialize
  57. /* Choose the size such that the process would most likely segfault if
  58. `foo' tried to allocate this much data on the stack. */
  59. foo (100000);
  60. assert (free_calls == 1);
  61. return EXIT_SUCCESS;
  62. }