summaryrefslogtreecommitdiffstats
path: root/mozglue/baseprofiler/public/ProfileBufferEntrySerialization.h
blob: bdb3b54d4156a274c4bb08c739a2b9409acf497b (plain)
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef ProfileBufferEntrySerialization_h
#define ProfileBufferEntrySerialization_h

#include "mozilla/Assertions.h"
#include "mozilla/leb128iterator.h"
#include "mozilla/Likely.h"
#include "mozilla/Maybe.h"
#include "mozilla/ProfileBufferIndex.h"
#include "mozilla/Span.h"
#include "mozilla/UniquePtrExtensions.h"
#include "mozilla/Unused.h"
#include "mozilla/Variant.h"

#include <string>
#include <tuple>

namespace mozilla {

class ProfileBufferEntryWriter;

// Iterator-like class used to read from an entry.
// An entry may be split in two memory segments (e.g., the ends of a ring
// buffer, or two chunks of a chunked buffer); it doesn't deal with this
// underlying buffer, but only with one or two spans pointing at the space
// where the entry lives.
class ProfileBufferEntryReader {
 public:
  using Byte = uint8_t;
  using Length = uint32_t;

  using SpanOfConstBytes = Span<const Byte>;

  // Class to be specialized for types to be read from a profile buffer entry.
  // See common specializations at the bottom of this header.
  // The following static functions must be provided:
  //   static void ReadInto(EntryReader aER&, T& aT)
  //   {
  //     /* Call `aER.ReadX(...)` function to deserialize into aT, be sure to
  //        read exactly `Bytes(aT)`! */
  //   }
  //   static T Read(EntryReader& aER) {
  //     /* Call `aER.ReadX(...)` function to deserialize and return a `T`, be
  //        sure to read exactly `Bytes(returned value)`! */
  //   }
  template <typename T>
  struct Deserializer;

  ProfileBufferEntryReader() = default;

  // Reader over one Span.
  ProfileBufferEntryReader(SpanOfConstBytes aSpan,
                           ProfileBufferBlockIndex aCurrentBlockIndex,
                           ProfileBufferBlockIndex aNextBlockIndex)
      : mCurrentSpan(aSpan),
        mNextSpanOrEmpty(aSpan.Last(0)),
        mCurrentBlockIndex(aCurrentBlockIndex),
        mNextBlockIndex(aNextBlockIndex) {
    // 2nd internal Span points at the end of the 1st internal Span, to enforce
    // invariants.
    CheckInvariants();
  }

  // Reader over two Spans, the second one must not be empty.
  ProfileBufferEntryReader(SpanOfConstBytes aSpanHead,
                           SpanOfConstBytes aSpanTail,
                           ProfileBufferBlockIndex aCurrentBlockIndex,
                           ProfileBufferBlockIndex aNextBlockIndex)
      : mCurrentSpan(aSpanHead),
        mNextSpanOrEmpty(aSpanTail),
        mCurrentBlockIndex(aCurrentBlockIndex),
        mNextBlockIndex(aNextBlockIndex) {
    MOZ_RELEASE_ASSERT(!mNextSpanOrEmpty.IsEmpty());
    if (MOZ_UNLIKELY(mCurrentSpan.IsEmpty())) {
      // First span is already empty, skip it.
      mCurrentSpan = mNextSpanOrEmpty;
      mNextSpanOrEmpty = mNextSpanOrEmpty.Last(0);
    }
    CheckInvariants();
  }

  // Allow copying, which is needed when used as an iterator in some std
  // functions (e.g., string assignment), and to occasionally backtrack.
  // Be aware that the main profile buffer APIs give a reference to an entry
  // reader, and expect that reader to advance to the end of the entry, so don't
  // just advance copies!
  ProfileBufferEntryReader(const ProfileBufferEntryReader&) = default;
  ProfileBufferEntryReader& operator=(const ProfileBufferEntryReader&) =
      default;

  // Don't =default moving, as it doesn't bring any benefit in this class.

  [[nodiscard]] Length RemainingBytes() const {
    return mCurrentSpan.LengthBytes() + mNextSpanOrEmpty.LengthBytes();
  }

  void SetRemainingBytes(Length aBytes) {
    MOZ_RELEASE_ASSERT(aBytes <= RemainingBytes());
    if (aBytes <= mCurrentSpan.LengthBytes()) {
      mCurrentSpan = mCurrentSpan.First(aBytes);
      mNextSpanOrEmpty = mCurrentSpan.Last(0);
    } else {
      mNextSpanOrEmpty =
          mNextSpanOrEmpty.First(aBytes - mCurrentSpan.LengthBytes());
    }
  }

  [[nodiscard]] ProfileBufferBlockIndex CurrentBlockIndex() const {
    return mCurrentBlockIndex;
  }

  [[nodiscard]] ProfileBufferBlockIndex NextBlockIndex() const {
    return mNextBlockIndex;
  }

  // Create a reader of size zero, pointing at aOffset past the current position
  // of this Reader, so it can be used as end iterator.
  [[nodiscard]] ProfileBufferEntryReader EmptyIteratorAtOffset(
      Length aOffset) const {
    MOZ_RELEASE_ASSERT(aOffset <= RemainingBytes());
    if (MOZ_LIKELY(aOffset < mCurrentSpan.LengthBytes())) {
      // aOffset is before the end of mCurrentSpan.
      return ProfileBufferEntryReader(mCurrentSpan.Subspan(aOffset, 0),
                                      mCurrentBlockIndex, mNextBlockIndex);
    }
    // aOffset is right at the end of mCurrentSpan, or inside mNextSpanOrEmpty.
    return ProfileBufferEntryReader(
        mNextSpanOrEmpty.Subspan(aOffset - mCurrentSpan.LengthBytes(), 0),
        mCurrentBlockIndex, mNextBlockIndex);
  }

  // Be like a limited input iterator, with only `*`, prefix-`++`, `==`, `!=`.
  // These definitions are expected by std functions, to recognize this as an
  // iterator. See https://en.cppreference.com/w/cpp/iterator/iterator_traits
  using difference_type = std::make_signed_t<Length>;
  using value_type = Byte;
  using pointer = const Byte*;
  using reference = const Byte&;
  using iterator_category = std::input_iterator_tag;

