@@ -131,147 +131,13 @@ swift::swift_allocPOD(size_t dataSize, size_t dataAlignmentMask) {
131
131
return BoxPair{obj, reinterpret_cast <OpaqueValue*>(data)};
132
132
}
133
133
134
- namespace {
135
- // / Header for a generic box created by swift_allocBox in the worst case.
136
- struct GenericBox : HeapObject {
137
- // / The type of the value inside the box.
138
- Metadata const *type;
139
-
140
- // / Returns the offset in bytes from the address of the box header to the
141
- // / address of the value inside the box.
142
- size_t getValueOffset () const {
143
- return getValueOffset (type);
144
- }
145
-
146
- // / Returns the offset in bytes from the address of the box header for
147
- // / a box containing a value of the given type to the address of the value
148
- // / inside the box.
149
- static size_t getValueOffset (Metadata const *type) {
150
- return llvm::RoundUpToAlignment (sizeof (GenericBox),
151
- type->getValueWitnesses ()->getAlignment ());
152
- }
153
-
154
- // / Returns the size of the allocation for the box, including the header
155
- // / and the value.
156
- size_t getAllocatedSize () const {
157
- return getAllocatedSize (type);
158
- }
159
-
160
- // / Returns the size of the allocation that would be made for a box
161
- // / containing a value of the given type, including the header and the value.
162
- static size_t getAllocatedSize (Metadata const *type) {
163
- return getValueOffset (type) + type->getValueWitnesses ()->stride ;
164
- }
165
-
166
- size_t getAllocatedAlignMask () const {
167
- return getAllocatedAlignMask (type);
168
- }
169
-
170
- // / Returns the alignment mask of the allocation that would be
171
- // / made for a box containing a value of the given type, including
172
- // / the header and the value.
173
- static size_t getAllocatedAlignMask (Metadata const *type) {
174
- return std::max (type->getValueWitnesses ()->getAlignmentMask (),
175
- alignof (GenericBox) - 1 );
176
- }
177
-
178
- // / Returns an opaque pointer to the value inside the box.
179
- OpaqueValue *getValuePointer () {
180
- char *p = reinterpret_cast <char *>(this ) + getValueOffset ();
181
- return reinterpret_cast <OpaqueValue*>(p);
182
- }
183
-
184
- // / Returns an opaque pointer to the value inside the box.
185
- OpaqueValue const *getValuePointer () const {
186
- auto *p = reinterpret_cast <char const *>(this ) + getValueOffset ();
187
- return reinterpret_cast <OpaqueValue const *>(p);
188
- }
189
- };
190
- }
191
-
192
- // / Heap object destructor for a generic box allocated with swift_allocBox.
193
- static void destroyGenericBox (HeapObject *o) {
194
- auto *box = static_cast <GenericBox*>(o);
195
-
196
- // Destroy the value inside the box.
197
- OpaqueValue *value = box->getValuePointer ();
198
- box->type ->getValueWitnesses ()->destroy (value, box->type );
199
-
200
- // Deallocate the buffer.
201
- return swift_deallocObject (o, box->getAllocatedSize (),
202
- box->getAllocatedAlignMask ());
203
- }
204
-
205
- // / Generic heap metadata for generic allocBox allocations.
206
- // / FIXME: It may be worth the tradeoff to instantiate type-specific
207
- // / heap metadata at runtime.
208
- static const FullMetadata<HeapMetadata> GenericBoxHeapMetadata{
209
- HeapMetadataHeader{{destroyGenericBox}, {nullptr }},
210
- HeapMetadata{Metadata{MetadataKind::HeapLocalVariable}}
211
- };
212
-
213
- BoxPair::Return
214
- swift::swift_allocBox (Metadata const *type) {
215
- return _swift_allocBox (type);
216
- }
217
- static BoxPair::Return _swift_allocBox_ (Metadata const *type) {
218
- // NB: Special cases here need to also be checked for and handled in
219
- // swift_deallocBox.
220
-
221
- // If the contained type is POD, perform a POD allocation.
222
- auto *vw = type->getValueWitnesses ();
223
- if (vw->isPOD ()) {
224
- return swift_allocPOD (vw->getSize (), vw->getAlignmentMask ());
225
- }
226
-
227
- // Allocate the box.
228
- HeapObject *obj = swift_allocObject (&GenericBoxHeapMetadata,
229
- GenericBox::getAllocatedSize (type),
230
- GenericBox::getAllocatedAlignMask (type));
231
- // allocObject will initialize the heap metadata pointer and refcount for us.
232
- // We also need to store the type metadata between the header and the
233
- // value.
234
- auto *box = static_cast <GenericBox *>(obj);
235
- box->type = type;
236
-
237
- // Return the box and the value pointer.
238
- return BoxPair{box, box->getValuePointer ()};
239
- }
240
- auto swift::_swift_allocBox = _swift_allocBox_;
241
-
242
- void swift::swift_deallocBox (HeapObject *box, Metadata const *type) {
243
- // NB: Special cases here need to also be checked for and handled in
244
- // swift_allocBox.
245
-
246
- // First, we need to recover what the allocation size was.
247
- size_t allocatedSize, allocatedAlignMask;
248
- auto *vw = type->getValueWitnesses ();
249
- if (vw->isPOD ()) {
250
- // If the contained type is POD, use the POD allocation size.
251
- allocatedSize = static_cast <PODBox*>(box)->allocatedSize ;
252
- allocatedAlignMask = static_cast <PODBox*>(box)->allocatedAlignMask ;
253
- } else {
254
- // Use the generic box size to deallocate the object.
255
- allocatedSize = GenericBox::getAllocatedSize (type);
256
- allocatedAlignMask = GenericBox::getAllocatedAlignMask (type);
257
- }
258
-
259
- // Deallocate the box.
260
- swift_deallocObject (box, allocatedSize, allocatedAlignMask);
261
- }
262
-
263
- void swift::swift_deallocPOD (HeapObject *obj) {
264
- swift_deallocObject (obj, static_cast <PODBox*>(obj)->allocatedSize ,
265
- static_cast <PODBox*>(obj)->allocatedAlignMask );
266
- }
267
-
268
134
namespace {
269
135
// / Heap metadata for runtime-instantiated generic boxes.
270
- struct GenericBox2HeapMetadata : public HeapMetadata {
136
+ struct GenericBoxHeapMetadata : public HeapMetadata {
271
137
// / The type inside the box.
272
138
const Metadata *BoxedType;
273
139
274
- constexpr GenericBox2HeapMetadata (MetadataKind kind,
140
+ constexpr GenericBoxHeapMetadata (MetadataKind kind,
275
141
const Metadata *boxedType)
276
142
: HeapMetadata{kind}, BoxedType(boxedType)
277
143
{}
@@ -301,9 +167,9 @@ struct GenericBox2HeapMetadata : public HeapMetadata {
301
167
}
302
168
};
303
169
304
- // / Heap object destructor for a generic box allocated with swift_allocBox2 .
305
- static void destroyGenericBox2 (HeapObject *o) {
306
- auto metadata = static_cast <const GenericBox2HeapMetadata *>(o->metadata );
170
+ // / Heap object destructor for a generic box allocated with swift_allocBox .
171
+ static void destroyGenericBox (HeapObject *o) {
172
+ auto metadata = static_cast <const GenericBoxHeapMetadata *>(o->metadata );
307
173
// Destroy the object inside.
308
174
auto *value = metadata->project (o);
309
175
metadata->BoxedType ->vw_destroy (value);
@@ -315,11 +181,11 @@ static void destroyGenericBox2(HeapObject *o) {
315
181
316
182
class BoxCacheEntry : public CacheEntry <BoxCacheEntry> {
317
183
public:
318
- FullMetadata<GenericBox2HeapMetadata > Metadata;
184
+ FullMetadata<GenericBoxHeapMetadata > Metadata;
319
185
320
186
BoxCacheEntry (size_t numArguments)
321
- : Metadata{HeapMetadataHeader{{destroyGenericBox2 }, {nullptr }},
322
- GenericBox2HeapMetadata {MetadataKind::HeapGenericLocalVariable,
187
+ : Metadata{HeapMetadataHeader{{destroyGenericBox }, {nullptr }},
188
+ GenericBoxHeapMetadata {MetadataKind::HeapGenericLocalVariable,
323
189
nullptr }} {
324
190
assert (numArguments == 1 );
325
191
}
@@ -328,10 +194,10 @@ class BoxCacheEntry : public CacheEntry<BoxCacheEntry> {
328
194
return 1 ;
329
195
}
330
196
331
- FullMetadata<GenericBox2HeapMetadata > *getData () {
197
+ FullMetadata<GenericBoxHeapMetadata > *getData () {
332
198
return &Metadata;
333
199
}
334
- const FullMetadata<GenericBox2HeapMetadata > *getData () const {
200
+ const FullMetadata<GenericBoxHeapMetadata > *getData () const {
335
201
return &Metadata;
336
202
}
337
203
};
@@ -341,10 +207,10 @@ class BoxCacheEntry : public CacheEntry<BoxCacheEntry> {
341
207
static Lazy<MetadataCache<BoxCacheEntry>> Boxes;
342
208
343
209
BoxPair::Return
344
- swift::swift_allocBox2 (const Metadata *type) {
345
- return _swift_allocBox2 (type);
210
+ swift::swift_allocBox (const Metadata *type) {
211
+ return _swift_allocBox (type);
346
212
}
347
- static BoxPair::Return _swift_allocBox2_ (const Metadata *type) {
213
+ static BoxPair::Return _swift_allocBox_ (const Metadata *type) {
348
214
// Get the heap metadata for the box.
349
215
auto &B = Boxes.get ();
350
216
const void *typeArg = type;
@@ -367,16 +233,16 @@ static BoxPair::Return _swift_allocBox2_(const Metadata *type) {
367
233
368
234
return BoxPair{allocation, projection};
369
235
}
370
- auto swift::_swift_allocBox2 = _swift_allocBox2_ ;
236
+ auto swift::_swift_allocBox = _swift_allocBox_ ;
371
237
372
- void swift::swift_deallocBox2 (HeapObject *o) {
373
- auto metadata = static_cast <const GenericBox2HeapMetadata *>(o->metadata );
238
+ void swift::swift_deallocBox (HeapObject *o) {
239
+ auto metadata = static_cast <const GenericBoxHeapMetadata *>(o->metadata );
374
240
swift_deallocObject (o, metadata->getAllocSize (),
375
241
metadata->getAllocAlignMask ());
376
242
}
377
243
378
- OpaqueValue *swift::swift_projectBox2 (HeapObject *o) {
379
- auto metadata = static_cast <const GenericBox2HeapMetadata *>(o->metadata );
244
+ OpaqueValue *swift::swift_projectBox (HeapObject *o) {
245
+ auto metadata = static_cast <const GenericBoxHeapMetadata *>(o->metadata );
380
246
return metadata->project (o);
381
247
}
382
248
0 commit comments