util.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Copyright (C) 2009-2022 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Transactional Memory Library (libitm).
  4. Libitm is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. Libitm is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "libitm_i.h"
  20. #include <stdarg.h>
  21. #include <stdio.h>
  22. namespace GTM HIDDEN {
  23. static void
  24. gtm_verror (const char *fmt, va_list list)
  25. {
  26. fputs ("\nlibitm: ", stderr);
  27. vfprintf (stderr, fmt, list);
  28. fputc ('\n', stderr);
  29. }
  30. void
  31. GTM_error (const char *fmt, ...)
  32. {
  33. va_list list;
  34. va_start (list, fmt);
  35. gtm_verror (fmt, list);
  36. va_end (list);
  37. }
  38. void
  39. GTM_fatal (const char *fmt, ...)
  40. {
  41. va_list list;
  42. va_start (list, fmt);
  43. gtm_verror (fmt, list);
  44. va_end (list);
  45. exit (EXIT_FAILURE);
  46. }
  47. void *
  48. xmalloc (size_t size, bool separate_cl)
  49. {
  50. void *r;
  51. #ifdef HAVE_POSIX_MEMALIGN
  52. if (separate_cl)
  53. {
  54. if (posix_memalign (&r, HW_CACHELINE_SIZE, size))
  55. GTM_fatal ("Out of memory allocating %lu bytes aligned on cache line",
  56. (unsigned long) size);
  57. }
  58. else
  59. #endif
  60. {
  61. r = malloc (size);
  62. if (r == 0)
  63. GTM_fatal ("Out of memory allocating %lu bytes",
  64. (unsigned long) size);
  65. }
  66. return r;
  67. }
  68. void *
  69. xcalloc (size_t size, bool separate_cl)
  70. {
  71. // TODO Use posix_memalign if separate_cl is true, or some other allocation
  72. // method that will avoid sharing cache lines with data used by other
  73. // threads.
  74. void *r = calloc (1, size);
  75. if (r == 0)
  76. GTM_fatal ("Out of memory allocating %lu bytes", (unsigned long) size);
  77. return r;
  78. }
  79. void *
  80. xrealloc (void *old, size_t size, bool separate_cl)
  81. {
  82. // TODO Use posix_memalign if separate_cl is true, or some other allocation
  83. // method that will avoid sharing cache lines with data used by other
  84. // threads.
  85. void *r = realloc (old, size);
  86. if (r == 0)
  87. GTM_fatal ("Out of memory allocating %lu bytes", (unsigned long) size);
  88. return r;
  89. }
  90. } // namespace GTM