Skip to content

Commit

Permalink
feat(core): change memcpy to std::copy_n
Browse files Browse the repository at this point in the history
  • Loading branch information
churchill-zhang authored and zoomchan-cxj committed Mar 16, 2023
1 parent 9acbf5c commit e4174dd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 4 additions & 2 deletions core/src/vm/v8/snapshot_deserializer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include "core/vm/v8/snapshot_deserializer.h"

#include <algorithm>

#include "core/base/common.h"

SnapshotDeserializer::SnapshotDeserializer(const std::vector<uint8_t>& buffer)
Expand All @@ -35,7 +37,7 @@ bool SnapshotDeserializer::ReadUInt32(uint32_t& value) {
error_message_ = GetErrorMessage("uint32", sizeof(value), length_ - position_);
return false;
}
memcpy(&value, buffer_ + position_, sizeof(value));
std::copy_n(buffer_ + position_, sizeof(value), reinterpret_cast<uint8_t*>(&value));
position_ += sizeof(value);
error_message_.clear();
return true;
Expand All @@ -61,7 +63,7 @@ bool SnapshotDeserializer::ReadBuffer(void* p, size_t length) {
error_message_ = GetErrorMessage("buffer", length, length_ - position_);
return false;
}
memcpy(p, buffer_ + position_, length);
std::copy_n(buffer_ + position_, length, reinterpret_cast<uint8_t*>(p));
position_ += length;
error_message_.clear();
return true;
Expand Down
5 changes: 4 additions & 1 deletion core/src/vm/v8/snapshot_serializer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include "core/vm/v8/snapshot_serializer.h"

#include <algorithm>

#include "core/base/common.h"

void SnapshotSerializer::WriteUInt32(uint32_t value) {
Expand All @@ -38,6 +40,7 @@ void SnapshotSerializer::WriteBuffer(const void* p, size_t length) {
if (buffer_.capacity() <= buffer_.size() + length) {
buffer_.resize(buffer_.size() + length);
}
memcpy(&buffer_[0] + position_, p, length);

std::copy_n(reinterpret_cast<const uint8_t*>(p), length, &buffer_[0] + position_);
position_ += length;
}

0 comments on commit e4174dd

Please sign in to comment.