  [[nodiscard]] const Byte& operator*() {
    // Assume the caller will read from the returned reference (and not just
    // take the address).
    MOZ_RELEASE_ASSERT(mCurrentSpan.LengthBytes() >= 1);
    return *(mCurrentSpan.Elements());
  }

  ProfileBufferEntryReader& operator++() {
    MOZ_RELEASE_ASSERT(mCurrentSpan.LengthBytes() >= 1);
    if (MOZ_LIKELY(mCurrentSpan.LengthBytes() > 1)) {
      // More than 1 byte left in mCurrentSpan, just eat it.
      mCurrentSpan = mCurrentSpan.From(1);
    } else {
      // mCurrentSpan will be empty, move mNextSpanOrEmpty to mCurrentSpan.
      mCurrentSpan = mNextSpanOrEmpty;
      mNextSpanOrEmpty = mNextSpanOrEmpty.Last(0);
    }
    CheckInvariants();
    return *this;
  }

  ProfileBufferEntryReader& operator+=(Length aBytes) {
    MOZ_RELEASE_ASSERT(aBytes <= RemainingBytes());
    if (MOZ_LIKELY(aBytes <= mCurrentSpan.LengthBytes())) {
      // All bytes are in mCurrentSpan.
      // Update mCurrentSpan past the read bytes.
      mCurrentSpan = mCurrentSpan.From(aBytes);
      if (mCurrentSpan.IsEmpty() && !mNextSpanOrEmpty.IsEmpty()) {
        // Don't leave mCurrentSpan empty, move non-empty mNextSpanOrEmpty into
        // mCurrentSpan.
        mCurrentSpan = mNextSpanOrEmpty;
        mNextSpanOrEmpty = mNextSpanOrEmpty.Last(0);
      }
    } else {
      // mCurrentSpan does not hold enough bytes.
      // This should only happen at most once: Only for double spans, and when
      // data crosses the gap.
      const Length tail =
          aBytes - static_cast<Length>(mCurrentSpan.LengthBytes());
      // Move mNextSpanOrEmpty to mCurrentSpan, past the data. So the next call
      // will go back to the true case above.
      mCurrentSpan = mNextSpanOrEmpty.From(tail);
      mNextSpanOrEmpty = mNextSpanOrEmpty.Last(0);
    }
    CheckInvariants();
    return *this;
  }

  [[nodiscard]] bool operator==(const ProfileBufferEntryReader& aOther) const {
    return mCurrentSpan.Elements() == aOther.mCurrentSpan.Elements();
  }
  [[nodiscard]] bool operator!=(const ProfileBufferEntryReader& aOther) const {
    return mCurrentSpan.Elements() != aOther.mCurrentSpan.Elements();
  }

  // Read an unsigned LEB128 number and move iterator ahead.
  template <typename T>
  [[nodiscard]] T ReadULEB128() {
    return ::mozilla::ReadULEB128<T>(*this);
  }

  // This struct points at a number of bytes through either one span, or two
  // separate spans (in the rare cases when it is split between two chunks).
  // So the possibilities are:
  // - Totally empty: { [] [] }
  // - First span is not empty: { [content] [] } (Most common case.)
  // - Both spans are not empty: { [cont] [ent] }
  // But something like { [] [content] } is not possible.
  //
  // Recommended usage patterns:
  // - Call a utility function like `CopyBytesTo` if you always need to copy the
  //   data to an outside buffer, e.g., to deserialize an aligned object.
  // - Access both spans one after the other; Note that the second one may be
  //   empty; and the fist could be empty as well if there is no data at all.
  // - Check is the second span is empty, in which case you only need to read
  //   the first one; and since its part of a chunk, it may be directly passed
  //   as an unaligned pointer or reference, thereby saving one copy. But
  //   remember to always handle the double-span case as well.
  //
  // Reminder: An empty span still has a non-null pointer, so it's safe to use
  // with functions like memcpy.
  struct DoubleSpanOfConstBytes {
    SpanOfConstBytes mFirstOrOnly;
    SpanOfConstBytes mSecondOrEmpty;

    void CheckInvariants() const {
      MOZ_ASSERT(mFirstOrOnly.IsEmpty() ? mSecondOrEmpty.IsEmpty() : true,
                 "mSecondOrEmpty should not be the only span to contain data");
    }

    DoubleSpanOfConstBytes() : mFirstOrOnly(), mSecondOrEmpty() {
      CheckInvariants();
    }

    DoubleSpanOfConstBytes(const Byte* aOnlyPointer, size_t aOnlyLength)
        : mFirstOrOnly(aOnlyPointer, aOnlyLength), mSecondOrEmpty() {
      CheckInvariants();
    }

    DoubleSpanOfConstBytes(const Byte* aFirstPointer, size_t aFirstLength,
                           const Byte* aSecondPointer, size_t aSecondLength)
        : mFirstOrOnly(aFirstPointer, aFirstLength),
          mSecondOrEmpty(aSecondPointer, aSecondLength) {
      CheckInvariants();
    }

    // Is there no data at all?
    [[nodiscard]] bool IsEmpty() const {
      // We only need to check the first span, because if it's empty, the second
      // one must be empty as well.
      return mFirstOrOnly.IsEmpty();
    }

    // Total length (in bytes) pointed at by both spans.
    [[nodiscard]] size_t LengthBytes() const {
      return mFirstOrOnly.LengthBytes() + mSecondOrEmpty.LengthBytes();
    }

    // Utility functions to copy all `LengthBytes()` to a given buffer.
    void CopyBytesTo(void* aDest) const {
      memcpy(aDest, mFirstOrOnly.Elements(), mFirstOrOnly.LengthBytes());
      if (MOZ_UNLIKELY(!mSecondOrEmpty.IsEmpty())) {
        memcpy(static_cast<Byte*>(aDest) + mFirstOrOnly.LengthBytes(),
               mSecondOrEmpty.Elements(), mSecondOrEmpty.LengthBytes());
      }
    }

    // If the second span is empty, only the first span may point at data.
    [[nodiscard]] bool IsSingleSpan() const { return mSecondOrEmpty.IsEmpty(); }
  };

