Skip to content

Commit

Permalink
-remove MemoryAllocStream
Browse files Browse the repository at this point in the history
+make old skip version
  • Loading branch information
guzibei committed Jan 26, 2025
1 parent 11e8363 commit 46b4a1d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 100 deletions.
30 changes: 0 additions & 30 deletions include/wabt/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,36 +195,6 @@ class MemoryStream : public Stream {
std::unique_ptr<OutputBuffer> buf_;
};

class MemoryAllocStream : public wabt::Stream {
public:
typedef void* (*wabt_realloc)(void* __ptr, size_t __size);

MemoryAllocStream(uint8_t* _output_data = nullptr,
size_t _total_size = 0,
wabt_realloc _realloc_fn = nullptr)
: output_data(_output_data), total_size(_total_size), used_size(0) {
if (_realloc_fn) {
realloc_fn = _realloc_fn;
} else {
realloc_fn = std::realloc;
}
}

wabt::Result WriteDataImpl(size_t dst_offset, const void* src, size_t size);

wabt::Result MoveDataImpl(size_t dst_offset, size_t src_offset, size_t size);

wabt::Result TruncateImpl(size_t size);
inline size_t GetSize() const { return used_size; }
inline uint8_t* GetData() { return output_data; }

private:
uint8_t* output_data;
size_t total_size;
size_t used_size;
wabt_realloc realloc_fn;
};

class FileStream : public Stream {
public:
WABT_DISALLOW_COPY_AND_ASSIGN(FileStream);
Expand Down
11 changes: 6 additions & 5 deletions src/binary-reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1941,12 +1941,18 @@ Result BinaryReader::ReadInstructions(Offset end_offset, const char* context) {
}

Result BinaryReader::ReadSkippedFunctionBody(Offset end_offset) {
#ifdef WABT_KEEP_OPCODE_WHEN_SKIP_FUNC_BODY
std::vector<uint8_t> opcode_buffer;
opcode_buffer.resize(end_offset - state_.offset);
memcpy(opcode_buffer.data(), state_.data + state_.offset,
opcode_buffer.size());
CALLBACK(OnSkipFunctionBodyExpr, opcode_buffer);
state_.offset = end_offset;
#else
// old version with OnEndExpr
state_.offset = end_offset;
CALLBACK0(OnEndExpr);
#endif

return Result::Ok;
}
Expand Down Expand Up @@ -2899,12 +2905,7 @@ Result BinaryReader::ReadCodeSection(Offset section_size) {
CALLBACK(EndLocalDecls);

if (options_.skip_function_bodies) {
#ifdef WABT_KEEP_OPCODE_WHEN_SKIP_FUNC_BODY
CHECK_RESULT(ReadSkippedFunctionBody(end_offset));
#else
state_.offset = end_offset;
CALLBACK0(OnEndExpr);
#endif
} else {
CHECK_RESULT(ReadFunctionBody(end_offset));
}
Expand Down
65 changes: 0 additions & 65 deletions src/stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,71 +229,6 @@ Result MemoryStream::TruncateImpl(size_t size) {
return Result::Ok;
}

Result MemoryAllocStream::WriteDataImpl(size_t dst_offset,
const void* src,
size_t size) {
if (size == 0) {
return wabt::Result::Ok;
}
size_t end = dst_offset + size;
bool needRealloc = false;
while (end > total_size) {
total_size = total_size << 1;
needRealloc = true;
}
if (needRealloc) {
output_data = static_cast<uint8_t*>(realloc_fn(output_data, total_size));
}

if (end > used_size) {
used_size = end;
}

memcpy(output_data + dst_offset, src, size);
return wabt::Result::Ok;
}

Result MemoryAllocStream::MoveDataImpl(size_t dst_offset,
size_t src_offset,
size_t size) {
if (size == 0) {
return wabt::Result::Ok;
}
size_t src_end = src_offset + size;
size_t dst_end = dst_offset + size;
size_t end = src_end > dst_end ? src_end : dst_end;

bool needRealloc = false;
while (end > total_size) {
total_size = total_size << 1;
needRealloc = true;
}
if (needRealloc) {
output_data = static_cast<uint8_t*>(realloc_fn(output_data, total_size));
}

if (end > used_size) {
used_size = end;
}

uint8_t* dst = output_data + dst_offset;
uint8_t* src = output_data + src_offset;
memmove(dst, src, size);
return wabt::Result::Ok;
}

Result MemoryAllocStream::TruncateImpl(size_t size) {
if (size > used_size) {
return wabt::Result::Error;
}
used_size = size;

// will not truncate size, we can truncate it at end
// output_data = (uint8_t*)realloc_fn(output_data, size);
// total_size = size;
return wabt::Result::Ok;
}

FileStream::FileStream(std::string_view filename, Stream* log_stream)
: Stream(log_stream), file_(nullptr), offset_(0), should_close_(false) {
std::string filename_str(filename);
Expand Down

0 comments on commit 46b4a1d

Please sign in to comment.