annotate libitm/alloc.cc @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1 /* Copyright (C) 2009-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
2 Contributed by Richard Henderson <rth@redhat.com>.
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of the GNU Transactional Memory Library (libitm).
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 Libitm is free software; you can redistribute it and/or modify it
kono
parents:
diff changeset
7 under the terms of the GNU General Public License as published by
kono
parents:
diff changeset
8 the Free Software Foundation; either version 3 of the License, or
kono
parents:
diff changeset
9 (at your option) any later version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 Libitm is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
kono
parents:
diff changeset
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
kono
parents:
diff changeset
14 more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
17 permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
18 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
21 a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
23 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 #include "libitm_i.h"
kono
parents:
diff changeset
26
kono
parents:
diff changeset
27 namespace GTM HIDDEN {
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 void
kono
parents:
diff changeset
30 gtm_thread::record_allocation (void *ptr, void (*free_fn)(void *))
kono
parents:
diff changeset
31 {
kono
parents:
diff changeset
32 // We do not deallocate before outermost commit, so we should never have
kono
parents:
diff changeset
33 // an existing log entry for a new allocation.
kono
parents:
diff changeset
34 gtm_alloc_action *a = this->alloc_actions.insert((uintptr_t) ptr);
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 a->free_fn = free_fn;
kono
parents:
diff changeset
37 a->free_fn_sz = 0;
kono
parents:
diff changeset
38 a->allocated = true;
kono
parents:
diff changeset
39 }
kono
parents:
diff changeset
40
kono
parents:
diff changeset
41 void
kono
parents:
diff changeset
42 gtm_thread::forget_allocation (void *ptr, void (*free_fn)(void *))
kono
parents:
diff changeset
43 {
kono
parents:
diff changeset
44 // We do not deallocate before outermost commit, so we should never have
kono
parents:
diff changeset
45 // an existing log entry for a deallocation at the same address. We may
kono
parents:
diff changeset
46 // have an existing entry for a matching allocation, but this is handled
kono
parents:
diff changeset
47 // correctly because both are complementary in that only one of these will
kono
parents:
diff changeset
48 // cause an action at commit or abort.
kono
parents:
diff changeset
49 gtm_alloc_action *a = this->alloc_actions.insert((uintptr_t) ptr);
kono
parents:
diff changeset
50 a->free_fn = free_fn;
kono
parents:
diff changeset
51 a->free_fn_sz = 0;
kono
parents:
diff changeset
52 a->allocated = false;
kono
parents:
diff changeset
53 }
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 void
kono
parents:
diff changeset
56 gtm_thread::forget_allocation (void *ptr, size_t sz,
kono
parents:
diff changeset
57 void (*free_fn_sz)(void *, size_t))
kono
parents:
diff changeset
58 {
kono
parents:
diff changeset
59 // Same as forget_allocation but with a size.
kono
parents:
diff changeset
60 gtm_alloc_action *a = this->alloc_actions.insert((uintptr_t) ptr);
kono
parents:
diff changeset
61 a->free_fn = 0;
kono
parents:
diff changeset
62 a->free_fn_sz = free_fn_sz;
kono
parents:
diff changeset
63 a->sz = sz;
kono
parents:
diff changeset
64 a->allocated = false;
kono
parents:
diff changeset
65 }
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 namespace {
kono
parents:
diff changeset
68 struct commit_cb_data {
kono
parents:
diff changeset
69 aa_tree<uintptr_t, gtm_alloc_action>* parent;
kono
parents:
diff changeset
70 bool revert_p;
kono
parents:
diff changeset
71 };
kono
parents:
diff changeset
72 }
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 static void
kono
parents:
diff changeset
75 commit_allocations_2 (uintptr_t key, gtm_alloc_action *a, void *data)
kono
parents:
diff changeset
76 {
kono
parents:
diff changeset
77 void *ptr = (void *)key;
kono
parents:
diff changeset
78 commit_cb_data *cb_data = static_cast<commit_cb_data *>(data);
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 if (cb_data->revert_p)
kono
parents:
diff changeset
81 {
kono
parents:
diff changeset
82 // Roll back nested allocations, discard deallocations.
kono
parents:
diff changeset
83 if (a->allocated)
kono
parents:
diff changeset
84 {
kono
parents:
diff changeset
85 if (a->free_fn_sz != 0)
kono
parents:
diff changeset
86 a->free_fn_sz (ptr, a->sz);
kono
parents:
diff changeset
87 else
kono
parents:
diff changeset
88 a->free_fn (ptr);
kono
parents:
diff changeset
89 }
kono
parents:
diff changeset
90 }
kono
parents:
diff changeset
91 else
kono
parents:
diff changeset
92 {
kono
parents:
diff changeset
93 // Add allocations and deallocations to parent.
kono
parents:
diff changeset
94 // ??? We could eliminate a (parent) allocation that matches this
kono
parents:
diff changeset
95 // a deallocation, if we had support for removing all accesses
kono
parents:
diff changeset
96 // to this allocation from the transaction's undo and redo logs
kono
parents:
diff changeset
97 // (otherwise, the parent transaction's undo or redo might write to
kono
parents:
diff changeset
98 // data that is already shared again because of calling free()).
kono
parents:
diff changeset
99 // We don't have this support currently, and the benefit of this
kono
parents:
diff changeset
100 // optimization is unknown, so just add it to the parent.
kono
parents:
diff changeset
101 gtm_alloc_action* a_parent = cb_data->parent->insert(key);
kono
parents:
diff changeset
102 *a_parent = *a;
kono
parents:
diff changeset
103 }
kono
parents:
diff changeset
104 }
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 static void
kono
parents:
diff changeset
107 commit_allocations_1 (uintptr_t key, gtm_alloc_action *a, void *cb_data)
kono
parents:
diff changeset
108 {
kono
parents:
diff changeset
109 void *ptr = (void *)key;
kono
parents:
diff changeset
110 bool revert_p = (bool) (uintptr_t) cb_data;
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112 if (revert_p == a->allocated)
kono
parents:
diff changeset
113 {
kono
parents:
diff changeset
114 if (a->free_fn_sz != 0)
kono
parents:
diff changeset
115 a->free_fn_sz (ptr, a->sz);
kono
parents:
diff changeset
116 else
kono
parents:
diff changeset
117 a->free_fn (ptr);
kono
parents:
diff changeset
118 }
kono
parents:
diff changeset
119 }
kono
parents:
diff changeset
120
kono
parents:
diff changeset
121 /* Permanently commit allocated memory during transaction.
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 REVERT_P is true if instead of committing the allocations, we want
kono
parents:
diff changeset
124 to roll them back (and vice versa). */
kono
parents:
diff changeset
125 void
kono
parents:
diff changeset
126 gtm_thread::commit_allocations (bool revert_p,
kono
parents:
diff changeset
127 aa_tree<uintptr_t, gtm_alloc_action>* parent)
kono
parents:
diff changeset
128 {
kono
parents:
diff changeset
129 if (parent)
kono
parents:
diff changeset
130 {
kono
parents:
diff changeset
131 commit_cb_data cb_data;
kono
parents:
diff changeset
132 cb_data.parent = parent;
kono
parents:
diff changeset
133 cb_data.revert_p = revert_p;
kono
parents:
diff changeset
134 this->alloc_actions.traverse (commit_allocations_2, &cb_data);
kono
parents:
diff changeset
135 }
kono
parents:
diff changeset
136 else
kono
parents:
diff changeset
137 this->alloc_actions.traverse (commit_allocations_1,
kono
parents:
diff changeset
138 (void *)(uintptr_t)revert_p);
kono
parents:
diff changeset
139 this->alloc_actions.clear ();
kono
parents:
diff changeset
140 }
kono
parents:
diff changeset
141
kono
parents:
diff changeset
142 } // namespace GTM