  // Get Span(s) to a sequence of bytes, see `DoubleSpanOfConstBytes` for usage.
  // Note that the reader location is *not* updated, do `+=` on it afterwards.
  [[nodiscard]] DoubleSpanOfConstBytes PeekSpans(Length aBytes) const {
    MOZ_RELEASE_ASSERT(aBytes <= RemainingBytes());
    if (MOZ_LIKELY(aBytes <= mCurrentSpan.LengthBytes())) {
      // All `aBytes` are in the current chunk, only one span is needed.
      return DoubleSpanOfConstBytes{mCurrentSpan.Elements(), aBytes};
    }
    // Otherwise the first span covers then end of the current chunk, and the
    // second span starts in the next chunk.
    return DoubleSpanOfConstBytes{
        mCurrentSpan.Elements(), mCurrentSpan.LengthBytes(),
        mNextSpanOrEmpty.Elements(), aBytes - mCurrentSpan.LengthBytes()};
  }

  // Get Span(s) to a sequence of bytes, see `DoubleSpanOfConstBytes` for usage,
  // and move the reader forward.
  [[nodiscard]] DoubleSpanOfConstBytes ReadSpans(Length aBytes) {
    DoubleSpanOfConstBytes spans = PeekSpans(aBytes);
    (*this) += aBytes;
    return spans;
  }

  // Read a sequence of bytes, like memcpy.
  void ReadBytes(void* aDest, Length aBytes) {
    DoubleSpanOfConstBytes spans = ReadSpans(aBytes);
    MOZ_ASSERT(spans.LengthBytes() == aBytes);
    spans.CopyBytesTo(aDest);
  }

  template <typename T>
  void ReadIntoObject(T& aObject) {
    Deserializer<T>::ReadInto(*this, aObject);
  }

  // Read into one or more objects, sequentially.
  // `EntryReader::ReadIntoObjects()` with nothing is implicitly allowed, this
  // could be useful for generic programming.
  template <typename... Ts>
  void ReadIntoObjects(Ts&... aTs) {
    (ReadIntoObject(aTs), ...);
  }

  // Read data as an object and move iterator ahead.
  template <typename T>
  [[nodiscard]] T ReadObject() {
    T ob = Deserializer<T>::Read(*this);
    return ob;
  }

 private:
  friend class ProfileBufferEntryWriter;

  // Invariants:
  // - mCurrentSpan cannot be empty unless mNextSpanOrEmpty is also empty. So
  //   mCurrentSpan always points at the next byte to read or the end.
  // - If mNextSpanOrEmpty is empty, it points at the end of mCurrentSpan. So
  //   when reaching the end of mCurrentSpan, we can blindly move
  //   mNextSpanOrEmpty to mCurrentSpan and keep the invariants.
  SpanOfConstBytes mCurrentSpan;
  SpanOfConstBytes mNextSpanOrEmpty;
  ProfileBufferBlockIndex mCurrentBlockIndex;
  ProfileBufferBlockIndex mNextBlockIndex;

  void CheckInvariants() const {
    MOZ_ASSERT(!mCurrentSpan.IsEmpty() || mNextSpanOrEmpty.IsEmpty());
    MOZ_ASSERT(!mNextSpanOrEmpty.IsEmpty() ||
               (mNextSpanOrEmpty == mCurrentSpan.Last(0)));
  }
};

// Iterator-like class used to write into an entry.
// An entry may be split in two memory segments (e.g., the ends of a ring
// buffer, or two chunks of a chunked buffer); it doesn't deal with this
// underlying buffer, but only with one or two spans pointing at the space
// reserved for the entry.
class ProfileBufferEntryWriter {
 public:
  using Byte = uint8_t;
  using Length = uint32_t;

  using SpanOfBytes = Span<Byte>;

  // Class to be specialized for types to be written in an entry.
  // See common specializations at the bottom of this header.
  // The following static functions must be provided:
  //   static Length Bytes(const T& aT) {
  //     /* Return number of bytes that will be written. */
  //   }
  //   static void Write(ProfileBufferEntryWriter& aEW,
  //                     const T& aT) {
  //     /* Call `aEW.WriteX(...)` functions to serialize aT, be sure to write
  //        exactly `Bytes(aT)` bytes! */
  //   }
  template <typename T>
  struct Serializer;

  ProfileBufferEntryWriter() = default;

  ProfileBufferEntryWriter(SpanOfBytes aSpan,
                           ProfileBufferBlockIndex aCurrentBlockIndex,
                           ProfileBufferBlockIndex aNextBlockIndex)
      : mCurrentSpan(aSpan),
        mCurrentBlockIndex(aCurrentBlockIndex),
        mNextBlockIndex(aNextBlockIndex) {}

  ProfileBufferEntryWriter(SpanOfBytes aSpanHead, SpanOfBytes aSpanTail,
                           ProfileBufferBlockIndex aCurrentBlockIndex,
                           ProfileBufferBlockIndex aNextBlockIndex)
      : mCurrentSpan(aSpanHead),
        mNextSpanOrEmpty(aSpanTail),
        mCurrentBlockIndex(aCurrentBlockIndex),
        mNextBlockIndex(aNextBlockIndex) {
    // Either:
    // - mCurrentSpan is not empty, OR
    // - mNextSpanOrEmpty is empty if mNextSpanOrEmpty is empty as well.
    MOZ_RELEASE_ASSERT(!mCurrentSpan.IsEmpty() || mNextSpanOrEmpty.IsEmpty());
  }

  // Disable copying and moving, so we can't have multiple writing heads.
  ProfileBufferEntryWriter(const ProfileBufferEntryWriter&) = delete;
  ProfileBufferEntryWriter& operator=(const ProfileBufferEntryWriter&) = delete;
  ProfileBufferEntryWriter(ProfileBufferEntryWriter&&) = delete;
  ProfileBufferEntryWriter& operator=(ProfileBufferEntryWriter&&) = delete;

  void Set() {
    mCurrentSpan = SpanOfBytes{};
    mNextSpanOrEmpty = SpanOfBytes{};
    mCurrentBlockIndex = nullptr;
    mNextBlockIndex = nullptr;
  }

