Fix -finit-derived for c_ptr and c_funptr in programs which use
[official-gcc.git] / libitm / clone.cc
blob56472788c6eb35aeeb5fee0d2f58aa1dc961046f
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 using namespace GTM;
29 struct clone_entry
31 void *orig, *clone;
34 struct clone_table
36 clone_entry *table;
37 size_t size;
38 clone_table *next;
41 static clone_table *all_tables;
43 static void *
44 find_clone (void *ptr)
46 clone_table *table;
48 for (table = all_tables; table ; table = table->next)
50 clone_entry *t = table->table;
51 size_t lo = 0, hi = table->size, i;
53 /* Quick test for whether PTR is present in this table. */
54 if (ptr < t[0].orig || ptr > t[hi - 1].orig)
55 continue;
57 /* Otherwise binary search. */
58 while (lo < hi)
60 i = (lo + hi) / 2;
61 if (ptr < t[i].orig)
62 hi = i;
63 else if (ptr > t[i].orig)
64 lo = i + 1;
65 else
66 return t[i].clone;
69 /* Given the quick test above, if we don't find the entry in
70 this table then it doesn't exist. */
71 break;
74 return NULL;
78 void * ITM_REGPARM
79 _ITM_getTMCloneOrIrrevocable (void *ptr)
81 void *ret = find_clone (ptr);
82 if (ret)
83 return ret;
85 gtm_thr()->serialirr_mode ();
87 return ptr;
90 void * ITM_REGPARM
91 _ITM_getTMCloneSafe (void *ptr)
93 void *ret = find_clone (ptr);
94 if (ret == NULL)
95 abort ();
96 return ret;
99 static int
100 clone_entry_compare (const void *a, const void *b)
102 const clone_entry *aa = (const clone_entry *)a;
103 const clone_entry *bb = (const clone_entry *)b;
105 if (aa->orig < bb->orig)
106 return -1;
107 else if (aa->orig > bb->orig)
108 return 1;
109 else
110 return 0;
113 namespace {
115 // Within find_clone, we know that we are inside a transaction. Because
116 // of that, we have already synchronized with serial_lock. By taking the
117 // serial_lock for write, we exclude all transactions while we make this
118 // change to the clone tables, without having to synchronize on a separate
119 // lock. Do be careful not to attempt a recursive write lock.
121 class ExcludeTransaction
123 bool do_lock;
125 public:
126 ExcludeTransaction()
128 gtm_thread *tx = gtm_thr();
129 do_lock = !(tx && (tx->state & gtm_thread::STATE_SERIAL));
131 if (do_lock)
132 gtm_thread::serial_lock.write_lock ();
135 ~ExcludeTransaction()
137 if (do_lock)
138 gtm_thread::serial_lock.write_unlock ();
142 } // end anon namespace
145 void
146 _ITM_registerTMCloneTable (void *xent, size_t size)
148 clone_entry *ent = static_cast<clone_entry *>(xent);
149 clone_table *table;
151 table = (clone_table *) xmalloc (sizeof (clone_table));
152 table->table = ent;
153 table->size = size;
155 qsort (ent, size, sizeof (clone_entry), clone_entry_compare);
157 // Hold the serial_lock while we update the ALL_TABLES datastructure.
159 ExcludeTransaction exclude;
160 table->next = all_tables;
161 all_tables = table;
165 void
166 _ITM_deregisterTMCloneTable (void *xent)
168 clone_entry *ent = static_cast<clone_entry *>(xent);
169 clone_table *tab;
171 // Hold the serial_lock while we update the ALL_TABLES datastructure.
173 ExcludeTransaction exclude;
174 clone_table **pprev;
176 for (pprev = &all_tables;
177 tab = *pprev, tab->table != ent;
178 pprev = &tab->next)
179 continue;
180 *pprev = tab->next;
183 free (tab);