alloc.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. namespace GTM HIDDEN {
  21. void
  22. gtm_thread::record_allocation (void *ptr, void (*free_fn)(void *))
  23. {
  24. // We do not deallocate before outermost commit, so we should never have
  25. // an existing log entry for a new allocation.
  26. gtm_alloc_action *a = this->alloc_actions.insert((uintptr_t) ptr);
  27. a->free_fn = free_fn;
  28. a->free_fn_sz = 0;
  29. a->allocated = true;
  30. }
  31. void
  32. gtm_thread::forget_allocation (void *ptr, void (*free_fn)(void *))
  33. {
  34. // We do not deallocate before outermost commit, so we should never have
  35. // an existing log entry for a deallocation at the same address. We may
  36. // have an existing entry for a matching allocation, but this is handled
  37. // correctly because both are complementary in that only one of these will
  38. // cause an action at commit or abort.
  39. gtm_alloc_action *a = this->alloc_actions.insert((uintptr_t) ptr);
  40. a->free_fn = free_fn;
  41. a->free_fn_sz = 0;
  42. a->allocated = false;
  43. }
  44. void
  45. gtm_thread::forget_allocation (void *ptr, size_t sz,
  46. void (*free_fn_sz)(void *, size_t))
  47. {
  48. // Same as forget_allocation but with a size.
  49. gtm_alloc_action *a = this->alloc_actions.insert((uintptr_t) ptr);
  50. a->free_fn = 0;
  51. a->free_fn_sz = free_fn_sz;
  52. a->sz = sz;
  53. a->allocated = false;
  54. }
  55. namespace {
  56. struct commit_cb_data {
  57. aa_tree<uintptr_t, gtm_alloc_action>* parent;
  58. bool revert_p;
  59. };
  60. }
  61. static void
  62. commit_allocations_2 (uintptr_t key, gtm_alloc_action *a, void *data)
  63. {
  64. void *ptr = (void *)key;
  65. commit_cb_data *cb_data = static_cast<commit_cb_data *>(data);
  66. if (cb_data->revert_p)
  67. {
  68. // Roll back nested allocations, discard deallocations.
  69. if (a->allocated)
  70. {
  71. if (a->free_fn_sz != 0)
  72. a->free_fn_sz (ptr, a->sz);
  73. else
  74. a->free_fn (ptr);
  75. }
  76. }
  77. else
  78. {
  79. // Add allocations and deallocations to parent.
  80. // ??? We could eliminate a (parent) allocation that matches this
  81. // a deallocation, if we had support for removing all accesses
  82. // to this allocation from the transaction's undo and redo logs
  83. // (otherwise, the parent transaction's undo or redo might write to
  84. // data that is already shared again because of calling free()).
  85. // We don't have this support currently, and the benefit of this
  86. // optimization is unknown, so just add it to the parent.
  87. gtm_alloc_action* a_parent = cb_data->parent->insert(key);
  88. *a_parent = *a;
  89. }
  90. }
  91. static void
  92. commit_allocations_1 (uintptr_t key, gtm_alloc_action *a, void *cb_data)
  93. {
  94. void *ptr = (void *)key;
  95. bool revert_p = (bool) (uintptr_t) cb_data;
  96. if (revert_p == a->allocated)
  97. {
  98. if (a->free_fn_sz != 0)
  99. a->free_fn_sz (ptr, a->sz);
  100. else
  101. a->free_fn (ptr);
  102. }
  103. }
  104. /* Permanently commit allocated memory during transaction.
  105. REVERT_P is true if instead of committing the allocations, we want
  106. to roll them back (and vice versa). */
  107. void
  108. gtm_thread::commit_allocations (bool revert_p,
  109. aa_tree<uintptr_t, gtm_alloc_action>* parent)
  110. {
  111. if (parent)
  112. {
  113. commit_cb_data cb_data;
  114. cb_data.parent = parent;
  115. cb_data.revert_p = revert_p;
  116. this->alloc_actions.traverse (commit_allocations_2, &cb_data);
  117. }
  118. else
  119. this->alloc_actions.traverse (commit_allocations_1,
  120. (void *)(uintptr_t)revert_p);
  121. this->alloc_actions.clear ();
  122. }
  123. } // namespace GTM