alloc.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Copyright (C) 2009-2015 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. namespace GTM HIDDEN {
  21. void
  22. gtm_thread::record_allocation (void *ptr, void (*free_fn)(void *))
  23. {
  24. uintptr_t iptr = (uintptr_t) ptr;
  25. gtm_alloc_action *a = this->alloc_actions.find(iptr);
  26. if (a == 0)
  27. a = this->alloc_actions.insert(iptr);
  28. a->free_fn = free_fn;
  29. a->allocated = true;
  30. }
  31. void
  32. gtm_thread::forget_allocation (void *ptr, void (*free_fn)(void *))
  33. {
  34. uintptr_t iptr = (uintptr_t) ptr;
  35. gtm_alloc_action *a = this->alloc_actions.find(iptr);
  36. if (a == 0)
  37. a = this->alloc_actions.insert(iptr);
  38. a->free_fn = free_fn;
  39. a->allocated = false;
  40. }
  41. namespace {
  42. struct commit_cb_data {
  43. aa_tree<uintptr_t, gtm_alloc_action>* parent;
  44. bool revert_p;
  45. };
  46. }
  47. static void
  48. commit_allocations_2 (uintptr_t key, gtm_alloc_action *a, void *data)
  49. {
  50. void *ptr = (void *)key;
  51. commit_cb_data *cb_data = static_cast<commit_cb_data *>(data);
  52. if (cb_data->revert_p)
  53. {
  54. // Roll back nested allocations.
  55. if (a->allocated)
  56. a->free_fn (ptr);
  57. }
  58. else
  59. {
  60. if (a->allocated)
  61. {
  62. // Add nested allocations to parent transaction.
  63. gtm_alloc_action* a_parent = cb_data->parent->insert(key);
  64. *a_parent = *a;
  65. }
  66. else
  67. {
  68. // ??? We could eliminate a parent allocation that matches this
  69. // memory release, if we had support for removing all accesses
  70. // to this allocation from the transaction's undo and redo logs
  71. // (otherwise, the parent transaction's undo or redo might write to
  72. // data that is already shared again because of calling free()).
  73. // We don't have this support currently, and the benefit of this
  74. // optimization is unknown, so just add it to the parent.
  75. gtm_alloc_action* a_parent;
  76. a_parent = cb_data->parent->insert(key);
  77. *a_parent = *a;
  78. }
  79. }
  80. }
  81. static void
  82. commit_allocations_1 (uintptr_t key, gtm_alloc_action *a, void *cb_data)
  83. {
  84. void *ptr = (void *)key;
  85. uintptr_t revert_p = (uintptr_t) cb_data;
  86. if (a->allocated == revert_p)
  87. a->free_fn (ptr);
  88. }
  89. /* Permanently commit allocated memory during transaction.
  90. REVERT_P is true if instead of committing the allocations, we want
  91. to roll them back (and vice versa). */
  92. void
  93. gtm_thread::commit_allocations (bool revert_p,
  94. aa_tree<uintptr_t, gtm_alloc_action>* parent)
  95. {
  96. if (parent)
  97. {
  98. commit_cb_data cb_data;
  99. cb_data.parent = parent;
  100. cb_data.revert_p = revert_p;
  101. this->alloc_actions.traverse (commit_allocations_2, &cb_data);
  102. }
  103. else
  104. this->alloc_actions.traverse (commit_allocations_1,
  105. (void *)(uintptr_t)revert_p);
  106. this->alloc_actions.clear ();
  107. }
  108. } // namespace GTM