  void Set(SpanOfBytes aSpan, ProfileBufferBlockIndex aCurrentBlockIndex,
           ProfileBufferBlockIndex aNextBlockIndex) {
    mCurrentSpan = aSpan;
    mNextSpanOrEmpty = SpanOfBytes{};
    mCurrentBlockIndex = aCurrentBlockIndex;
    mNextBlockIndex = aNextBlockIndex;
  }

  void Set(SpanOfBytes aSpan0, SpanOfBytes aSpan1,
           ProfileBufferBlockIndex aCurrentBlockIndex,
           ProfileBufferBlockIndex aNextBlockIndex) {
    mCurrentSpan = aSpan0;
    mNextSpanOrEmpty = aSpan1;
    mCurrentBlockIndex = aCurrentBlockIndex;
    mNextBlockIndex = aNextBlockIndex;
    // Either:
    // - mCurrentSpan is not empty, OR
    // - mNextSpanOrEmpty is empty if mNextSpanOrEmpty is empty as well.
    MOZ_RELEASE_ASSERT(!mCurrentSpan.IsEmpty() || mNextSpanOrEmpty.IsEmpty());
  }

  [[nodiscard]] Length RemainingBytes() const {
    return mCurrentSpan.LengthBytes() + mNextSpanOrEmpty.LengthBytes();
  }

  [[nodiscard]] ProfileBufferBlockIndex CurrentBlockIndex() const {
    return mCurrentBlockIndex;
  }

  [[nodiscard]] ProfileBufferBlockIndex NextBlockIndex() const {
    return mNextBlockIndex;
  }

  // Be like a limited output iterator, with only `*` and prefix-`++`.
  // These definitions are expected by std functions, to recognize this as an
  // iterator. See https://en.cppreference.com/w/cpp/iterator/iterator_traits
  using value_type = Byte;
  using pointer = Byte*;
  using reference = Byte&;
  using iterator_category = std::output_iterator_tag;

  [[nodiscard]] Byte& operator*() {
    MOZ_RELEASE_ASSERT(RemainingBytes() >= 1);
    return *(
        (MOZ_LIKELY(!mCurrentSpan.IsEmpty()) ? mCurrentSpan : mNextSpanOrEmpty)
            .Elements());
  }

  ProfileBufferEntryWriter& operator++() {
    if (MOZ_LIKELY(mCurrentSpan.LengthBytes() >= 1)) {
      // There is at least 1 byte in mCurrentSpan, eat it.
      mCurrentSpan = mCurrentSpan.From(1);
    } else {
      // mCurrentSpan is empty, move mNextSpanOrEmpty (past the first byte) to
      // mCurrentSpan.
      MOZ_RELEASE_ASSERT(mNextSpanOrEmpty.LengthBytes() >= 1);
      mCurrentSpan = mNextSpanOrEmpty.From(1);
      mNextSpanOrEmpty = mNextSpanOrEmpty.First(0);
    }
    return *this;
  }

  ProfileBufferEntryWriter& operator+=(Length aBytes) {
    // Note: This is a rare operation. The code below is a copy of `WriteBytes`
    // but without the `memcpy`s.
    MOZ_RELEASE_ASSERT(aBytes <= RemainingBytes());
    if (MOZ_LIKELY(aBytes <= mCurrentSpan.LengthBytes())) {
      // Data fits in mCurrentSpan.
      // Update mCurrentSpan. It may become empty, so in case of a double span,
      // the next call will go to the false case below.
      mCurrentSpan = mCurrentSpan.From(aBytes);
    } else {
      // Data does not fully fit in mCurrentSpan.
      // This should only happen at most once: Only for double spans, and when
      // data crosses the gap or starts there.
      const Length tail =
          aBytes - static_cast<Length>(mCurrentSpan.LengthBytes());
      // Move mNextSpanOrEmpty to mCurrentSpan, past the data. So the next call
      // will go back to the true case above.
      mCurrentSpan = mNextSpanOrEmpty.From(tail);
      mNextSpanOrEmpty = mNextSpanOrEmpty.First(0);
    }
    return *this;
  }

  // Number of bytes needed to represent `aValue` in unsigned LEB128.
  template <typename T>
  [[nodiscard]] static unsigned ULEB128Size(T aValue) {
    return ::mozilla::ULEB128Size(aValue);
  }

  // Write number as unsigned LEB128 and move iterator ahead.
  template <typename T>
  void WriteULEB128(T aValue) {
    ::mozilla::WriteULEB128(aValue, *this);
  }

  // Number of bytes needed to serialize objects.
  template <typename... Ts>
  [[nodiscard]] static Length SumBytes(const Ts&... aTs) {
    return (0 + ... + Serializer<Ts>::Bytes(aTs));
  }

  // Write a sequence of bytes, like memcpy.
  void WriteBytes(const void* aSrc, Length aBytes) {
    MOZ_RELEASE_ASSERT(aBytes <= RemainingBytes());
    if (MOZ_LIKELY(aBytes <= mCurrentSpan.LengthBytes())) {
      // Data fits in mCurrentSpan.
      memcpy(mCurrentSpan.Elements(), aSrc, aBytes);
      // Update mCurrentSpan. It may become empty, so in case of a double span,
      // the next call will go to the false case below.
      mCurrentSpan = mCurrentSpan.From(aBytes);
    } else {
      // Data does not fully fit in mCurrentSpan.
      // This should only happen at most once: Only for double spans, and when
      // data crosses the gap or starts there.
      // Split data between the end of mCurrentSpan and the beginning of
      // mNextSpanOrEmpty. (mCurrentSpan could be empty, it's ok to do a memcpy
      // because Span::Elements() is never null.)
      memcpy(mCurrentSpan.Elements(), aSrc, mCurrentSpan.LengthBytes());
      const Length tail =
          aBytes - static_cast<Length>(mCurrentSpan.LengthBytes());
      memcpy(mNextSpanOrEmpty.Elements(),
             reinterpret_cast<const Byte*>(aSrc) + mCurrentSpan.LengthBytes(),
             tail);
      // Move mNextSpanOrEmpty to mCurrentSpan, past the data. So the next call
      // will go back to the true case above.
      mCurrentSpan = mNextSpanOrEmpty.From(tail);
      mNextSpanOrEmpty = mNextSpanOrEmpty.First(0);
    }
  }

