]>
Commit | Line | Data |
---|---|---|
5624e564 | 1 | /* Copyright (C) 2009-2015 Free Software Foundation, Inc. |
0a35513e AH |
2 | Contributed by Richard Henderson <rth@redhat.com>. |
3 | ||
4 | This file is part of the GNU Transactional Memory Library (libitm). | |
5 | ||
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. | |
10 | ||
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 | |
14 | more details. | |
15 | ||
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. | |
19 | ||
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/>. */ | |
24 | ||
25 | #include "libitm_i.h" | |
26 | ||
27 | namespace GTM HIDDEN { | |
28 | ||
29 | void | |
30 | gtm_thread::record_allocation (void *ptr, void (*free_fn)(void *)) | |
31 | { | |
32 | uintptr_t iptr = (uintptr_t) ptr; | |
33 | ||
34 | gtm_alloc_action *a = this->alloc_actions.find(iptr); | |
35 | if (a == 0) | |
36 | a = this->alloc_actions.insert(iptr); | |
37 | ||
38 | a->free_fn = free_fn; | |
39 | a->allocated = true; | |
40 | } | |
41 | ||
42 | void | |
43 | gtm_thread::forget_allocation (void *ptr, void (*free_fn)(void *)) | |
44 | { | |
45 | uintptr_t iptr = (uintptr_t) ptr; | |
46 | ||
47 | gtm_alloc_action *a = this->alloc_actions.find(iptr); | |
48 | if (a == 0) | |
49 | a = this->alloc_actions.insert(iptr); | |
50 | ||
51 | a->free_fn = free_fn; | |
52 | a->allocated = false; | |
53 | } | |
54 | ||
55 | namespace { | |
56 | struct commit_cb_data { | |
57 | aa_tree<uintptr_t, gtm_alloc_action>* parent; | |
58 | bool revert_p; | |
59 | }; | |
60 | } | |
61 | ||
62 | static void | |
63 | commit_allocations_2 (uintptr_t key, gtm_alloc_action *a, void *data) | |
64 | { | |
65 | void *ptr = (void *)key; | |
66 | commit_cb_data *cb_data = static_cast<commit_cb_data *>(data); | |
67 | ||
68 | if (cb_data->revert_p) | |
69 | { | |
70 | // Roll back nested allocations. | |
71 | if (a->allocated) | |
72 | a->free_fn (ptr); | |
73 | } | |
74 | else | |
75 | { | |
76 | if (a->allocated) | |
77 | { | |
78 | // Add nested allocations to parent transaction. | |
79 | gtm_alloc_action* a_parent = cb_data->parent->insert(key); | |
80 | *a_parent = *a; | |
81 | } | |
82 | else | |
83 | { | |
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); | |
93 | *a_parent = *a; | |
94 | } | |
95 | } | |
96 | } | |
97 | ||
98 | static void | |
99 | commit_allocations_1 (uintptr_t key, gtm_alloc_action *a, void *cb_data) | |
100 | { | |
101 | void *ptr = (void *)key; | |
102 | uintptr_t revert_p = (uintptr_t) cb_data; | |
103 | ||
104 | if (a->allocated == revert_p) | |
105 | a->free_fn (ptr); | |
106 | } | |
107 | ||
108 | /* Permanently commit allocated memory during transaction. | |
109 | ||
110 | REVERT_P is true if instead of committing the allocations, we want | |
111 | to roll them back (and vice versa). */ | |
112 | void | |
113 | gtm_thread::commit_allocations (bool revert_p, | |
114 | aa_tree<uintptr_t, gtm_alloc_action>* parent) | |
115 | { | |
116 | if (parent) | |
117 | { | |
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); | |
122 | } | |
123 | else | |
124 | this->alloc_actions.traverse (commit_allocations_1, | |
125 | (void *)(uintptr_t)revert_p); | |
126 | this->alloc_actions.clear (); | |
127 | } | |
128 | ||
129 | } // namespace GTM |