-
Notifications
You must be signed in to change notification settings - Fork 54
/
BufferStream.h
62 lines (48 loc) · 1.68 KB
/
BufferStream.h
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
/* -*- Mode: C++; tab-width: 8; 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 mozilla_recordreplay_BufferStream_h
#define mozilla_recordreplay_BufferStream_h
#include "InfallibleVector.h"
namespace mozilla {
namespace recordreplay {
// BufferStream is a simplified form of Stream from Recording.h, allowing
// reading or writing to a stream of data backed by an in memory buffer.
class BufferStream {
InfallibleVector<char>* mOutput;
const char* mInput;
size_t mInputSize;
public:
BufferStream(const char* aInput, size_t aInputSize)
: mOutput(nullptr), mInput(aInput), mInputSize(aInputSize) {}
explicit BufferStream(InfallibleVector<char>* aOutput)
: mOutput(aOutput), mInput(nullptr), mInputSize(0) {}
void WriteBytes(const void* aData, size_t aSize) {
MOZ_RELEASE_ASSERT(mOutput);
mOutput->append((char*)aData, aSize);
}
void WriteScalar(size_t aValue) { WriteBytes(&aValue, sizeof(aValue)); }
void ReadBytes(void* aData, size_t aSize) {
if (aSize) {
MOZ_RELEASE_ASSERT(mInput);
MOZ_RELEASE_ASSERT(aSize <= mInputSize);
memcpy(aData, mInput, aSize);
mInput += aSize;
mInputSize -= aSize;
}
}
size_t ReadScalar() {
size_t rv;
ReadBytes(&rv, sizeof(rv));
return rv;
}
bool IsEmpty() {
MOZ_RELEASE_ASSERT(mInput);
return mInputSize == 0;
}
};
} // namespace recordreplay
} // namespace mozilla
#endif // mozilla_recordreplay_BufferStream_h