  void WriteFromReader(ProfileBufferEntryReader& aReader, Length aBytes) {
    MOZ_RELEASE_ASSERT(aBytes <= RemainingBytes());
    MOZ_RELEASE_ASSERT(aBytes <= aReader.RemainingBytes());
    Length read0 = std::min(
        aBytes, static_cast<Length>(aReader.mCurrentSpan.LengthBytes()));
    if (read0 != 0) {
      WriteBytes(aReader.mCurrentSpan.Elements(), read0);
    }
    Length read1 = aBytes - read0;
    if (read1 != 0) {
      WriteBytes(aReader.mNextSpanOrEmpty.Elements(), read1);
    }
    aReader += aBytes;
  }

  // Write a single object by using the appropriate Serializer.
  template <typename T>
  void WriteObject(const T& aObject) {
    Serializer<T>::Write(*this, aObject);
  }

  // Write one or more objects, sequentially.
  // Allow `EntryWrite::WriteObjects()` with nothing, this could be useful
  // for generic programming.
  template <typename... Ts>
  void WriteObjects(const Ts&... aTs) {
    (WriteObject(aTs), ...);
  }

 private:
  // The two spans covering the memory still to be written.
  SpanOfBytes mCurrentSpan;
  SpanOfBytes mNextSpanOrEmpty;
  ProfileBufferBlockIndex mCurrentBlockIndex;
  ProfileBufferBlockIndex mNextBlockIndex;
};

// ============================================================================
// Serializer and Deserializer ready-to-use specializations.

// ----------------------------------------------------------------------------
// Trivially-copyable types (default)

// The default implementation works for all trivially-copyable types (e.g.,
// PODs).
//
// Usage: `aEW.WriteObject(123);`.
//
// Raw pointers, though trivially-copyable, are explictly forbidden when writing
// (to avoid unexpected leaks/UAFs), instead use one of
// `WrapProfileBufferLiteralCStringPointer`, `WrapProfileBufferUnownedCString`,
// or `WrapProfileBufferRawPointer` as needed.
template <typename T>
struct ProfileBufferEntryWriter::Serializer {
  static_assert(std::is_trivially_copyable<T>::value,
                "Serializer only works with trivially-copyable types by "
                "default, use/add specialization for other types.");

  static constexpr Length Bytes(const T&) { return sizeof(T); }

  static void Write(ProfileBufferEntryWriter& aEW, const T& aT) {
    static_assert(!std::is_pointer<T>::value,
                  "Serializer won't write raw pointers by default, use "
                  "WrapProfileBufferRawPointer or other.");
    aEW.WriteBytes(&aT, sizeof(T));
  }
};

// Usage: `aER.ReadObject<int>();` or `int x; aER.ReadIntoObject(x);`.
template <typename T>
struct ProfileBufferEntryReader::Deserializer {
  static_assert(std::is_trivially_copyable<T>::value,
                "Deserializer only works with trivially-copyable types by "
                "default, use/add specialization for other types.");

  static void ReadInto(ProfileBufferEntryReader& aER, T& aT) {
    aER.ReadBytes(&aT, sizeof(T));
  }

  static T Read(ProfileBufferEntryReader& aER) {
    // Note that this creates a default `T` first, and then overwrites it with
    // bytes from the buffer. Trivially-copyable types support this without UB.
    T ob;
    ReadInto(aER, ob);
    return ob;
  }
};

// ----------------------------------------------------------------------------
// Strip const/volatile/reference from types.

// Automatically strip `const`.
template <typename T>
struct ProfileBufferEntryWriter::Serializer<const T>
    : public ProfileBufferEntryWriter::Serializer<T> {};

template <typename T>
struct ProfileBufferEntryReader::Deserializer<const T>
    : public ProfileBufferEntryReader::Deserializer<T> {};

// Automatically strip `volatile`.
template <typename T>
struct ProfileBufferEntryWriter::Serializer<volatile T>
    : public ProfileBufferEntryWriter::Serializer<T> {};

template <typename T>
struct ProfileBufferEntryReader::Deserializer<volatile T>
    : public ProfileBufferEntryReader::Deserializer<T> {};

// Automatically strip `lvalue-reference`.
template <typename T>
struct ProfileBufferEntryWriter::Serializer<T&>
    : public ProfileBufferEntryWriter::Serializer<T> {};

template <typename T>
struct ProfileBufferEntryReader::Deserializer<T&>
    : public ProfileBufferEntryReader::Deserializer<T> {};

// Automatically strip `rvalue-reference`.
template <typename T>
struct ProfileBufferEntryWriter::Serializer<T&&>
    : public ProfileBufferEntryWriter::Serializer<T> {};

template <typename T>
struct ProfileBufferEntryReader::Deserializer<T&&>
    : public ProfileBufferEntryReader::Deserializer<T> {};

// ----------------------------------------------------------------------------
// ProfileBufferBlockIndex

// ProfileBufferBlockIndex, serialized as the underlying value.
template <>
struct ProfileBufferEntryWriter::Serializer<ProfileBufferBlockIndex> {
  static constexpr Length Bytes(const ProfileBufferBlockIndex& aBlockIndex) {
    return sizeof(ProfileBufferBlockIndex);
  }

  static void Write(ProfileBufferEntryWriter& aEW,
                    const ProfileBufferBlockIndex& aBlockIndex) {
    aEW.WriteBytes(&aBlockIndex, sizeof(aBlockIndex));
  }
};

template <>
struct ProfileBufferEntryReader::Deserializer<ProfileBufferBlockIndex> {
  static void ReadInto(ProfileBufferEntryReader& aER,
                       ProfileBufferBlockIndex& aBlockIndex) {
    aER.ReadBytes(&aBlockIndex, sizeof(aBlockIndex));
  }

