Daily bump.
[official-gcc.git] / libitm / alloc.cc
blob5d2ab750e5db8e94e573752aa13992546bf8ce26
1 /* Copyright (C) 2009-2018 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
14 more details.
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/>. */
25 #include "libitm_i.h"
27 namespace GTM HIDDEN {
29 void
30 gtm_thread::record_allocation (void *ptr, void (*free_fn)(void *))
32 // We do not deallocate before outermost commit, so we should never have
33 // an existing log entry for a new allocation.
34 gtm_alloc_action *a = this->alloc_actions.insert((uintptr_t) ptr);
36 a->free_fn = free_fn;
37 a->free_fn_sz = 0;
38 a->allocated = true;
41 void
42 gtm_thread::forget_allocation (void *ptr, void (*free_fn)(void *))
44 // We do not deallocate before outermost commit, so we should never have
45 // an existing log entry for a deallocation at the same address. We may
46 // have an existing entry for a matching allocation, but this is handled
47 // correctly because both are complementary in that only one of these will
48 // cause an action at commit or abort.
49 gtm_alloc_action *a = this->alloc_actions.insert((uintptr_t) ptr);
50 a->free_fn = free_fn;
51 a->free_fn_sz = 0;
52 a->allocated = false;
55 void
56 gtm_thread::forget_allocation (void *ptr, size_t sz,
57 void (*free_fn_sz)(void *, size_t))
59 // Same as forget_allocation but with a size.
60 gtm_alloc_action *a = this->alloc_actions.insert((uintptr_t) ptr);
61 a->free_fn = 0;
62 a->free_fn_sz = free_fn_sz;
63 a->sz = sz;
64 a->allocated = false;
67 namespace {
68 struct commit_cb_data {
69 aa_tree<uintptr_t, gtm_alloc_action>* parent;
70 bool revert_p;
74 static void
75 commit_allocations_2 (uintptr_t key, gtm_alloc_action *a, void *data)
77 void *ptr = (void *)key;
78 commit_cb_data *cb_data = static_cast<commit_cb_data *>(data);
80 if (cb_data->revert_p)
82 // Roll back nested allocations, discard deallocations.
83 if (a->allocated)
85 if (a->free_fn_sz != 0)
86 a->free_fn_sz (ptr, a->sz);
87 else
88 a->free_fn (ptr);
91 else
93 // Add allocations and deallocations to parent.
94 // ??? We could eliminate a (parent) allocation that matches this
95 // a deallocation, if we had support for removing all accesses
96 // to this allocation from the transaction's undo and redo logs
97 // (otherwise, the parent transaction's undo or redo might write to
98 // data that is already shared again because of calling free()).
99 // We don't have this support currently, and the benefit of this
100 // optimization is unknown, so just add it to the parent.
101 gtm_alloc_action* a_parent = cb_data->parent->insert(key);
102 *a_parent = *a;
106 static void
107 commit_allocations_1 (uintptr_t key, gtm_alloc_action *a, void *cb_data)
109 void *ptr = (void *)key;
110 bool revert_p = (bool) (uintptr_t) cb_data;
112 if (revert_p == a->allocated)
114 if (a->free_fn_sz != 0)
115 a->free_fn_sz (ptr, a->sz);
116 else
117 a->free_fn (ptr);
121 /* Permanently commit allocated memory during transaction.
123 REVERT_P is true if instead of committing the allocations, we want
124 to roll them back (and vice versa). */
125 void
126 gtm_thread::commit_allocations (bool revert_p,
127 aa_tree<uintptr_t, gtm_alloc_action>* parent)
129 if (parent)
131 commit_cb_data cb_data;
132 cb_data.parent = parent;
133 cb_data.revert_p = revert_p;
134 this->alloc_actions.traverse (commit_allocations_2, &cb_data);
136 else
137 this->alloc_actions.traverse (commit_allocations_1,
138 (void *)(uintptr_t)revert_p);
139 this->alloc_actions.clear ();
142 } // namespace GTM