1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
|
// slang-blob-builder.cpp
#include "slang-blob-builder.h"
namespace Slang
{
//
// BlobBuilder
//
BlobBuilder::BlobBuilder()
: _arena(4096)
{
}
void BlobBuilder::writeTo(Stream* stream)
{
// First we scan through all the chunks to set their
// absolute offsets, which enables us to compute the
// correct values for relative pointers when we write
// them out.
//
SLANG_MAYBE_UNUSED
Size sizeComputed = _calcSizeAndSetCachedChunkOffsets();
// Now we can scan through the chunks again and write
// the bytes of each of their shards.
//
SLANG_MAYBE_UNUSED
Size sizeWritten = _writeChunksTo(stream);
SLANG_ASSERT(sizeComputed == sizeWritten);
}
Size BlobBuilder::_calcSizeAndSetCachedChunkOffsets()
{
Size totalSize = 0;
for (auto chunk : _chunks)
{
auto chunkPrefixSize = chunk->getPrefixSize();
auto chunkContentSize = chunk->getContentSize();
auto chunkAlignment = chunk->getAlignment();
// We add the size of the chunk prefix (if any) *before*
// aligning the current offset. Doing it this way
// means that sometimes the prefix can fit "for free"
// in space that would otherwise be padding.
//
// For example, if the current `totalSize` is 4, the
// `chunkAlignment` is 16, and the `chunkPrefixSize` is 8,
// then the sequence will be:
//
// * Add `totalSize += chunkPrefixSize`, resulting in a `totalSize`
// of 12.
//
// * Round the `totalSize` up to the `chunkAlignment`, to compute
// a `chunkOffset` of 16.
//
// The result is that the chunk's content starts at offset 16,
// and the prefix can occupy the 8 bytes before that (so the
// prefix is at offset 8).
//
// In the best case this approach can save a few bytes here or
// there when `chunkAlignment` is larger than the `chunkPrefixSize`,
// and in the worst case it does no harm.
totalSize += chunkPrefixSize;
auto chunkOffset = roundUpToAlignment(totalSize, chunkAlignment);
chunk->_setCachedOffset(chunkOffset);
totalSize = chunkOffset + chunkContentSize;
}
return totalSize;
}
Size BlobBuilder::_writeChunksTo(Stream* stream)
{
Size totalSize = 0;
for (auto chunk : _chunks)
{
auto chunkPrefixSize = chunk->getPrefixSize();
auto chunkContentSize = chunk->getContentSize();
auto chunkOffset = chunk->_getCachedOffset();
SLANG_ASSERT(
chunkOffset == roundUpToAlignment(totalSize + chunkPrefixSize, chunk->getAlignment()));
auto paddingSize = chunkOffset - totalSize;
// The "padding" before the chunk's content also
// includes the space reserved for the chunk's prefix.
//
// We can thus subtract the prefix size from the number
// of pad bytes to write.
SLANG_ASSERT(paddingSize >= chunkPrefixSize);
paddingSize -= chunkPrefixSize;
while (paddingSize--)
{
Byte padding = 0;
stream->write(&padding, sizeof(padding));
}
// The `ChunkBuilder::_writeTo()` call will write the
// prefix *and* the chunk content, so it is appropriate
// to call it here even when the total number of bytes
// written to the stream is not equal to the `chunkOffset`
// (because in that case the total bytes written so far
// should be `chunkOffset - chunkPrefixSize`).
//
chunk->_writeTo(stream);
totalSize = chunkOffset + chunkContentSize;
}
return totalSize;
}
void BlobBuilder::writeToBlob(ISlangBlob** outBlob)
{
OwnedMemoryStream stream(FileAccess::Write);
writeTo(&stream);
List<uint8_t> data;
stream.swapContents(data);
*outBlob = ListBlob::moveCreate(data).detach();
}
ChunkBuilder* BlobBuilder::createUnparentedChunk()
{
auto chunk = new (_arena) ChunkBuilder(this);
return chunk;
}
void BlobBuilder::addChunk(ChunkBuilder* chunk)
{
// TODO(tfoley): it would be good to have a way to assert
// that the chunk has not already been added.
_chunks.add(chunk);
}
ChunkBuilder* BlobBuilder::addChunk()
{
auto chunk = new (_arena) ChunkBuilder(this);
_chunks.add(chunk);
return chunk;
}
ChunkBuilder* BlobBuilder::addChunkAfter(ChunkBuilder* existingChunk)
{
auto newChunk = new (_arena) ChunkBuilder(this);
_chunks.insertAfter(existingChunk, newChunk);
return newChunk;
}
//
// ChunkBuilder
//
void ChunkBuilder::setAlignmentToAtLeast(Size alignment)
{
SLANG_ASSERT(alignment > 0);
SLANG_ASSERT(isPowerOfTwo(alignment));
_contentAlignment = std::max(_contentAlignment, alignment);
}
void ChunkBuilder::writePaddingToAlignTo(Size alignment)
{
setAlignmentToAtLeast(alignment);
auto alignedSize = roundUpToAlignment(_contentSize, alignment);
auto requiredPaddingSize = alignedSize - _contentSize;
while (requiredPaddingSize)
{
Byte padByte = 0;
writeData(&padByte, sizeof(padByte));
requiredPaddingSize -= sizeof(padByte);
}
}
ChunkBuilder::ChunkBuilder(BlobBuilder* parentBlob)
: _parentBlob(parentBlob)
{
}
Size ChunkBuilder::getContentSize() const
{
return _contentSize;
}
Size ChunkBuilder::getPrefixSize() const
{
if (!_prefixShard)
return 0;
return _prefixShard->getSize();
}
Size ChunkBuilder::getAlignment() const
{
return _contentAlignment;
}
void ChunkBuilder::writeData(void const* data, Size size)
{
// Adding no data should be a no-op.
//
if (size == 0)
return;
// The most interesting implementation detail here
// is that we will try to detect cases where we
// can re-use an existing `ShardBuilder` by adding the data
// to the end of that shard's allocation.
//
// This is only possible because of the way that
// we are using a single `MemoryArena` to allocate
// everything, which makes it possible that the
// next address the arena would return for an allocation
// of `size` bytes is the same as the ending address
// of the payload for the last shard of this chunk.
//
auto& arena = getParentBlob()->_getArena();
// We start by checking if this chunk already has
// a last shard that we could consider appending to.
//
auto lastShard = _childShards.getLast();
if (lastShard && lastShard->_kind == ShardBuilder::Kind::Data)
{
// If there is a last shard, then we can compute
// the end address of its payload, and see if
// it is the same as the cursor of the arena
// we are allocating from.
//
auto payload = lastShard->_data.ptr;
auto payloadSize = lastShard->_size;
auto payloadEnd = (Byte*)payload + payloadSize;
if (payloadEnd == arena.getCursor())
{
// Now that we've confirmed that the shard's
// payload ends at an address the arena could
// conceivably allocate from, we need to ask
// the arena to allocate `size` bytes from
// the current block it is using, and see if
// doing so succeeds.
//
if (arena.allocateCurrentUnaligned(size))
{
// At this point, we've confirmed that we
// are in our special case, and the relevant
// bytes have been allocated from the arena.
//
// Now we can simply write the new data at
// what used to be the end address for the
// shard's payload, and adjust its state
// to account for the new allocation.
//
::memcpy(payloadEnd, data, size);
lastShard->_size = payloadSize + size;
_contentSize += size;
return;
}
}
}
// If the special case above doesn't apply, we simply
// allocate a new shard to hold the data that was
// passed in.
//
auto shard = _createDataShard(data, size);
_childShards.add(shard);
_contentSize += size;
}
void ChunkBuilder::addContentsOf(ChunkBuilder* otherChunk)
{
auto otherPrefixShard = otherChunk->_prefixShard;
auto otherChunkSize = otherChunk->getContentSize();
auto otherChunkAlignment = otherChunk->getAlignment();
auto otherChunkShards = otherChunk->_childShards;
otherChunk->_prefixShard = nullptr;
otherChunk->_contentSize = 0;
otherChunk->_contentAlignment = 1;
otherChunk->_childShards = InternallyLinkedList<ShardBuilder>();
if (otherPrefixShard)
{
// If the other chunk included a prefix, then
// it only makes sense to append it in the case
// where *this* chunk is completely empty.
SLANG_ASSERT(!_prefixShard);
SLANG_ASSERT(!_childShards.getFirst());
_prefixShard = otherPrefixShard;
}
writePaddingToAlignTo(otherChunkAlignment);
_childShards.append(otherChunkShards);
_contentSize += otherChunkSize;
}
ChunkBuilder* ChunkBuilder::addChunkAfter()
{
return getParentBlob()->addChunkAfter(this);
}
void ChunkBuilder::_writeRelativePtr(ChunkBuilder* targetChunk, Size ptrSize)
{
SLANG_ASSERT(ptrSize != 0);
SLANG_ASSERT(ptrSize <= sizeof(UInt64));
writePaddingToAlignTo(ptrSize);
if (!targetChunk)
{
UInt64 value = 0;
writeData(&value, ptrSize);
return;
}
auto shard = _createRelativePtrShard(targetChunk, ptrSize);
_childShards.add(shard);
_contentSize += ptrSize;
}
void ChunkBuilder::_addPrefixRelativePtr(ChunkBuilder* targetChunk, Size ptrSize)
{
SLANG_ASSERT(targetChunk != nullptr);
SLANG_ASSERT(ptrSize != 0);
SLANG_ASSERT(!_prefixShard);
setAlignmentToAtLeast(ptrSize);
auto shard = _createRelativePtrShard(targetChunk, ptrSize);
_prefixShard = shard;
}
void ChunkBuilder::addPrefixData(void const* data, Size size)
{
SLANG_ASSERT(data != nullptr);
SLANG_ASSERT(size != 0);
SLANG_ASSERT(!_prefixShard);
setAlignmentToAtLeast(size);
auto shard = _createDataShard(data, size);
_prefixShard = shard;
}
ShardBuilder* ChunkBuilder::_createDataShard(void const* data, Size size)
{
auto& arena = getParentBlob()->_getArena();
auto shard = new (arena) ShardBuilder(ShardBuilder::Kind::Data);
auto shardData = arena.allocateUnaligned(size);
::memcpy(shardData, data, size);
shard->_data.ptr = shardData;
shard->_size = size;
return shard;
}
ShardBuilder* ChunkBuilder::_createRelativePtrShard(ChunkBuilder* targetChunk, Size ptrSize)
{
auto& arena = getParentBlob()->_getArena();
auto shard = new (arena) ShardBuilder(ShardBuilder::Kind::RelativePtr);
shard->_relativePtr.targetChunk = targetChunk;
shard->_size = ptrSize;
return shard;
}
void ChunkBuilder::_writeTo(Stream* stream)
{
auto chunkOffset = _getCachedOffset();
if (_prefixShard)
{
// Note that the prefix is written *before* the
// starting offset of the chunk's content, so we
// compute the appropriate offset to pass down.
//
auto prefixSize = _prefixShard->getSize();
auto prefixOffset = chunkOffset - prefixSize;
_prefixShard->_writeTo(stream, prefixOffset);
}
auto shardOffset = chunkOffset;
for (auto shard : _childShards)
{
shard->_writeTo(stream, shardOffset);
shardOffset += shard->getSize();
}
SLANG_ASSERT(shardOffset == chunkOffset + getContentSize());
}
//
// ShardBuilder
//
ShardBuilder::ShardBuilder(Kind kind)
: _kind(kind)
{
}
void ShardBuilder::_writeTo(Stream* stream, Size inSelfOffset)
{
switch (_kind)
{
case Kind::Data:
{
stream->write(_data.ptr, _size);
}
break;
case Kind::RelativePtr:
{
auto targetChunk = _relativePtr.targetChunk;
SLANG_ASSERT(targetChunk);
auto selfOffset = intptr_t(inSelfOffset);
auto targetOffset = intptr_t(targetChunk->_getCachedOffset());
intptr_t relativeOffset = targetOffset - selfOffset;
switch (_size)
{
case sizeof(Int32):
{
auto value = Int32(relativeOffset);
stream->write(&value, sizeof(value));
}
break;
case sizeof(Int64):
{
auto value = Int64(relativeOffset);
stream->write(&value, sizeof(value));
}
break;
default:
SLANG_UNEXPECTED("unsupported relative pointer size");
break;
}
}
break;
default:
SLANG_UNEXPECTED("unknown Fossil::ShardBuilder::Kind");
break;
}
}
} // namespace Slang
|