22 #ifndef COMMON_MEMSTREAM_H 23 #define COMMON_MEMSTREAM_H 25 #include "common/stream.h" 26 #include "common/types.h" 27 #include "common/util.h" 45 struct CastFreeDeleter {
46 inline void operator()(
const byte *
object) {
47 free(const_cast<byte *>(
object));
73 MemoryReadStream(
const byte *dataPtr, uint32 dataSize, DisposeAfterUse::Flag disposeMemory = DisposeAfterUse::NO) :
74 _ptrOrig(dataPtr, disposeMemory),
87 uint32
read(
void *dataPtr, uint32 dataSize);
89 bool eos()
const {
return _eos; }
92 int64
pos()
const {
return _pos; }
93 int64
size()
const {
return _size; }
95 bool seek(int64 offs,
int whence = SEEK_SET);
105 MemoryReadStreamEndian(
const byte *buf, uint32 len,
bool bigEndian, DisposeAfterUse::Flag disposeMemory = DisposeAfterUse::NO)
122 const uint32 _bufSize;
128 MemoryWriteStream(byte *buf, uint32 len) : _ptr(buf), _bufSize(len), _pos(0), _err(
false) {}
130 uint32
write(
const void *dataPtr, uint32 dataSize)
override {
132 if (dataSize > _bufSize - _pos) {
133 dataSize = _bufSize - _pos;
137 memcpy(_ptr, dataPtr, dataSize);
143 int64
pos()
const override {
return _pos; }
144 int64
size()
const override {
return _bufSize; }
146 bool err()
const override {
return _err; }
149 bool seek(int64 offset,
int whence = SEEK_SET)
override {
return false; }
161 bool seek(int64 offset,
int whence = SEEK_SET)
override {
166 offset =
size() + offset;
171 _ptr = _ptrOrig + offset;
180 if ((int32)_pos >
size()) {
182 _ptr = _ptrOrig + _pos;
201 DisposeAfterUse::Flag _disposeMemory;
203 void ensureCapacity(uint32 capacity) {
204 if (capacity <= _capacity)
207 byte *old_data = _data;
209 _capacity = capacity;
210 _data = (byte *)malloc(_capacity);
215 memcpy(_data, old_data, _size);
225 while (capa < capacity)
230 explicit MemoryWriteStreamDynamic(DisposeAfterUse::Flag disposeMemory) : _capacity(0), _size(0), _ptr(
nullptr), _data(
nullptr), _pos(0), _disposeMemory(disposeMemory) {}
237 uint32
write(
const void *dataPtr, uint32 dataSize)
override {
238 if ((_pos + dataSize) >= _capacity)
239 ensureCapacity(roundUpCapacity(_pos + dataSize));
241 memcpy(_ptr, dataPtr, dataSize);
249 int64
pos()
const override {
return _pos; }
250 int64
size()
const override {
return _size; }
252 byte *getData() {
return _data; }
254 bool seek(int64 offs,
int whence = SEEK_SET)
override {
256 assert(_pos <= _size);
275 assert(_pos <= _size);
288 uint32 _writePos, _readPos, _pos, _length;
289 DisposeAfterUse::Flag _disposeMemory;
292 void ensureCapacity(uint32 new_len) {
293 if (new_len <= _capacity)
296 byte *old_data = _data;
297 uint32 oldCapacity = _capacity;
299 _capacity =
MAX(new_len + 32, _capacity * 2);
300 _data = (byte *)malloc(_capacity);
304 if (_readPos < _writePos) {
305 memcpy(_data, old_data + _readPos, _writePos - _readPos);
309 memcpy(_data, old_data + _readPos, oldCapacity - _readPos);
310 memcpy(_data + oldCapacity - _readPos, old_data, _writePos);
318 explicit MemoryReadWriteStream(DisposeAfterUse::Flag disposeMemory) : _capacity(0), _size(0), _data(
nullptr), _writePos(0), _readPos(0), _pos(0), _length(0), _disposeMemory(disposeMemory), _eos(
false) {}
325 uint32
write(
const void *dataPtr, uint32 dataSize)
override {
326 ensureCapacity(_length + dataSize);
327 if (_writePos + dataSize < _capacity) {
328 memcpy(_data + _writePos, dataPtr, dataSize);
330 memcpy(_data + _writePos, dataPtr, _capacity - _writePos);
331 const byte *shiftedPtr = (
const byte *)dataPtr + _capacity - _writePos;
332 memcpy(_data, shiftedPtr, dataSize - (_capacity - _writePos));
334 _writePos = (_writePos + dataSize) % _capacity;
342 uint32
read(
void *dataPtr, uint32 dataSize)
override {
343 if (_length < dataSize) {
347 if (dataSize == 0 || _capacity == 0)
return 0;
348 if (_readPos + dataSize < _capacity) {
349 memcpy(dataPtr, _data + _readPos, dataSize);
351 memcpy(dataPtr, _data + _readPos, _capacity - _readPos);
352 byte *shiftedPtr = (byte *)dataPtr + _capacity - _readPos;
353 memcpy(shiftedPtr, _data, dataSize - (_capacity - _readPos));
355 _readPos = (_readPos + dataSize) % _capacity;
360 bool seek(int64 offset,
int whence)
override {
365 offset =
size() + offset;
379 _eos = (int64)_readPos >=
size();
383 int64
pos()
const override {
return _pos - _length; }
384 int64
size()
const override {
return _size; }
385 bool eos()
const override {
return _eos; }
388 byte *getData() {
return _data; }
396 const uint32 _bufSize;
402 DisposeAfterUse::Flag _disposeMemory;
406 _ptrOrig(buf), _ptr(buf), _bufSize(len), _pos(0), _err(
false), _eos(
false), _disposeMemory(disposeMemory) {}
409 if (_disposeMemory) {
414 uint32
write(
const void *dataPtr, uint32 dataSize)
override {
416 if (dataSize > _bufSize - _pos) {
417 dataSize = _bufSize - _pos;
421 memcpy(_ptr, dataPtr, dataSize);
427 int64
pos()
const override {
return _pos; }
428 int64
size()
const override {
return _bufSize; }
430 bool eos()
const override {
return _eos; }
432 bool err()
const override {
return _err; }
435 inline void rewind(int32 bytes) {
440 if (_bufSize - _pos <= 0) {
447 uint32
read(
void *dataPtr, uint32 dataSize)
override {
449 if (dataSize > _bufSize - _pos) {
450 dataSize = _bufSize - _pos;
453 memcpy(dataPtr, _ptr, dataSize);
461 bool seek(int64 offset,
int whence = SEEK_SET)
override {
466 offset =
size() + offset;
471 _ptr = _ptrOrig + offset;
480 if ((int32)_pos >
size()) {
482 _ptr = _ptrOrig + _pos;
void clearErr() override
Definition: memstream.h:147
int64 size() const override
Definition: memstream.h:144
bool err() const override
Definition: memstream.h:146
uint32 write(const void *dataPtr, uint32 dataSize) override
Definition: memstream.h:414
bool eos() const override
Definition: memstream.h:385
int64 size() const override
Definition: memstream.h:428
int64 size() const override
Definition: memstream.h:250
Definition: memstream.h:394
uint32 read(void *dataPtr, uint32 dataSize) override
Definition: memstream.h:342
bool seek(int64 offset, int whence=SEEK_SET) override
Definition: memstream.h:461
int64 pos() const
Definition: memstream.h:92
int64 pos() const override
Definition: memstream.h:249
Definition: memstream.h:194
uint32 write(const void *dataPtr, uint32 dataSize) override
Definition: memstream.h:130
int64 size() const override
Definition: memstream.h:109
bool seek(int64 offset, int whence=SEEK_SET) override
Definition: memstream.h:149
Definition: memstream.h:103
PointerType get() const
Definition: ptr.h:229
int64 pos() const override
Definition: memstream.h:427
void clearErr()
Definition: memstream.h:90
bool seek(int64 offset, int whence) override
Definition: memstream.h:360
bool seek(int64 offs, int whence=SEEK_SET) override
Definition: memstream.h:254
int64 pos() const override
Definition: memstream.h:383
bool err() const override
Definition: memstream.h:432
uint32 read(void *dataPtr, uint32 dataSize) override
Definition: memstream.h:447
static size_t roundUpCapacity(size_t capacity)
Definition: memstream.h:223
uint32 write(const void *dataPtr, uint32 dataSize) override
Definition: memstream.h:325
bool seek(int64 offs, int whence=SEEK_SET) override
Definition: memstream.h:111
int64 pos() const override
Definition: memstream.h:108
Definition: algorithm.h:29
int64 size() const
Definition: memstream.h:93
void clearErr() override
Definition: memstream.h:386
bool eos() const override
Definition: memstream.h:430
uint32 read(void *dataPtr, uint32 dataSize)
MemoryReadStream(const byte *dataPtr, uint32 dataSize, DisposeAfterUse::Flag disposeMemory=DisposeAfterUse::NO)
Definition: memstream.h:73
Definition: memstream.h:283
bool eos() const
Definition: memstream.h:89
Out move(In first, In last, Out dst)
Definition: algorithm.h:109
Definition: memstream.h:43
bool skip(uint32 offset) override
Definition: memstream.h:113
int64 size() const override
Definition: memstream.h:384
bool seek(int64 offs, int whence=SEEK_SET)
uint32 write(const void *dataPtr, uint32 dataSize) override
Definition: memstream.h:237
T MAX(T a, T b)
Definition: util.h:62
int64 pos() const override
Definition: memstream.h:143
Definition: memstream.h:120
void clearErr() override
Definition: memstream.h:433
Definition: memstream.h:155
bool seek(int64 offset, int whence=SEEK_SET) override
Definition: memstream.h:161