24
24
25
25
#include < vector>
26
26
#include < unordered_map>
27
- #include < utility>
28
27
29
28
namespace swift {
30
29
namespace remote {
31
30
32
- template <typename Runtime>
33
- using SharedTargetMetadataRef = std::shared_ptr<TargetMetadata<Runtime>>;
34
-
35
- using SharedCaptureDescriptor = std::shared_ptr<const CaptureDescriptor>;
36
-
37
- template <typename Runtime>
38
- using SharedTargetNominalTypeDescriptorRef
39
- = std::shared_ptr<TargetNominalTypeDescriptor<Runtime>>;
40
-
41
- template <typename Runtime>
42
- using SharedProtocolDescriptorRef
43
- = std::shared_ptr<TargetProtocolDescriptor<Runtime>>;
44
-
45
31
// / A utility class for constructing abstract types from
46
32
// / a textual mangling.
47
33
template <typename BuilderType>
@@ -351,6 +337,14 @@ class RemoteRef {
351
337
}
352
338
};
353
339
340
+ // / A structure, designed for use with std::unique_ptr, which destroys
341
+ // / a pointer by calling free on it (and not trying to call a destructor).
342
+ struct delete_with_free {
343
+ void operator ()(const void *memory) {
344
+ free (const_cast <void *>(memory));
345
+ }
346
+ };
347
+
354
348
// / A generic reader of metadata.
355
349
// /
356
350
// / BuilderType must implement a particular interface which is currently
@@ -373,22 +367,30 @@ class MetadataReader {
373
367
374
368
using MetadataRef =
375
369
RemoteRef<Runtime, TargetMetadata<Runtime>>;
376
- using SharedMetadataRef =
377
- SharedTargetMetadataRef< Runtime>;
370
+ using OwnedMetadataRef =
371
+ std::unique_ptr< const TargetMetadata< Runtime>, delete_with_free >;
378
372
379
373
// / A cache of read type metadata, keyed by the address of the metadata.
380
- std::unordered_map<StoredPointer, SharedMetadataRef> MetadataCache;
374
+ std::unordered_map<StoredPointer, OwnedMetadataRef>
375
+ MetadataCache;
381
376
382
377
using NominalTypeDescriptorRef =
383
378
RemoteRef<Runtime, TargetNominalTypeDescriptor<Runtime>>;
384
- using SharedNominalTypeDescriptorRef =
385
- SharedTargetNominalTypeDescriptorRef<Runtime>;
379
+ using OwnedNominalTypeDescriptorRef =
380
+ std::unique_ptr<const TargetNominalTypeDescriptor<Runtime>,
381
+ delete_with_free>;
386
382
387
383
// / A cache of read nominal type descriptors, keyed by the address of the
388
384
// / nominal type descriptor.
389
- std::unordered_map<StoredPointer, SharedNominalTypeDescriptorRef >
385
+ std::unordered_map<StoredPointer, OwnedNominalTypeDescriptorRef >
390
386
NominalTypeDescriptorCache;
391
387
388
+ using OwnedProtocolDescriptorRef =
389
+ std::unique_ptr<const TargetProtocolDescriptor<Runtime>, delete_with_free>;
390
+
391
+ using OwnedCaptureDescriptor =
392
+ std::unique_ptr<const CaptureDescriptor, delete_with_free>;
393
+
392
394
public:
393
395
BuilderType Builder;
394
396
@@ -421,21 +423,12 @@ class MetadataReader {
421
423
}
422
424
423
425
// / Given a remote pointer to metadata, attempt to discover its MetadataKind.
424
- std::pair<bool ,MetadataKind>
426
+ std::pair<bool , MetadataKind>
425
427
readKindFromMetadata (StoredPointer MetadataAddress) {
426
- auto Cached = MetadataCache.find (MetadataAddress);
427
- if (Cached != MetadataCache.end ()) {
428
- if (auto Meta = Cached->second ) {
429
- return {true ,Cached->second ->getKind ()};
430
- } else {
431
- return {false ,MetadataKind::Opaque};
432
- }
433
- }
434
-
435
- auto Meta = readMetadata (MetadataAddress);
436
- if (!Meta) return {false ,MetadataKind::Opaque};
428
+ auto meta = readMetadata (MetadataAddress);
429
+ if (!meta) return {false , MetadataKind::Opaque};
437
430
438
- return {true ,Meta ->getKind ()};
431
+ return {true , meta ->getKind ()};
439
432
}
440
433
441
434
// / Given a remote pointer to metadata, attempt to turn it into a type.
@@ -590,7 +583,7 @@ class MetadataReader {
590
583
}
591
584
592
585
auto metadata = reinterpret_cast <TargetMetadata<Runtime>*>(buffer);
593
- MetadataCache.insert ({ address, SharedMetadataRef (metadata, free)} );
586
+ MetadataCache.insert (std::make_pair ( address, OwnedMetadataRef (metadata)) );
594
587
return MetadataRef (address, metadata);
595
588
}
596
589
@@ -703,8 +696,8 @@ class MetadataReader {
703
696
auto descriptor
704
697
= reinterpret_cast <TargetNominalTypeDescriptor<Runtime> *>(buffer);
705
698
706
- NominalTypeDescriptorCache.insert ({address,
707
- SharedNominalTypeDescriptorRef (descriptor, free)} );
699
+ NominalTypeDescriptorCache.insert (
700
+ std::make_pair (address, OwnedNominalTypeDescriptorRef (descriptor)) );
708
701
return NominalTypeDescriptorRef (address, descriptor);
709
702
}
710
703
@@ -724,7 +717,7 @@ class MetadataReader {
724
717
return decl;
725
718
}
726
719
727
- SharedProtocolDescriptorRef<Runtime>
720
+ OwnedProtocolDescriptorRef
728
721
readProtocolDescriptor (StoredPointer Address) {
729
722
auto Size = sizeof (TargetProtocolDescriptor<Runtime>);
730
723
auto Buffer = (uint8_t *)malloc (Size);
@@ -734,7 +727,7 @@ class MetadataReader {
734
727
}
735
728
auto Casted
736
729
= reinterpret_cast <TargetProtocolDescriptor<Runtime> *>(Buffer);
737
- return SharedProtocolDescriptorRef<Runtime> (Casted, free );
730
+ return OwnedProtocolDescriptorRef (Casted);
738
731
}
739
732
740
733
StoredPointer getNominalParent (MetadataRef metadata,
@@ -830,7 +823,7 @@ class MetadataReader {
830
823
// / Read the entire CaptureDescriptor in this address space, including
831
824
// / trailing capture typeref relative offsets, and GenericMetadataSource
832
825
// / pairs.
833
- SharedCaptureDescriptor readCaptureDescriptor (StoredPointer Address) {
826
+ OwnedCaptureDescriptor readCaptureDescriptor (StoredPointer Address) {
834
827
835
828
uint32_t NumCaptures = 0 ;
836
829
uint32_t NumMetadataSources = 0 ;
@@ -857,7 +850,7 @@ class MetadataReader {
857
850
}
858
851
859
852
auto RawDescriptor = reinterpret_cast <const CaptureDescriptor *>(Buffer);
860
- return SharedCaptureDescriptor (RawDescriptor, /* deleter */ free );
853
+ return OwnedCaptureDescriptor (RawDescriptor);
861
854
}
862
855
};
863
856
0 commit comments