heap-allocated.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* GCC-StarPU
  2. Copyright (C) 2011, 2012 INRIA
  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. assert ((char *) &m[0][0] != (char *) &m);
  23. return &m[x][y];
  24. }
  25. size_t minus_one = size - 1;
  26. float *test_array_static_parm (float m[static minus_one][23], int x, int y)
  27. {
  28. assert ((char *) &m[0][0] != (char *) &m);
  29. return &m[x][y];
  30. }
  31. float *test_pointer_parm (float *m, int x, int y)
  32. {
  33. return &m[23 * x + y];
  34. }
  35. expected_malloc_argument = size * 23 * sizeof (float);
  36. /* The idea is that this code should be compilable both with and without
  37. the attribute. */
  38. float m[size][23] __attribute__ ((heap_allocated));
  39. assert (malloc_calls == 1);
  40. /* `&m' points to the on-stack area that stores the underlying pointer,
  41. whereas `m' and `m[0][0]' should point to the heap-allocated area. */
  42. assert ((char *) &m != (char *) m);
  43. assert ((char *) &m[0][0] != (char *) &m);
  44. assert ((char *) &m[0][0] == (char *) m);
  45. /* Make sure "array arithmetic" works. */
  46. assert ((char *) &m[0][1] - (char *) &m[0][0] == sizeof m[0][0]);
  47. assert ((char *) &m[1][0] - (char *) &m[0][22] == sizeof m[0][0]);
  48. unsigned int x, y;
  49. for (x = 0; x < size; x++)
  50. for (y = 0; y < 23; y++)
  51. {
  52. assert (&m[x][y] == test_array_parm (m, x, y));
  53. assert (&m[x][y] == test_array_static_parm (m, x, y));
  54. assert (&m[x][y] == test_pointer_parm ((float *) m, x, y));
  55. }
  56. /* Freed when going out of scope. */
  57. expected_free_argument = m;
  58. }
  59. int
  60. main (int argc, char *argv[])
  61. {
  62. #pragma starpu initialize
  63. /* Choose the size such that the process would most likely segfault if
  64. `foo' tried to allocate this much data on the stack. */
  65. foo (100000);
  66. assert (free_calls == 1);
  67. return EXIT_SUCCESS;
  68. }