  static ProfileBufferBlockIndex Read(ProfileBufferEntryReader& aER) {
    ProfileBufferBlockIndex blockIndex;
    ReadInto(aER, blockIndex);
    return blockIndex;
  }
};

// ----------------------------------------------------------------------------
// Literal C string pointer

// Wrapper around a pointer to a literal C string.
template <size_t NonTerminalCharacters>
struct ProfileBufferLiteralCStringPointer {
  const char* mCString;
};

// Wrap a pointer to a literal C string.
template <size_t CharactersIncludingTerminal>
ProfileBufferLiteralCStringPointer<CharactersIncludingTerminal - 1>
WrapProfileBufferLiteralCStringPointer(
    const char (&aCString)[CharactersIncludingTerminal]) {
  return {aCString};
}

// Literal C strings, serialized as the raw pointer because it is unique and
// valid for the whole program lifetime.
//
// Usage: `aEW.WriteObject(WrapProfileBufferLiteralCStringPointer("hi"));`.
//
// No deserializer is provided for this type, instead it must be deserialized as
// a raw pointer: `aER.ReadObject<const char*>();`
template <size_t CharactersIncludingTerminal>
struct ProfileBufferEntryReader::Deserializer<
    ProfileBufferLiteralCStringPointer<CharactersIncludingTerminal>> {
  static constexpr Length Bytes(
      const ProfileBufferLiteralCStringPointer<CharactersIncludingTerminal>&) {
    // We're only storing a pointer, its size is independent from the pointer
    // value.
    return sizeof(const char*);
  }

  static void Write(
      ProfileBufferEntryWriter& aEW,
      const ProfileBufferLiteralCStringPointer<CharactersIncludingTerminal>&
          aWrapper) {
    // Write the pointer *value*, not the string contents.
    aEW.WriteBytes(aWrapper.mCString, sizeof(aWrapper.mCString));
  }
};

// ----------------------------------------------------------------------------
// C string contents

// Wrapper around a pointer to a C string whose contents will be serialized.
struct ProfileBufferUnownedCString {
  const char* mCString;
};

// Wrap a pointer to a C string whose contents will be serialized.
inline ProfileBufferUnownedCString WrapProfileBufferUnownedCString(
    const char* aCString) {
  return {aCString};
}

// The contents of a (probably) unowned C string are serialized as the number of
// characters (encoded as ULEB128) and all the characters in the string. The
// terminal '\0' is omitted.
//
// Usage: `aEW.WriteObject(WrapProfileBufferUnownedCString(str.c_str()))`.
//
// No deserializer is provided for this pointer type, instead it must be
// deserialized as one of the other string types that manages its contents,
// e.g.: `aER.ReadObject<std::string>();`
template <>
struct ProfileBufferEntryWriter::Serializer<ProfileBufferUnownedCString> {
  static Length Bytes(const ProfileBufferUnownedCString& aS) {
    const auto len = strlen(aS.mCString);
    return ULEB128Size(len) + len;
  }

  static void Write(ProfileBufferEntryWriter& aEW,
                    const ProfileBufferUnownedCString& aS) {
    const auto len = strlen(aS.mCString);
    aEW.WriteULEB128(len);
    aEW.WriteBytes(aS.mCString, len);
  }
};

// ----------------------------------------------------------------------------
// Raw pointers

// Wrapper around a pointer to be serialized as the raw pointer value.
template <typename T>
struct ProfileBufferRawPointer {
  T* mRawPointer;
};

// Wrap a pointer to be serialized as the raw pointer value.
template <typename T>
ProfileBufferRawPointer<T> WrapProfileBufferRawPointer(T* aRawPointer) {
  return {aRawPointer};
}

// Raw pointers are serialized as the raw pointer value.
//
// Usage: `aEW.WriteObject(WrapProfileBufferRawPointer(ptr));`
//
// The wrapper is compulsory when writing pointers (to avoid unexpected
// leaks/UAFs), but reading can be done straight into a raw pointer object,
// e.g.: `aER.ReadObject<Foo*>;`.
template <typename T>
struct ProfileBufferEntryWriter::Serializer<ProfileBufferRawPointer<T>> {
  template <typename U>
  static constexpr Length Bytes(const U&) {
    return sizeof(T*);
  }

  static void Write(ProfileBufferEntryWriter& aEW,
                    const ProfileBufferRawPointer<T>& aWrapper) {
    aEW.WriteBytes(&aWrapper.mRawPointer, sizeof(aWrapper.mRawPointer));
  }
};

// Usage: `aER.ReadObject<Foo*>;` or `Foo* p; aER.ReadIntoObject(p);`, no
// wrapper necessary.
template <typename T>
struct ProfileBufferEntryReader::Deserializer<ProfileBufferRawPointer<T>> {
  static void ReadInto(ProfileBufferEntryReader& aER,
                       ProfileBufferRawPointer<T>& aPtr) {
    aER.ReadBytes(&aPtr.mRawPointer, sizeof(aPtr));
  }

  static ProfileBufferRawPointer<T> Read(ProfileBufferEntryReader& aER) {
    ProfileBufferRawPointer<T> rawPointer;
    ReadInto(aER, rawPointer);
    return rawPointer;
  }
};

// ----------------------------------------------------------------------------
// std::string contents

// std::string contents are serialized as the number of characters (encoded as
// ULEB128) and all the characters in the string. The terminal '\0' is omitted.
//
// Usage: `std::string s = ...; aEW.WriteObject(s);`
template <typename CHAR>
struct ProfileBufferEntryWriter::Serializer<std::basic_string<CHAR>> {
  static Length Bytes(const std::basic_string<CHAR>& aS) {
    const Length len = static_cast<Length>(aS.length());
    return ULEB128Size(len) + len;
  }

  static void Write(ProfileBufferEntryWriter& aEW,
                    const std::basic_string<CHAR>& aS) {
    const Length len = static_cast<Length>(aS.length());
    aEW.WriteULEB128(len);
    aEW.WriteBytes(aS.c_str(), len * sizeof(CHAR));
  }
};

// Usage: `std::string s = aEW.ReadObject<std::string>(s);` or
// `std::string s; aER.ReadIntoObject(s);`
template <typename CHAR>
struct ProfileBufferEntryReader::Deserializer<std::basic_string<CHAR>> {
  static void ReadCharsInto(ProfileBufferEntryReader& aER,
                            std::basic_string<CHAR>& aS, size_t aLength) {
    // Assign to `aS` by using iterators.
    // (`aER+0` so we get the same iterator type as `aER+len`.)
    aS.assign(aER, aER.EmptyIteratorAtOffset(aLength));
    aER += aLength;
  }

  static void ReadInto(ProfileBufferEntryReader& aER,
                       std::basic_string<CHAR>& aS) {
    ReadCharsInto(
        aER, aS,
        aER.ReadULEB128<typename std::basic_string<CHAR>::size_type>());
  }

