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
|
/* -*- Mode: C++; tab-width: 9; 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/. */
// This is included first to ensure it doesn't implicitly depend on anything
// else.
#include "mozilla/BufferList.h"
// It would be nice if we could use the InfallibleAllocPolicy from mozalloc,
// but MFBT cannot use mozalloc.
class InfallibleAllocPolicy {
public:
template <typename T>
T* pod_malloc(size_t aNumElems) {
if (aNumElems & mozilla::tl::MulOverflowMask<sizeof(T)>::value) {
MOZ_CRASH("TestBufferList.cpp: overflow");
}
T* rv = static_cast<T*>(malloc(aNumElems * sizeof(T)));
if (!rv) {
MOZ_CRASH("TestBufferList.cpp: out of memory");
}
return rv;
}
template <typename T>
void free_(T* aPtr, size_t aNumElems = 0) {
free(aPtr);
}
void reportAllocOverflow() const {}
bool checkSimulatedOOM() const { return true; }
};
typedef mozilla::BufferList<InfallibleAllocPolicy> BufferList;
int main(void) {
const size_t kInitialSize = 16;
const size_t kInitialCapacity = 24;
const size_t kStandardCapacity = 32;
BufferList bl(kInitialSize, kInitialCapacity, kStandardCapacity);
memset(bl.Start(), 0x0c, kInitialSize);
MOZ_RELEASE_ASSERT(bl.Size() == kInitialSize);
// Simple iteration and access.
BufferList::IterImpl iter(bl.Iter());
MOZ_RELEASE_ASSERT(iter.RemainingInSegment() == kInitialSize);
MOZ_RELEASE_ASSERT(iter.HasRoomFor(kInitialSize));
MOZ_RELEASE_ASSERT(!iter.HasRoomFor(kInitialSize + 1));
MOZ_RELEASE_ASSERT(!iter.HasRoomFor(size_t(-1)));
MOZ_RELEASE_ASSERT(*iter.Data() == 0x0c);
MOZ_RELEASE_ASSERT(!iter.Done());
iter.Advance(bl, 4);
MOZ_RELEASE_ASSERT(iter.RemainingInSegment() == kInitialSize - 4);
MOZ_RELEASE_ASSERT(iter.HasRoomFor(kInitialSize - 4));
MOZ_RELEASE_ASSERT(*iter.Data() == 0x0c);
MOZ_RELEASE_ASSERT(!iter.Done());
iter.Advance(bl, 11);
MOZ_RELEASE_ASSERT(iter.RemainingInSegment() == kInitialSize - 4 - 11);
MOZ_RELEASE_ASSERT(iter.HasRoomFor(kInitialSize - 4 - 11));
MOZ_RELEASE_ASSERT(!iter.HasRoomFor(kInitialSize - 4 - 11 + 1));
MOZ_RELEASE_ASSERT(*iter.Data() == 0x0c);
MOZ_RELEASE_ASSERT(!iter.Done());
iter.Advance(bl, kInitialSize - 4 - 11);
MOZ_RELEASE_ASSERT(iter.RemainingInSegment() == 0);
MOZ_RELEASE_ASSERT(!iter.HasRoomFor(1));
MOZ_RELEASE_ASSERT(iter.Done());
// Writing to the buffer.
const size_t kSmallWrite = 16;
char toWrite[kSmallWrite];
memset(toWrite, 0x0a, kSmallWrite);
MOZ_ALWAYS_TRUE(bl.WriteBytes(toWrite, kSmallWrite));
MOZ_RELEASE_ASSERT(bl.Size() == kInitialSize + kSmallWrite);
iter = bl.Iter();
iter.Advance(bl, kInitialSize);
MOZ_RELEASE_ASSERT(!iter.Done());
MOZ_RELEASE_ASSERT(iter.RemainingInSegment() ==
kInitialCapacity - kInitialSize);
MOZ_RELEASE_ASSERT(iter.HasRoomFor(kInitialCapacity - kInitialSize));
MOZ_RELEASE_ASSERT(*iter.Data() == 0x0a);
// AdvanceAcrossSegments.
iter = bl.Iter();
MOZ_RELEASE_ASSERT(iter.AdvanceAcrossSegments(bl, kInitialCapacity - 4));
MOZ_RELEASE_ASSERT(!iter.Done());
MOZ_RELEASE_ASSERT(iter.RemainingInSegment() == 4);
MOZ_RELEASE_ASSERT(iter.HasRoomFor(4));
MOZ_RELEASE_ASSERT(*iter.Data() == 0x0a);
iter = bl.Iter();
MOZ_RELEASE_ASSERT(
iter.AdvanceAcrossSegments(bl, kInitialSize + kSmallWrite - 4));
MOZ_RELEASE_ASSERT(!iter.Done());
MOZ_RELEASE_ASSERT(iter.RemainingInSegment() == 4);
MOZ_RELEASE_ASSERT(iter.HasRoomFor(4));
MOZ_RELEASE_ASSERT(*iter.Data() == 0x0a);
MOZ_RELEASE_ASSERT(
bl.Iter().AdvanceAcrossSegments(bl, kInitialSize + kSmallWrite - 1));
MOZ_RELEASE_ASSERT(
bl.Iter().AdvanceAcrossSegments(bl, kInitialSize + kSmallWrite));
MOZ_RELEASE_ASSERT(
!bl.Iter().AdvanceAcrossSegments(bl, kInitialSize + kSmallWrite + 1));
MOZ_RELEASE_ASSERT(!bl.Iter().AdvanceAcrossSegments(bl, size_t(-1)));
// Reading non-contiguous bytes.
char toRead[kSmallWrite];
iter = bl.Iter();
iter.Advance(bl, kInitialSize);
bl.ReadBytes(iter, toRead, kSmallWrite);
MOZ_RELEASE_ASSERT(memcmp(toRead, toWrite, kSmallWrite) == 0);
MOZ_RELEASE_ASSERT(iter.Done());
// Make sure reading up to the end of a segment advances the iter to the next
// segment.
iter = bl.Iter();
bl.ReadBytes(iter, toRead, kInitialSize);
MOZ_RELEASE_ASSERT(!iter.Done());
MOZ_RELEASE_ASSERT(iter.RemainingInSegment() ==
kInitialCapacity - kInitialSize);
const size_t kBigWrite = 1024;
char* toWriteBig = static_cast<char*>(malloc(kBigWrite));
for (unsigned i = 0; i < kBigWrite; i++) {
toWriteBig[i] = i % 37;
}
MOZ_ALWAYS_TRUE(bl.WriteBytes(toWriteBig, kBigWrite));
char* toReadBig = static_cast<char*>(malloc(kBigWrite));
iter = bl.Iter();
MOZ_RELEASE_ASSERT(
iter.AdvanceAcrossSegments(bl, kInitialSize + kSmallWrite));
bl.ReadBytes(iter, toReadBig, kBigWrite);
MOZ_RELEASE_ASSERT(memcmp(toReadBig, toWriteBig, kBigWrite) == 0);
MOZ_RELEASE_ASSERT(iter.Done());
free(toReadBig);
free(toWriteBig);
// Currently bl contains these segments:
// #0: offset 0, [0x0c]*16 + [0x0a]*8, size 24
// #1: offset 24, [0x0a]*8 + [i%37 for i in 0..24], size 32
// #2: offset 56, [i%37 for i in 24..56, size 32
// ...
// #32: offset 1016, [i%37 for i in 984..1016], size 32
// #33: offset 1048, [i%37 for i in 1016..1024], size 8
static size_t kTotalSize = kInitialSize + kSmallWrite + kBigWrite;
MOZ_RELEASE_ASSERT(bl.Size() == kTotalSize);
static size_t kLastSegmentSize =
(kTotalSize - kInitialCapacity) % kStandardCapacity;
iter = bl.Iter();
MOZ_RELEASE_ASSERT(iter.AdvanceAcrossSegments(
bl, kTotalSize - kLastSegmentSize - kStandardCapacity));
MOZ_RELEASE_ASSERT(iter.RemainingInSegment() == kStandardCapacity);
iter.Advance(bl, kStandardCapacity);
MOZ_RELEASE_ASSERT(iter.RemainingInSegment() == kLastSegmentSize);
MOZ_RELEASE_ASSERT(
unsigned(*iter.Data()) ==
(kTotalSize - kLastSegmentSize - kInitialSize - kSmallWrite) % 37);
// Clear.
bl.Clear();
MOZ_RELEASE_ASSERT(bl.Size() == 0);
MOZ_RELEASE_ASSERT(bl.Iter().Done());
// Move assignment.
const size_t kSmallCapacity = 8;
BufferList bl2(0, kSmallCapacity, kSmallCapacity);
MOZ_ALWAYS_TRUE(bl2.WriteBytes(toWrite, kSmallWrite));
MOZ_ALWAYS_TRUE(bl2.WriteBytes(toWrite, kSmallWrite));
MOZ_ALWAYS_TRUE(bl2.WriteBytes(toWrite, kSmallWrite));
bl = std::move(bl2);
MOZ_RELEASE_ASSERT(bl2.Size() == 0);
MOZ_RELEASE_ASSERT(bl2.Iter().Done());
iter = bl.Iter();
MOZ_RELEASE_ASSERT(iter.AdvanceAcrossSegments(bl, kSmallWrite * 3));
MOZ_RELEASE_ASSERT(iter.Done());
// MoveFallible
bool success;
bl2 = bl.MoveFallible<InfallibleAllocPolicy>(&success);
MOZ_RELEASE_ASSERT(success);
MOZ_RELEASE_ASSERT(bl.Size() == 0);
MOZ_RELEASE_ASSERT(bl.Iter().Done());
MOZ_RELEASE_ASSERT(bl2.Size() == kSmallWrite * 3);
iter = bl2.Iter();
MOZ_RELEASE_ASSERT(iter.AdvanceAcrossSegments(bl2, kSmallWrite * 3));
MOZ_RELEASE_ASSERT(iter.Done());
bl = bl2.MoveFallible<InfallibleAllocPolicy>(&success);
// Borrowing.
const size_t kBorrowStart = 4;
const size_t kBorrowSize = 24;
iter = bl.Iter();
iter.Advance(bl, kBorrowStart);
bl2 = bl.Borrow<InfallibleAllocPolicy>(iter, kBorrowSize, &success);
MOZ_RELEASE_ASSERT(success);
MOZ_RELEASE_ASSERT(bl2.Size() == kBorrowSize);
MOZ_RELEASE_ASSERT(iter.AdvanceAcrossSegments(
bl, kSmallWrite * 3 - kBorrowSize - kBorrowStart));
MOZ_RELEASE_ASSERT(iter.Done());
iter = bl2.Iter();
MOZ_RELEASE_ASSERT(iter.AdvanceAcrossSegments(bl2, kBorrowSize));
MOZ_RELEASE_ASSERT(iter.Done());
BufferList::IterImpl iter1(bl.Iter()), iter2(bl2.Iter());
iter1.Advance(bl, kBorrowStart);
MOZ_RELEASE_ASSERT(iter1.Data() == iter2.Data());
MOZ_RELEASE_ASSERT(iter1.AdvanceAcrossSegments(bl, kBorrowSize - 5));
MOZ_RELEASE_ASSERT(iter2.AdvanceAcrossSegments(bl2, kBorrowSize - 5));
MOZ_RELEASE_ASSERT(iter1.Data() == iter2.Data());
// Extracting.
const size_t kExtractStart = 8;
const size_t kExtractSize = 24;
const size_t kExtractOverSize = 1000;
iter = bl.Iter();
iter.Advance(bl, kExtractStart);
bl2 = bl.Extract(iter, kExtractSize, &success);
MOZ_RELEASE_ASSERT(success);
MOZ_RELEASE_ASSERT(bl2.Size() == kExtractSize);
BufferList bl3 = bl.Extract(iter, kExtractOverSize, &success);
MOZ_RELEASE_ASSERT(!success);
iter = bl2.Iter();
MOZ_RELEASE_ASSERT(iter.AdvanceAcrossSegments(bl2, kExtractSize));
MOZ_RELEASE_ASSERT(iter.Done());
BufferList bl4(8, 8, 8);
MOZ_ALWAYS_TRUE(bl4.WriteBytes("abcd1234", 8));
iter = bl4.Iter();
iter.Advance(bl4, 8);
BufferList bl5 = bl4.Extract(iter, kExtractSize, &success);
MOZ_RELEASE_ASSERT(!success);
BufferList bl6(0, 0, 16);
MOZ_ALWAYS_TRUE(bl6.WriteBytes("abcdefgh12345678", 16));
MOZ_ALWAYS_TRUE(bl6.WriteBytes("ijklmnop87654321", 16));
iter = bl6.Iter();
iter.Advance(bl6, 8);
BufferList bl7 = bl6.Extract(iter, 16, &success);
MOZ_RELEASE_ASSERT(success);
char data[16];
MOZ_RELEASE_ASSERT(bl6.ReadBytes(iter, data, 8));
MOZ_RELEASE_ASSERT(memcmp(data, "87654321", 8) == 0);
iter = bl7.Iter();
MOZ_RELEASE_ASSERT(bl7.ReadBytes(iter, data, 16));
MOZ_RELEASE_ASSERT(memcmp(data, "12345678ijklmnop", 16) == 0);
BufferList bl8(0, 0, 16);
MOZ_ALWAYS_TRUE(bl8.WriteBytes("abcdefgh12345678", 16));
iter = bl8.Iter();
BufferList bl9 = bl8.Extract(iter, 8, &success);
MOZ_RELEASE_ASSERT(success);
MOZ_RELEASE_ASSERT(bl9.Size() == 8);
MOZ_RELEASE_ASSERT(!iter.Done());
BufferList bl10(0, 0, 8);
MOZ_ALWAYS_TRUE(bl10.WriteBytes("abcdefgh", 8));
MOZ_ALWAYS_TRUE(bl10.WriteBytes("12345678", 8));
iter = bl10.Iter();
BufferList bl11 = bl10.Extract(iter, 16, &success);
MOZ_RELEASE_ASSERT(success);
MOZ_RELEASE_ASSERT(bl11.Size() == 16);
MOZ_RELEASE_ASSERT(iter.Done());
iter = bl11.Iter();
MOZ_RELEASE_ASSERT(bl11.ReadBytes(iter, data, 16));
MOZ_RELEASE_ASSERT(memcmp(data, "abcdefgh12345678", 16) == 0);
// RangeLength.
BufferList bl12(0, 0, 8);
MOZ_ALWAYS_TRUE(bl12.WriteBytes("abcdefgh", 8));
MOZ_ALWAYS_TRUE(bl12.WriteBytes("12345678", 8));
// |iter| is at position 0 (1st segment).
iter = bl12.Iter();
iter1 = bl12.Iter();
MOZ_RELEASE_ASSERT(bl12.RangeLength(iter, iter1) == 0);
MOZ_RELEASE_ASSERT(iter1.AdvanceAcrossSegments(bl12, 4));
MOZ_RELEASE_ASSERT(bl12.RangeLength(iter, iter1) == 4);
MOZ_RELEASE_ASSERT(iter1.AdvanceAcrossSegments(bl12, 4));
MOZ_RELEASE_ASSERT(bl12.RangeLength(iter, iter1) == 8);
MOZ_RELEASE_ASSERT(iter1.AdvanceAcrossSegments(bl12, 4));
MOZ_RELEASE_ASSERT(bl12.RangeLength(iter, iter1) == 12);
MOZ_RELEASE_ASSERT(iter1.AdvanceAcrossSegments(bl12, 3));
MOZ_RELEASE_ASSERT(bl12.RangeLength(iter, iter1) == 15);
MOZ_RELEASE_ASSERT(iter1.AdvanceAcrossSegments(bl12, 1));
MOZ_RELEASE_ASSERT(iter1.Done());
// |iter| is at position 1 (1st segment).
iter = bl12.Iter();
iter1 = bl12.Iter();
MOZ_RELEASE_ASSERT(iter.AdvanceAcrossSegments(bl12, 1));
MOZ_RELEASE_ASSERT(iter1.AdvanceAcrossSegments(bl12, 1));
MOZ_RELEASE_ASSERT(bl12.RangeLength(iter, iter1) == 0);
MOZ_RELEASE_ASSERT(iter1.AdvanceAcrossSegments(bl12, 4));
MOZ_RELEASE_ASSERT(bl12.RangeLength(iter, iter1) == 4);
MOZ_RELEASE_ASSERT(iter1.AdvanceAcrossSegments(bl12, 4));
MOZ_RELEASE_ASSERT(bl12.RangeLength(iter, iter1) == 8);
MOZ_RELEASE_ASSERT(iter1.AdvanceAcrossSegments(bl12, 4));
MOZ_RELEASE_ASSERT(bl12.RangeLength(iter, iter1) == 12);
MOZ_RELEASE_ASSERT(iter1.AdvanceAcrossSegments(bl12, 2));
MOZ_RELEASE_ASSERT(bl12.RangeLength(iter, iter1) == 14);
MOZ_RELEASE_ASSERT(iter1.AdvanceAcrossSegments(bl12, 1));
MOZ_RELEASE_ASSERT(iter1.Done());
// |iter| is at position 8 (2nd segment).
iter = bl12.Iter();
iter1 = bl12.Iter();
MOZ_RELEASE_ASSERT(iter.AdvanceAcrossSegments(bl12, 8));
MOZ_RELEASE_ASSERT(iter1.AdvanceAcrossSegments(bl12, 8));
MOZ_RELEASE_ASSERT(bl12.RangeLength(iter, iter1) == 0);
MOZ_RELEASE_ASSERT(iter1.AdvanceAcrossSegments(bl12, 4));
MOZ_RELEASE_ASSERT(bl12.RangeLength(iter, iter1) == 4);
MOZ_RELEASE_ASSERT(iter1.AdvanceAcrossSegments(bl12, 3));
MOZ_RELEASE_ASSERT(bl12.RangeLength(iter, iter1) == 7);
MOZ_RELEASE_ASSERT(iter1.AdvanceAcrossSegments(bl12, 1));
MOZ_RELEASE_ASSERT(iter1.Done());
// |iter| is at position 9 (2nd segment).
iter = bl12.Iter();
iter1 = bl12.Iter();
MOZ_RELEASE_ASSERT(iter.AdvanceAcrossSegments(bl12, 9));
MOZ_RELEASE_ASSERT(iter1.AdvanceAcrossSegments(bl12, 9));
MOZ_RELEASE_ASSERT(bl12.RangeLength(iter, iter1) == 0);
MOZ_RELEASE_ASSERT(iter1.AdvanceAcrossSegments(bl12, 4));
MOZ_RELEASE_ASSERT(bl12.RangeLength(iter, iter1) == 4);
MOZ_RELEASE_ASSERT(iter1.AdvanceAcrossSegments(bl12, 2));
MOZ_RELEASE_ASSERT(bl12.RangeLength(iter, iter1) == 6);
MOZ_RELEASE_ASSERT(iter1.AdvanceAcrossSegments(bl12, 1));
MOZ_RELEASE_ASSERT(iter1.Done());
return 0;
}
|