1 /* Copyright (C) 2009, 2011 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@redhat.com>.
4 This file is part of the GNU Transactional Memory Library (libitm).
6 Libitm is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 Libitm is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
27 namespace GTM HIDDEN
{
30 gtm_thread::record_allocation (void *ptr
, void (*free_fn
)(void *))
32 uintptr_t iptr
= (uintptr_t) ptr
;
34 gtm_alloc_action
*a
= this->alloc_actions
.find(iptr
);
36 a
= this->alloc_actions
.insert(iptr
);
43 gtm_thread::forget_allocation (void *ptr
, void (*free_fn
)(void *))
45 uintptr_t iptr
= (uintptr_t) ptr
;
47 gtm_alloc_action
*a
= this->alloc_actions
.find(iptr
);
49 a
= this->alloc_actions
.insert(iptr
);
56 struct commit_cb_data
{
57 aa_tree
<uintptr_t, gtm_alloc_action
>* parent
;
63 commit_allocations_2 (uintptr_t key
, gtm_alloc_action
*a
, void *data
)
65 void *ptr
= (void *)key
;
66 commit_cb_data
*cb_data
= static_cast<commit_cb_data
*>(data
);
68 if (cb_data
->revert_p
)
70 // Roll back nested allocations.
78 // Add nested allocations to parent transaction.
79 gtm_alloc_action
* a_parent
= cb_data
->parent
->insert(key
);
84 // ??? We could eliminate a parent allocation that matches this
85 // memory release, if we had support for removing all accesses
86 // to this allocation from the transaction's undo and redo logs
87 // (otherwise, the parent transaction's undo or redo might write to
88 // data that is already shared again because of calling free()).
89 // We don't have this support currently, and the benefit of this
90 // optimization is unknown, so just add it to the parent.
91 gtm_alloc_action
* a_parent
;
92 a_parent
= cb_data
->parent
->insert(key
);
99 commit_allocations_1 (uintptr_t key
, gtm_alloc_action
*a
, void *cb_data
)
101 void *ptr
= (void *)key
;
102 uintptr_t revert_p
= (uintptr_t) cb_data
;
104 if (a
->allocated
== revert_p
)
108 /* Permanently commit allocated memory during transaction.
110 REVERT_P is true if instead of committing the allocations, we want
111 to roll them back (and vice versa). */
113 gtm_thread::commit_allocations (bool revert_p
,
114 aa_tree
<uintptr_t, gtm_alloc_action
>* parent
)
118 commit_cb_data cb_data
;
119 cb_data
.parent
= parent
;
120 cb_data
.revert_p
= revert_p
;
121 this->alloc_actions
.traverse (commit_allocations_2
, &cb_data
);
124 this->alloc_actions
.traverse (commit_allocations_1
,
125 (void *)(uintptr_t)revert_p
);
126 this->alloc_actions
.clear ();