  static std::basic_string<CHAR> ReadChars(ProfileBufferEntryReader& aER,
                                           size_t aLength) {
    // Construct a string by using iterators.
    // (`aER+0` so we get the same iterator type as `aER+len`.)
    std::basic_string<CHAR> s(aER, aER.EmptyIteratorAtOffset(aLength));
    aER += aLength;
    return s;
  }

  static std::basic_string<CHAR> Read(ProfileBufferEntryReader& aER) {
    return ReadChars(
        aER, aER.ReadULEB128<typename std::basic_string<CHAR>::size_type>());
  }
};

// ----------------------------------------------------------------------------
// mozilla::UniqueFreePtr<CHAR>

// UniqueFreePtr<CHAR>, which points at a string allocated with `malloc`
// (typically generated by `strdup()`), is serialized as the number of
// *bytes* (encoded as ULEB128) and all the characters in the string. The
// null terminator is omitted.
// `CHAR` can be any type that has a specialization for
// `std::char_traits<CHAR>::length(const CHAR*)`.
//
// Note: A nullptr pointer will be serialized like an empty string, so when
// deserializing it will result in an allocated buffer only containing a
// single null terminator.
template <typename CHAR>
struct ProfileBufferEntryWriter::Serializer<UniqueFreePtr<CHAR>> {
  static Length Bytes(const UniqueFreePtr<CHAR>& aS) {
    if (!aS) {
      // Null pointer, store it as if it was an empty string (so: 0 bytes).
      return ULEB128Size(0u);
    }
    // Note that we store the size in *bytes*, not in number of characters.
    const auto bytes = std::char_traits<CHAR>::length(aS.get()) * sizeof(CHAR);
    return ULEB128Size(bytes) + bytes;
  }

  static void Write(ProfileBufferEntryWriter& aEW,
                    const UniqueFreePtr<CHAR>& aS) {
    if (!aS) {
      // Null pointer, store it as if it was an empty string (so we write a
      // length of 0 bytes).
      aEW.WriteULEB128(0u);
      return;
    }
    // Note that we store the size in *bytes*, not in number of characters.
    const auto bytes = std::char_traits<CHAR>::length(aS.get()) * sizeof(CHAR);
    aEW.WriteULEB128(bytes);
    aEW.WriteBytes(aS.get(), bytes);
  }
};

template <typename CHAR>
struct ProfileBufferEntryReader::Deserializer<UniqueFreePtr<CHAR>> {
  static void ReadInto(ProfileBufferEntryReader& aER, UniqueFreePtr<CHAR>& aS) {
    aS = Read(aER);
  }

  static UniqueFreePtr<CHAR> Read(ProfileBufferEntryReader& aER) {
    // Read the number of *bytes* that follow.
    const auto bytes = aER.ReadULEB128<size_t>();
    // We need a buffer of the non-const character type.
    using NC_CHAR = std::remove_const_t<CHAR>;
    // We allocate the required number of bytes, plus one extra character for
    // the null terminator.
    NC_CHAR* buffer = static_cast<NC_CHAR*>(malloc(bytes + sizeof(NC_CHAR)));
    // Copy the characters into the buffer.
    aER.ReadBytes(buffer, bytes);
    // And append a null terminator.
    buffer[bytes / sizeof(NC_CHAR)] = NC_CHAR(0);
    return UniqueFreePtr<CHAR>(buffer);
  }
};

// ----------------------------------------------------------------------------
// std::tuple

// std::tuple is serialized as a sequence of each recursively-serialized item.
//
// This is equivalent to manually serializing each item, so reading/writing
// tuples is equivalent to reading/writing their elements in order, e.g.:
// ```
// std::tuple<int, std::string> is = ...;
// aEW.WriteObject(is); // Write the tuple, equivalent to:
// aEW.WriteObject(/* int */ std::get<0>(is), /* string */ std::get<1>(is));
// ...
// // Reading back can be done directly into a tuple:
// auto is = aER.ReadObject<std::tuple<int, std::string>>();
// // Or each item could be read separately:
// auto i = aER.ReadObject<int>(); auto s = aER.ReadObject<std::string>();
// ```
template <typename... Ts>
struct ProfileBufferEntryWriter::Serializer<std::tuple<Ts...>> {
 private:
  template <size_t... Is>
  static Length TupleBytes(const std::tuple<Ts...>& aTuple,
                           std::index_sequence<Is...>) {
    return (0 + ... + SumBytes(std::get<Is>(aTuple)));
  }

  template <size_t... Is>
  static void TupleWrite(ProfileBufferEntryWriter& aEW,
                         const std::tuple<Ts...>& aTuple,
                         std::index_sequence<Is...>) {
    (aEW.WriteObject(std::get<Is>(aTuple)), ...);
  }

 public:
  static Length Bytes(const std::tuple<Ts...>& aTuple) {
    // Generate a 0..N-1 index pack, we'll add the sizes of each item.
    return TupleBytes(aTuple, std::index_sequence_for<Ts...>());
  }

  static void Write(ProfileBufferEntryWriter& aEW,
                    const std::tuple<Ts...>& aTuple) {
    // Generate a 0..N-1 index pack, we'll write each item.
    TupleWrite(aEW, aTuple, std::index_sequence_for<Ts...>());
  }
};

template <typename... Ts>
struct ProfileBufferEntryReader::Deserializer<std::tuple<Ts...>> {
  template <size_t I>
  static void TupleIReadInto(ProfileBufferEntryReader& aER,
                             std::tuple<Ts...>& aTuple) {
    aER.ReadIntoObject(std::get<I>(aTuple));
  }

  template <size_t... Is>
  static void TupleReadInto(ProfileBufferEntryReader& aER,
                            std::tuple<Ts...>& aTuple,
                            std::index_sequence<Is...>) {
    (TupleIReadInto<Is>(aER, aTuple), ...);
  }

  static void ReadInto(ProfileBufferEntryReader& aER,
                       std::tuple<Ts...>& aTuple) {
    TupleReadInto(aER, aTuple, std::index_sequence_for<Ts...>());
  }

