|
| 1 | +//===--- Metadata.cpp -----------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// Backward deployment of swift_allocateMetadataPack() and |
| 14 | +// swift_allocateWitnessTablePack() runtime entry points. |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#include "../../public/runtime/MetadataCache.h" |
| 19 | + |
| 20 | +// Allow this library to get force-loaded by autolinking |
| 21 | +__attribute__((weak, visibility("hidden"))) |
| 22 | +extern "C" |
| 23 | +char _swift_FORCE_LOAD_$_swiftCompatibilityPacks = 0; |
| 24 | + |
| 25 | +using namespace swift; |
| 26 | + |
| 27 | +/// Copy and paste some symbols that need to exist. |
| 28 | +void *MetadataAllocator::Allocate(size_t size, size_t alignment) { |
| 29 | + return malloc(size); |
| 30 | +} |
| 31 | + |
| 32 | +/// Avoid depending on non-inline parts of llvm::hashing. |
| 33 | +inline llvm::hash_code our_hash_integer_value(uint64_t value) { |
| 34 | + const char *s = reinterpret_cast<const char *>(&value); |
| 35 | + const uint64_t a = llvm::hashing::detail::fetch32(s); |
| 36 | + return llvm::hashing::detail::hash_16_bytes( |
| 37 | + (a << 3), llvm::hashing::detail::fetch32(s + 4)); |
| 38 | +} |
| 39 | + |
| 40 | +static inline llvm::hash_code our_hash_combine(llvm::hash_code seed, llvm::hash_code v) { |
| 41 | + return seed ^ (v + 0x9e3779b9 + (seed<<6) + (seed>>2)); |
| 42 | +} |
| 43 | + |
| 44 | +/// Copy and paste from Metadata.cpp. |
| 45 | + |
| 46 | +namespace { |
| 47 | + |
| 48 | +template<typename PackType> |
| 49 | +class PackCacheEntry { |
| 50 | +public: |
| 51 | + size_t Count; |
| 52 | + |
| 53 | + const PackType * const * getElements() const { |
| 54 | + return reinterpret_cast<const PackType * const *>(this + 1); |
| 55 | + } |
| 56 | + |
| 57 | + const PackType ** getElements() { |
| 58 | + return reinterpret_cast<const PackType **>(this + 1); |
| 59 | + } |
| 60 | + |
| 61 | + struct Key { |
| 62 | + const PackType *const *Data; |
| 63 | + const size_t Count; |
| 64 | + |
| 65 | + size_t getCount() const { |
| 66 | + return Count; |
| 67 | + } |
| 68 | + |
| 69 | + const PackType *getElement(size_t index) const { |
| 70 | + assert(index < Count); |
| 71 | + return Data[index]; |
| 72 | + } |
| 73 | + |
| 74 | + friend llvm::hash_code hash_value(const Key &key) { |
| 75 | + llvm::hash_code hash = 0; |
| 76 | + for (size_t i = 0; i != key.getCount(); ++i) { |
| 77 | + hash = our_hash_combine(hash, our_hash_integer_value( |
| 78 | + reinterpret_cast<uint64_t>(key.getElement(i)))); |
| 79 | + } |
| 80 | + return hash; |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | + PackCacheEntry(const Key &key); |
| 85 | + |
| 86 | + intptr_t getKeyIntValueForDump() { |
| 87 | + return 0; // No single meaningful value here. |
| 88 | + } |
| 89 | + |
| 90 | + bool matchesKey(const Key &key) const { |
| 91 | + if (key.getCount() != Count) |
| 92 | + return false; |
| 93 | + for (unsigned i = 0; i != Count; ++i) { |
| 94 | + if (key.getElement(i) != getElements()[i]) |
| 95 | + return false; |
| 96 | + } |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + friend llvm::hash_code hash_value(const PackCacheEntry<PackType> &value) { |
| 101 | + llvm::hash_code hash = 0; |
| 102 | + for (size_t i = 0; i != value.Count; ++i) { |
| 103 | + hash = our_hash_combine(hash, our_hash_integer_value( |
| 104 | + reinterpret_cast<uint64_t>(value.getElements()[i]))); |
| 105 | + } |
| 106 | + return hash; |
| 107 | + } |
| 108 | + |
| 109 | + static size_t getExtraAllocationSize(const Key &key) { |
| 110 | + return getExtraAllocationSize(key.Count); |
| 111 | + } |
| 112 | + |
| 113 | + size_t getExtraAllocationSize() const { |
| 114 | + return getExtraAllocationSize(Count); |
| 115 | + } |
| 116 | + |
| 117 | + static size_t getExtraAllocationSize(unsigned count) { |
| 118 | + return count * sizeof(const Metadata * const *); |
| 119 | + } |
| 120 | +}; |
| 121 | + |
| 122 | +template<typename PackType> |
| 123 | +PackCacheEntry<PackType>::PackCacheEntry( |
| 124 | + const typename PackCacheEntry<PackType>::Key &key) { |
| 125 | + Count = key.getCount(); |
| 126 | + |
| 127 | + for (unsigned i = 0; i < Count; ++i) |
| 128 | + getElements()[i] = key.getElement(i); |
| 129 | +} |
| 130 | + |
| 131 | +} // end anonymous namespace |
| 132 | + |
| 133 | +/// The uniquing structure for metadata packs. |
| 134 | +static SimpleGlobalCache<PackCacheEntry<Metadata>, |
| 135 | + MetadataPackTag> MetadataPacks; |
| 136 | + |
| 137 | +SWIFT_RUNTIME_EXPORT SWIFT_CC(swift) |
| 138 | +const Metadata * const * |
| 139 | +swift_allocateMetadataPack(const Metadata * const *ptr, size_t count) { |
| 140 | + if (MetadataPackPointer(reinterpret_cast<uintptr_t>(ptr)).getLifetime() |
| 141 | + == PackLifetime::OnHeap) |
| 142 | + return ptr; |
| 143 | + |
| 144 | + PackCacheEntry<Metadata>::Key key{ptr, count}; |
| 145 | + auto bytes = MetadataPacks.getOrInsert(key).first->getElements(); |
| 146 | + |
| 147 | + MetadataPackPointer pack(bytes, PackLifetime::OnHeap); |
| 148 | + assert(pack.getNumElements() == count); |
| 149 | + return pack.getPointer(); |
| 150 | +} |
| 151 | + |
| 152 | +/// The uniquing structure for witness table packs. |
| 153 | +static SimpleGlobalCache<PackCacheEntry<WitnessTable>, |
| 154 | + WitnessTablePackTag> WitnessTablePacks; |
| 155 | + |
| 156 | +SWIFT_RUNTIME_EXPORT SWIFT_CC(swift) |
| 157 | +const WitnessTable * const * |
| 158 | +swift_allocateWitnessTablePack(const WitnessTable * const *ptr, size_t count) { |
| 159 | + if (WitnessTablePackPointer(reinterpret_cast<uintptr_t>(ptr)).getLifetime() |
| 160 | + == PackLifetime::OnHeap) |
| 161 | + return ptr; |
| 162 | + |
| 163 | + PackCacheEntry<WitnessTable>::Key key{ptr, count}; |
| 164 | + auto bytes = WitnessTablePacks.getOrInsert(key).first->getElements(); |
| 165 | + |
| 166 | + WitnessTablePackPointer pack(bytes, PackLifetime::OnHeap); |
| 167 | + assert(pack.getNumElements() == count); |
| 168 | + return pack.getPointer(); |
| 169 | +} |
0 commit comments