Add missing testcase.
[official-gcc.git] / libitm / alloc.cc
bloba4c294cf88e235bc162b2f4851d40f2aa7d040c0
1 /* Copyright (C) 2009-2013 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 uintptr_t iptr = (uintptr_t) ptr;
34 gtm_alloc_action *a = this->alloc_actions.find(iptr);
35 if (a == 0)
36 a = this->alloc_actions.insert(iptr);
38 a->free_fn = free_fn;
39 a->allocated = true;
42 void
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);
48 if (a == 0)
49 a = this->alloc_actions.insert(iptr);
51 a->free_fn = free_fn;
52 a->allocated = false;
55 namespace {
56 struct commit_cb_data {
57 aa_tree<uintptr_t, gtm_alloc_action>* parent;
58 bool revert_p;
62 static void
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.
71 if (a->allocated)
72 a->free_fn (ptr);
74 else
76 if (a->allocated)
78 // Add nested allocations to parent transaction.
79 gtm_alloc_action* a_parent = cb_data->parent->insert(key);
80 *a_parent = *a;
82 else
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;
98 static void
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)
105 a->free_fn (ptr);
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). */
112 void
113 gtm_thread::commit_allocations (bool revert_p,
114 aa_tree<uintptr_t, gtm_alloc_action>* parent)
116 if (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);
123 else
124 this->alloc_actions.traverse (commit_allocations_1,
125 (void *)(uintptr_t)revert_p);
126 this->alloc_actions.clear ();
129 } // namespace GTM