  static std::tuple<Ts...> Read(ProfileBufferEntryReader& aER) {
    // Note that this creates default `Ts` first, and then overwrites them.
    std::tuple<Ts...> ob;
    ReadInto(aER, ob);
    return ob;
  }
};
// ----------------------------------------------------------------------------
// mozilla::Span

// Span. All elements are serialized in sequence.
// The caller is assumed to know the number of elements (they may manually
// write&read it before the span if needed).
// Similar to tuples, reading/writing spans is equivalent to reading/writing
// their elements in order.
template <class T, size_t N>
struct ProfileBufferEntryWriter::Serializer<Span<T, N>> {
  static Length Bytes(const Span<T, N>& aSpan) {
    Length bytes = 0;
    for (const T& element : aSpan) {
      bytes += SumBytes(element);
    }
    return bytes;
  }

  static void Write(ProfileBufferEntryWriter& aEW, const Span<T, N>& aSpan) {
    for (const T& element : aSpan) {
      aEW.WriteObject(element);
    }
  }
};

template <class T, size_t N>
struct ProfileBufferEntryReader::Deserializer<Span<T, N>> {
  // Read elements back into span pointing at a pre-allocated buffer.
  static void ReadInto(ProfileBufferEntryReader& aER, Span<T, N>& aSpan) {
    for (T& element : aSpan) {
      aER.ReadIntoObject(element);
    }
  }

  // A Span does not own its data, this would probably leak so we forbid this.
  static Span<T, N> Read(ProfileBufferEntryReader& aER) = delete;
};

// ----------------------------------------------------------------------------
// mozilla::Maybe

// Maybe<T> is serialized as one byte containing either 'm' (Nothing),
// or 'M' followed by the recursively-serialized `T` object.
template <typename T>
struct ProfileBufferEntryWriter::Serializer<Maybe<T>> {
  static Length Bytes(const Maybe<T>& aMaybe) {
    // 1 byte to store nothing/something flag, then object size if present.
    return aMaybe.isNothing() ? 1 : (1 + SumBytes(aMaybe.ref()));
  }

  static void Write(ProfileBufferEntryWriter& aEW, const Maybe<T>& aMaybe) {
    // 'm'/'M' is just an arbitrary 1-byte value to distinguish states.
    if (aMaybe.isNothing()) {
      aEW.WriteObject<char>('m');
    } else {
      aEW.WriteObject<char>('M');
      // Use the Serializer for the contained type.
      aEW.WriteObject(aMaybe.ref());
    }
  }
};

template <typename T>
struct ProfileBufferEntryReader::Deserializer<Maybe<T>> {
  static void ReadInto(ProfileBufferEntryReader& aER, Maybe<T>& aMaybe) {
    char c = aER.ReadObject<char>();
    if (c == 'm') {
      aMaybe.reset();
    } else {
      MOZ_ASSERT(c == 'M');
      // If aMaybe is empty, create a default `T` first, to be overwritten.
      // Otherwise we'll just overwrite whatever was already there.
      if (aMaybe.isNothing()) {
        aMaybe.emplace();
      }
      // Use the Deserializer for the contained type.
      aER.ReadIntoObject(aMaybe.ref());
    }
  }

  static Maybe<T> Read(ProfileBufferEntryReader& aER) {
    Maybe<T> maybe;
    char c = aER.ReadObject<char>();
    MOZ_ASSERT(c == 'M' || c == 'm');
    if (c == 'M') {
      // Note that this creates a default `T` inside the Maybe first, and then
      // overwrites it.
      maybe = Some(T{});
      // Use the Deserializer for the contained type.
      aER.ReadIntoObject(maybe.ref());
    }
    return maybe;
  }
};

// ----------------------------------------------------------------------------
// mozilla::Variant

// Variant is serialized as the tag (0-based index of the stored type, encoded
// as ULEB128), and the recursively-serialized object.
template <typename... Ts>
struct ProfileBufferEntryWriter::Serializer<Variant<Ts...>> {
 public:
  static Length Bytes(const Variant<Ts...>& aVariantTs) {
    return aVariantTs.match([](auto aIndex, const auto& aAlternative) {
      return ULEB128Size(aIndex) + SumBytes(aAlternative);
    });
  }

  static void Write(ProfileBufferEntryWriter& aEW,
                    const Variant<Ts...>& aVariantTs) {
    aVariantTs.match([&aEW](auto aIndex, const auto& aAlternative) {
      aEW.WriteULEB128(aIndex);
      aEW.WriteObject(aAlternative);
    });
  }
};

template <typename... Ts>
struct ProfileBufferEntryReader::Deserializer<Variant<Ts...>> {
 private:
  // Called from the fold expression in `VariantReadInto()`, only the selected
  // variant will deserialize the object.
  template <size_t I>
  static void VariantIReadInto(ProfileBufferEntryReader& aER,
                               Variant<Ts...>& aVariantTs, unsigned aTag) {
    if (I == aTag) {
      // Ensure the variant contains the target type. Note that this may create
      // a default object.
      if (!aVariantTs.template is<I>()) {
        aVariantTs = Variant<Ts...>(VariantIndex<I>{});
      }
      aER.ReadIntoObject(aVariantTs.template as<I>());
    }
  }

  template <size_t... Is>
  static void VariantReadInto(ProfileBufferEntryReader& aER,
                              Variant<Ts...>& aVariantTs,
                              std::index_sequence<Is...>) {
    unsigned tag = aER.ReadULEB128<unsigned>();
    (VariantIReadInto<Is>(aER, aVariantTs, tag), ...);
  }

 public:
  static void ReadInto(ProfileBufferEntryReader& aER,
                       Variant<Ts...>& aVariantTs) {
    // Generate a 0..N-1 index pack, the selected variant will deserialize
    // itself.
    VariantReadInto(aER, aVariantTs, std::index_sequence_for<Ts...>());
  }

  static Variant<Ts...> Read(ProfileBufferEntryReader& aER) {
    // Note that this creates a default `Variant` of the first type, and then
    // overwrites it. Consider using `ReadInto` for more control if needed.
    Variant<Ts...> variant(VariantIndex<0>{});
    ReadInto(aER, variant);
    return variant;
  }
};

}  // namespace mozilla

#endif  // ProfileBufferEntrySerialization_h