File: | tools/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp |
Warning: | line 67, column 29 Use of memory after it is freed |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===-- SymbolFileDWARFDebugMap.cpp -----------------------------*- C++ -*-===// | |||
2 | // | |||
3 | // The LLVM Compiler Infrastructure | |||
4 | // | |||
5 | // This file is distributed under the University of Illinois Open Source | |||
6 | // License. See LICENSE.TXT for details. | |||
7 | // | |||
8 | //===----------------------------------------------------------------------===// | |||
9 | ||||
10 | // C Includes | |||
11 | // C++ Includes | |||
12 | // Other libraries and framework includes | |||
13 | // Project includes | |||
14 | #include "SymbolFileDWARFDebugMap.h" | |||
15 | ||||
16 | #include "DWARFDebugAranges.h" | |||
17 | ||||
18 | #include "lldb/Core/Module.h" | |||
19 | #include "lldb/Core/ModuleList.h" | |||
20 | #include "lldb/Core/PluginManager.h" | |||
21 | #include "lldb/Core/RangeMap.h" | |||
22 | #include "lldb/Core/Section.h" | |||
23 | #include "lldb/Host/FileSystem.h" | |||
24 | #include "lldb/Utility/RegularExpression.h" | |||
25 | #include "lldb/Utility/Timer.h" | |||
26 | ||||
27 | //#define DEBUG_OSO_DMAP // DO NOT CHECKIN WITH THIS NOT COMMENTED OUT | |||
28 | #if defined(DEBUG_OSO_DMAP) | |||
29 | #include "lldb/Core/StreamFile.h" | |||
30 | #endif | |||
31 | ||||
32 | #include "lldb/Symbol/CompileUnit.h" | |||
33 | #include "lldb/Symbol/LineTable.h" | |||
34 | #include "lldb/Symbol/ObjectFile.h" | |||
35 | #include "lldb/Symbol/SymbolVendor.h" | |||
36 | #include "lldb/Symbol/TypeMap.h" | |||
37 | #include "lldb/Symbol/VariableList.h" | |||
38 | #include "llvm/Support/ScopedPrinter.h" | |||
39 | ||||
40 | #include "LogChannelDWARF.h" | |||
41 | #include "SymbolFileDWARF.h" | |||
42 | ||||
43 | using namespace lldb; | |||
44 | using namespace lldb_private; | |||
45 | ||||
46 | // Subclass lldb_private::Module so we can intercept the | |||
47 | // "Module::GetObjectFile()" | |||
48 | // (so we can fixup the object file sections) and also for | |||
49 | // "Module::GetSymbolVendor()" | |||
50 | // (so we can fixup the symbol file id. | |||
51 | ||||
52 | const SymbolFileDWARFDebugMap::FileRangeMap & | |||
53 | SymbolFileDWARFDebugMap::CompileUnitInfo::GetFileRangeMap( | |||
54 | SymbolFileDWARFDebugMap *exe_symfile) { | |||
55 | if (file_range_map_valid) | |||
56 | return file_range_map; | |||
57 | ||||
58 | file_range_map_valid = true; | |||
59 | ||||
60 | Module *oso_module = exe_symfile->GetModuleByCompUnitInfo(this); | |||
61 | if (!oso_module) | |||
62 | return file_range_map; | |||
63 | ||||
64 | ObjectFile *oso_objfile = oso_module->GetObjectFile(); | |||
65 | if (!oso_objfile) | |||
66 | return file_range_map; | |||
67 | ||||
68 | Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_MAP(1u << 8))); | |||
69 | if (log) { | |||
70 | ConstString object_name(oso_module->GetObjectName()); | |||
71 | log->Printf( | |||
72 | "%p: SymbolFileDWARFDebugMap::CompileUnitInfo::GetFileRangeMap ('%s')", | |||
73 | static_cast<void *>(this), | |||
74 | oso_module->GetSpecificationDescription().c_str()); | |||
75 | } | |||
76 | ||||
77 | std::vector<SymbolFileDWARFDebugMap::CompileUnitInfo *> cu_infos; | |||
78 | if (exe_symfile->GetCompUnitInfosForModule(oso_module, cu_infos)) { | |||
79 | for (auto comp_unit_info : cu_infos) { | |||
80 | Symtab *exe_symtab = exe_symfile->GetObjectFile()->GetSymtab(); | |||
81 | ModuleSP oso_module_sp(oso_objfile->GetModule()); | |||
82 | Symtab *oso_symtab = oso_objfile->GetSymtab(); | |||
83 | ||||
84 | /// const uint32_t fun_resolve_flags = SymbolContext::Module | | |||
85 | /// eSymbolContextCompUnit | eSymbolContextFunction; | |||
86 | // SectionList *oso_sections = oso_objfile->Sections(); | |||
87 | // Now we need to make sections that map from zero based object | |||
88 | // file addresses to where things ended up in the main executable. | |||
89 | ||||
90 | assert(comp_unit_info->first_symbol_index != UINT32_MAX)(static_cast <bool> (comp_unit_info->first_symbol_index != (4294967295U)) ? void (0) : __assert_fail ("comp_unit_info->first_symbol_index != UINT32_MAX" , "/build/llvm-toolchain-snapshot-7~svn326246/tools/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp" , 90, __extension__ __PRETTY_FUNCTION__)); | |||
91 | // End index is one past the last valid symbol index | |||
92 | const uint32_t oso_end_idx = comp_unit_info->last_symbol_index + 1; | |||
93 | for (uint32_t idx = comp_unit_info->first_symbol_index + | |||
94 | 2; // Skip the N_SO and N_OSO | |||
95 | idx < oso_end_idx; | |||
96 | ++idx) { | |||
97 | Symbol *exe_symbol = exe_symtab->SymbolAtIndex(idx); | |||
98 | if (exe_symbol) { | |||
99 | if (exe_symbol->IsDebug() == false) | |||
100 | continue; | |||
101 | ||||
102 | switch (exe_symbol->GetType()) { | |||
103 | default: | |||
104 | break; | |||
105 | ||||
106 | case eSymbolTypeCode: { | |||
107 | // For each N_FUN, or function that we run into in the debug map | |||
108 | // we make a new section that we add to the sections found in the | |||
109 | // .o file. This new section has the file address set to what the | |||
110 | // addresses are in the .o file, and the load address is adjusted | |||
111 | // to match where it ended up in the final executable! We do this | |||
112 | // before we parse any dwarf info so that when it goes get parsed | |||
113 | // all section/offset addresses that get registered will resolve | |||
114 | // correctly to the new addresses in the main executable. | |||
115 | ||||
116 | // First we find the original symbol in the .o file's symbol table | |||
117 | Symbol *oso_fun_symbol = oso_symtab->FindFirstSymbolWithNameAndType( | |||
118 | exe_symbol->GetMangled().GetName(lldb::eLanguageTypeUnknown, | |||
119 | Mangled::ePreferMangled), | |||
120 | eSymbolTypeCode, Symtab::eDebugNo, Symtab::eVisibilityAny); | |||
121 | if (oso_fun_symbol) { | |||
122 | // Add the inverse OSO file address to debug map entry mapping | |||
123 | exe_symfile->AddOSOFileRange( | |||
124 | this, exe_symbol->GetAddressRef().GetFileAddress(), | |||
125 | exe_symbol->GetByteSize(), | |||
126 | oso_fun_symbol->GetAddressRef().GetFileAddress(), | |||
127 | oso_fun_symbol->GetByteSize()); | |||
128 | } | |||
129 | } break; | |||
130 | ||||
131 | case eSymbolTypeData: { | |||
132 | // For each N_GSYM we remap the address for the global by making | |||
133 | // a new section that we add to the sections found in the .o file. | |||
134 | // This new section has the file address set to what the | |||
135 | // addresses are in the .o file, and the load address is adjusted | |||
136 | // to match where it ended up in the final executable! We do this | |||
137 | // before we parse any dwarf info so that when it goes get parsed | |||
138 | // all section/offset addresses that get registered will resolve | |||
139 | // correctly to the new addresses in the main executable. We | |||
140 | // initially set the section size to be 1 byte, but will need to | |||
141 | // fix up these addresses further after all globals have been | |||
142 | // parsed to span the gaps, or we can find the global variable | |||
143 | // sizes from the DWARF info as we are parsing. | |||
144 | ||||
145 | // Next we find the non-stab entry that corresponds to the N_GSYM in | |||
146 | // the .o file | |||
147 | Symbol *oso_gsym_symbol = | |||
148 | oso_symtab->FindFirstSymbolWithNameAndType( | |||
149 | exe_symbol->GetMangled().GetName(lldb::eLanguageTypeUnknown, | |||
150 | Mangled::ePreferMangled), | |||
151 | eSymbolTypeData, Symtab::eDebugNo, Symtab::eVisibilityAny); | |||
152 | if (exe_symbol && oso_gsym_symbol && exe_symbol->ValueIsAddress() && | |||
153 | oso_gsym_symbol->ValueIsAddress()) { | |||
154 | // Add the inverse OSO file address to debug map entry mapping | |||
155 | exe_symfile->AddOSOFileRange( | |||
156 | this, exe_symbol->GetAddressRef().GetFileAddress(), | |||
157 | exe_symbol->GetByteSize(), | |||
158 | oso_gsym_symbol->GetAddressRef().GetFileAddress(), | |||
159 | oso_gsym_symbol->GetByteSize()); | |||
160 | } | |||
161 | } break; | |||
162 | } | |||
163 | } | |||
164 | } | |||
165 | ||||
166 | exe_symfile->FinalizeOSOFileRanges(this); | |||
167 | // We don't need the symbols anymore for the .o files | |||
168 | oso_objfile->ClearSymtab(); | |||
169 | } | |||
170 | } | |||
171 | return file_range_map; | |||
172 | } | |||
173 | ||||
174 | class DebugMapModule : public Module { | |||
175 | public: | |||
176 | DebugMapModule(const ModuleSP &exe_module_sp, uint32_t cu_idx, | |||
177 | const FileSpec &file_spec, const ArchSpec &arch, | |||
178 | const ConstString *object_name, off_t object_offset, | |||
179 | const llvm::sys::TimePoint<> object_mod_time) | |||
180 | : Module(file_spec, arch, object_name, object_offset, object_mod_time), | |||
181 | m_exe_module_wp(exe_module_sp), m_cu_idx(cu_idx) {} | |||
182 | ||||
183 | ~DebugMapModule() override = default; | |||
184 | ||||
185 | SymbolVendor * | |||
186 | GetSymbolVendor(bool can_create = true, | |||
187 | lldb_private::Stream *feedback_strm = NULL__null) override { | |||
188 | // Scope for locker | |||
189 | if (m_symfile_ap.get() || can_create == false) | |||
190 | return m_symfile_ap.get(); | |||
191 | ||||
192 | ModuleSP exe_module_sp(m_exe_module_wp.lock()); | |||
193 | if (exe_module_sp) { | |||
194 | // Now get the object file outside of a locking scope | |||
195 | ObjectFile *oso_objfile = GetObjectFile(); | |||
196 | if (oso_objfile) { | |||
197 | std::lock_guard<std::recursive_mutex> guard(m_mutex); | |||
198 | SymbolVendor *symbol_vendor = | |||
199 | Module::GetSymbolVendor(can_create, feedback_strm); | |||
200 | if (symbol_vendor) { | |||
201 | // Set a pointer to this class to set our OSO DWARF file know | |||
202 | // that the DWARF is being used along with a debug map and that | |||
203 | // it will have the remapped sections that we do below. | |||
204 | SymbolFileDWARF *oso_symfile = | |||
205 | SymbolFileDWARFDebugMap::GetSymbolFileAsSymbolFileDWARF( | |||
206 | symbol_vendor->GetSymbolFile()); | |||
207 | ||||
208 | if (!oso_symfile) | |||
209 | return NULL__null; | |||
210 | ||||
211 | ObjectFile *exe_objfile = exe_module_sp->GetObjectFile(); | |||
212 | SymbolVendor *exe_sym_vendor = exe_module_sp->GetSymbolVendor(); | |||
213 | ||||
214 | if (exe_objfile && exe_sym_vendor) { | |||
215 | oso_symfile->SetDebugMapModule(exe_module_sp); | |||
216 | // Set the ID of the symbol file DWARF to the index of the OSO | |||
217 | // shifted left by 32 bits to provide a unique prefix for any | |||
218 | // UserID's that get created in the symbol file. | |||
219 | oso_symfile->SetID(((uint64_t)m_cu_idx + 1ull) << 32ull); | |||
220 | } | |||
221 | return symbol_vendor; | |||
222 | } | |||
223 | } | |||
224 | } | |||
225 | return NULL__null; | |||
226 | } | |||
227 | ||||
228 | protected: | |||
229 | ModuleWP m_exe_module_wp; | |||
230 | const uint32_t m_cu_idx; | |||
231 | }; | |||
232 | ||||
233 | void SymbolFileDWARFDebugMap::Initialize() { | |||
234 | PluginManager::RegisterPlugin(GetPluginNameStatic(), | |||
235 | GetPluginDescriptionStatic(), CreateInstance); | |||
236 | } | |||
237 | ||||
238 | void SymbolFileDWARFDebugMap::Terminate() { | |||
239 | PluginManager::UnregisterPlugin(CreateInstance); | |||
240 | } | |||
241 | ||||
242 | lldb_private::ConstString SymbolFileDWARFDebugMap::GetPluginNameStatic() { | |||
243 | static ConstString g_name("dwarf-debugmap"); | |||
244 | return g_name; | |||
245 | } | |||
246 | ||||
247 | const char *SymbolFileDWARFDebugMap::GetPluginDescriptionStatic() { | |||
248 | return "DWARF and DWARF3 debug symbol file reader (debug map)."; | |||
249 | } | |||
250 | ||||
251 | SymbolFile *SymbolFileDWARFDebugMap::CreateInstance(ObjectFile *obj_file) { | |||
252 | return new SymbolFileDWARFDebugMap(obj_file); | |||
253 | } | |||
254 | ||||
255 | SymbolFileDWARFDebugMap::SymbolFileDWARFDebugMap(ObjectFile *ofile) | |||
256 | : SymbolFile(ofile), m_flags(), m_compile_unit_infos(), m_func_indexes(), | |||
257 | m_glob_indexes(), | |||
258 | m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate) {} | |||
259 | ||||
260 | SymbolFileDWARFDebugMap::~SymbolFileDWARFDebugMap() {} | |||
261 | ||||
262 | void SymbolFileDWARFDebugMap::InitializeObject() {} | |||
263 | ||||
264 | void SymbolFileDWARFDebugMap::InitOSO() { | |||
265 | if (m_flags.test(kHaveInitializedOSOs)) | |||
266 | return; | |||
267 | ||||
268 | m_flags.set(kHaveInitializedOSOs); | |||
269 | ||||
270 | // If the object file has been stripped, there is no sense in looking further | |||
271 | // as all of the debug symbols for the debug map will not be available | |||
272 | if (m_obj_file->IsStripped()) | |||
273 | return; | |||
274 | ||||
275 | // Also make sure the file type is some sort of executable. Core files, debug | |||
276 | // info files (dSYM), object files (.o files), and stub libraries all can | |||
277 | switch (m_obj_file->GetType()) { | |||
278 | case ObjectFile::eTypeInvalid: | |||
279 | case ObjectFile::eTypeCoreFile: | |||
280 | case ObjectFile::eTypeDebugInfo: | |||
281 | case ObjectFile::eTypeObjectFile: | |||
282 | case ObjectFile::eTypeStubLibrary: | |||
283 | case ObjectFile::eTypeUnknown: | |||
284 | case ObjectFile::eTypeJIT: | |||
285 | return; | |||
286 | ||||
287 | case ObjectFile::eTypeExecutable: | |||
288 | case ObjectFile::eTypeDynamicLinker: | |||
289 | case ObjectFile::eTypeSharedLibrary: | |||
290 | break; | |||
291 | } | |||
292 | ||||
293 | // In order to get the abilities of this plug-in, we look at the list of | |||
294 | // N_OSO entries (object files) from the symbol table and make sure that | |||
295 | // these files exist and also contain valid DWARF. If we get any of that | |||
296 | // then we return the abilities of the first N_OSO's DWARF. | |||
297 | ||||
298 | Symtab *symtab = m_obj_file->GetSymtab(); | |||
299 | if (symtab) { | |||
300 | Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_MAP(1u << 8))); | |||
301 | ||||
302 | std::vector<uint32_t> oso_indexes; | |||
303 | // When a mach-o symbol is encoded, the n_type field is encoded in bits | |||
304 | // 23:16, and the n_desc field is encoded in bits 15:0. | |||
305 | // | |||
306 | // To find all N_OSO entries that are part of the DWARF + debug map | |||
307 | // we find only object file symbols with the flags value as follows: | |||
308 | // bits 23:16 == 0x66 (N_OSO) | |||
309 | // bits 15: 0 == 0x0001 (specifies this is a debug map object file) | |||
310 | const uint32_t k_oso_symbol_flags_value = 0x660001u; | |||
311 | ||||
312 | const uint32_t oso_index_count = | |||
313 | symtab->AppendSymbolIndexesWithTypeAndFlagsValue( | |||
314 | eSymbolTypeObjectFile, k_oso_symbol_flags_value, oso_indexes); | |||
315 | ||||
316 | if (oso_index_count > 0) { | |||
317 | symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugYes, | |||
318 | Symtab::eVisibilityAny, | |||
319 | m_func_indexes); | |||
320 | symtab->AppendSymbolIndexesWithType(eSymbolTypeData, Symtab::eDebugYes, | |||
321 | Symtab::eVisibilityAny, | |||
322 | m_glob_indexes); | |||
323 | ||||
324 | symtab->SortSymbolIndexesByValue(m_func_indexes, true); | |||
325 | symtab->SortSymbolIndexesByValue(m_glob_indexes, true); | |||
326 | ||||
327 | for (uint32_t sym_idx : m_func_indexes) { | |||
328 | const Symbol *symbol = symtab->SymbolAtIndex(sym_idx); | |||
329 | lldb::addr_t file_addr = symbol->GetAddressRef().GetFileAddress(); | |||
330 | lldb::addr_t byte_size = symbol->GetByteSize(); | |||
331 | DebugMap::Entry debug_map_entry( | |||
332 | file_addr, byte_size, OSOEntry(sym_idx, LLDB_INVALID_ADDRESS(18446744073709551615UL))); | |||
333 | m_debug_map.Append(debug_map_entry); | |||
334 | } | |||
335 | for (uint32_t sym_idx : m_glob_indexes) { | |||
336 | const Symbol *symbol = symtab->SymbolAtIndex(sym_idx); | |||
337 | lldb::addr_t file_addr = symbol->GetAddressRef().GetFileAddress(); | |||
338 | lldb::addr_t byte_size = symbol->GetByteSize(); | |||
339 | DebugMap::Entry debug_map_entry( | |||
340 | file_addr, byte_size, OSOEntry(sym_idx, LLDB_INVALID_ADDRESS(18446744073709551615UL))); | |||
341 | m_debug_map.Append(debug_map_entry); | |||
342 | } | |||
343 | m_debug_map.Sort(); | |||
344 | ||||
345 | m_compile_unit_infos.resize(oso_index_count); | |||
346 | ||||
347 | for (uint32_t i = 0; i < oso_index_count; ++i) { | |||
348 | const uint32_t so_idx = oso_indexes[i] - 1; | |||
349 | const uint32_t oso_idx = oso_indexes[i]; | |||
350 | const Symbol *so_symbol = symtab->SymbolAtIndex(so_idx); | |||
351 | const Symbol *oso_symbol = symtab->SymbolAtIndex(oso_idx); | |||
352 | if (so_symbol && oso_symbol && | |||
353 | so_symbol->GetType() == eSymbolTypeSourceFile && | |||
354 | oso_symbol->GetType() == eSymbolTypeObjectFile) { | |||
355 | m_compile_unit_infos[i].so_file.SetFile( | |||
356 | so_symbol->GetName().AsCString(), false); | |||
357 | m_compile_unit_infos[i].oso_path = oso_symbol->GetName(); | |||
358 | m_compile_unit_infos[i].oso_mod_time = | |||
359 | llvm::sys::toTimePoint(oso_symbol->GetIntegerValue(0)); | |||
360 | uint32_t sibling_idx = so_symbol->GetSiblingIndex(); | |||
361 | // The sibling index can't be less that or equal to the current index | |||
362 | // "i" | |||
363 | if (sibling_idx == UINT32_MAX(4294967295U)) { | |||
364 | m_obj_file->GetModule()->ReportError( | |||
365 | "N_SO in symbol with UID %u has invalid sibling in debug map, " | |||
366 | "please file a bug and attach the binary listed in this error", | |||
367 | so_symbol->GetID()); | |||
368 | } else { | |||
369 | const Symbol *last_symbol = symtab->SymbolAtIndex(sibling_idx - 1); | |||
370 | m_compile_unit_infos[i].first_symbol_index = so_idx; | |||
371 | m_compile_unit_infos[i].last_symbol_index = sibling_idx - 1; | |||
372 | m_compile_unit_infos[i].first_symbol_id = so_symbol->GetID(); | |||
373 | m_compile_unit_infos[i].last_symbol_id = last_symbol->GetID(); | |||
374 | ||||
375 | if (log) | |||
376 | log->Printf("Initialized OSO 0x%8.8x: file=%s", i, | |||
377 | oso_symbol->GetName().GetCString()); | |||
378 | } | |||
379 | } else { | |||
380 | if (oso_symbol == NULL__null) | |||
381 | m_obj_file->GetModule()->ReportError( | |||
382 | "N_OSO symbol[%u] can't be found, please file a bug and attach " | |||
383 | "the binary listed in this error", | |||
384 | oso_idx); | |||
385 | else if (so_symbol == NULL__null) | |||
386 | m_obj_file->GetModule()->ReportError( | |||
387 | "N_SO not found for N_OSO symbol[%u], please file a bug and " | |||
388 | "attach the binary listed in this error", | |||
389 | oso_idx); | |||
390 | else if (so_symbol->GetType() != eSymbolTypeSourceFile) | |||
391 | m_obj_file->GetModule()->ReportError( | |||
392 | "N_SO has incorrect symbol type (%u) for N_OSO symbol[%u], " | |||
393 | "please file a bug and attach the binary listed in this error", | |||
394 | so_symbol->GetType(), oso_idx); | |||
395 | else if (oso_symbol->GetType() != eSymbolTypeSourceFile) | |||
396 | m_obj_file->GetModule()->ReportError( | |||
397 | "N_OSO has incorrect symbol type (%u) for N_OSO symbol[%u], " | |||
398 | "please file a bug and attach the binary listed in this error", | |||
399 | oso_symbol->GetType(), oso_idx); | |||
400 | } | |||
401 | } | |||
402 | } | |||
403 | } | |||
404 | } | |||
405 | ||||
406 | Module *SymbolFileDWARFDebugMap::GetModuleByOSOIndex(uint32_t oso_idx) { | |||
407 | const uint32_t cu_count = GetNumCompileUnits(); | |||
408 | if (oso_idx < cu_count) | |||
409 | return GetModuleByCompUnitInfo(&m_compile_unit_infos[oso_idx]); | |||
410 | return NULL__null; | |||
411 | } | |||
412 | ||||
413 | Module *SymbolFileDWARFDebugMap::GetModuleByCompUnitInfo( | |||
414 | CompileUnitInfo *comp_unit_info) { | |||
415 | if (!comp_unit_info->oso_sp) { | |||
416 | auto pos = m_oso_map.find(comp_unit_info->oso_path); | |||
417 | if (pos != m_oso_map.end()) { | |||
418 | comp_unit_info->oso_sp = pos->second; | |||
419 | } else { | |||
420 | ObjectFile *obj_file = GetObjectFile(); | |||
421 | comp_unit_info->oso_sp.reset(new OSOInfo()); | |||
422 | m_oso_map[comp_unit_info->oso_path] = comp_unit_info->oso_sp; | |||
423 | const char *oso_path = comp_unit_info->oso_path.GetCString(); | |||
424 | FileSpec oso_file(oso_path, false); | |||
425 | ConstString oso_object; | |||
426 | if (oso_file.Exists()) { | |||
427 | auto oso_mod_time = FileSystem::GetModificationTime(oso_file); | |||
428 | if (oso_mod_time != comp_unit_info->oso_mod_time) { | |||
429 | obj_file->GetModule()->ReportError( | |||
430 | "debug map object file '%s' has changed (actual time is " | |||
431 | "%s, debug map time is %s" | |||
432 | ") since this executable was linked, file will be ignored", | |||
433 | oso_file.GetPath().c_str(), llvm::to_string(oso_mod_time).c_str(), | |||
434 | llvm::to_string(comp_unit_info->oso_mod_time).c_str()); | |||
435 | return NULL__null; | |||
436 | } | |||
437 | ||||
438 | } else { | |||
439 | const bool must_exist = true; | |||
440 | ||||
441 | if (!ObjectFile::SplitArchivePathWithObject(oso_path, oso_file, | |||
442 | oso_object, must_exist)) { | |||
443 | return NULL__null; | |||
444 | } | |||
445 | } | |||
446 | // Always create a new module for .o files. Why? Because we | |||
447 | // use the debug map, to add new sections to each .o file and | |||
448 | // even though a .o file might not have changed, the sections | |||
449 | // that get added to the .o file can change. | |||
450 | ArchSpec oso_arch; | |||
451 | // Only adopt the architecture from the module (not the vendor or OS) | |||
452 | // since .o files for "i386-apple-ios" will historically show up as | |||
453 | // "i386-apple-macosx" | |||
454 | // due to the lack of a LC_VERSION_MIN_MACOSX or LC_VERSION_MIN_IPHONEOS | |||
455 | // load command... | |||
456 | oso_arch.SetTriple(m_obj_file->GetModule() | |||
457 | ->GetArchitecture() | |||
458 | .GetTriple() | |||
459 | .getArchName() | |||
460 | .str() | |||
461 | .c_str()); | |||
462 | comp_unit_info->oso_sp->module_sp.reset(new DebugMapModule( | |||
463 | obj_file->GetModule(), GetCompUnitInfoIndex(comp_unit_info), oso_file, | |||
464 | oso_arch, oso_object ? &oso_object : NULL__null, 0, | |||
465 | oso_object ? comp_unit_info->oso_mod_time | |||
466 | : llvm::sys::TimePoint<>())); | |||
467 | } | |||
468 | } | |||
469 | if (comp_unit_info->oso_sp) | |||
470 | return comp_unit_info->oso_sp->module_sp.get(); | |||
471 | return NULL__null; | |||
472 | } | |||
473 | ||||
474 | bool SymbolFileDWARFDebugMap::GetFileSpecForSO(uint32_t oso_idx, | |||
475 | FileSpec &file_spec) { | |||
476 | if (oso_idx < m_compile_unit_infos.size()) { | |||
477 | if (m_compile_unit_infos[oso_idx].so_file) { | |||
478 | file_spec = m_compile_unit_infos[oso_idx].so_file; | |||
479 | return true; | |||
480 | } | |||
481 | } | |||
482 | return false; | |||
483 | } | |||
484 | ||||
485 | ObjectFile *SymbolFileDWARFDebugMap::GetObjectFileByOSOIndex(uint32_t oso_idx) { | |||
486 | Module *oso_module = GetModuleByOSOIndex(oso_idx); | |||
487 | if (oso_module) | |||
488 | return oso_module->GetObjectFile(); | |||
489 | return NULL__null; | |||
490 | } | |||
491 | ||||
492 | SymbolFileDWARF * | |||
493 | SymbolFileDWARFDebugMap::GetSymbolFile(const SymbolContext &sc) { | |||
494 | CompileUnitInfo *comp_unit_info = GetCompUnitInfo(sc); | |||
495 | if (comp_unit_info) | |||
496 | return GetSymbolFileByCompUnitInfo(comp_unit_info); | |||
497 | return NULL__null; | |||
498 | } | |||
499 | ||||
500 | ObjectFile *SymbolFileDWARFDebugMap::GetObjectFileByCompUnitInfo( | |||
501 | CompileUnitInfo *comp_unit_info) { | |||
502 | Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info); | |||
503 | if (oso_module) | |||
504 | return oso_module->GetObjectFile(); | |||
505 | return NULL__null; | |||
506 | } | |||
507 | ||||
508 | uint32_t SymbolFileDWARFDebugMap::GetCompUnitInfoIndex( | |||
509 | const CompileUnitInfo *comp_unit_info) { | |||
510 | if (!m_compile_unit_infos.empty()) { | |||
511 | const CompileUnitInfo *first_comp_unit_info = &m_compile_unit_infos.front(); | |||
512 | const CompileUnitInfo *last_comp_unit_info = &m_compile_unit_infos.back(); | |||
513 | if (first_comp_unit_info <= comp_unit_info && | |||
514 | comp_unit_info <= last_comp_unit_info) | |||
515 | return comp_unit_info - first_comp_unit_info; | |||
516 | } | |||
517 | return UINT32_MAX(4294967295U); | |||
518 | } | |||
519 | ||||
520 | SymbolFileDWARF * | |||
521 | SymbolFileDWARFDebugMap::GetSymbolFileByOSOIndex(uint32_t oso_idx) { | |||
522 | if (oso_idx < m_compile_unit_infos.size()) | |||
523 | return GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[oso_idx]); | |||
524 | return NULL__null; | |||
525 | } | |||
526 | ||||
527 | SymbolFileDWARF * | |||
528 | SymbolFileDWARFDebugMap::GetSymbolFileAsSymbolFileDWARF(SymbolFile *sym_file) { | |||
529 | if (sym_file && | |||
530 | sym_file->GetPluginName() == SymbolFileDWARF::GetPluginNameStatic()) | |||
531 | return (SymbolFileDWARF *)sym_file; | |||
532 | return NULL__null; | |||
533 | } | |||
534 | ||||
535 | SymbolFileDWARF *SymbolFileDWARFDebugMap::GetSymbolFileByCompUnitInfo( | |||
536 | CompileUnitInfo *comp_unit_info) { | |||
537 | Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info); | |||
538 | if (oso_module) { | |||
539 | SymbolVendor *sym_vendor = oso_module->GetSymbolVendor(); | |||
540 | if (sym_vendor) | |||
541 | return GetSymbolFileAsSymbolFileDWARF(sym_vendor->GetSymbolFile()); | |||
542 | } | |||
543 | return NULL__null; | |||
544 | } | |||
545 | ||||
546 | uint32_t SymbolFileDWARFDebugMap::CalculateAbilities() { | |||
547 | // In order to get the abilities of this plug-in, we look at the list of | |||
548 | // N_OSO entries (object files) from the symbol table and make sure that | |||
549 | // these files exist and also contain valid DWARF. If we get any of that | |||
550 | // then we return the abilities of the first N_OSO's DWARF. | |||
551 | ||||
552 | const uint32_t oso_index_count = GetNumCompileUnits(); | |||
553 | if (oso_index_count > 0) { | |||
554 | InitOSO(); | |||
555 | if (!m_compile_unit_infos.empty()) { | |||
556 | return SymbolFile::CompileUnits | SymbolFile::Functions | | |||
557 | SymbolFile::Blocks | SymbolFile::GlobalVariables | | |||
558 | SymbolFile::LocalVariables | SymbolFile::VariableTypes | | |||
559 | SymbolFile::LineTables; | |||
560 | } | |||
561 | } | |||
562 | return 0; | |||
563 | } | |||
564 | ||||
565 | uint32_t SymbolFileDWARFDebugMap::GetNumCompileUnits() { | |||
566 | InitOSO(); | |||
567 | return m_compile_unit_infos.size(); | |||
568 | } | |||
569 | ||||
570 | CompUnitSP SymbolFileDWARFDebugMap::ParseCompileUnitAtIndex(uint32_t cu_idx) { | |||
571 | CompUnitSP comp_unit_sp; | |||
572 | const uint32_t cu_count = GetNumCompileUnits(); | |||
573 | ||||
574 | if (cu_idx < cu_count) { | |||
575 | Module *oso_module = GetModuleByCompUnitInfo(&m_compile_unit_infos[cu_idx]); | |||
576 | if (oso_module) { | |||
577 | FileSpec so_file_spec; | |||
578 | if (GetFileSpecForSO(cu_idx, so_file_spec)) { | |||
579 | // User zero as the ID to match the compile unit at offset | |||
580 | // zero in each .o file since each .o file can only have | |||
581 | // one compile unit for now. | |||
582 | lldb::user_id_t cu_id = 0; | |||
583 | m_compile_unit_infos[cu_idx].compile_unit_sp.reset( | |||
584 | new CompileUnit(m_obj_file->GetModule(), NULL__null, so_file_spec, cu_id, | |||
585 | eLanguageTypeUnknown, eLazyBoolCalculate)); | |||
586 | ||||
587 | if (m_compile_unit_infos[cu_idx].compile_unit_sp) { | |||
588 | // Let our symbol vendor know about this compile unit | |||
589 | m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex( | |||
590 | cu_idx, m_compile_unit_infos[cu_idx].compile_unit_sp); | |||
591 | } | |||
592 | } | |||
593 | } | |||
594 | comp_unit_sp = m_compile_unit_infos[cu_idx].compile_unit_sp; | |||
595 | } | |||
596 | ||||
597 | return comp_unit_sp; | |||
598 | } | |||
599 | ||||
600 | SymbolFileDWARFDebugMap::CompileUnitInfo * | |||
601 | SymbolFileDWARFDebugMap::GetCompUnitInfo(const SymbolContext &sc) { | |||
602 | const uint32_t cu_count = GetNumCompileUnits(); | |||
603 | for (uint32_t i = 0; i < cu_count; ++i) { | |||
604 | if (sc.comp_unit == m_compile_unit_infos[i].compile_unit_sp.get()) | |||
605 | return &m_compile_unit_infos[i]; | |||
606 | } | |||
607 | return NULL__null; | |||
608 | } | |||
609 | ||||
610 | size_t SymbolFileDWARFDebugMap::GetCompUnitInfosForModule( | |||
611 | const lldb_private::Module *module, | |||
612 | std::vector<CompileUnitInfo *> &cu_infos) { | |||
613 | const uint32_t cu_count = GetNumCompileUnits(); | |||
614 | for (uint32_t i = 0; i < cu_count; ++i) { | |||
615 | if (module == GetModuleByCompUnitInfo(&m_compile_unit_infos[i])) | |||
616 | cu_infos.push_back(&m_compile_unit_infos[i]); | |||
617 | } | |||
618 | return cu_infos.size(); | |||
619 | } | |||
620 | ||||
621 | lldb::LanguageType | |||
622 | SymbolFileDWARFDebugMap::ParseCompileUnitLanguage(const SymbolContext &sc) { | |||
623 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc); | |||
624 | if (oso_dwarf) | |||
625 | return oso_dwarf->ParseCompileUnitLanguage(sc); | |||
626 | return eLanguageTypeUnknown; | |||
627 | } | |||
628 | ||||
629 | size_t | |||
630 | SymbolFileDWARFDebugMap::ParseCompileUnitFunctions(const SymbolContext &sc) { | |||
631 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc); | |||
632 | if (oso_dwarf) | |||
633 | return oso_dwarf->ParseCompileUnitFunctions(sc); | |||
634 | return 0; | |||
635 | } | |||
636 | ||||
637 | bool SymbolFileDWARFDebugMap::ParseCompileUnitLineTable( | |||
638 | const SymbolContext &sc) { | |||
639 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc); | |||
640 | if (oso_dwarf) | |||
641 | return oso_dwarf->ParseCompileUnitLineTable(sc); | |||
642 | return false; | |||
643 | } | |||
644 | ||||
645 | bool SymbolFileDWARFDebugMap::ParseCompileUnitDebugMacros( | |||
646 | const SymbolContext &sc) { | |||
647 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc); | |||
648 | if (oso_dwarf) | |||
649 | return oso_dwarf->ParseCompileUnitDebugMacros(sc); | |||
650 | return false; | |||
651 | } | |||
652 | ||||
653 | bool SymbolFileDWARFDebugMap::ParseCompileUnitSupportFiles( | |||
654 | const SymbolContext &sc, FileSpecList &support_files) { | |||
655 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc); | |||
656 | if (oso_dwarf) | |||
657 | return oso_dwarf->ParseCompileUnitSupportFiles(sc, support_files); | |||
658 | return false; | |||
659 | } | |||
660 | ||||
661 | bool SymbolFileDWARFDebugMap::ParseCompileUnitIsOptimized( | |||
662 | const lldb_private::SymbolContext &sc) { | |||
663 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc); | |||
664 | if (oso_dwarf) | |||
665 | return oso_dwarf->ParseCompileUnitIsOptimized(sc); | |||
666 | return false; | |||
667 | } | |||
668 | ||||
669 | bool SymbolFileDWARFDebugMap::ParseImportedModules( | |||
670 | const SymbolContext &sc, std::vector<ConstString> &imported_modules) { | |||
671 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc); | |||
672 | if (oso_dwarf) | |||
673 | return oso_dwarf->ParseImportedModules(sc, imported_modules); | |||
674 | return false; | |||
675 | } | |||
676 | ||||
677 | size_t SymbolFileDWARFDebugMap::ParseFunctionBlocks(const SymbolContext &sc) { | |||
678 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc); | |||
679 | if (oso_dwarf) | |||
680 | return oso_dwarf->ParseFunctionBlocks(sc); | |||
681 | return 0; | |||
682 | } | |||
683 | ||||
684 | size_t SymbolFileDWARFDebugMap::ParseTypes(const SymbolContext &sc) { | |||
685 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc); | |||
686 | if (oso_dwarf) | |||
687 | return oso_dwarf->ParseTypes(sc); | |||
688 | return 0; | |||
689 | } | |||
690 | ||||
691 | size_t | |||
692 | SymbolFileDWARFDebugMap::ParseVariablesForContext(const SymbolContext &sc) { | |||
693 | SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc); | |||
694 | if (oso_dwarf) | |||
695 | return oso_dwarf->ParseVariablesForContext(sc); | |||
696 | return 0; | |||
697 | } | |||
698 | ||||
699 | Type *SymbolFileDWARFDebugMap::ResolveTypeUID(lldb::user_id_t type_uid) { | |||
700 | const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid); | |||
701 | SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx); | |||
702 | if (oso_dwarf) | |||
703 | return oso_dwarf->ResolveTypeUID(type_uid); | |||
704 | return NULL__null; | |||
705 | } | |||
706 | ||||
707 | bool SymbolFileDWARFDebugMap::CompleteType(CompilerType &compiler_type) { | |||
708 | bool success = false; | |||
709 | if (compiler_type) { | |||
710 | ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { | |||
711 | if (oso_dwarf->HasForwardDeclForClangType(compiler_type)) { | |||
712 | oso_dwarf->CompleteType(compiler_type); | |||
713 | success = true; | |||
714 | return true; | |||
715 | } | |||
716 | return false; | |||
717 | }); | |||
718 | } | |||
719 | return success; | |||
720 | } | |||
721 | ||||
722 | uint32_t SymbolFileDWARFDebugMap::ResolveSymbolContext( | |||
723 | const Address &exe_so_addr, uint32_t resolve_scope, SymbolContext &sc) { | |||
724 | uint32_t resolved_flags = 0; | |||
725 | Symtab *symtab = m_obj_file->GetSymtab(); | |||
726 | if (symtab) { | |||
727 | const addr_t exe_file_addr = exe_so_addr.GetFileAddress(); | |||
728 | ||||
729 | const DebugMap::Entry *debug_map_entry = | |||
730 | m_debug_map.FindEntryThatContains(exe_file_addr); | |||
731 | if (debug_map_entry) { | |||
732 | ||||
733 | sc.symbol = | |||
734 | symtab->SymbolAtIndex(debug_map_entry->data.GetExeSymbolIndex()); | |||
735 | ||||
736 | if (sc.symbol != NULL__null) { | |||
737 | resolved_flags |= eSymbolContextSymbol; | |||
738 | ||||
739 | uint32_t oso_idx = 0; | |||
740 | CompileUnitInfo *comp_unit_info = | |||
741 | GetCompileUnitInfoForSymbolWithID(sc.symbol->GetID(), &oso_idx); | |||
742 | if (comp_unit_info) { | |||
743 | comp_unit_info->GetFileRangeMap(this); | |||
744 | Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info); | |||
745 | if (oso_module) { | |||
746 | lldb::addr_t oso_file_addr = | |||
747 | exe_file_addr - debug_map_entry->GetRangeBase() + | |||
748 | debug_map_entry->data.GetOSOFileAddress(); | |||
749 | Address oso_so_addr; | |||
750 | if (oso_module->ResolveFileAddress(oso_file_addr, oso_so_addr)) { | |||
751 | resolved_flags |= | |||
752 | oso_module->GetSymbolVendor()->ResolveSymbolContext( | |||
753 | oso_so_addr, resolve_scope, sc); | |||
754 | } | |||
755 | } | |||
756 | } | |||
757 | } | |||
758 | } | |||
759 | } | |||
760 | return resolved_flags; | |||
761 | } | |||
762 | ||||
763 | uint32_t SymbolFileDWARFDebugMap::ResolveSymbolContext( | |||
764 | const FileSpec &file_spec, uint32_t line, bool check_inlines, | |||
765 | uint32_t resolve_scope, SymbolContextList &sc_list) { | |||
766 | const uint32_t initial = sc_list.GetSize(); | |||
767 | const uint32_t cu_count = GetNumCompileUnits(); | |||
768 | ||||
769 | for (uint32_t i = 0; i < cu_count; ++i) { | |||
770 | // If we are checking for inlines, then we need to look through all | |||
771 | // compile units no matter if "file_spec" matches. | |||
772 | bool resolve = check_inlines; | |||
773 | ||||
774 | if (!resolve) { | |||
775 | FileSpec so_file_spec; | |||
776 | if (GetFileSpecForSO(i, so_file_spec)) { | |||
777 | // Match the full path if the incoming file_spec has a directory (not | |||
778 | // just a basename) | |||
779 | const bool full_match = (bool)file_spec.GetDirectory(); | |||
780 | resolve = FileSpec::Equal(file_spec, so_file_spec, full_match); | |||
781 | } | |||
782 | } | |||
783 | if (resolve) { | |||
784 | SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(i); | |||
785 | if (oso_dwarf) | |||
786 | oso_dwarf->ResolveSymbolContext(file_spec, line, check_inlines, | |||
787 | resolve_scope, sc_list); | |||
788 | } | |||
789 | } | |||
790 | return sc_list.GetSize() - initial; | |||
791 | } | |||
792 | ||||
793 | uint32_t SymbolFileDWARFDebugMap::PrivateFindGlobalVariables( | |||
794 | const ConstString &name, const CompilerDeclContext *parent_decl_ctx, | |||
795 | const std::vector<uint32_t> | |||
796 | &indexes, // Indexes into the symbol table that match "name" | |||
797 | uint32_t max_matches, | |||
798 | VariableList &variables) { | |||
799 | const uint32_t original_size = variables.GetSize(); | |||
800 | const size_t match_count = indexes.size(); | |||
801 | for (size_t i = 0; i < match_count; ++i) { | |||
802 | uint32_t oso_idx; | |||
803 | CompileUnitInfo *comp_unit_info = | |||
804 | GetCompileUnitInfoForSymbolWithIndex(indexes[i], &oso_idx); | |||
805 | if (comp_unit_info) { | |||
806 | SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx); | |||
807 | if (oso_dwarf) { | |||
808 | if (oso_dwarf->FindGlobalVariables(name, parent_decl_ctx, true, | |||
809 | max_matches, variables)) | |||
810 | if (variables.GetSize() > max_matches) | |||
811 | break; | |||
812 | } | |||
813 | } | |||
814 | } | |||
815 | return variables.GetSize() - original_size; | |||
816 | } | |||
817 | ||||
818 | uint32_t SymbolFileDWARFDebugMap::FindGlobalVariables( | |||
819 | const ConstString &name, const CompilerDeclContext *parent_decl_ctx, | |||
820 | bool append, uint32_t max_matches, VariableList &variables) { | |||
821 | ||||
822 | // If we aren't appending the results to this list, then clear the list | |||
823 | if (!append) | |||
824 | variables.Clear(); | |||
825 | ||||
826 | // Remember how many variables are in the list before we search in case | |||
827 | // we are appending the results to a variable list. | |||
828 | const uint32_t original_size = variables.GetSize(); | |||
829 | ||||
830 | uint32_t total_matches = 0; | |||
831 | ||||
832 | ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { | |||
833 | const uint32_t oso_matches = oso_dwarf->FindGlobalVariables( | |||
834 | name, parent_decl_ctx, true, max_matches, variables); | |||
835 | if (oso_matches > 0) { | |||
836 | total_matches += oso_matches; | |||
837 | ||||
838 | // Are we getting all matches? | |||
839 | if (max_matches == UINT32_MAX(4294967295U)) | |||
840 | return false; // Yep, continue getting everything | |||
841 | ||||
842 | // If we have found enough matches, lets get out | |||
843 | if (max_matches >= total_matches) | |||
844 | return true; | |||
845 | ||||
846 | // Update the max matches for any subsequent calls to find globals | |||
847 | // in any other object files with DWARF | |||
848 | max_matches -= oso_matches; | |||
849 | } | |||
850 | ||||
851 | return false; | |||
852 | }); | |||
853 | ||||
854 | // Return the number of variable that were appended to the list | |||
855 | return variables.GetSize() - original_size; | |||
856 | } | |||
857 | ||||
858 | uint32_t | |||
859 | SymbolFileDWARFDebugMap::FindGlobalVariables(const RegularExpression ®ex, | |||
860 | bool append, uint32_t max_matches, | |||
861 | VariableList &variables) { | |||
862 | // If we aren't appending the results to this list, then clear the list | |||
863 | if (!append) | |||
864 | variables.Clear(); | |||
865 | ||||
866 | // Remember how many variables are in the list before we search in case | |||
867 | // we are appending the results to a variable list. | |||
868 | const uint32_t original_size = variables.GetSize(); | |||
869 | ||||
870 | uint32_t total_matches = 0; | |||
871 | ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { | |||
872 | const uint32_t oso_matches = | |||
873 | oso_dwarf->FindGlobalVariables(regex, true, max_matches, variables); | |||
874 | if (oso_matches > 0) { | |||
875 | total_matches += oso_matches; | |||
876 | ||||
877 | // Are we getting all matches? | |||
878 | if (max_matches == UINT32_MAX(4294967295U)) | |||
879 | return false; // Yep, continue getting everything | |||
880 | ||||
881 | // If we have found enough matches, lets get out | |||
882 | if (max_matches >= total_matches) | |||
883 | return true; | |||
884 | ||||
885 | // Update the max matches for any subsequent calls to find globals | |||
886 | // in any other object files with DWARF | |||
887 | max_matches -= oso_matches; | |||
888 | } | |||
889 | ||||
890 | return false; | |||
891 | }); | |||
892 | ||||
893 | // Return the number of variable that were appended to the list | |||
894 | return variables.GetSize() - original_size; | |||
895 | } | |||
896 | ||||
897 | int SymbolFileDWARFDebugMap::SymbolContainsSymbolWithIndex( | |||
898 | uint32_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) { | |||
899 | const uint32_t symbol_idx = *symbol_idx_ptr; | |||
900 | ||||
901 | if (symbol_idx < comp_unit_info->first_symbol_index) | |||
902 | return -1; | |||
903 | ||||
904 | if (symbol_idx <= comp_unit_info->last_symbol_index) | |||
905 | return 0; | |||
906 | ||||
907 | return 1; | |||
908 | } | |||
909 | ||||
910 | int SymbolFileDWARFDebugMap::SymbolContainsSymbolWithID( | |||
911 | user_id_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) { | |||
912 | const user_id_t symbol_id = *symbol_idx_ptr; | |||
913 | ||||
914 | if (symbol_id < comp_unit_info->first_symbol_id) | |||
915 | return -1; | |||
916 | ||||
917 | if (symbol_id <= comp_unit_info->last_symbol_id) | |||
918 | return 0; | |||
919 | ||||
920 | return 1; | |||
921 | } | |||
922 | ||||
923 | SymbolFileDWARFDebugMap::CompileUnitInfo * | |||
924 | SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithIndex( | |||
925 | uint32_t symbol_idx, uint32_t *oso_idx_ptr) { | |||
926 | const uint32_t oso_index_count = m_compile_unit_infos.size(); | |||
927 | CompileUnitInfo *comp_unit_info = NULL__null; | |||
928 | if (oso_index_count) { | |||
929 | comp_unit_info = (CompileUnitInfo *)bsearch( | |||
930 | &symbol_idx, &m_compile_unit_infos[0], m_compile_unit_infos.size(), | |||
931 | sizeof(CompileUnitInfo), | |||
932 | (ComparisonFunction)SymbolContainsSymbolWithIndex); | |||
933 | } | |||
934 | ||||
935 | if (oso_idx_ptr) { | |||
936 | if (comp_unit_info != NULL__null) | |||
937 | *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0]; | |||
938 | else | |||
939 | *oso_idx_ptr = UINT32_MAX(4294967295U); | |||
940 | } | |||
941 | return comp_unit_info; | |||
942 | } | |||
943 | ||||
944 | SymbolFileDWARFDebugMap::CompileUnitInfo * | |||
945 | SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithID( | |||
946 | user_id_t symbol_id, uint32_t *oso_idx_ptr) { | |||
947 | const uint32_t oso_index_count = m_compile_unit_infos.size(); | |||
948 | CompileUnitInfo *comp_unit_info = NULL__null; | |||
949 | if (oso_index_count) { | |||
950 | comp_unit_info = (CompileUnitInfo *)::bsearch( | |||
951 | &symbol_id, &m_compile_unit_infos[0], m_compile_unit_infos.size(), | |||
952 | sizeof(CompileUnitInfo), | |||
953 | (ComparisonFunction)SymbolContainsSymbolWithID); | |||
954 | } | |||
955 | ||||
956 | if (oso_idx_ptr) { | |||
957 | if (comp_unit_info != NULL__null) | |||
958 | *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0]; | |||
959 | else | |||
960 | *oso_idx_ptr = UINT32_MAX(4294967295U); | |||
961 | } | |||
962 | return comp_unit_info; | |||
963 | } | |||
964 | ||||
965 | static void RemoveFunctionsWithModuleNotEqualTo(const ModuleSP &module_sp, | |||
966 | SymbolContextList &sc_list, | |||
967 | uint32_t start_idx) { | |||
968 | // We found functions in .o files. Not all functions in the .o files | |||
969 | // will have made it into the final output file. The ones that did | |||
970 | // make it into the final output file will have a section whose module | |||
971 | // matches the module from the ObjectFile for this SymbolFile. When | |||
972 | // the modules don't match, then we have something that was in a | |||
973 | // .o file, but doesn't map to anything in the final executable. | |||
974 | uint32_t i = start_idx; | |||
975 | while (i < sc_list.GetSize()) { | |||
976 | SymbolContext sc; | |||
977 | sc_list.GetContextAtIndex(i, sc); | |||
978 | if (sc.function) { | |||
979 | const SectionSP section_sp( | |||
980 | sc.function->GetAddressRange().GetBaseAddress().GetSection()); | |||
981 | if (section_sp->GetModule() != module_sp) { | |||
982 | sc_list.RemoveContextAtIndex(i); | |||
983 | continue; | |||
984 | } | |||
985 | } | |||
986 | ++i; | |||
987 | } | |||
988 | } | |||
989 | ||||
990 | uint32_t SymbolFileDWARFDebugMap::FindFunctions( | |||
991 | const ConstString &name, const CompilerDeclContext *parent_decl_ctx, | |||
992 | uint32_t name_type_mask, bool include_inlines, bool append, | |||
993 | SymbolContextList &sc_list) { | |||
994 | static Timer::Category func_cat(LLVM_PRETTY_FUNCTION__PRETTY_FUNCTION__); | |||
995 | Timer scoped_timer(func_cat, | |||
996 | "SymbolFileDWARFDebugMap::FindFunctions (name = %s)", | |||
997 | name.GetCString()); | |||
998 | ||||
999 | uint32_t initial_size = 0; | |||
1000 | if (append) | |||
1001 | initial_size = sc_list.GetSize(); | |||
1002 | else | |||
1003 | sc_list.Clear(); | |||
1004 | ||||
1005 | ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { | |||
1006 | uint32_t sc_idx = sc_list.GetSize(); | |||
1007 | if (oso_dwarf->FindFunctions(name, parent_decl_ctx, name_type_mask, | |||
1008 | include_inlines, true, sc_list)) { | |||
1009 | RemoveFunctionsWithModuleNotEqualTo(m_obj_file->GetModule(), sc_list, | |||
1010 | sc_idx); | |||
1011 | } | |||
1012 | return false; | |||
1013 | }); | |||
1014 | ||||
1015 | return sc_list.GetSize() - initial_size; | |||
1016 | } | |||
1017 | ||||
1018 | uint32_t SymbolFileDWARFDebugMap::FindFunctions(const RegularExpression ®ex, | |||
1019 | bool include_inlines, | |||
1020 | bool append, | |||
1021 | SymbolContextList &sc_list) { | |||
1022 | static Timer::Category func_cat(LLVM_PRETTY_FUNCTION__PRETTY_FUNCTION__); | |||
1023 | Timer scoped_timer(func_cat, | |||
1024 | "SymbolFileDWARFDebugMap::FindFunctions (regex = '%s')", | |||
1025 | regex.GetText().str().c_str()); | |||
1026 | ||||
1027 | uint32_t initial_size = 0; | |||
1028 | if (append) | |||
1029 | initial_size = sc_list.GetSize(); | |||
1030 | else | |||
1031 | sc_list.Clear(); | |||
1032 | ||||
1033 | ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { | |||
1034 | uint32_t sc_idx = sc_list.GetSize(); | |||
1035 | ||||
1036 | if (oso_dwarf->FindFunctions(regex, include_inlines, true, sc_list)) { | |||
1037 | RemoveFunctionsWithModuleNotEqualTo(m_obj_file->GetModule(), sc_list, | |||
1038 | sc_idx); | |||
1039 | } | |||
1040 | return false; | |||
1041 | }); | |||
1042 | ||||
1043 | return sc_list.GetSize() - initial_size; | |||
1044 | } | |||
1045 | ||||
1046 | size_t SymbolFileDWARFDebugMap::GetTypes(SymbolContextScope *sc_scope, | |||
1047 | uint32_t type_mask, | |||
1048 | TypeList &type_list) { | |||
1049 | static Timer::Category func_cat(LLVM_PRETTY_FUNCTION__PRETTY_FUNCTION__); | |||
1050 | Timer scoped_timer(func_cat, | |||
1051 | "SymbolFileDWARFDebugMap::GetTypes (type_mask = 0x%8.8x)", | |||
1052 | type_mask); | |||
1053 | ||||
1054 | uint32_t initial_size = type_list.GetSize(); | |||
1055 | SymbolFileDWARF *oso_dwarf = NULL__null; | |||
1056 | if (sc_scope) { | |||
1057 | SymbolContext sc; | |||
1058 | sc_scope->CalculateSymbolContext(&sc); | |||
1059 | ||||
1060 | CompileUnitInfo *cu_info = GetCompUnitInfo(sc); | |||
1061 | if (cu_info) { | |||
1062 | oso_dwarf = GetSymbolFileByCompUnitInfo(cu_info); | |||
1063 | if (oso_dwarf) | |||
1064 | oso_dwarf->GetTypes(sc_scope, type_mask, type_list); | |||
1065 | } | |||
1066 | } else { | |||
1067 | ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { | |||
1068 | oso_dwarf->GetTypes(sc_scope, type_mask, type_list); | |||
1069 | return false; | |||
1070 | }); | |||
1071 | } | |||
1072 | return type_list.GetSize() - initial_size; | |||
1073 | } | |||
1074 | ||||
1075 | TypeSP SymbolFileDWARFDebugMap::FindDefinitionTypeForDWARFDeclContext( | |||
1076 | const DWARFDeclContext &die_decl_ctx) { | |||
1077 | TypeSP type_sp; | |||
1078 | ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { | |||
1079 | type_sp = oso_dwarf->FindDefinitionTypeForDWARFDeclContext(die_decl_ctx); | |||
1080 | return ((bool)type_sp); | |||
1081 | }); | |||
1082 | return type_sp; | |||
1083 | } | |||
1084 | ||||
1085 | bool SymbolFileDWARFDebugMap::Supports_DW_AT_APPLE_objc_complete_type( | |||
1086 | SymbolFileDWARF *skip_dwarf_oso) { | |||
1087 | if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) { | |||
1088 | m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo; | |||
1089 | ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { | |||
1090 | if (skip_dwarf_oso != oso_dwarf && | |||
1091 | oso_dwarf->Supports_DW_AT_APPLE_objc_complete_type(NULL__null)) { | |||
1092 | m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes; | |||
1093 | return true; | |||
1094 | } | |||
1095 | return false; | |||
1096 | }); | |||
1097 | } | |||
1098 | return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes; | |||
1099 | } | |||
1100 | ||||
1101 | TypeSP SymbolFileDWARFDebugMap::FindCompleteObjCDefinitionTypeForDIE( | |||
1102 | const DWARFDIE &die, const ConstString &type_name, | |||
1103 | bool must_be_implementation) { | |||
1104 | // If we have a debug map, we will have an Objective C symbol whose name is | |||
1105 | // the type name and whose type is eSymbolTypeObjCClass. If we can find that | |||
1106 | // symbol and find its containing parent, we can locate the .o file that will | |||
1107 | // contain the implementation definition since it will be scoped inside the | |||
1108 | // N_SO | |||
1109 | // and we can then locate the SymbolFileDWARF that corresponds to that N_SO. | |||
1110 | SymbolFileDWARF *oso_dwarf = NULL__null; | |||
1111 | TypeSP type_sp; | |||
1112 | ObjectFile *module_objfile = m_obj_file->GetModule()->GetObjectFile(); | |||
1113 | if (module_objfile) { | |||
1114 | Symtab *symtab = module_objfile->GetSymtab(); | |||
1115 | if (symtab) { | |||
1116 | Symbol *objc_class_symbol = symtab->FindFirstSymbolWithNameAndType( | |||
1117 | type_name, eSymbolTypeObjCClass, Symtab::eDebugAny, | |||
1118 | Symtab::eVisibilityAny); | |||
1119 | if (objc_class_symbol) { | |||
1120 | // Get the N_SO symbol that contains the objective C class symbol as | |||
1121 | // this | |||
1122 | // should be the .o file that contains the real definition... | |||
1123 | const Symbol *source_file_symbol = symtab->GetParent(objc_class_symbol); | |||
1124 | ||||
1125 | if (source_file_symbol && | |||
1126 | source_file_symbol->GetType() == eSymbolTypeSourceFile) { | |||
1127 | const uint32_t source_file_symbol_idx = | |||
1128 | symtab->GetIndexForSymbol(source_file_symbol); | |||
1129 | if (source_file_symbol_idx != UINT32_MAX(4294967295U)) { | |||
1130 | CompileUnitInfo *compile_unit_info = | |||
1131 | GetCompileUnitInfoForSymbolWithIndex(source_file_symbol_idx, | |||
1132 | NULL__null); | |||
1133 | if (compile_unit_info) { | |||
1134 | oso_dwarf = GetSymbolFileByCompUnitInfo(compile_unit_info); | |||
1135 | if (oso_dwarf) { | |||
1136 | TypeSP type_sp(oso_dwarf->FindCompleteObjCDefinitionTypeForDIE( | |||
1137 | die, type_name, must_be_implementation)); | |||
1138 | if (type_sp) { | |||
1139 | return type_sp; | |||
1140 | } | |||
1141 | } | |||
1142 | } | |||
1143 | } | |||
1144 | } | |||
1145 | } | |||
1146 | } | |||
1147 | } | |||
1148 | ||||
1149 | // Only search all .o files for the definition if we don't need the | |||
1150 | // implementation | |||
1151 | // because otherwise, with a valid debug map we should have the ObjC class | |||
1152 | // symbol and | |||
1153 | // the code above should have found it. | |||
1154 | if (must_be_implementation == false) { | |||
1155 | TypeSP type_sp; | |||
1156 | ||||
1157 | ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { | |||
1158 | type_sp = oso_dwarf->FindCompleteObjCDefinitionTypeForDIE( | |||
1159 | die, type_name, must_be_implementation); | |||
1160 | return (bool)type_sp; | |||
1161 | }); | |||
1162 | ||||
1163 | return type_sp; | |||
1164 | } | |||
1165 | return TypeSP(); | |||
1166 | } | |||
1167 | ||||
1168 | uint32_t SymbolFileDWARFDebugMap::FindTypes( | |||
1169 | const SymbolContext &sc, const ConstString &name, | |||
1170 | const CompilerDeclContext *parent_decl_ctx, bool append, | |||
1171 | uint32_t max_matches, | |||
1172 | llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, | |||
1173 | TypeMap &types) { | |||
1174 | if (!append) | |||
1175 | types.Clear(); | |||
1176 | ||||
1177 | const uint32_t initial_types_size = types.GetSize(); | |||
1178 | SymbolFileDWARF *oso_dwarf; | |||
1179 | ||||
1180 | if (sc.comp_unit) { | |||
1181 | oso_dwarf = GetSymbolFile(sc); | |||
1182 | if (oso_dwarf) | |||
1183 | return oso_dwarf->FindTypes(sc, name, parent_decl_ctx, append, | |||
1184 | max_matches, searched_symbol_files, types); | |||
1185 | } else { | |||
1186 | ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { | |||
1187 | oso_dwarf->FindTypes(sc, name, parent_decl_ctx, append, max_matches, | |||
1188 | searched_symbol_files, types); | |||
1189 | if (types.GetSize() >= max_matches) | |||
1190 | return true; | |||
1191 | else | |||
1192 | return false; | |||
1193 | }); | |||
1194 | } | |||
1195 | ||||
1196 | return types.GetSize() - initial_types_size; | |||
1197 | } | |||
1198 | ||||
1199 | // | |||
1200 | // uint32_t | |||
1201 | // SymbolFileDWARFDebugMap::FindTypes (const SymbolContext& sc, const | |||
1202 | // RegularExpression& regex, bool append, uint32_t max_matches, Type::Encoding | |||
1203 | // encoding, lldb::user_id_t udt_uid, TypeList& types) | |||
1204 | //{ | |||
1205 | // SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc); | |||
1206 | // if (oso_dwarf) | |||
1207 | // return oso_dwarf->FindTypes (sc, regex, append, max_matches, encoding, | |||
1208 | // udt_uid, types); | |||
1209 | // return 0; | |||
1210 | //} | |||
1211 | ||||
1212 | CompilerDeclContext SymbolFileDWARFDebugMap::FindNamespace( | |||
1213 | const lldb_private::SymbolContext &sc, | |||
1214 | const lldb_private::ConstString &name, | |||
1215 | const CompilerDeclContext *parent_decl_ctx) { | |||
1216 | CompilerDeclContext matching_namespace; | |||
1217 | SymbolFileDWARF *oso_dwarf; | |||
1218 | ||||
1219 | if (sc.comp_unit) { | |||
1220 | oso_dwarf = GetSymbolFile(sc); | |||
1221 | if (oso_dwarf) | |||
1222 | matching_namespace = oso_dwarf->FindNamespace(sc, name, parent_decl_ctx); | |||
1223 | } else { | |||
1224 | ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { | |||
1225 | matching_namespace = oso_dwarf->FindNamespace(sc, name, parent_decl_ctx); | |||
1226 | ||||
1227 | return (bool)matching_namespace; | |||
1228 | }); | |||
1229 | } | |||
1230 | ||||
1231 | return matching_namespace; | |||
1232 | } | |||
1233 | ||||
1234 | //------------------------------------------------------------------ | |||
1235 | // PluginInterface protocol | |||
1236 | //------------------------------------------------------------------ | |||
1237 | lldb_private::ConstString SymbolFileDWARFDebugMap::GetPluginName() { | |||
1238 | return GetPluginNameStatic(); | |||
1239 | } | |||
1240 | ||||
1241 | uint32_t SymbolFileDWARFDebugMap::GetPluginVersion() { return 1; } | |||
1242 | ||||
1243 | lldb::CompUnitSP | |||
1244 | SymbolFileDWARFDebugMap::GetCompileUnit(SymbolFileDWARF *oso_dwarf) { | |||
1245 | if (oso_dwarf) { | |||
1246 | const uint32_t cu_count = GetNumCompileUnits(); | |||
1247 | for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) { | |||
1248 | SymbolFileDWARF *oso_symfile = | |||
1249 | GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]); | |||
1250 | if (oso_symfile == oso_dwarf) { | |||
1251 | if (!m_compile_unit_infos[cu_idx].compile_unit_sp) | |||
1252 | m_compile_unit_infos[cu_idx].compile_unit_sp = | |||
1253 | ParseCompileUnitAtIndex(cu_idx); | |||
1254 | ||||
1255 | return m_compile_unit_infos[cu_idx].compile_unit_sp; | |||
1256 | } | |||
1257 | } | |||
1258 | } | |||
1259 | llvm_unreachable("this shouldn't happen")::llvm::llvm_unreachable_internal("this shouldn't happen", "/build/llvm-toolchain-snapshot-7~svn326246/tools/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp" , 1259); | |||
1260 | } | |||
1261 | ||||
1262 | SymbolFileDWARFDebugMap::CompileUnitInfo * | |||
1263 | SymbolFileDWARFDebugMap::GetCompileUnitInfo(SymbolFileDWARF *oso_dwarf) { | |||
1264 | if (oso_dwarf) { | |||
1265 | const uint32_t cu_count = GetNumCompileUnits(); | |||
1266 | for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) { | |||
1267 | SymbolFileDWARF *oso_symfile = | |||
1268 | GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]); | |||
1269 | if (oso_symfile == oso_dwarf) { | |||
1270 | return &m_compile_unit_infos[cu_idx]; | |||
1271 | } | |||
1272 | } | |||
1273 | } | |||
1274 | return NULL__null; | |||
1275 | } | |||
1276 | ||||
1277 | void SymbolFileDWARFDebugMap::SetCompileUnit(SymbolFileDWARF *oso_dwarf, | |||
1278 | const CompUnitSP &cu_sp) { | |||
1279 | if (oso_dwarf) { | |||
| ||||
1280 | const uint32_t cu_count = GetNumCompileUnits(); | |||
1281 | for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) { | |||
1282 | SymbolFileDWARF *oso_symfile = | |||
1283 | GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]); | |||
1284 | if (oso_symfile == oso_dwarf) { | |||
1285 | if (m_compile_unit_infos[cu_idx].compile_unit_sp) { | |||
1286 | assert(m_compile_unit_infos[cu_idx].compile_unit_sp.get() ==(static_cast <bool> (m_compile_unit_infos[cu_idx].compile_unit_sp .get() == cu_sp.get()) ? void (0) : __assert_fail ("m_compile_unit_infos[cu_idx].compile_unit_sp.get() == cu_sp.get()" , "/build/llvm-toolchain-snapshot-7~svn326246/tools/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp" , 1287, __extension__ __PRETTY_FUNCTION__)) | |||
1287 | cu_sp.get())(static_cast <bool> (m_compile_unit_infos[cu_idx].compile_unit_sp .get() == cu_sp.get()) ? void (0) : __assert_fail ("m_compile_unit_infos[cu_idx].compile_unit_sp.get() == cu_sp.get()" , "/build/llvm-toolchain-snapshot-7~svn326246/tools/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp" , 1287, __extension__ __PRETTY_FUNCTION__)); | |||
1288 | } else { | |||
1289 | m_compile_unit_infos[cu_idx].compile_unit_sp = cu_sp; | |||
1290 | m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex( | |||
1291 | cu_idx, cu_sp); | |||
1292 | } | |||
1293 | } | |||
1294 | } | |||
1295 | } | |||
1296 | } | |||
1297 | ||||
1298 | CompilerDeclContext | |||
1299 | SymbolFileDWARFDebugMap::GetDeclContextForUID(lldb::user_id_t type_uid) { | |||
1300 | const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid); | |||
1301 | SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx); | |||
1302 | if (oso_dwarf) | |||
1303 | return oso_dwarf->GetDeclContextForUID(type_uid); | |||
1304 | return CompilerDeclContext(); | |||
1305 | } | |||
1306 | ||||
1307 | CompilerDeclContext | |||
1308 | SymbolFileDWARFDebugMap::GetDeclContextContainingUID(lldb::user_id_t type_uid) { | |||
1309 | const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid); | |||
1310 | SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx); | |||
1311 | if (oso_dwarf) | |||
1312 | return oso_dwarf->GetDeclContextContainingUID(type_uid); | |||
1313 | return CompilerDeclContext(); | |||
1314 | } | |||
1315 | ||||
1316 | void SymbolFileDWARFDebugMap::ParseDeclsForContext( | |||
1317 | lldb_private::CompilerDeclContext decl_ctx) { | |||
1318 | ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { | |||
1319 | oso_dwarf->ParseDeclsForContext(decl_ctx); | |||
1320 | return true; // Keep iterating | |||
1321 | }); | |||
1322 | } | |||
1323 | ||||
1324 | bool SymbolFileDWARFDebugMap::AddOSOFileRange(CompileUnitInfo *cu_info, | |||
1325 | lldb::addr_t exe_file_addr, | |||
1326 | lldb::addr_t exe_byte_size, | |||
1327 | lldb::addr_t oso_file_addr, | |||
1328 | lldb::addr_t oso_byte_size) { | |||
1329 | const uint32_t debug_map_idx = | |||
1330 | m_debug_map.FindEntryIndexThatContains(exe_file_addr); | |||
1331 | if (debug_map_idx != UINT32_MAX(4294967295U)) { | |||
1332 | DebugMap::Entry *debug_map_entry = | |||
1333 | m_debug_map.FindEntryThatContains(exe_file_addr); | |||
1334 | debug_map_entry->data.SetOSOFileAddress(oso_file_addr); | |||
1335 | addr_t range_size = std::min<addr_t>(exe_byte_size, oso_byte_size); | |||
1336 | if (range_size == 0) { | |||
1337 | range_size = std::max<addr_t>(exe_byte_size, oso_byte_size); | |||
1338 | if (range_size == 0) | |||
1339 | range_size = 1; | |||
1340 | } | |||
1341 | cu_info->file_range_map.Append( | |||
1342 | FileRangeMap::Entry(oso_file_addr, range_size, exe_file_addr)); | |||
1343 | return true; | |||
1344 | } | |||
1345 | return false; | |||
1346 | } | |||
1347 | ||||
1348 | void SymbolFileDWARFDebugMap::FinalizeOSOFileRanges(CompileUnitInfo *cu_info) { | |||
1349 | cu_info->file_range_map.Sort(); | |||
1350 | #if defined(DEBUG_OSO_DMAP) | |||
1351 | const FileRangeMap &oso_file_range_map = cu_info->GetFileRangeMap(this); | |||
1352 | const size_t n = oso_file_range_map.GetSize(); | |||
1353 | printf("SymbolFileDWARFDebugMap::FinalizeOSOFileRanges (cu_info = %p) %s\n", | |||
1354 | cu_info, cu_info->oso_sp->module_sp->GetFileSpec().GetPath().c_str()); | |||
1355 | for (size_t i = 0; i < n; ++i) { | |||
1356 | const FileRangeMap::Entry &entry = oso_file_range_map.GetEntryRef(i); | |||
1357 | printf("oso [0x%16.16" PRIx64"l" "x" " - 0x%16.16" PRIx64"l" "x" | |||
1358 | ") ==> exe [0x%16.16" PRIx64"l" "x" " - 0x%16.16" PRIx64"l" "x" ")\n", | |||
1359 | entry.GetRangeBase(), entry.GetRangeEnd(), entry.data, | |||
1360 | entry.data + entry.GetByteSize()); | |||
1361 | } | |||
1362 | #endif | |||
1363 | } | |||
1364 | ||||
1365 | lldb::addr_t | |||
1366 | SymbolFileDWARFDebugMap::LinkOSOFileAddress(SymbolFileDWARF *oso_symfile, | |||
1367 | lldb::addr_t oso_file_addr) { | |||
1368 | CompileUnitInfo *cu_info = GetCompileUnitInfo(oso_symfile); | |||
1369 | if (cu_info) { | |||
1370 | const FileRangeMap::Entry *oso_range_entry = | |||
1371 | cu_info->GetFileRangeMap(this).FindEntryThatContains(oso_file_addr); | |||
1372 | if (oso_range_entry) { | |||
1373 | const DebugMap::Entry *debug_map_entry = | |||
1374 | m_debug_map.FindEntryThatContains(oso_range_entry->data); | |||
1375 | if (debug_map_entry) { | |||
1376 | const lldb::addr_t offset = | |||
1377 | oso_file_addr - oso_range_entry->GetRangeBase(); | |||
1378 | const lldb::addr_t exe_file_addr = | |||
1379 | debug_map_entry->GetRangeBase() + offset; | |||
1380 | return exe_file_addr; | |||
1381 | } | |||
1382 | } | |||
1383 | } | |||
1384 | return LLDB_INVALID_ADDRESS(18446744073709551615UL); | |||
1385 | } | |||
1386 | ||||
1387 | bool SymbolFileDWARFDebugMap::LinkOSOAddress(Address &addr) { | |||
1388 | // Make sure this address hasn't been fixed already | |||
1389 | Module *exe_module = GetObjectFile()->GetModule().get(); | |||
1390 | Module *addr_module = addr.GetModule().get(); | |||
1391 | if (addr_module == exe_module) | |||
1392 | return true; // Address is already in terms of the main executable module | |||
1393 | ||||
1394 | CompileUnitInfo *cu_info = GetCompileUnitInfo(GetSymbolFileAsSymbolFileDWARF( | |||
1395 | addr_module->GetSymbolVendor()->GetSymbolFile())); | |||
1396 | if (cu_info) { | |||
1397 | const lldb::addr_t oso_file_addr = addr.GetFileAddress(); | |||
1398 | const FileRangeMap::Entry *oso_range_entry = | |||
1399 | cu_info->GetFileRangeMap(this).FindEntryThatContains(oso_file_addr); | |||
1400 | if (oso_range_entry) { | |||
1401 | const DebugMap::Entry *debug_map_entry = | |||
1402 | m_debug_map.FindEntryThatContains(oso_range_entry->data); | |||
1403 | if (debug_map_entry) { | |||
1404 | const lldb::addr_t offset = | |||
1405 | oso_file_addr - oso_range_entry->GetRangeBase(); | |||
1406 | const lldb::addr_t exe_file_addr = | |||
1407 | debug_map_entry->GetRangeBase() + offset; | |||
1408 | return exe_module->ResolveFileAddress(exe_file_addr, addr); | |||
1409 | } | |||
1410 | } | |||
1411 | } | |||
1412 | return true; | |||
1413 | } | |||
1414 | ||||
1415 | LineTable *SymbolFileDWARFDebugMap::LinkOSOLineTable(SymbolFileDWARF *oso_dwarf, | |||
1416 | LineTable *line_table) { | |||
1417 | CompileUnitInfo *cu_info = GetCompileUnitInfo(oso_dwarf); | |||
1418 | if (cu_info) | |||
1419 | return line_table->LinkLineTable(cu_info->GetFileRangeMap(this)); | |||
1420 | return NULL__null; | |||
1421 | } | |||
1422 | ||||
1423 | size_t | |||
1424 | SymbolFileDWARFDebugMap::AddOSOARanges(SymbolFileDWARF *dwarf2Data, | |||
1425 | DWARFDebugAranges *debug_aranges) { | |||
1426 | size_t num_line_entries_added = 0; | |||
1427 | if (debug_aranges && dwarf2Data) { | |||
1428 | CompileUnitInfo *compile_unit_info = GetCompileUnitInfo(dwarf2Data); | |||
1429 | if (compile_unit_info) { | |||
1430 | const FileRangeMap &file_range_map = | |||
1431 | compile_unit_info->GetFileRangeMap(this); | |||
1432 | for (size_t idx = 0; idx < file_range_map.GetSize(); idx++) { | |||
1433 | const FileRangeMap::Entry *entry = file_range_map.GetEntryAtIndex(idx); | |||
1434 | if (entry) { | |||
1435 | debug_aranges->AppendRange(dwarf2Data->GetID(), entry->GetRangeBase(), | |||
1436 | entry->GetRangeEnd()); | |||
1437 | num_line_entries_added++; | |||
1438 | } | |||
1439 | } | |||
1440 | } | |||
1441 | } | |||
1442 | return num_line_entries_added; | |||
1443 | } |
1 | // shared_ptr and weak_ptr implementation -*- C++ -*- |
2 | |
3 | // Copyright (C) 2007-2017 Free Software Foundation, Inc. |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free |
6 | // software; you can redistribute it and/or modify it under the |
7 | // terms of the GNU General Public License as published by the |
8 | // Free Software Foundation; either version 3, or (at your option) |
9 | // any later version. |
10 | |
11 | // This library is distributed in the hope that it will be useful, |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | // GNU General Public License for 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 | // GCC Note: Based on files from version 1.32.0 of the Boost library. |
26 | |
27 | // shared_count.hpp |
28 | // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd. |
29 | |
30 | // shared_ptr.hpp |
31 | // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes. |
32 | // Copyright (C) 2001, 2002, 2003 Peter Dimov |
33 | |
34 | // weak_ptr.hpp |
35 | // Copyright (C) 2001, 2002, 2003 Peter Dimov |
36 | |
37 | // enable_shared_from_this.hpp |
38 | // Copyright (C) 2002 Peter Dimov |
39 | |
40 | // Distributed under the Boost Software License, Version 1.0. (See |
41 | // accompanying file LICENSE_1_0.txt or copy at |
42 | // http://www.boost.org/LICENSE_1_0.txt) |
43 | |
44 | /** @file |
45 | * This is an internal header file, included by other library headers. |
46 | * Do not attempt to use it directly. @headername{memory} |
47 | */ |
48 | |
49 | #ifndef _SHARED_PTR_H1 |
50 | #define _SHARED_PTR_H1 1 |
51 | |
52 | #include <bits/shared_ptr_base.h> |
53 | |
54 | namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
55 | { |
56 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
57 | |
58 | /** |
59 | * @addtogroup pointer_abstractions |
60 | * @{ |
61 | */ |
62 | |
63 | /// 20.7.2.2.11 shared_ptr I/O |
64 | template<typename _Ch, typename _Tr, typename _Tp, _Lock_policy _Lp> |
65 | inline std::basic_ostream<_Ch, _Tr>& |
66 | operator<<(std::basic_ostream<_Ch, _Tr>& __os, |
67 | const __shared_ptr<_Tp, _Lp>& __p) |
68 | { |
69 | __os << __p.get(); |
70 | return __os; |
71 | } |
72 | |
73 | /// 20.7.2.2.10 shared_ptr get_deleter |
74 | template<typename _Del, typename _Tp, _Lock_policy _Lp> |
75 | inline _Del* |
76 | get_deleter(const __shared_ptr<_Tp, _Lp>& __p) noexcept |
77 | { |
78 | #if __cpp_rtti199711 |
79 | return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); |
80 | #else |
81 | return 0; |
82 | #endif |
83 | } |
84 | |
85 | |
86 | /** |
87 | * @brief A smart pointer with reference-counted copy semantics. |
88 | * |
89 | * The object pointed to is deleted when the last shared_ptr pointing to |
90 | * it is destroyed or reset. |
91 | */ |
92 | template<typename _Tp> |
93 | class shared_ptr : public __shared_ptr<_Tp> |
94 | { |
95 | template<typename... _Args> |
96 | using _Constructible = typename enable_if< |
97 | is_constructible<__shared_ptr<_Tp>, _Args...>::value |
98 | >::type; |
99 | |
100 | template<typename _Arg> |
101 | using _Assignable = typename enable_if< |
102 | is_assignable<__shared_ptr<_Tp>&, _Arg>::value, shared_ptr& |
103 | >::type; |
104 | |
105 | public: |
106 | |
107 | using element_type = typename __shared_ptr<_Tp>::element_type; |
108 | |
109 | #if __cplusplus201103L > 201402L |
110 | # define __cpp_lib_shared_ptr_weak_type 201606 |
111 | using weak_type = weak_ptr<_Tp>; |
112 | #endif |
113 | /** |
114 | * @brief Construct an empty %shared_ptr. |
115 | * @post use_count()==0 && get()==0 |
116 | */ |
117 | constexpr shared_ptr() noexcept : __shared_ptr<_Tp>() { } |
118 | |
119 | shared_ptr(const shared_ptr&) noexcept = default; |
120 | |
121 | /** |
122 | * @brief Construct a %shared_ptr that owns the pointer @a __p. |
123 | * @param __p A pointer that is convertible to element_type*. |
124 | * @post use_count() == 1 && get() == __p |
125 | * @throw std::bad_alloc, in which case @c delete @a __p is called. |
126 | */ |
127 | template<typename _Yp, typename = _Constructible<_Yp*>> |
128 | explicit |
129 | shared_ptr(_Yp* __p) : __shared_ptr<_Tp>(__p) { } |
130 | |
131 | /** |
132 | * @brief Construct a %shared_ptr that owns the pointer @a __p |
133 | * and the deleter @a __d. |
134 | * @param __p A pointer. |
135 | * @param __d A deleter. |
136 | * @post use_count() == 1 && get() == __p |
137 | * @throw std::bad_alloc, in which case @a __d(__p) is called. |
138 | * |
139 | * Requirements: _Deleter's copy constructor and destructor must |
140 | * not throw |
141 | * |
142 | * __shared_ptr will release __p by calling __d(__p) |
143 | */ |
144 | template<typename _Yp, typename _Deleter, |
145 | typename = _Constructible<_Yp*, _Deleter>> |
146 | shared_ptr(_Yp* __p, _Deleter __d) |
147 | : __shared_ptr<_Tp>(__p, std::move(__d)) { } |
148 | |
149 | /** |
150 | * @brief Construct a %shared_ptr that owns a null pointer |
151 | * and the deleter @a __d. |
152 | * @param __p A null pointer constant. |
153 | * @param __d A deleter. |
154 | * @post use_count() == 1 && get() == __p |
155 | * @throw std::bad_alloc, in which case @a __d(__p) is called. |
156 | * |
157 | * Requirements: _Deleter's copy constructor and destructor must |
158 | * not throw |
159 | * |
160 | * The last owner will call __d(__p) |
161 | */ |
162 | template<typename _Deleter> |
163 | shared_ptr(nullptr_t __p, _Deleter __d) |
164 | : __shared_ptr<_Tp>(__p, std::move(__d)) { } |
165 | |
166 | /** |
167 | * @brief Construct a %shared_ptr that owns the pointer @a __p |
168 | * and the deleter @a __d. |
169 | * @param __p A pointer. |
170 | * @param __d A deleter. |
171 | * @param __a An allocator. |
172 | * @post use_count() == 1 && get() == __p |
173 | * @throw std::bad_alloc, in which case @a __d(__p) is called. |
174 | * |
175 | * Requirements: _Deleter's copy constructor and destructor must |
176 | * not throw _Alloc's copy constructor and destructor must not |
177 | * throw. |
178 | * |
179 | * __shared_ptr will release __p by calling __d(__p) |
180 | */ |
181 | template<typename _Yp, typename _Deleter, typename _Alloc, |
182 | typename = _Constructible<_Yp*, _Deleter, _Alloc>> |
183 | shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a) |
184 | : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { } |
185 | |
186 | /** |
187 | * @brief Construct a %shared_ptr that owns a null pointer |
188 | * and the deleter @a __d. |
189 | * @param __p A null pointer constant. |
190 | * @param __d A deleter. |
191 | * @param __a An allocator. |
192 | * @post use_count() == 1 && get() == __p |
193 | * @throw std::bad_alloc, in which case @a __d(__p) is called. |
194 | * |
195 | * Requirements: _Deleter's copy constructor and destructor must |
196 | * not throw _Alloc's copy constructor and destructor must not |
197 | * throw. |
198 | * |
199 | * The last owner will call __d(__p) |
200 | */ |
201 | template<typename _Deleter, typename _Alloc> |
202 | shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a) |
203 | : __shared_ptr<_Tp>(__p, std::move(__d), std::move(__a)) { } |
204 | |
205 | // Aliasing constructor |
206 | |
207 | /** |
208 | * @brief Constructs a %shared_ptr instance that stores @a __p |
209 | * and shares ownership with @a __r. |
210 | * @param __r A %shared_ptr. |
211 | * @param __p A pointer that will remain valid while @a *__r is valid. |
212 | * @post get() == __p && use_count() == __r.use_count() |
213 | * |
214 | * This can be used to construct a @c shared_ptr to a sub-object |
215 | * of an object managed by an existing @c shared_ptr. |
216 | * |
217 | * @code |
218 | * shared_ptr< pair<int,int> > pii(new pair<int,int>()); |
219 | * shared_ptr<int> pi(pii, &pii->first); |
220 | * assert(pii.use_count() == 2); |
221 | * @endcode |
222 | */ |
223 | template<typename _Yp> |
224 | shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) noexcept |
225 | : __shared_ptr<_Tp>(__r, __p) { } |
226 | |
227 | /** |
228 | * @brief If @a __r is empty, constructs an empty %shared_ptr; |
229 | * otherwise construct a %shared_ptr that shares ownership |
230 | * with @a __r. |
231 | * @param __r A %shared_ptr. |
232 | * @post get() == __r.get() && use_count() == __r.use_count() |
233 | */ |
234 | template<typename _Yp, |
235 | typename = _Constructible<const shared_ptr<_Yp>&>> |
236 | shared_ptr(const shared_ptr<_Yp>& __r) noexcept |
237 | : __shared_ptr<_Tp>(__r) { } |
238 | |
239 | /** |
240 | * @brief Move-constructs a %shared_ptr instance from @a __r. |
241 | * @param __r A %shared_ptr rvalue. |
242 | * @post *this contains the old value of @a __r, @a __r is empty. |
243 | */ |
244 | shared_ptr(shared_ptr&& __r) noexcept |
245 | : __shared_ptr<_Tp>(std::move(__r)) { } |
246 | |
247 | /** |
248 | * @brief Move-constructs a %shared_ptr instance from @a __r. |
249 | * @param __r A %shared_ptr rvalue. |
250 | * @post *this contains the old value of @a __r, @a __r is empty. |
251 | */ |
252 | template<typename _Yp, typename = _Constructible<shared_ptr<_Yp>>> |
253 | shared_ptr(shared_ptr<_Yp>&& __r) noexcept |
254 | : __shared_ptr<_Tp>(std::move(__r)) { } |
255 | |
256 | /** |
257 | * @brief Constructs a %shared_ptr that shares ownership with @a __r |
258 | * and stores a copy of the pointer stored in @a __r. |
259 | * @param __r A weak_ptr. |
260 | * @post use_count() == __r.use_count() |
261 | * @throw bad_weak_ptr when __r.expired(), |
262 | * in which case the constructor has no effect. |
263 | */ |
264 | template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>> |
265 | explicit shared_ptr(const weak_ptr<_Yp>& __r) |
266 | : __shared_ptr<_Tp>(__r) { } |
267 | |
268 | #if _GLIBCXX_USE_DEPRECATED1 |
269 | template<typename _Yp, typename = _Constructible<auto_ptr<_Yp>>> |
270 | shared_ptr(auto_ptr<_Yp>&& __r); |
271 | #endif |
272 | |
273 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
274 | // 2399. shared_ptr's constructor from unique_ptr should be constrained |
275 | template<typename _Yp, typename _Del, |
276 | typename = _Constructible<unique_ptr<_Yp, _Del>>> |
277 | shared_ptr(unique_ptr<_Yp, _Del>&& __r) |
278 | : __shared_ptr<_Tp>(std::move(__r)) { } |
279 | |
280 | #if __cplusplus201103L <= 201402L && _GLIBCXX_USE_DEPRECATED1 |
281 | // This non-standard constructor exists to support conversions that |
282 | // were possible in C++11 and C++14 but are ill-formed in C++17. |
283 | // If an exception is thrown this constructor has no effect. |
284 | template<typename _Yp, typename _Del, |
285 | _Constructible<unique_ptr<_Yp, _Del>, __sp_array_delete>* = 0> |
286 | shared_ptr(unique_ptr<_Yp, _Del>&& __r) |
287 | : __shared_ptr<_Tp>(std::move(__r), __sp_array_delete()) { } |
288 | #endif |
289 | |
290 | /** |
291 | * @brief Construct an empty %shared_ptr. |
292 | * @post use_count() == 0 && get() == nullptr |
293 | */ |
294 | constexpr shared_ptr(nullptr_t) noexcept : shared_ptr() { } |
295 | |
296 | shared_ptr& operator=(const shared_ptr&) noexcept = default; |
297 | |
298 | template<typename _Yp> |
299 | _Assignable<const shared_ptr<_Yp>&> |
300 | operator=(const shared_ptr<_Yp>& __r) noexcept |
301 | { |
302 | this->__shared_ptr<_Tp>::operator=(__r); |
303 | return *this; |
304 | } |
305 | |
306 | #if _GLIBCXX_USE_DEPRECATED1 |
307 | template<typename _Yp> |
308 | _Assignable<auto_ptr<_Yp>> |
309 | operator=(auto_ptr<_Yp>&& __r) |
310 | { |
311 | this->__shared_ptr<_Tp>::operator=(std::move(__r)); |
312 | return *this; |
313 | } |
314 | #endif |
315 | |
316 | shared_ptr& |
317 | operator=(shared_ptr&& __r) noexcept |
318 | { |
319 | this->__shared_ptr<_Tp>::operator=(std::move(__r)); |
320 | return *this; |
321 | } |
322 | |
323 | template<class _Yp> |
324 | _Assignable<shared_ptr<_Yp>> |
325 | operator=(shared_ptr<_Yp>&& __r) noexcept |
326 | { |
327 | this->__shared_ptr<_Tp>::operator=(std::move(__r)); |
328 | return *this; |
329 | } |
330 | |
331 | template<typename _Yp, typename _Del> |
332 | _Assignable<unique_ptr<_Yp, _Del>> |
333 | operator=(unique_ptr<_Yp, _Del>&& __r) |
334 | { |
335 | this->__shared_ptr<_Tp>::operator=(std::move(__r)); |
336 | return *this; |
337 | } |
338 | |
339 | private: |
340 | // This constructor is non-standard, it is used by allocate_shared. |
341 | template<typename _Alloc, typename... _Args> |
342 | shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a, |
343 | _Args&&... __args) |
344 | : __shared_ptr<_Tp>(__tag, __a, std::forward<_Args>(__args)...) |
345 | { } |
346 | |
347 | template<typename _Yp, typename _Alloc, typename... _Args> |
348 | friend shared_ptr<_Yp> |
349 | allocate_shared(const _Alloc& __a, _Args&&... __args); |
350 | |
351 | // This constructor is non-standard, it is used by weak_ptr::lock(). |
352 | shared_ptr(const weak_ptr<_Tp>& __r, std::nothrow_t) |
353 | : __shared_ptr<_Tp>(__r, std::nothrow) { } |
354 | |
355 | friend class weak_ptr<_Tp>; |
356 | }; |
357 | |
358 | #if __cpp_deduction_guides >= 201606 |
359 | template<typename _Tp> |
360 | shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>; |
361 | template<typename _Tp, typename _Del> |
362 | shared_ptr(unique_ptr<_Tp, _Del>) -> shared_ptr<_Tp>; |
363 | #endif |
364 | |
365 | // 20.7.2.2.7 shared_ptr comparisons |
366 | template<typename _Tp, typename _Up> |
367 | inline bool |
368 | operator==(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
369 | { return __a.get() == __b.get(); } |
370 | |
371 | template<typename _Tp> |
372 | inline bool |
373 | operator==(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
374 | { return !__a; } |
375 | |
376 | template<typename _Tp> |
377 | inline bool |
378 | operator==(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
379 | { return !__a; } |
380 | |
381 | template<typename _Tp, typename _Up> |
382 | inline bool |
383 | operator!=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
384 | { return __a.get() != __b.get(); } |
385 | |
386 | template<typename _Tp> |
387 | inline bool |
388 | operator!=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
389 | { return (bool)__a; } |
390 | |
391 | template<typename _Tp> |
392 | inline bool |
393 | operator!=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
394 | { return (bool)__a; } |
395 | |
396 | template<typename _Tp, typename _Up> |
397 | inline bool |
398 | operator<(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
399 | { |
400 | using _Tp_elt = typename shared_ptr<_Tp>::element_type; |
401 | using _Up_elt = typename shared_ptr<_Up>::element_type; |
402 | using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type; |
403 | return less<_Vp>()(__a.get(), __b.get()); |
404 | } |
405 | |
406 | template<typename _Tp> |
407 | inline bool |
408 | operator<(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
409 | { |
410 | using _Tp_elt = typename shared_ptr<_Tp>::element_type; |
411 | return less<_Tp_elt*>()(__a.get(), nullptr); |
412 | } |
413 | |
414 | template<typename _Tp> |
415 | inline bool |
416 | operator<(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
417 | { |
418 | using _Tp_elt = typename shared_ptr<_Tp>::element_type; |
419 | return less<_Tp_elt*>()(nullptr, __a.get()); |
420 | } |
421 | |
422 | template<typename _Tp, typename _Up> |
423 | inline bool |
424 | operator<=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
425 | { return !(__b < __a); } |
426 | |
427 | template<typename _Tp> |
428 | inline bool |
429 | operator<=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
430 | { return !(nullptr < __a); } |
431 | |
432 | template<typename _Tp> |
433 | inline bool |
434 | operator<=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
435 | { return !(__a < nullptr); } |
436 | |
437 | template<typename _Tp, typename _Up> |
438 | inline bool |
439 | operator>(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
440 | { return (__b < __a); } |
441 | |
442 | template<typename _Tp> |
443 | inline bool |
444 | operator>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
445 | { return nullptr < __a; } |
446 | |
447 | template<typename _Tp> |
448 | inline bool |
449 | operator>(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
450 | { return __a < nullptr; } |
451 | |
452 | template<typename _Tp, typename _Up> |
453 | inline bool |
454 | operator>=(const shared_ptr<_Tp>& __a, const shared_ptr<_Up>& __b) noexcept |
455 | { return !(__a < __b); } |
456 | |
457 | template<typename _Tp> |
458 | inline bool |
459 | operator>=(const shared_ptr<_Tp>& __a, nullptr_t) noexcept |
460 | { return !(__a < nullptr); } |
461 | |
462 | template<typename _Tp> |
463 | inline bool |
464 | operator>=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept |
465 | { return !(nullptr < __a); } |
466 | |
467 | template<typename _Tp> |
468 | struct less<shared_ptr<_Tp>> : public _Sp_less<shared_ptr<_Tp>> |
469 | { }; |
470 | |
471 | // 20.7.2.2.8 shared_ptr specialized algorithms. |
472 | template<typename _Tp> |
473 | inline void |
474 | swap(shared_ptr<_Tp>& __a, shared_ptr<_Tp>& __b) noexcept |
475 | { __a.swap(__b); } |
476 | |
477 | // 20.7.2.2.9 shared_ptr casts. |
478 | template<typename _Tp, typename _Up> |
479 | inline shared_ptr<_Tp> |
480 | static_pointer_cast(const shared_ptr<_Up>& __r) noexcept |
481 | { |
482 | using _Sp = shared_ptr<_Tp>; |
483 | return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get())); |
484 | } |
485 | |
486 | template<typename _Tp, typename _Up> |
487 | inline shared_ptr<_Tp> |
488 | const_pointer_cast(const shared_ptr<_Up>& __r) noexcept |
489 | { |
490 | using _Sp = shared_ptr<_Tp>; |
491 | return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get())); |
492 | } |
493 | |
494 | template<typename _Tp, typename _Up> |
495 | inline shared_ptr<_Tp> |
496 | dynamic_pointer_cast(const shared_ptr<_Up>& __r) noexcept |
497 | { |
498 | using _Sp = shared_ptr<_Tp>; |
499 | if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get())) |
500 | return _Sp(__r, __p); |
501 | return _Sp(); |
502 | } |
503 | |
504 | #if __cplusplus201103L > 201402L |
505 | template<typename _Tp, typename _Up> |
506 | inline shared_ptr<_Tp> |
507 | reinterpret_pointer_cast(const shared_ptr<_Up>& __r) noexcept |
508 | { |
509 | using _Sp = shared_ptr<_Tp>; |
510 | return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get())); |
511 | } |
512 | #endif |
513 | |
514 | /** |
515 | * @brief A smart pointer with weak semantics. |
516 | * |
517 | * With forwarding constructors and assignment operators. |
518 | */ |
519 | template<typename _Tp> |
520 | class weak_ptr : public __weak_ptr<_Tp> |
521 | { |
522 | template<typename _Arg> |
523 | using _Constructible = typename enable_if< |
524 | is_constructible<__weak_ptr<_Tp>, _Arg>::value |
525 | >::type; |
526 | |
527 | template<typename _Arg> |
528 | using _Assignable = typename enable_if< |
529 | is_assignable<__weak_ptr<_Tp>&, _Arg>::value, weak_ptr& |
530 | >::type; |
531 | |
532 | public: |
533 | constexpr weak_ptr() noexcept = default; |
534 | |
535 | template<typename _Yp, |
536 | typename = _Constructible<const shared_ptr<_Yp>&>> |
537 | weak_ptr(const shared_ptr<_Yp>& __r) noexcept |
538 | : __weak_ptr<_Tp>(__r) { } |
539 | |
540 | weak_ptr(const weak_ptr&) noexcept = default; |
541 | |
542 | template<typename _Yp, typename = _Constructible<const weak_ptr<_Yp>&>> |
543 | weak_ptr(const weak_ptr<_Yp>& __r) noexcept |
544 | : __weak_ptr<_Tp>(__r) { } |
545 | |
546 | weak_ptr(weak_ptr&&) noexcept = default; |
547 | |
548 | template<typename _Yp, typename = _Constructible<weak_ptr<_Yp>>> |
549 | weak_ptr(weak_ptr<_Yp>&& __r) noexcept |
550 | : __weak_ptr<_Tp>(std::move(__r)) { } |
551 | |
552 | weak_ptr& |
553 | operator=(const weak_ptr& __r) noexcept = default; |
554 | |
555 | template<typename _Yp> |
556 | _Assignable<const weak_ptr<_Yp>&> |
557 | operator=(const weak_ptr<_Yp>& __r) noexcept |
558 | { |
559 | this->__weak_ptr<_Tp>::operator=(__r); |
560 | return *this; |
561 | } |
562 | |
563 | template<typename _Yp> |
564 | _Assignable<const shared_ptr<_Yp>&> |
565 | operator=(const shared_ptr<_Yp>& __r) noexcept |
566 | { |
567 | this->__weak_ptr<_Tp>::operator=(__r); |
568 | return *this; |
569 | } |
570 | |
571 | weak_ptr& |
572 | operator=(weak_ptr&& __r) noexcept = default; |
573 | |
574 | template<typename _Yp> |
575 | _Assignable<weak_ptr<_Yp>> |
576 | operator=(weak_ptr<_Yp>&& __r) noexcept |
577 | { |
578 | this->__weak_ptr<_Tp>::operator=(std::move(__r)); |
579 | return *this; |
580 | } |
581 | |
582 | shared_ptr<_Tp> |
583 | lock() const noexcept |
584 | { return shared_ptr<_Tp>(*this, std::nothrow); } |
585 | }; |
586 | |
587 | #if __cpp_deduction_guides >= 201606 |
588 | template<typename _Tp> |
589 | weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>; |
590 | #endif |
591 | |
592 | // 20.7.2.3.6 weak_ptr specialized algorithms. |
593 | template<typename _Tp> |
594 | inline void |
595 | swap(weak_ptr<_Tp>& __a, weak_ptr<_Tp>& __b) noexcept |
596 | { __a.swap(__b); } |
597 | |
598 | |
599 | /// Primary template owner_less |
600 | template<typename _Tp = void> |
601 | struct owner_less; |
602 | |
603 | /// Void specialization of owner_less |
604 | template<> |
605 | struct owner_less<void> : _Sp_owner_less<void, void> |
606 | { }; |
607 | |
608 | /// Partial specialization of owner_less for shared_ptr. |
609 | template<typename _Tp> |
610 | struct owner_less<shared_ptr<_Tp>> |
611 | : public _Sp_owner_less<shared_ptr<_Tp>, weak_ptr<_Tp>> |
612 | { }; |
613 | |
614 | /// Partial specialization of owner_less for weak_ptr. |
615 | template<typename _Tp> |
616 | struct owner_less<weak_ptr<_Tp>> |
617 | : public _Sp_owner_less<weak_ptr<_Tp>, shared_ptr<_Tp>> |
618 | { }; |
619 | |
620 | /** |
621 | * @brief Base class allowing use of member function shared_from_this. |
622 | */ |
623 | template<typename _Tp> |
624 | class enable_shared_from_this |
625 | { |
626 | protected: |
627 | constexpr enable_shared_from_this() noexcept { } |
628 | |
629 | enable_shared_from_this(const enable_shared_from_this&) noexcept { } |
630 | |
631 | enable_shared_from_this& |
632 | operator=(const enable_shared_from_this&) noexcept |
633 | { return *this; } |
634 | |
635 | ~enable_shared_from_this() { } |
636 | |
637 | public: |
638 | shared_ptr<_Tp> |
639 | shared_from_this() |
640 | { return shared_ptr<_Tp>(this->_M_weak_this); } |
641 | |
642 | shared_ptr<const _Tp> |
643 | shared_from_this() const |
644 | { return shared_ptr<const _Tp>(this->_M_weak_this); } |
645 | |
646 | #if __cplusplus201103L > 201402L || !defined(__STRICT_ANSI__1) // c++1z or gnu++11 |
647 | #define __cpp_lib_enable_shared_from_this 201603 |
648 | weak_ptr<_Tp> |
649 | weak_from_this() noexcept |
650 | { return this->_M_weak_this; } |
651 | |
652 | weak_ptr<const _Tp> |
653 | weak_from_this() const noexcept |
654 | { return this->_M_weak_this; } |
655 | #endif |
656 | |
657 | private: |
658 | template<typename _Tp1> |
659 | void |
660 | _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept |
661 | { _M_weak_this._M_assign(__p, __n); } |
662 | |
663 | // Found by ADL when this is an associated class. |
664 | friend const enable_shared_from_this* |
665 | __enable_shared_from_this_base(const __shared_count<>&, |
666 | const enable_shared_from_this* __p) |
667 | { return __p; } |
668 | |
669 | template<typename, _Lock_policy> |
670 | friend class __shared_ptr; |
671 | |
672 | mutable weak_ptr<_Tp> _M_weak_this; |
673 | }; |
674 | |
675 | /** |
676 | * @brief Create an object that is owned by a shared_ptr. |
677 | * @param __a An allocator. |
678 | * @param __args Arguments for the @a _Tp object's constructor. |
679 | * @return A shared_ptr that owns the newly created object. |
680 | * @throw An exception thrown from @a _Alloc::allocate or from the |
681 | * constructor of @a _Tp. |
682 | * |
683 | * A copy of @a __a will be used to allocate memory for the shared_ptr |
684 | * and the new object. |
685 | */ |
686 | template<typename _Tp, typename _Alloc, typename... _Args> |
687 | inline shared_ptr<_Tp> |
688 | allocate_shared(const _Alloc& __a, _Args&&... __args) |
689 | { |
690 | return shared_ptr<_Tp>(_Sp_make_shared_tag(), __a, |
691 | std::forward<_Args>(__args)...); |
692 | } |
693 | |
694 | /** |
695 | * @brief Create an object that is owned by a shared_ptr. |
696 | * @param __args Arguments for the @a _Tp object's constructor. |
697 | * @return A shared_ptr that owns the newly created object. |
698 | * @throw std::bad_alloc, or an exception thrown from the |
699 | * constructor of @a _Tp. |
700 | */ |
701 | template<typename _Tp, typename... _Args> |
702 | inline shared_ptr<_Tp> |
703 | make_shared(_Args&&... __args) |
704 | { |
705 | typedef typename std::remove_const<_Tp>::type _Tp_nc; |
706 | return std::allocate_shared<_Tp>(std::allocator<_Tp_nc>(), |
707 | std::forward<_Args>(__args)...); |
708 | } |
709 | |
710 | /// std::hash specialization for shared_ptr. |
711 | template<typename _Tp> |
712 | struct hash<shared_ptr<_Tp>> |
713 | : public __hash_base<size_t, shared_ptr<_Tp>> |
714 | { |
715 | size_t |
716 | operator()(const shared_ptr<_Tp>& __s) const noexcept |
717 | { |
718 | return std::hash<typename shared_ptr<_Tp>::element_type*>()(__s.get()); |
719 | } |
720 | }; |
721 | |
722 | // @} group pointer_abstractions |
723 | |
724 | _GLIBCXX_END_NAMESPACE_VERSION |
725 | } // namespace |
726 | |
727 | #endif // _SHARED_PTR_H |
1 | // shared_ptr and weak_ptr implementation details -*- C++ -*- |
2 | |
3 | // Copyright (C) 2007-2017 Free Software Foundation, Inc. |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free |
6 | // software; you can redistribute it and/or modify it under the |
7 | // terms of the GNU General Public License as published by the |
8 | // Free Software Foundation; either version 3, or (at your option) |
9 | // any later version. |
10 | |
11 | // This library is distributed in the hope that it will be useful, |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | // GNU General Public License for 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 | // GCC Note: Based on files from version 1.32.0 of the Boost library. |
26 | |
27 | // shared_count.hpp |
28 | // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd. |
29 | |
30 | // shared_ptr.hpp |
31 | // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes. |
32 | // Copyright (C) 2001, 2002, 2003 Peter Dimov |
33 | |
34 | // weak_ptr.hpp |
35 | // Copyright (C) 2001, 2002, 2003 Peter Dimov |
36 | |
37 | // enable_shared_from_this.hpp |
38 | // Copyright (C) 2002 Peter Dimov |
39 | |
40 | // Distributed under the Boost Software License, Version 1.0. (See |
41 | // accompanying file LICENSE_1_0.txt or copy at |
42 | // http://www.boost.org/LICENSE_1_0.txt) |
43 | |
44 | /** @file bits/shared_ptr_base.h |
45 | * This is an internal header file, included by other library headers. |
46 | * Do not attempt to use it directly. @headername{memory} |
47 | */ |
48 | |
49 | #ifndef _SHARED_PTR_BASE_H1 |
50 | #define _SHARED_PTR_BASE_H1 1 |
51 | |
52 | #if __cpp_rtti199711 |
53 | # include <typeinfo> |
54 | #endif |
55 | #include <bits/allocated_ptr.h> |
56 | #include <bits/refwrap.h> |
57 | #include <bits/stl_function.h> |
58 | #include <ext/aligned_buffer.h> |
59 | |
60 | namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
61 | { |
62 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
63 | |
64 | #if _GLIBCXX_USE_DEPRECATED1 |
65 | template<typename> class auto_ptr; |
66 | #endif |
67 | |
68 | /** |
69 | * @brief Exception possibly thrown by @c shared_ptr. |
70 | * @ingroup exceptions |
71 | */ |
72 | class bad_weak_ptr : public std::exception |
73 | { |
74 | public: |
75 | virtual char const* what() const noexcept; |
76 | |
77 | virtual ~bad_weak_ptr() noexcept; |
78 | }; |
79 | |
80 | // Substitute for bad_weak_ptr object in the case of -fno-exceptions. |
81 | inline void |
82 | __throw_bad_weak_ptr() |
83 | { _GLIBCXX_THROW_OR_ABORT(bad_weak_ptr())(__builtin_abort()); } |
84 | |
85 | using __gnu_cxx::_Lock_policy; |
86 | using __gnu_cxx::__default_lock_policy; |
87 | using __gnu_cxx::_S_single; |
88 | using __gnu_cxx::_S_mutex; |
89 | using __gnu_cxx::_S_atomic; |
90 | |
91 | // Empty helper class except when the template argument is _S_mutex. |
92 | template<_Lock_policy _Lp> |
93 | class _Mutex_base |
94 | { |
95 | protected: |
96 | // The atomic policy uses fully-fenced builtins, single doesn't care. |
97 | enum { _S_need_barriers = 0 }; |
98 | }; |
99 | |
100 | template<> |
101 | class _Mutex_base<_S_mutex> |
102 | : public __gnu_cxx::__mutex |
103 | { |
104 | protected: |
105 | // This policy is used when atomic builtins are not available. |
106 | // The replacement atomic operations might not have the necessary |
107 | // memory barriers. |
108 | enum { _S_need_barriers = 1 }; |
109 | }; |
110 | |
111 | template<_Lock_policy _Lp = __default_lock_policy> |
112 | class _Sp_counted_base |
113 | : public _Mutex_base<_Lp> |
114 | { |
115 | public: |
116 | _Sp_counted_base() noexcept |
117 | : _M_use_count(1), _M_weak_count(1) { } |
118 | |
119 | virtual |
120 | ~_Sp_counted_base() noexcept |
121 | { } |
122 | |
123 | // Called when _M_use_count drops to zero, to release the resources |
124 | // managed by *this. |
125 | virtual void |
126 | _M_dispose() noexcept = 0; |
127 | |
128 | // Called when _M_weak_count drops to zero. |
129 | virtual void |
130 | _M_destroy() noexcept |
131 | { delete this; } |
132 | |
133 | virtual void* |
134 | _M_get_deleter(const std::type_info&) noexcept = 0; |
135 | |
136 | void |
137 | _M_add_ref_copy() |
138 | { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); } |
139 | |
140 | void |
141 | _M_add_ref_lock(); |
142 | |
143 | bool |
144 | _M_add_ref_lock_nothrow(); |
145 | |
146 | void |
147 | _M_release() noexcept |
148 | { |
149 | // Be race-detector-friendly. For more info see bits/c++config. |
150 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_use_count); |
151 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1) |
152 | { |
153 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_use_count); |
154 | _M_dispose(); |
155 | // There must be a memory barrier between dispose() and destroy() |
156 | // to ensure that the effects of dispose() are observed in the |
157 | // thread that runs destroy(). |
158 | // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html |
159 | if (_Mutex_base<_Lp>::_S_need_barriers) |
160 | { |
161 | __atomic_thread_fence (__ATOMIC_ACQ_REL4); |
162 | } |
163 | |
164 | // Be race-detector-friendly. For more info see bits/c++config. |
165 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count); |
166 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, |
167 | -1) == 1) |
168 | { |
169 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count); |
170 | _M_destroy(); |
171 | } |
172 | } |
173 | } |
174 | |
175 | void |
176 | _M_weak_add_ref() noexcept |
177 | { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); } |
178 | |
179 | void |
180 | _M_weak_release() noexcept |
181 | { |
182 | // Be race-detector-friendly. For more info see bits/c++config. |
183 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_weak_count); |
184 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1) |
185 | { |
186 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_weak_count); |
187 | if (_Mutex_base<_Lp>::_S_need_barriers) |
188 | { |
189 | // See _M_release(), |
190 | // destroy() must observe results of dispose() |
191 | __atomic_thread_fence (__ATOMIC_ACQ_REL4); |
192 | } |
193 | _M_destroy(); |
194 | } |
195 | } |
196 | |
197 | long |
198 | _M_get_use_count() const noexcept |
199 | { |
200 | // No memory barrier is used here so there is no synchronization |
201 | // with other threads. |
202 | return __atomic_load_n(&_M_use_count, __ATOMIC_RELAXED0); |
203 | } |
204 | |
205 | private: |
206 | _Sp_counted_base(_Sp_counted_base const&) = delete; |
207 | _Sp_counted_base& operator=(_Sp_counted_base const&) = delete; |
208 | |
209 | _Atomic_word _M_use_count; // #shared |
210 | _Atomic_word _M_weak_count; // #weak + (#shared != 0) |
211 | }; |
212 | |
213 | template<> |
214 | inline void |
215 | _Sp_counted_base<_S_single>:: |
216 | _M_add_ref_lock() |
217 | { |
218 | if (_M_use_count == 0) |
219 | __throw_bad_weak_ptr(); |
220 | ++_M_use_count; |
221 | } |
222 | |
223 | template<> |
224 | inline void |
225 | _Sp_counted_base<_S_mutex>:: |
226 | _M_add_ref_lock() |
227 | { |
228 | __gnu_cxx::__scoped_lock sentry(*this); |
229 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0) |
230 | { |
231 | _M_use_count = 0; |
232 | __throw_bad_weak_ptr(); |
233 | } |
234 | } |
235 | |
236 | template<> |
237 | inline void |
238 | _Sp_counted_base<_S_atomic>:: |
239 | _M_add_ref_lock() |
240 | { |
241 | // Perform lock-free add-if-not-zero operation. |
242 | _Atomic_word __count = _M_get_use_count(); |
243 | do |
244 | { |
245 | if (__count == 0) |
246 | __throw_bad_weak_ptr(); |
247 | // Replace the current counter value with the old value + 1, as |
248 | // long as it's not changed meanwhile. |
249 | } |
250 | while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1, |
251 | true, __ATOMIC_ACQ_REL4, |
252 | __ATOMIC_RELAXED0)); |
253 | } |
254 | |
255 | template<> |
256 | inline bool |
257 | _Sp_counted_base<_S_single>:: |
258 | _M_add_ref_lock_nothrow() |
259 | { |
260 | if (_M_use_count == 0) |
261 | return false; |
262 | ++_M_use_count; |
263 | return true; |
264 | } |
265 | |
266 | template<> |
267 | inline bool |
268 | _Sp_counted_base<_S_mutex>:: |
269 | _M_add_ref_lock_nothrow() |
270 | { |
271 | __gnu_cxx::__scoped_lock sentry(*this); |
272 | if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0) |
273 | { |
274 | _M_use_count = 0; |
275 | return false; |
276 | } |
277 | return true; |
278 | } |
279 | |
280 | template<> |
281 | inline bool |
282 | _Sp_counted_base<_S_atomic>:: |
283 | _M_add_ref_lock_nothrow() |
284 | { |
285 | // Perform lock-free add-if-not-zero operation. |
286 | _Atomic_word __count = _M_get_use_count(); |
287 | do |
288 | { |
289 | if (__count == 0) |
290 | return false; |
291 | // Replace the current counter value with the old value + 1, as |
292 | // long as it's not changed meanwhile. |
293 | } |
294 | while (!__atomic_compare_exchange_n(&_M_use_count, &__count, __count + 1, |
295 | true, __ATOMIC_ACQ_REL4, |
296 | __ATOMIC_RELAXED0)); |
297 | return true; |
298 | } |
299 | |
300 | template<> |
301 | inline void |
302 | _Sp_counted_base<_S_single>::_M_add_ref_copy() |
303 | { ++_M_use_count; } |
304 | |
305 | template<> |
306 | inline void |
307 | _Sp_counted_base<_S_single>::_M_release() noexcept |
308 | { |
309 | if (--_M_use_count == 0) |
310 | { |
311 | _M_dispose(); |
312 | if (--_M_weak_count == 0) |
313 | _M_destroy(); |
314 | } |
315 | } |
316 | |
317 | template<> |
318 | inline void |
319 | _Sp_counted_base<_S_single>::_M_weak_add_ref() noexcept |
320 | { ++_M_weak_count; } |
321 | |
322 | template<> |
323 | inline void |
324 | _Sp_counted_base<_S_single>::_M_weak_release() noexcept |
325 | { |
326 | if (--_M_weak_count == 0) |
327 | _M_destroy(); |
328 | } |
329 | |
330 | template<> |
331 | inline long |
332 | _Sp_counted_base<_S_single>::_M_get_use_count() const noexcept |
333 | { return _M_use_count; } |
334 | |
335 | |
336 | // Forward declarations. |
337 | template<typename _Tp, _Lock_policy _Lp = __default_lock_policy> |
338 | class __shared_ptr; |
339 | |
340 | template<typename _Tp, _Lock_policy _Lp = __default_lock_policy> |
341 | class __weak_ptr; |
342 | |
343 | template<typename _Tp, _Lock_policy _Lp = __default_lock_policy> |
344 | class __enable_shared_from_this; |
345 | |
346 | template<typename _Tp> |
347 | class shared_ptr; |
348 | |
349 | template<typename _Tp> |
350 | class weak_ptr; |
351 | |
352 | template<typename _Tp> |
353 | struct owner_less; |
354 | |
355 | template<typename _Tp> |
356 | class enable_shared_from_this; |
357 | |
358 | template<_Lock_policy _Lp = __default_lock_policy> |
359 | class __weak_count; |
360 | |
361 | template<_Lock_policy _Lp = __default_lock_policy> |
362 | class __shared_count; |
363 | |
364 | |
365 | // Counted ptr with no deleter or allocator support |
366 | template<typename _Ptr, _Lock_policy _Lp> |
367 | class _Sp_counted_ptr final : public _Sp_counted_base<_Lp> |
368 | { |
369 | public: |
370 | explicit |
371 | _Sp_counted_ptr(_Ptr __p) noexcept |
372 | : _M_ptr(__p) { } |
373 | |
374 | virtual void |
375 | _M_dispose() noexcept |
376 | { delete _M_ptr; } |
377 | |
378 | virtual void |
379 | _M_destroy() noexcept |
380 | { delete this; } |
381 | |
382 | virtual void* |
383 | _M_get_deleter(const std::type_info&) noexcept |
384 | { return nullptr; } |
385 | |
386 | _Sp_counted_ptr(const _Sp_counted_ptr&) = delete; |
387 | _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete; |
388 | |
389 | private: |
390 | _Ptr _M_ptr; |
391 | }; |
392 | |
393 | template<> |
394 | inline void |
395 | _Sp_counted_ptr<nullptr_t, _S_single>::_M_dispose() noexcept { } |
396 | |
397 | template<> |
398 | inline void |
399 | _Sp_counted_ptr<nullptr_t, _S_mutex>::_M_dispose() noexcept { } |
400 | |
401 | template<> |
402 | inline void |
403 | _Sp_counted_ptr<nullptr_t, _S_atomic>::_M_dispose() noexcept { } |
404 | |
405 | template<int _Nm, typename _Tp, |
406 | bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)> |
407 | struct _Sp_ebo_helper; |
408 | |
409 | /// Specialization using EBO. |
410 | template<int _Nm, typename _Tp> |
411 | struct _Sp_ebo_helper<_Nm, _Tp, true> : private _Tp |
412 | { |
413 | explicit _Sp_ebo_helper(const _Tp& __tp) : _Tp(__tp) { } |
414 | explicit _Sp_ebo_helper(_Tp&& __tp) : _Tp(std::move(__tp)) { } |
415 | |
416 | static _Tp& |
417 | _S_get(_Sp_ebo_helper& __eboh) { return static_cast<_Tp&>(__eboh); } |
418 | }; |
419 | |
420 | /// Specialization not using EBO. |
421 | template<int _Nm, typename _Tp> |
422 | struct _Sp_ebo_helper<_Nm, _Tp, false> |
423 | { |
424 | explicit _Sp_ebo_helper(const _Tp& __tp) : _M_tp(__tp) { } |
425 | explicit _Sp_ebo_helper(_Tp&& __tp) : _M_tp(std::move(__tp)) { } |
426 | |
427 | static _Tp& |
428 | _S_get(_Sp_ebo_helper& __eboh) |
429 | { return __eboh._M_tp; } |
430 | |
431 | private: |
432 | _Tp _M_tp; |
433 | }; |
434 | |
435 | // Support for custom deleter and/or allocator |
436 | template<typename _Ptr, typename _Deleter, typename _Alloc, _Lock_policy _Lp> |
437 | class _Sp_counted_deleter final : public _Sp_counted_base<_Lp> |
438 | { |
439 | class _Impl : _Sp_ebo_helper<0, _Deleter>, _Sp_ebo_helper<1, _Alloc> |
440 | { |
441 | typedef _Sp_ebo_helper<0, _Deleter> _Del_base; |
442 | typedef _Sp_ebo_helper<1, _Alloc> _Alloc_base; |
443 | |
444 | public: |
445 | _Impl(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept |
446 | : _M_ptr(__p), _Del_base(std::move(__d)), _Alloc_base(__a) |
447 | { } |
448 | |
449 | _Deleter& _M_del() noexcept { return _Del_base::_S_get(*this); } |
450 | _Alloc& _M_alloc() noexcept { return _Alloc_base::_S_get(*this); } |
451 | |
452 | _Ptr _M_ptr; |
453 | }; |
454 | |
455 | public: |
456 | using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_deleter>; |
457 | |
458 | // __d(__p) must not throw. |
459 | _Sp_counted_deleter(_Ptr __p, _Deleter __d) noexcept |
460 | : _M_impl(__p, std::move(__d), _Alloc()) { } |
461 | |
462 | // __d(__p) must not throw. |
463 | _Sp_counted_deleter(_Ptr __p, _Deleter __d, const _Alloc& __a) noexcept |
464 | : _M_impl(__p, std::move(__d), __a) { } |
465 | |
466 | ~_Sp_counted_deleter() noexcept { } |
467 | |
468 | virtual void |
469 | _M_dispose() noexcept |
470 | { _M_impl._M_del()(_M_impl._M_ptr); } |
471 | |
472 | virtual void |
473 | _M_destroy() noexcept |
474 | { |
475 | __allocator_type __a(_M_impl._M_alloc()); |
476 | __allocated_ptr<__allocator_type> __guard_ptr{ __a, this }; |
477 | this->~_Sp_counted_deleter(); |
478 | } |
479 | |
480 | virtual void* |
481 | _M_get_deleter(const std::type_info& __ti) noexcept |
482 | { |
483 | #if __cpp_rtti199711 |
484 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
485 | // 2400. shared_ptr's get_deleter() should use addressof() |
486 | return __ti == typeid(_Deleter) |
487 | ? std::__addressof(_M_impl._M_del()) |
488 | : nullptr; |
489 | #else |
490 | return nullptr; |
491 | #endif |
492 | } |
493 | |
494 | private: |
495 | _Impl _M_impl; |
496 | }; |
497 | |
498 | // helpers for make_shared / allocate_shared |
499 | |
500 | struct _Sp_make_shared_tag { }; |
501 | |
502 | template<typename _Tp, typename _Alloc, _Lock_policy _Lp> |
503 | class _Sp_counted_ptr_inplace final : public _Sp_counted_base<_Lp> |
504 | { |
505 | class _Impl : _Sp_ebo_helper<0, _Alloc> |
506 | { |
507 | typedef _Sp_ebo_helper<0, _Alloc> _A_base; |
508 | |
509 | public: |
510 | explicit _Impl(_Alloc __a) noexcept : _A_base(__a) { } |
511 | |
512 | _Alloc& _M_alloc() noexcept { return _A_base::_S_get(*this); } |
513 | |
514 | __gnu_cxx::__aligned_buffer<_Tp> _M_storage; |
515 | }; |
516 | |
517 | public: |
518 | using __allocator_type = __alloc_rebind<_Alloc, _Sp_counted_ptr_inplace>; |
519 | |
520 | template<typename... _Args> |
521 | _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args) |
522 | : _M_impl(__a) |
523 | { |
524 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
525 | // 2070. allocate_shared should use allocator_traits<A>::construct |
526 | allocator_traits<_Alloc>::construct(__a, _M_ptr(), |
527 | std::forward<_Args>(__args)...); // might throw |
528 | } |
529 | |
530 | ~_Sp_counted_ptr_inplace() noexcept { } |
531 | |
532 | virtual void |
533 | _M_dispose() noexcept |
534 | { |
535 | allocator_traits<_Alloc>::destroy(_M_impl._M_alloc(), _M_ptr()); |
536 | } |
537 | |
538 | // Override because the allocator needs to know the dynamic type |
539 | virtual void |
540 | _M_destroy() noexcept |
541 | { |
542 | __allocator_type __a(_M_impl._M_alloc()); |
543 | __allocated_ptr<__allocator_type> __guard_ptr{ __a, this }; |
544 | this->~_Sp_counted_ptr_inplace(); |
545 | } |
546 | |
547 | // Sneaky trick so __shared_ptr can get the managed pointer |
548 | virtual void* |
549 | _M_get_deleter(const std::type_info& __ti) noexcept |
550 | { |
551 | #if __cpp_rtti199711 |
552 | if (__ti == typeid(_Sp_make_shared_tag)) |
553 | return const_cast<typename remove_cv<_Tp>::type*>(_M_ptr()); |
554 | #endif |
555 | return nullptr; |
556 | } |
557 | |
558 | private: |
559 | _Tp* _M_ptr() noexcept { return _M_impl._M_storage._M_ptr(); } |
560 | |
561 | _Impl _M_impl; |
562 | }; |
563 | |
564 | // The default deleter for shared_ptr<T[]> and shared_ptr<T[N]>. |
565 | struct __sp_array_delete |
566 | { |
567 | template<typename _Yp> |
568 | void operator()(_Yp* __p) const { delete[] __p; } |
569 | }; |
570 | |
571 | template<_Lock_policy _Lp> |
572 | class __shared_count |
573 | { |
574 | public: |
575 | constexpr __shared_count() noexcept : _M_pi(0) |
576 | { } |
577 | |
578 | template<typename _Ptr> |
579 | explicit |
580 | __shared_count(_Ptr __p) : _M_pi(0) |
581 | { |
582 | __tryif (true) |
583 | { |
584 | _M_pi = new _Sp_counted_ptr<_Ptr, _Lp>(__p); |
585 | } |
586 | __catch(...)if (false) |
587 | { |
588 | delete __p; |
589 | __throw_exception_again; |
590 | } |
591 | } |
592 | |
593 | template<typename _Ptr> |
594 | __shared_count(_Ptr __p, /* is_array = */ false_type) |
595 | : __shared_count(__p) |
596 | { } |
597 | |
598 | template<typename _Ptr> |
599 | __shared_count(_Ptr __p, /* is_array = */ true_type) |
600 | : __shared_count(__p, __sp_array_delete{}, allocator<void>()) |
601 | { } |
602 | |
603 | template<typename _Ptr, typename _Deleter> |
604 | __shared_count(_Ptr __p, _Deleter __d) |
605 | : __shared_count(__p, std::move(__d), allocator<void>()) |
606 | { } |
607 | |
608 | template<typename _Ptr, typename _Deleter, typename _Alloc> |
609 | __shared_count(_Ptr __p, _Deleter __d, _Alloc __a) : _M_pi(0) |
610 | { |
611 | typedef _Sp_counted_deleter<_Ptr, _Deleter, _Alloc, _Lp> _Sp_cd_type; |
612 | __tryif (true) |
613 | { |
614 | typename _Sp_cd_type::__allocator_type __a2(__a); |
615 | auto __guard = std::__allocate_guarded(__a2); |
616 | _Sp_cd_type* __mem = __guard.get(); |
617 | ::new (__mem) _Sp_cd_type(__p, std::move(__d), std::move(__a)); |
618 | _M_pi = __mem; |
619 | __guard = nullptr; |
620 | } |
621 | __catch(...)if (false) |
622 | { |
623 | __d(__p); // Call _Deleter on __p. |
624 | __throw_exception_again; |
625 | } |
626 | } |
627 | |
628 | template<typename _Tp, typename _Alloc, typename... _Args> |
629 | __shared_count(_Sp_make_shared_tag, _Tp*, const _Alloc& __a, |
630 | _Args&&... __args) |
631 | : _M_pi(0) |
632 | { |
633 | typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type; |
634 | typename _Sp_cp_type::__allocator_type __a2(__a); |
635 | auto __guard = std::__allocate_guarded(__a2); |
636 | _Sp_cp_type* __mem = __guard.get(); |
637 | ::new (__mem) _Sp_cp_type(std::move(__a), |
638 | std::forward<_Args>(__args)...); |
639 | _M_pi = __mem; |
640 | __guard = nullptr; |
641 | } |
642 | |
643 | #if _GLIBCXX_USE_DEPRECATED1 |
644 | // Special case for auto_ptr<_Tp> to provide the strong guarantee. |
645 | template<typename _Tp> |
646 | explicit |
647 | __shared_count(std::auto_ptr<_Tp>&& __r); |
648 | #endif |
649 | |
650 | // Special case for unique_ptr<_Tp,_Del> to provide the strong guarantee. |
651 | template<typename _Tp, typename _Del> |
652 | explicit |
653 | __shared_count(std::unique_ptr<_Tp, _Del>&& __r) : _M_pi(0) |
654 | { |
655 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
656 | // 2415. Inconsistency between unique_ptr and shared_ptr |
657 | if (__r.get() == nullptr) |
658 | return; |
659 | |
660 | using _Ptr = typename unique_ptr<_Tp, _Del>::pointer; |
661 | using _Del2 = typename conditional<is_reference<_Del>::value, |
662 | reference_wrapper<typename remove_reference<_Del>::type>, |
663 | _Del>::type; |
664 | using _Sp_cd_type |
665 | = _Sp_counted_deleter<_Ptr, _Del2, allocator<void>, _Lp>; |
666 | using _Alloc = allocator<_Sp_cd_type>; |
667 | using _Alloc_traits = allocator_traits<_Alloc>; |
668 | _Alloc __a; |
669 | _Sp_cd_type* __mem = _Alloc_traits::allocate(__a, 1); |
670 | _Alloc_traits::construct(__a, __mem, __r.release(), |
671 | __r.get_deleter()); // non-throwing |
672 | _M_pi = __mem; |
673 | } |
674 | |
675 | // Throw bad_weak_ptr when __r._M_get_use_count() == 0. |
676 | explicit __shared_count(const __weak_count<_Lp>& __r); |
677 | |
678 | // Does not throw if __r._M_get_use_count() == 0, caller must check. |
679 | explicit __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t); |
680 | |
681 | ~__shared_count() noexcept |
682 | { |
683 | if (_M_pi != nullptr) |
684 | _M_pi->_M_release(); |
685 | } |
686 | |
687 | __shared_count(const __shared_count& __r) noexcept |
688 | : _M_pi(__r._M_pi) |
689 | { |
690 | if (_M_pi != 0) |
691 | _M_pi->_M_add_ref_copy(); |
692 | } |
693 | |
694 | __shared_count& |
695 | operator=(const __shared_count& __r) noexcept |
696 | { |
697 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
698 | if (__tmp != _M_pi) |
699 | { |
700 | if (__tmp != 0) |
701 | __tmp->_M_add_ref_copy(); |
702 | if (_M_pi != 0) |
703 | _M_pi->_M_release(); |
704 | _M_pi = __tmp; |
705 | } |
706 | return *this; |
707 | } |
708 | |
709 | void |
710 | _M_swap(__shared_count& __r) noexcept |
711 | { |
712 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
713 | __r._M_pi = _M_pi; |
714 | _M_pi = __tmp; |
715 | } |
716 | |
717 | long |
718 | _M_get_use_count() const noexcept |
719 | { return _M_pi != 0 ? _M_pi->_M_get_use_count() : 0; } |
720 | |
721 | bool |
722 | _M_unique() const noexcept |
723 | { return this->_M_get_use_count() == 1; } |
724 | |
725 | void* |
726 | _M_get_deleter(const std::type_info& __ti) const noexcept |
727 | { return _M_pi ? _M_pi->_M_get_deleter(__ti) : nullptr; } |
728 | |
729 | bool |
730 | _M_less(const __shared_count& __rhs) const noexcept |
731 | { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); } |
732 | |
733 | bool |
734 | _M_less(const __weak_count<_Lp>& __rhs) const noexcept |
735 | { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); } |
736 | |
737 | // Friend function injected into enclosing namespace and found by ADL |
738 | friend inline bool |
739 | operator==(const __shared_count& __a, const __shared_count& __b) noexcept |
740 | { return __a._M_pi == __b._M_pi; } |
741 | |
742 | private: |
743 | friend class __weak_count<_Lp>; |
744 | |
745 | _Sp_counted_base<_Lp>* _M_pi; |
746 | }; |
747 | |
748 | |
749 | template<_Lock_policy _Lp> |
750 | class __weak_count |
751 | { |
752 | public: |
753 | constexpr __weak_count() noexcept : _M_pi(nullptr) |
754 | { } |
755 | |
756 | __weak_count(const __shared_count<_Lp>& __r) noexcept |
757 | : _M_pi(__r._M_pi) |
758 | { |
759 | if (_M_pi != nullptr) |
760 | _M_pi->_M_weak_add_ref(); |
761 | } |
762 | |
763 | __weak_count(const __weak_count& __r) noexcept |
764 | : _M_pi(__r._M_pi) |
765 | { |
766 | if (_M_pi != nullptr) |
767 | _M_pi->_M_weak_add_ref(); |
768 | } |
769 | |
770 | __weak_count(__weak_count&& __r) noexcept |
771 | : _M_pi(__r._M_pi) |
772 | { __r._M_pi = nullptr; } |
773 | |
774 | ~__weak_count() noexcept |
775 | { |
776 | if (_M_pi != nullptr) |
777 | _M_pi->_M_weak_release(); |
778 | } |
779 | |
780 | __weak_count& |
781 | operator=(const __shared_count<_Lp>& __r) noexcept |
782 | { |
783 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
784 | if (__tmp != nullptr) |
785 | __tmp->_M_weak_add_ref(); |
786 | if (_M_pi != nullptr) |
787 | _M_pi->_M_weak_release(); |
788 | _M_pi = __tmp; |
789 | return *this; |
790 | } |
791 | |
792 | __weak_count& |
793 | operator=(const __weak_count& __r) noexcept |
794 | { |
795 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
796 | if (__tmp != nullptr) |
797 | __tmp->_M_weak_add_ref(); |
798 | if (_M_pi != nullptr) |
799 | _M_pi->_M_weak_release(); |
800 | _M_pi = __tmp; |
801 | return *this; |
802 | } |
803 | |
804 | __weak_count& |
805 | operator=(__weak_count&& __r) noexcept |
806 | { |
807 | if (_M_pi != nullptr) |
808 | _M_pi->_M_weak_release(); |
809 | _M_pi = __r._M_pi; |
810 | __r._M_pi = nullptr; |
811 | return *this; |
812 | } |
813 | |
814 | void |
815 | _M_swap(__weak_count& __r) noexcept |
816 | { |
817 | _Sp_counted_base<_Lp>* __tmp = __r._M_pi; |
818 | __r._M_pi = _M_pi; |
819 | _M_pi = __tmp; |
820 | } |
821 | |
822 | long |
823 | _M_get_use_count() const noexcept |
824 | { return _M_pi != nullptr ? _M_pi->_M_get_use_count() : 0; } |
825 | |
826 | bool |
827 | _M_less(const __weak_count& __rhs) const noexcept |
828 | { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); } |
829 | |
830 | bool |
831 | _M_less(const __shared_count<_Lp>& __rhs) const noexcept |
832 | { return std::less<_Sp_counted_base<_Lp>*>()(this->_M_pi, __rhs._M_pi); } |
833 | |
834 | // Friend function injected into enclosing namespace and found by ADL |
835 | friend inline bool |
836 | operator==(const __weak_count& __a, const __weak_count& __b) noexcept |
837 | { return __a._M_pi == __b._M_pi; } |
838 | |
839 | private: |
840 | friend class __shared_count<_Lp>; |
841 | |
842 | _Sp_counted_base<_Lp>* _M_pi; |
843 | }; |
844 | |
845 | // Now that __weak_count is defined we can define this constructor: |
846 | template<_Lock_policy _Lp> |
847 | inline |
848 | __shared_count<_Lp>::__shared_count(const __weak_count<_Lp>& __r) |
849 | : _M_pi(__r._M_pi) |
850 | { |
851 | if (_M_pi != nullptr) |
852 | _M_pi->_M_add_ref_lock(); |
853 | else |
854 | __throw_bad_weak_ptr(); |
855 | } |
856 | |
857 | // Now that __weak_count is defined we can define this constructor: |
858 | template<_Lock_policy _Lp> |
859 | inline |
860 | __shared_count<_Lp>:: |
861 | __shared_count(const __weak_count<_Lp>& __r, std::nothrow_t) |
862 | : _M_pi(__r._M_pi) |
863 | { |
864 | if (_M_pi != nullptr) |
865 | if (!_M_pi->_M_add_ref_lock_nothrow()) |
866 | _M_pi = nullptr; |
867 | } |
868 | |
869 | #define __cpp_lib_shared_ptr_arrays201603 201603 |
870 | |
871 | // Helper traits for shared_ptr of array: |
872 | |
873 | // A pointer type Y* is said to be compatible with a pointer type T* when |
874 | // either Y* is convertible to T* or Y is U[N] and T is U cv []. |
875 | template<typename _Yp_ptr, typename _Tp_ptr> |
876 | struct __sp_compatible_with |
877 | : false_type |
878 | { }; |
879 | |
880 | template<typename _Yp, typename _Tp> |
881 | struct __sp_compatible_with<_Yp*, _Tp*> |
882 | : is_convertible<_Yp*, _Tp*>::type |
883 | { }; |
884 | |
885 | template<typename _Up, size_t _Nm> |
886 | struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]> |
887 | : true_type |
888 | { }; |
889 | |
890 | template<typename _Up, size_t _Nm> |
891 | struct __sp_compatible_with<_Up(*)[_Nm], const _Up(*)[]> |
892 | : true_type |
893 | { }; |
894 | |
895 | template<typename _Up, size_t _Nm> |
896 | struct __sp_compatible_with<_Up(*)[_Nm], volatile _Up(*)[]> |
897 | : true_type |
898 | { }; |
899 | |
900 | template<typename _Up, size_t _Nm> |
901 | struct __sp_compatible_with<_Up(*)[_Nm], const volatile _Up(*)[]> |
902 | : true_type |
903 | { }; |
904 | |
905 | // Test conversion from Y(*)[N] to U(*)[N] without forming invalid type Y[N]. |
906 | template<typename _Up, size_t _Nm, typename _Yp, typename = void> |
907 | struct __sp_is_constructible_arrN |
908 | : false_type |
909 | { }; |
910 | |
911 | template<typename _Up, size_t _Nm, typename _Yp> |
912 | struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>> |
913 | : is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type |
914 | { }; |
915 | |
916 | // Test conversion from Y(*)[] to U(*)[] without forming invalid type Y[]. |
917 | template<typename _Up, typename _Yp, typename = void> |
918 | struct __sp_is_constructible_arr |
919 | : false_type |
920 | { }; |
921 | |
922 | template<typename _Up, typename _Yp> |
923 | struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>> |
924 | : is_convertible<_Yp(*)[], _Up(*)[]>::type |
925 | { }; |
926 | |
927 | // Trait to check if shared_ptr<T> can be constructed from Y*. |
928 | template<typename _Tp, typename _Yp> |
929 | struct __sp_is_constructible; |
930 | |
931 | // When T is U[N], Y(*)[N] shall be convertible to T*; |
932 | template<typename _Up, size_t _Nm, typename _Yp> |
933 | struct __sp_is_constructible<_Up[_Nm], _Yp> |
934 | : __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type |
935 | { }; |
936 | |
937 | // when T is U[], Y(*)[] shall be convertible to T*; |
938 | template<typename _Up, typename _Yp> |
939 | struct __sp_is_constructible<_Up[], _Yp> |
940 | : __sp_is_constructible_arr<_Up, _Yp>::type |
941 | { }; |
942 | |
943 | // otherwise, Y* shall be convertible to T*. |
944 | template<typename _Tp, typename _Yp> |
945 | struct __sp_is_constructible |
946 | : is_convertible<_Yp*, _Tp*>::type |
947 | { }; |
948 | |
949 | |
950 | // Define operator* and operator-> for shared_ptr<T>. |
951 | template<typename _Tp, _Lock_policy _Lp, |
952 | bool = is_array<_Tp>::value, bool = is_void<_Tp>::value> |
953 | class __shared_ptr_access |
954 | { |
955 | public: |
956 | using element_type = _Tp; |
957 | |
958 | element_type& |
959 | operator*() const noexcept |
960 | { |
961 | __glibcxx_assert(_M_get() != nullptr); |
962 | return *_M_get(); |
963 | } |
964 | |
965 | element_type* |
966 | operator->() const noexcept |
967 | { |
968 | _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr); |
969 | return _M_get(); |
970 | } |
971 | |
972 | private: |
973 | element_type* |
974 | _M_get() const noexcept |
975 | { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); } |
976 | }; |
977 | |
978 | // Define operator-> for shared_ptr<cv void>. |
979 | template<typename _Tp, _Lock_policy _Lp> |
980 | class __shared_ptr_access<_Tp, _Lp, false, true> |
981 | { |
982 | public: |
983 | using element_type = _Tp; |
984 | |
985 | element_type* |
986 | operator->() const noexcept |
987 | { |
988 | auto __ptr = static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); |
989 | _GLIBCXX_DEBUG_PEDASSERT(__ptr != nullptr); |
990 | return __ptr; |
991 | } |
992 | }; |
993 | |
994 | // Define operator[] for shared_ptr<T[]> and shared_ptr<T[N]>. |
995 | template<typename _Tp, _Lock_policy _Lp> |
996 | class __shared_ptr_access<_Tp, _Lp, true, false> |
997 | { |
998 | public: |
999 | using element_type = typename remove_extent<_Tp>::type; |
1000 | |
1001 | #if __cplusplus201103L <= 201402L |
1002 | [[__deprecated__("shared_ptr<T[]>::operator* is absent from C++17")]] |
1003 | element_type& |
1004 | operator*() const noexcept |
1005 | { |
1006 | __glibcxx_assert(_M_get() != nullptr); |
1007 | return *_M_get(); |
1008 | } |
1009 | |
1010 | [[__deprecated__("shared_ptr<T[]>::operator-> is absent from C++17")]] |
1011 | element_type* |
1012 | operator->() const noexcept |
1013 | { |
1014 | _GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr); |
1015 | return _M_get(); |
1016 | } |
1017 | #endif |
1018 | |
1019 | element_type& |
1020 | operator[](ptrdiff_t __i) const |
1021 | { |
1022 | __glibcxx_assert(_M_get() != nullptr); |
1023 | __glibcxx_assert(!extent<_Tp>::value || __i < extent<_Tp>::value); |
1024 | return _M_get()[__i]; |
1025 | } |
1026 | |
1027 | private: |
1028 | element_type* |
1029 | _M_get() const noexcept |
1030 | { return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); } |
1031 | }; |
1032 | |
1033 | template<typename _Tp, _Lock_policy _Lp> |
1034 | class __shared_ptr |
1035 | : public __shared_ptr_access<_Tp, _Lp> |
1036 | { |
1037 | public: |
1038 | using element_type = typename remove_extent<_Tp>::type; |
1039 | |
1040 | private: |
1041 | // Constraint for taking ownership of a pointer of type _Yp*: |
1042 | template<typename _Yp> |
1043 | using _SafeConv |
1044 | = typename enable_if<__sp_is_constructible<_Tp, _Yp>::value>::type; |
1045 | |
1046 | // Constraint for construction from shared_ptr and weak_ptr: |
1047 | template<typename _Yp, typename _Res = void> |
1048 | using _Compatible = typename |
1049 | enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type; |
1050 | |
1051 | // Constraint for assignment from shared_ptr and weak_ptr: |
1052 | template<typename _Yp> |
1053 | using _Assignable = _Compatible<_Yp, __shared_ptr&>; |
1054 | |
1055 | // Constraint for construction from unique_ptr: |
1056 | template<typename _Yp, typename _Del, typename _Res = void, |
1057 | typename _Ptr = typename unique_ptr<_Yp, _Del>::pointer> |
1058 | using _UniqCompatible = typename enable_if<__and_< |
1059 | __sp_compatible_with<_Yp*, _Tp*>, is_convertible<_Ptr, element_type*> |
1060 | >::value, _Res>::type; |
1061 | |
1062 | // Constraint for assignment from unique_ptr: |
1063 | template<typename _Yp, typename _Del> |
1064 | using _UniqAssignable = _UniqCompatible<_Yp, _Del, __shared_ptr&>; |
1065 | |
1066 | public: |
1067 | |
1068 | #if __cplusplus201103L > 201402L |
1069 | using weak_type = __weak_ptr<_Tp, _Lp>; |
1070 | #endif |
1071 | |
1072 | constexpr __shared_ptr() noexcept |
1073 | : _M_ptr(0), _M_refcount() |
1074 | { } |
1075 | |
1076 | template<typename _Yp, typename = _SafeConv<_Yp>> |
1077 | explicit |
1078 | __shared_ptr(_Yp* __p) |
1079 | : _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type()) |
1080 | { |
1081 | static_assert( !is_void<_Yp>::value, "incomplete type" ); |
1082 | static_assert( sizeof(_Yp) > 0, "incomplete type" ); |
1083 | _M_enable_shared_from_this_with(__p); |
1084 | } |
1085 | |
1086 | template<typename _Yp, typename _Deleter, typename = _SafeConv<_Yp>> |
1087 | __shared_ptr(_Yp* __p, _Deleter __d) |
1088 | : _M_ptr(__p), _M_refcount(__p, std::move(__d)) |
1089 | { |
1090 | static_assert(__is_invocable<_Deleter&, _Yp*&>::value, |
1091 | "deleter expression d(p) is well-formed"); |
1092 | _M_enable_shared_from_this_with(__p); |
1093 | } |
1094 | |
1095 | template<typename _Yp, typename _Deleter, typename _Alloc, |
1096 | typename = _SafeConv<_Yp>> |
1097 | __shared_ptr(_Yp* __p, _Deleter __d, _Alloc __a) |
1098 | : _M_ptr(__p), _M_refcount(__p, std::move(__d), std::move(__a)) |
1099 | { |
1100 | static_assert(__is_invocable<_Deleter&, _Yp*&>::value, |
1101 | "deleter expression d(p) is well-formed"); |
1102 | _M_enable_shared_from_this_with(__p); |
1103 | } |
1104 | |
1105 | template<typename _Deleter> |
1106 | __shared_ptr(nullptr_t __p, _Deleter __d) |
1107 | : _M_ptr(0), _M_refcount(__p, std::move(__d)) |
1108 | { } |
1109 | |
1110 | template<typename _Deleter, typename _Alloc> |
1111 | __shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a) |
1112 | : _M_ptr(0), _M_refcount(__p, std::move(__d), std::move(__a)) |
1113 | { } |
1114 | |
1115 | template<typename _Yp> |
1116 | __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r, |
1117 | element_type* __p) noexcept |
1118 | : _M_ptr(__p), _M_refcount(__r._M_refcount) // never throws |
1119 | { } |
1120 | |
1121 | __shared_ptr(const __shared_ptr&) noexcept = default; |
1122 | __shared_ptr& operator=(const __shared_ptr&) noexcept = default; |
1123 | ~__shared_ptr() = default; |
1124 | |
1125 | template<typename _Yp, typename = _Compatible<_Yp>> |
1126 | __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept |
1127 | : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) |
1128 | { } |
1129 | |
1130 | __shared_ptr(__shared_ptr&& __r) noexcept |
1131 | : _M_ptr(__r._M_ptr), _M_refcount() |
1132 | { |
1133 | _M_refcount._M_swap(__r._M_refcount); |
1134 | __r._M_ptr = 0; |
1135 | } |
1136 | |
1137 | template<typename _Yp, typename = _Compatible<_Yp>> |
1138 | __shared_ptr(__shared_ptr<_Yp, _Lp>&& __r) noexcept |
1139 | : _M_ptr(__r._M_ptr), _M_refcount() |
1140 | { |
1141 | _M_refcount._M_swap(__r._M_refcount); |
1142 | __r._M_ptr = 0; |
1143 | } |
1144 | |
1145 | template<typename _Yp, typename = _Compatible<_Yp>> |
1146 | explicit __shared_ptr(const __weak_ptr<_Yp, _Lp>& __r) |
1147 | : _M_refcount(__r._M_refcount) // may throw |
1148 | { |
1149 | // It is now safe to copy __r._M_ptr, as |
1150 | // _M_refcount(__r._M_refcount) did not throw. |
1151 | _M_ptr = __r._M_ptr; |
1152 | } |
1153 | |
1154 | // If an exception is thrown this constructor has no effect. |
1155 | template<typename _Yp, typename _Del, |
1156 | typename = _UniqCompatible<_Yp, _Del>> |
1157 | __shared_ptr(unique_ptr<_Yp, _Del>&& __r) |
1158 | : _M_ptr(__r.get()), _M_refcount() |
1159 | { |
1160 | auto __raw = _S_raw_ptr(__r.get()); |
1161 | _M_refcount = __shared_count<_Lp>(std::move(__r)); |
1162 | _M_enable_shared_from_this_with(__raw); |
1163 | } |
1164 | |
1165 | #if __cplusplus201103L <= 201402L && _GLIBCXX_USE_DEPRECATED1 |
1166 | protected: |
1167 | // If an exception is thrown this constructor has no effect. |
1168 | template<typename _Tp1, typename _Del, |
1169 | typename enable_if<__and_< |
1170 | __not_<is_array<_Tp>>, is_array<_Tp1>, |
1171 | is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*> |
1172 | >::value, bool>::type = true> |
1173 | __shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete) |
1174 | : _M_ptr(__r.get()), _M_refcount() |
1175 | { |
1176 | auto __raw = _S_raw_ptr(__r.get()); |
1177 | _M_refcount = __shared_count<_Lp>(std::move(__r)); |
1178 | _M_enable_shared_from_this_with(__raw); |
1179 | } |
1180 | public: |
1181 | #endif |
1182 | |
1183 | #if _GLIBCXX_USE_DEPRECATED1 |
1184 | // Postcondition: use_count() == 1 and __r.get() == 0 |
1185 | template<typename _Yp, typename = _Compatible<_Yp>> |
1186 | __shared_ptr(auto_ptr<_Yp>&& __r); |
1187 | #endif |
1188 | |
1189 | constexpr __shared_ptr(nullptr_t) noexcept : __shared_ptr() { } |
1190 | |
1191 | template<typename _Yp> |
1192 | _Assignable<_Yp> |
1193 | operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept |
1194 | { |
1195 | _M_ptr = __r._M_ptr; |
1196 | _M_refcount = __r._M_refcount; // __shared_count::op= doesn't throw |
1197 | return *this; |
1198 | } |
1199 | |
1200 | #if _GLIBCXX_USE_DEPRECATED1 |
1201 | template<typename _Yp> |
1202 | _Assignable<_Yp> |
1203 | operator=(auto_ptr<_Yp>&& __r) |
1204 | { |
1205 | __shared_ptr(std::move(__r)).swap(*this); |
1206 | return *this; |
1207 | } |
1208 | #endif |
1209 | |
1210 | __shared_ptr& |
1211 | operator=(__shared_ptr&& __r) noexcept |
1212 | { |
1213 | __shared_ptr(std::move(__r)).swap(*this); |
1214 | return *this; |
1215 | } |
1216 | |
1217 | template<class _Yp> |
1218 | _Assignable<_Yp> |
1219 | operator=(__shared_ptr<_Yp, _Lp>&& __r) noexcept |
1220 | { |
1221 | __shared_ptr(std::move(__r)).swap(*this); |
1222 | return *this; |
1223 | } |
1224 | |
1225 | template<typename _Yp, typename _Del> |
1226 | _UniqAssignable<_Yp, _Del> |
1227 | operator=(unique_ptr<_Yp, _Del>&& __r) |
1228 | { |
1229 | __shared_ptr(std::move(__r)).swap(*this); |
1230 | return *this; |
1231 | } |
1232 | |
1233 | void |
1234 | reset() noexcept |
1235 | { __shared_ptr().swap(*this); } |
1236 | |
1237 | template<typename _Yp> |
1238 | _SafeConv<_Yp> |
1239 | reset(_Yp* __p) // _Yp must be complete. |
1240 | { |
1241 | // Catch self-reset errors. |
1242 | __glibcxx_assert(__p == 0 || __p != _M_ptr); |
1243 | __shared_ptr(__p).swap(*this); |
1244 | } |
1245 | |
1246 | template<typename _Yp, typename _Deleter> |
1247 | _SafeConv<_Yp> |
1248 | reset(_Yp* __p, _Deleter __d) |
1249 | { __shared_ptr(__p, std::move(__d)).swap(*this); } |
1250 | |
1251 | template<typename _Yp, typename _Deleter, typename _Alloc> |
1252 | _SafeConv<_Yp> |
1253 | reset(_Yp* __p, _Deleter __d, _Alloc __a) |
1254 | { __shared_ptr(__p, std::move(__d), std::move(__a)).swap(*this); } |
1255 | |
1256 | element_type* |
1257 | get() const noexcept |
1258 | { return _M_ptr; } |
1259 | |
1260 | explicit operator bool() const // never throws |
1261 | { return _M_ptr == 0 ? false : true; } |
1262 | |
1263 | bool |
1264 | unique() const noexcept |
1265 | { return _M_refcount._M_unique(); } |
1266 | |
1267 | long |
1268 | use_count() const noexcept |
1269 | { return _M_refcount._M_get_use_count(); } |
1270 | |
1271 | void |
1272 | swap(__shared_ptr<_Tp, _Lp>& __other) noexcept |
1273 | { |
1274 | std::swap(_M_ptr, __other._M_ptr); |
1275 | _M_refcount._M_swap(__other._M_refcount); |
1276 | } |
1277 | |
1278 | template<typename _Tp1> |
1279 | bool |
1280 | owner_before(__shared_ptr<_Tp1, _Lp> const& __rhs) const noexcept |
1281 | { return _M_refcount._M_less(__rhs._M_refcount); } |
1282 | |
1283 | template<typename _Tp1> |
1284 | bool |
1285 | owner_before(__weak_ptr<_Tp1, _Lp> const& __rhs) const noexcept |
1286 | { return _M_refcount._M_less(__rhs._M_refcount); } |
1287 | |
1288 | #if __cpp_rtti199711 |
1289 | protected: |
1290 | // This constructor is non-standard, it is used by allocate_shared. |
1291 | template<typename _Alloc, typename... _Args> |
1292 | __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a, |
1293 | _Args&&... __args) |
1294 | : _M_ptr(), _M_refcount(__tag, (_Tp*)0, __a, |
1295 | std::forward<_Args>(__args)...) |
1296 | { |
1297 | // _M_ptr needs to point to the newly constructed object. |
1298 | // This relies on _Sp_counted_ptr_inplace::_M_get_deleter. |
1299 | void* __p = _M_refcount._M_get_deleter(typeid(__tag)); |
1300 | _M_ptr = static_cast<_Tp*>(__p); |
1301 | _M_enable_shared_from_this_with(_M_ptr); |
1302 | } |
1303 | #else |
1304 | template<typename _Alloc> |
1305 | struct _Deleter |
1306 | { |
1307 | void operator()(typename _Alloc::value_type* __ptr) |
1308 | { |
1309 | __allocated_ptr<_Alloc> __guard{ _M_alloc, __ptr }; |
1310 | allocator_traits<_Alloc>::destroy(_M_alloc, __guard.get()); |
1311 | } |
1312 | _Alloc _M_alloc; |
1313 | }; |
1314 | |
1315 | template<typename _Alloc, typename... _Args> |
1316 | __shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a, |
1317 | _Args&&... __args) |
1318 | : _M_ptr(), _M_refcount() |
1319 | { |
1320 | typedef typename allocator_traits<_Alloc>::template |
1321 | rebind_traits<typename std::remove_cv<_Tp>::type> __traits; |
1322 | _Deleter<typename __traits::allocator_type> __del = { __a }; |
1323 | auto __guard = std::__allocate_guarded(__del._M_alloc); |
1324 | auto __ptr = __guard.get(); |
1325 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
1326 | // 2070. allocate_shared should use allocator_traits<A>::construct |
1327 | __traits::construct(__del._M_alloc, __ptr, |
1328 | std::forward<_Args>(__args)...); |
1329 | __guard = nullptr; |
1330 | __shared_count<_Lp> __count(__ptr, __del, __del._M_alloc); |
1331 | _M_refcount._M_swap(__count); |
1332 | _M_ptr = __ptr; |
1333 | _M_enable_shared_from_this_with(_M_ptr); |
1334 | } |
1335 | #endif |
1336 | |
1337 | template<typename _Tp1, _Lock_policy _Lp1, typename _Alloc, |
1338 | typename... _Args> |
1339 | friend __shared_ptr<_Tp1, _Lp1> |
1340 | __allocate_shared(const _Alloc& __a, _Args&&... __args); |
1341 | |
1342 | // This constructor is used by __weak_ptr::lock() and |
1343 | // shared_ptr::shared_ptr(const weak_ptr&, std::nothrow_t). |
1344 | __shared_ptr(const __weak_ptr<_Tp, _Lp>& __r, std::nothrow_t) |
1345 | : _M_refcount(__r._M_refcount, std::nothrow) |
1346 | { |
1347 | _M_ptr = _M_refcount._M_get_use_count() ? __r._M_ptr : nullptr; |
1348 | } |
1349 | |
1350 | friend class __weak_ptr<_Tp, _Lp>; |
1351 | |
1352 | private: |
1353 | |
1354 | template<typename _Yp> |
1355 | using __esft_base_t = decltype(__enable_shared_from_this_base( |
1356 | std::declval<const __shared_count<_Lp>&>(), |
1357 | std::declval<_Yp*>())); |
1358 | |
1359 | // Detect an accessible and unambiguous enable_shared_from_this base. |
1360 | template<typename _Yp, typename = void> |
1361 | struct __has_esft_base |
1362 | : false_type { }; |
1363 | |
1364 | template<typename _Yp> |
1365 | struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>> |
1366 | : __not_<is_array<_Tp>> { }; // No enable shared_from_this for arrays |
1367 | |
1368 | template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type> |
1369 | typename enable_if<__has_esft_base<_Yp2>::value>::type |
1370 | _M_enable_shared_from_this_with(_Yp* __p) noexcept |
1371 | { |
1372 | if (auto __base = __enable_shared_from_this_base(_M_refcount, __p)) |
1373 | __base->_M_weak_assign(const_cast<_Yp2*>(__p), _M_refcount); |
1374 | } |
1375 | |
1376 | template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type> |
1377 | typename enable_if<!__has_esft_base<_Yp2>::value>::type |
1378 | _M_enable_shared_from_this_with(_Yp*) noexcept |
1379 | { } |
1380 | |
1381 | void* |
1382 | _M_get_deleter(const std::type_info& __ti) const noexcept |
1383 | { return _M_refcount._M_get_deleter(__ti); } |
1384 | |
1385 | template<typename _Tp1> |
1386 | static _Tp1* |
1387 | _S_raw_ptr(_Tp1* __ptr) |
1388 | { return __ptr; } |
1389 | |
1390 | template<typename _Tp1> |
1391 | static auto |
1392 | _S_raw_ptr(_Tp1 __ptr) -> decltype(std::__addressof(*__ptr)) |
1393 | { return std::__addressof(*__ptr); } |
1394 | |
1395 | template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr; |
1396 | template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr; |
1397 | |
1398 | template<typename _Del, typename _Tp1, _Lock_policy _Lp1> |
1399 | friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept; |
1400 | |
1401 | element_type* _M_ptr; // Contained pointer. |
1402 | __shared_count<_Lp> _M_refcount; // Reference counter. |
1403 | }; |
1404 | |
1405 | |
1406 | // 20.7.2.2.7 shared_ptr comparisons |
1407 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
1408 | inline bool |
1409 | operator==(const __shared_ptr<_Tp1, _Lp>& __a, |
1410 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
1411 | { return __a.get() == __b.get(); } |
1412 | |
1413 | template<typename _Tp, _Lock_policy _Lp> |
1414 | inline bool |
1415 | operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
1416 | { return !__a; } |
1417 | |
1418 | template<typename _Tp, _Lock_policy _Lp> |
1419 | inline bool |
1420 | operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
1421 | { return !__a; } |
1422 | |
1423 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
1424 | inline bool |
1425 | operator!=(const __shared_ptr<_Tp1, _Lp>& __a, |
1426 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
1427 | { return __a.get() != __b.get(); } |
1428 | |
1429 | template<typename _Tp, _Lock_policy _Lp> |
1430 | inline bool |
1431 | operator!=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
1432 | { return (bool)__a; } |
1433 | |
1434 | template<typename _Tp, _Lock_policy _Lp> |
1435 | inline bool |
1436 | operator!=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
1437 | { return (bool)__a; } |
1438 | |
1439 | template<typename _Tp, typename _Up, _Lock_policy _Lp> |
1440 | inline bool |
1441 | operator<(const __shared_ptr<_Tp, _Lp>& __a, |
1442 | const __shared_ptr<_Up, _Lp>& __b) noexcept |
1443 | { |
1444 | using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type; |
1445 | using _Up_elt = typename __shared_ptr<_Up, _Lp>::element_type; |
1446 | using _Vp = typename common_type<_Tp_elt*, _Up_elt*>::type; |
1447 | return less<_Vp>()(__a.get(), __b.get()); |
1448 | } |
1449 | |
1450 | template<typename _Tp, _Lock_policy _Lp> |
1451 | inline bool |
1452 | operator<(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
1453 | { |
1454 | using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type; |
1455 | return less<_Tp_elt*>()(__a.get(), nullptr); |
1456 | } |
1457 | |
1458 | template<typename _Tp, _Lock_policy _Lp> |
1459 | inline bool |
1460 | operator<(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
1461 | { |
1462 | using _Tp_elt = typename __shared_ptr<_Tp, _Lp>::element_type; |
1463 | return less<_Tp_elt*>()(nullptr, __a.get()); |
1464 | } |
1465 | |
1466 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
1467 | inline bool |
1468 | operator<=(const __shared_ptr<_Tp1, _Lp>& __a, |
1469 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
1470 | { return !(__b < __a); } |
1471 | |
1472 | template<typename _Tp, _Lock_policy _Lp> |
1473 | inline bool |
1474 | operator<=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
1475 | { return !(nullptr < __a); } |
1476 | |
1477 | template<typename _Tp, _Lock_policy _Lp> |
1478 | inline bool |
1479 | operator<=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
1480 | { return !(__a < nullptr); } |
1481 | |
1482 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
1483 | inline bool |
1484 | operator>(const __shared_ptr<_Tp1, _Lp>& __a, |
1485 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
1486 | { return (__b < __a); } |
1487 | |
1488 | template<typename _Tp, _Lock_policy _Lp> |
1489 | inline bool |
1490 | operator>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
1491 | { return nullptr < __a; } |
1492 | |
1493 | template<typename _Tp, _Lock_policy _Lp> |
1494 | inline bool |
1495 | operator>(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
1496 | { return __a < nullptr; } |
1497 | |
1498 | template<typename _Tp1, typename _Tp2, _Lock_policy _Lp> |
1499 | inline bool |
1500 | operator>=(const __shared_ptr<_Tp1, _Lp>& __a, |
1501 | const __shared_ptr<_Tp2, _Lp>& __b) noexcept |
1502 | { return !(__a < __b); } |
1503 | |
1504 | template<typename _Tp, _Lock_policy _Lp> |
1505 | inline bool |
1506 | operator>=(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept |
1507 | { return !(__a < nullptr); } |
1508 | |
1509 | template<typename _Tp, _Lock_policy _Lp> |
1510 | inline bool |
1511 | operator>=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept |
1512 | { return !(nullptr < __a); } |
1513 | |
1514 | template<typename _Sp> |
1515 | struct _Sp_less : public binary_function<_Sp, _Sp, bool> |
1516 | { |
1517 | bool |
1518 | operator()(const _Sp& __lhs, const _Sp& __rhs) const noexcept |
1519 | { |
1520 | typedef typename _Sp::element_type element_type; |
1521 | return std::less<element_type*>()(__lhs.get(), __rhs.get()); |
1522 | } |
1523 | }; |
1524 | |
1525 | template<typename _Tp, _Lock_policy _Lp> |
1526 | struct less<__shared_ptr<_Tp, _Lp>> |
1527 | : public _Sp_less<__shared_ptr<_Tp, _Lp>> |
1528 | { }; |
1529 | |
1530 | // 20.7.2.2.8 shared_ptr specialized algorithms. |
1531 | template<typename _Tp, _Lock_policy _Lp> |
1532 | inline void |
1533 | swap(__shared_ptr<_Tp, _Lp>& __a, __shared_ptr<_Tp, _Lp>& __b) noexcept |
1534 | { __a.swap(__b); } |
1535 | |
1536 | // 20.7.2.2.9 shared_ptr casts |
1537 | |
1538 | // The seemingly equivalent code: |
1539 | // shared_ptr<_Tp, _Lp>(static_cast<_Tp*>(__r.get())) |
1540 | // will eventually result in undefined behaviour, attempting to |
1541 | // delete the same object twice. |
1542 | /// static_pointer_cast |
1543 | template<typename _Tp, typename _Tp1, _Lock_policy _Lp> |
1544 | inline __shared_ptr<_Tp, _Lp> |
1545 | static_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
1546 | { |
1547 | using _Sp = __shared_ptr<_Tp, _Lp>; |
1548 | return _Sp(__r, static_cast<typename _Sp::element_type*>(__r.get())); |
1549 | } |
1550 | |
1551 | // The seemingly equivalent code: |
1552 | // shared_ptr<_Tp, _Lp>(const_cast<_Tp*>(__r.get())) |
1553 | // will eventually result in undefined behaviour, attempting to |
1554 | // delete the same object twice. |
1555 | /// const_pointer_cast |
1556 | template<typename _Tp, typename _Tp1, _Lock_policy _Lp> |
1557 | inline __shared_ptr<_Tp, _Lp> |
1558 | const_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
1559 | { |
1560 | using _Sp = __shared_ptr<_Tp, _Lp>; |
1561 | return _Sp(__r, const_cast<typename _Sp::element_type*>(__r.get())); |
1562 | } |
1563 | |
1564 | // The seemingly equivalent code: |
1565 | // shared_ptr<_Tp, _Lp>(dynamic_cast<_Tp*>(__r.get())) |
1566 | // will eventually result in undefined behaviour, attempting to |
1567 | // delete the same object twice. |
1568 | /// dynamic_pointer_cast |
1569 | template<typename _Tp, typename _Tp1, _Lock_policy _Lp> |
1570 | inline __shared_ptr<_Tp, _Lp> |
1571 | dynamic_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
1572 | { |
1573 | using _Sp = __shared_ptr<_Tp, _Lp>; |
1574 | if (auto* __p = dynamic_cast<typename _Sp::element_type*>(__r.get())) |
1575 | return _Sp(__r, __p); |
1576 | return _Sp(); |
1577 | } |
1578 | |
1579 | #if __cplusplus201103L > 201402L |
1580 | template<typename _Tp, typename _Tp1, _Lock_policy _Lp> |
1581 | inline __shared_ptr<_Tp, _Lp> |
1582 | reinterpret_pointer_cast(const __shared_ptr<_Tp1, _Lp>& __r) noexcept |
1583 | { |
1584 | using _Sp = __shared_ptr<_Tp, _Lp>; |
1585 | return _Sp(__r, reinterpret_cast<typename _Sp::element_type*>(__r.get())); |
1586 | } |
1587 | #endif |
1588 | |
1589 | template<typename _Tp, _Lock_policy _Lp> |
1590 | class __weak_ptr |
1591 | { |
1592 | template<typename _Yp, typename _Res = void> |
1593 | using _Compatible = typename |
1594 | enable_if<__sp_compatible_with<_Yp*, _Tp*>::value, _Res>::type; |
1595 | |
1596 | // Constraint for assignment from shared_ptr and weak_ptr: |
1597 | template<typename _Yp> |
1598 | using _Assignable = _Compatible<_Yp, __weak_ptr&>; |
1599 | |
1600 | public: |
1601 | using element_type = typename remove_extent<_Tp>::type; |
1602 | |
1603 | constexpr __weak_ptr() noexcept |
1604 | : _M_ptr(nullptr), _M_refcount() |
1605 | { } |
1606 | |
1607 | __weak_ptr(const __weak_ptr&) noexcept = default; |
1608 | |
1609 | ~__weak_ptr() = default; |
1610 | |
1611 | // The "obvious" converting constructor implementation: |
1612 | // |
1613 | // template<typename _Tp1> |
1614 | // __weak_ptr(const __weak_ptr<_Tp1, _Lp>& __r) |
1615 | // : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws |
1616 | // { } |
1617 | // |
1618 | // has a serious problem. |
1619 | // |
1620 | // __r._M_ptr may already have been invalidated. The _M_ptr(__r._M_ptr) |
1621 | // conversion may require access to *__r._M_ptr (virtual inheritance). |
1622 | // |
1623 | // It is not possible to avoid spurious access violations since |
1624 | // in multithreaded programs __r._M_ptr may be invalidated at any point. |
1625 | template<typename _Yp, typename = _Compatible<_Yp>> |
1626 | __weak_ptr(const __weak_ptr<_Yp, _Lp>& __r) noexcept |
1627 | : _M_refcount(__r._M_refcount) |
1628 | { _M_ptr = __r.lock().get(); } |
1629 | |
1630 | template<typename _Yp, typename = _Compatible<_Yp>> |
1631 | __weak_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept |
1632 | : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) |
1633 | { } |
1634 | |
1635 | __weak_ptr(__weak_ptr&& __r) noexcept |
1636 | : _M_ptr(__r._M_ptr), _M_refcount(std::move(__r._M_refcount)) |
1637 | { __r._M_ptr = nullptr; } |
1638 | |
1639 | template<typename _Yp, typename = _Compatible<_Yp>> |
1640 | __weak_ptr(__weak_ptr<_Yp, _Lp>&& __r) noexcept |
1641 | : _M_ptr(__r.lock().get()), _M_refcount(std::move(__r._M_refcount)) |
1642 | { __r._M_ptr = nullptr; } |
1643 | |
1644 | __weak_ptr& |
1645 | operator=(const __weak_ptr& __r) noexcept = default; |
1646 | |
1647 | template<typename _Yp> |
1648 | _Assignable<_Yp> |
1649 | operator=(const __weak_ptr<_Yp, _Lp>& __r) noexcept |
1650 | { |
1651 | _M_ptr = __r.lock().get(); |
1652 | _M_refcount = __r._M_refcount; |
1653 | return *this; |
1654 | } |
1655 | |
1656 | template<typename _Yp> |
1657 | _Assignable<_Yp> |
1658 | operator=(const __shared_ptr<_Yp, _Lp>& __r) noexcept |
1659 | { |
1660 | _M_ptr = __r._M_ptr; |
1661 | _M_refcount = __r._M_refcount; |
1662 | return *this; |
1663 | } |
1664 | |
1665 | __weak_ptr& |
1666 | operator=(__weak_ptr&& __r) noexcept |
1667 | { |
1668 | _M_ptr = __r._M_ptr; |
1669 | _M_refcount = std::move(__r._M_refcount); |
1670 | __r._M_ptr = nullptr; |
1671 | return *this; |
1672 | } |
1673 | |
1674 | template<typename _Yp> |
1675 | _Assignable<_Yp> |
1676 | operator=(__weak_ptr<_Yp, _Lp>&& __r) noexcept |
1677 | { |
1678 | _M_ptr = __r.lock().get(); |
1679 | _M_refcount = std::move(__r._M_refcount); |
1680 | __r._M_ptr = nullptr; |
1681 | return *this; |
1682 | } |
1683 | |
1684 | __shared_ptr<_Tp, _Lp> |
1685 | lock() const noexcept |
1686 | { return __shared_ptr<element_type, _Lp>(*this, std::nothrow); } |
1687 | |
1688 | long |
1689 | use_count() const noexcept |
1690 | { return _M_refcount._M_get_use_count(); } |
1691 | |
1692 | bool |
1693 | expired() const noexcept |
1694 | { return _M_refcount._M_get_use_count() == 0; } |
1695 | |
1696 | template<typename _Tp1> |
1697 | bool |
1698 | owner_before(const __shared_ptr<_Tp1, _Lp>& __rhs) const noexcept |
1699 | { return _M_refcount._M_less(__rhs._M_refcount); } |
1700 | |
1701 | template<typename _Tp1> |
1702 | bool |
1703 | owner_before(const __weak_ptr<_Tp1, _Lp>& __rhs) const noexcept |
1704 | { return _M_refcount._M_less(__rhs._M_refcount); } |
1705 | |
1706 | void |
1707 | reset() noexcept |
1708 | { __weak_ptr().swap(*this); } |
1709 | |
1710 | void |
1711 | swap(__weak_ptr& __s) noexcept |
1712 | { |
1713 | std::swap(_M_ptr, __s._M_ptr); |
1714 | _M_refcount._M_swap(__s._M_refcount); |
1715 | } |
1716 | |
1717 | private: |
1718 | // Used by __enable_shared_from_this. |
1719 | void |
1720 | _M_assign(_Tp* __ptr, const __shared_count<_Lp>& __refcount) noexcept |
1721 | { |
1722 | if (use_count() == 0) |
1723 | { |
1724 | _M_ptr = __ptr; |
1725 | _M_refcount = __refcount; |
1726 | } |
1727 | } |
1728 | |
1729 | template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr; |
1730 | template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr; |
1731 | friend class __enable_shared_from_this<_Tp, _Lp>; |
1732 | friend class enable_shared_from_this<_Tp>; |
1733 | |
1734 | element_type* _M_ptr; // Contained pointer. |
1735 | __weak_count<_Lp> _M_refcount; // Reference counter. |
1736 | }; |
1737 | |
1738 | // 20.7.2.3.6 weak_ptr specialized algorithms. |
1739 | template<typename _Tp, _Lock_policy _Lp> |
1740 | inline void |
1741 | swap(__weak_ptr<_Tp, _Lp>& __a, __weak_ptr<_Tp, _Lp>& __b) noexcept |
1742 | { __a.swap(__b); } |
1743 | |
1744 | template<typename _Tp, typename _Tp1> |
1745 | struct _Sp_owner_less : public binary_function<_Tp, _Tp, bool> |
1746 | { |
1747 | bool |
1748 | operator()(const _Tp& __lhs, const _Tp& __rhs) const noexcept |
1749 | { return __lhs.owner_before(__rhs); } |
1750 | |
1751 | bool |
1752 | operator()(const _Tp& __lhs, const _Tp1& __rhs) const noexcept |
1753 | { return __lhs.owner_before(__rhs); } |
1754 | |
1755 | bool |
1756 | operator()(const _Tp1& __lhs, const _Tp& __rhs) const noexcept |
1757 | { return __lhs.owner_before(__rhs); } |
1758 | }; |
1759 | |
1760 | template<> |
1761 | struct _Sp_owner_less<void, void> |
1762 | { |
1763 | template<typename _Tp, typename _Up> |
1764 | auto |
1765 | operator()(const _Tp& __lhs, const _Up& __rhs) const noexcept |
1766 | -> decltype(__lhs.owner_before(__rhs)) |
1767 | { return __lhs.owner_before(__rhs); } |
1768 | |
1769 | using is_transparent = void; |
1770 | }; |
1771 | |
1772 | template<typename _Tp, _Lock_policy _Lp> |
1773 | struct owner_less<__shared_ptr<_Tp, _Lp>> |
1774 | : public _Sp_owner_less<__shared_ptr<_Tp, _Lp>, __weak_ptr<_Tp, _Lp>> |
1775 | { }; |
1776 | |
1777 | template<typename _Tp, _Lock_policy _Lp> |
1778 | struct owner_less<__weak_ptr<_Tp, _Lp>> |
1779 | : public _Sp_owner_less<__weak_ptr<_Tp, _Lp>, __shared_ptr<_Tp, _Lp>> |
1780 | { }; |
1781 | |
1782 | |
1783 | template<typename _Tp, _Lock_policy _Lp> |
1784 | class __enable_shared_from_this |
1785 | { |
1786 | protected: |
1787 | constexpr __enable_shared_from_this() noexcept { } |
1788 | |
1789 | __enable_shared_from_this(const __enable_shared_from_this&) noexcept { } |
1790 | |
1791 | __enable_shared_from_this& |
1792 | operator=(const __enable_shared_from_this&) noexcept |
1793 | { return *this; } |
1794 | |
1795 | ~__enable_shared_from_this() { } |
1796 | |
1797 | public: |
1798 | __shared_ptr<_Tp, _Lp> |
1799 | shared_from_this() |
1800 | { return __shared_ptr<_Tp, _Lp>(this->_M_weak_this); } |
1801 | |
1802 | __shared_ptr<const _Tp, _Lp> |
1803 | shared_from_this() const |
1804 | { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); } |
1805 | |
1806 | #if __cplusplus201103L > 201402L || !defined(__STRICT_ANSI__1) // c++1z or gnu++11 |
1807 | __weak_ptr<_Tp, _Lp> |
1808 | weak_from_this() noexcept |
1809 | { return this->_M_weak_this; } |
1810 | |
1811 | __weak_ptr<const _Tp, _Lp> |
1812 | weak_from_this() const noexcept |
1813 | { return this->_M_weak_this; } |
1814 | #endif |
1815 | |
1816 | private: |
1817 | template<typename _Tp1> |
1818 | void |
1819 | _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept |
1820 | { _M_weak_this._M_assign(__p, __n); } |
1821 | |
1822 | friend const __enable_shared_from_this* |
1823 | __enable_shared_from_this_base(const __shared_count<_Lp>&, |
1824 | const __enable_shared_from_this* __p) |
1825 | { return __p; } |
1826 | |
1827 | template<typename, _Lock_policy> |
1828 | friend class __shared_ptr; |
1829 | |
1830 | mutable __weak_ptr<_Tp, _Lp> _M_weak_this; |
1831 | }; |
1832 | |
1833 | template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args> |
1834 | inline __shared_ptr<_Tp, _Lp> |
1835 | __allocate_shared(const _Alloc& __a, _Args&&... __args) |
1836 | { |
1837 | return __shared_ptr<_Tp, _Lp>(_Sp_make_shared_tag(), __a, |
1838 | std::forward<_Args>(__args)...); |
1839 | } |
1840 | |
1841 | template<typename _Tp, _Lock_policy _Lp, typename... _Args> |
1842 | inline __shared_ptr<_Tp, _Lp> |
1843 | __make_shared(_Args&&... __args) |
1844 | { |
1845 | typedef typename std::remove_const<_Tp>::type _Tp_nc; |
1846 | return std::__allocate_shared<_Tp, _Lp>(std::allocator<_Tp_nc>(), |
1847 | std::forward<_Args>(__args)...); |
1848 | } |
1849 | |
1850 | /// std::hash specialization for __shared_ptr. |
1851 | template<typename _Tp, _Lock_policy _Lp> |
1852 | struct hash<__shared_ptr<_Tp, _Lp>> |
1853 | : public __hash_base<size_t, __shared_ptr<_Tp, _Lp>> |
1854 | { |
1855 | size_t |
1856 | operator()(const __shared_ptr<_Tp, _Lp>& __s) const noexcept |
1857 | { |
1858 | return hash<typename __shared_ptr<_Tp, _Lp>::element_type*>()( |
1859 | __s.get()); |
1860 | } |
1861 | }; |
1862 | |
1863 | _GLIBCXX_END_NAMESPACE_VERSION |
1864 | } // namespace |
1865 | |
1866 | #endif // _SHARED_PTR_BASE_H |
1 | // Support for atomic operations -*- C++ -*- | |||
2 | ||||
3 | // Copyright (C) 2004-2017 Free Software Foundation, Inc. | |||
4 | // | |||
5 | // This file is part of the GNU ISO C++ Library. This library is free | |||
6 | // software; you can redistribute it and/or modify it under the | |||
7 | // terms of the GNU General Public License as published by the | |||
8 | // Free Software Foundation; either version 3, or (at your option) | |||
9 | // any later version. | |||
10 | ||||
11 | // This library is distributed in the hope that it will be useful, | |||
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
14 | // GNU General Public License for 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 | /** @file ext/atomicity.h | |||
26 | * This file is a GNU extension to the Standard C++ Library. | |||
27 | */ | |||
28 | ||||
29 | #ifndef _GLIBCXX_ATOMICITY_H1 | |||
30 | #define _GLIBCXX_ATOMICITY_H1 1 | |||
31 | ||||
32 | #pragma GCC system_header | |||
33 | ||||
34 | #include <bits/c++config.h> | |||
35 | #include <bits/gthr.h> | |||
36 | #include <bits/atomic_word.h> | |||
37 | ||||
38 | namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) | |||
39 | { | |||
40 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |||
41 | ||||
42 | // Functions for portable atomic access. | |||
43 | // To abstract locking primitives across all thread policies, use: | |||
44 | // __exchange_and_add_dispatch | |||
45 | // __atomic_add_dispatch | |||
46 | #ifdef _GLIBCXX_ATOMIC_BUILTINS1 | |||
47 | static inline _Atomic_word | |||
48 | __exchange_and_add(volatile _Atomic_word* __mem, int __val) | |||
49 | { return __atomic_fetch_add(__mem, __val, __ATOMIC_ACQ_REL4); } | |||
50 | ||||
51 | static inline void | |||
52 | __atomic_add(volatile _Atomic_word* __mem, int __val) | |||
53 | { __atomic_fetch_add(__mem, __val, __ATOMIC_ACQ_REL4); } | |||
54 | #else | |||
55 | _Atomic_word | |||
56 | __attribute__ ((__unused__)) | |||
57 | __exchange_and_add(volatile _Atomic_word*, int) throw (); | |||
58 | ||||
59 | void | |||
60 | __attribute__ ((__unused__)) | |||
61 | __atomic_add(volatile _Atomic_word*, int) throw (); | |||
62 | #endif | |||
63 | ||||
64 | static inline _Atomic_word | |||
65 | __exchange_and_add_single(_Atomic_word* __mem, int __val) | |||
66 | { | |||
67 | _Atomic_word __result = *__mem; | |||
| ||||
68 | *__mem += __val; | |||
69 | return __result; | |||
70 | } | |||
71 | ||||
72 | static inline void | |||
73 | __atomic_add_single(_Atomic_word* __mem, int __val) | |||
74 | { *__mem += __val; } | |||
75 | ||||
76 | static inline _Atomic_word | |||
77 | __attribute__ ((__unused__)) | |||
78 | __exchange_and_add_dispatch(_Atomic_word* __mem, int __val) | |||
79 | { | |||
80 | #ifdef __GTHREADS1 | |||
81 | if (__gthread_active_p()) | |||
82 | return __exchange_and_add(__mem, __val); | |||
83 | else | |||
84 | return __exchange_and_add_single(__mem, __val); | |||
85 | #else | |||
86 | return __exchange_and_add_single(__mem, __val); | |||
87 | #endif | |||
88 | } | |||
89 | ||||
90 | static inline void | |||
91 | __attribute__ ((__unused__)) | |||
92 | __atomic_add_dispatch(_Atomic_word* __mem, int __val) | |||
93 | { | |||
94 | #ifdef __GTHREADS1 | |||
95 | if (__gthread_active_p()) | |||
96 | __atomic_add(__mem, __val); | |||
97 | else | |||
98 | __atomic_add_single(__mem, __val); | |||
99 | #else | |||
100 | __atomic_add_single(__mem, __val); | |||
101 | #endif | |||
102 | } | |||
103 | ||||
104 | _GLIBCXX_END_NAMESPACE_VERSION | |||
105 | } // namespace | |||
106 | ||||
107 | // Even if the CPU doesn't need a memory barrier, we need to ensure | |||
108 | // that the compiler doesn't reorder memory accesses across the | |||
109 | // barriers. | |||
110 | #ifndef _GLIBCXX_READ_MEM_BARRIER__atomic_thread_fence (2) | |||
111 | #define _GLIBCXX_READ_MEM_BARRIER__atomic_thread_fence (2) __atomic_thread_fence (__ATOMIC_ACQUIRE2) | |||
112 | #endif | |||
113 | #ifndef _GLIBCXX_WRITE_MEM_BARRIER__atomic_thread_fence (3) | |||
114 | #define _GLIBCXX_WRITE_MEM_BARRIER__atomic_thread_fence (3) __atomic_thread_fence (__ATOMIC_RELEASE3) | |||
115 | #endif | |||
116 | ||||
117 | #endif |