Modernize memcpy() and memmove() usage.

This CL replaces memcpy() with std::memcpy()
and memmove() with std::memmove(), and #includes
<cstring> in files that use either function.

PiperOrigin-RevId: 306067788
This commit is contained in:
Victor Costan
2020-04-12 00:01:01 +00:00
parent d674348a0c
commit 14bef66290
4 changed files with 37 additions and 33 deletions

View File

@@ -26,7 +26,8 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <string.h> #include <cstddef>
#include <cstring>
#include "snappy-sinksource.h" #include "snappy-sinksource.h"
@@ -74,7 +75,7 @@ UncheckedByteArraySink::~UncheckedByteArraySink() { }
void UncheckedByteArraySink::Append(const char* data, size_t n) { void UncheckedByteArraySink::Append(const char* data, size_t n) {
// Do no copying if the caller filled in the result of GetAppendBuffer() // Do no copying if the caller filled in the result of GetAppendBuffer()
if (data != dest_) { if (data != dest_) {
memcpy(dest_, data, n); std::memcpy(dest_, data, n);
} }
dest_ += n; dest_ += n;
} }
@@ -88,7 +89,7 @@ void UncheckedByteArraySink::AppendAndTakeOwnership(
void (*deleter)(void*, const char*, size_t), void (*deleter)(void*, const char*, size_t),
void *deleter_arg) { void *deleter_arg) {
if (data != dest_) { if (data != dest_) {
memcpy(dest_, data, n); std::memcpy(dest_, data, n);
(*deleter)(deleter_arg, data, n); (*deleter)(deleter_arg, data, n);
} }
dest_ += n; dest_ += n;

View File

@@ -35,6 +35,7 @@
#include "config.h" #include "config.h"
#endif #endif
#include <cstring>
#include <string> #include <string>
#include <assert.h> #include <assert.h>
@@ -205,12 +206,12 @@ struct Unaligned32Struct {
inline uint64 UNALIGNED_LOAD64(const void *p) { inline uint64 UNALIGNED_LOAD64(const void *p) {
uint64 t; uint64 t;
memcpy(&t, p, sizeof t); std::memcpy(&t, p, sizeof t);
return t; return t;
} }
inline void UNALIGNED_STORE64(void *p, uint64 v) { inline void UNALIGNED_STORE64(void *p, uint64 v) {
memcpy(p, &v, sizeof v); std::memcpy(p, &v, sizeof v);
} }
#else #else
@@ -220,32 +221,32 @@ inline void UNALIGNED_STORE64(void *p, uint64 v) {
inline uint16 UNALIGNED_LOAD16(const void *p) { inline uint16 UNALIGNED_LOAD16(const void *p) {
uint16 t; uint16 t;
memcpy(&t, p, sizeof t); std::memcpy(&t, p, sizeof t);
return t; return t;
} }
inline uint32 UNALIGNED_LOAD32(const void *p) { inline uint32 UNALIGNED_LOAD32(const void *p) {
uint32 t; uint32 t;
memcpy(&t, p, sizeof t); std::memcpy(&t, p, sizeof t);
return t; return t;
} }
inline uint64 UNALIGNED_LOAD64(const void *p) { inline uint64 UNALIGNED_LOAD64(const void *p) {
uint64 t; uint64 t;
memcpy(&t, p, sizeof t); std::memcpy(&t, p, sizeof t);
return t; return t;
} }
inline void UNALIGNED_STORE16(void *p, uint16 v) { inline void UNALIGNED_STORE16(void *p, uint16 v) {
memcpy(p, &v, sizeof v); std::memcpy(p, &v, sizeof v);
} }
inline void UNALIGNED_STORE32(void *p, uint32 v) { inline void UNALIGNED_STORE32(void *p, uint32 v) {
memcpy(p, &v, sizeof v); std::memcpy(p, &v, sizeof v);
} }
inline void UNALIGNED_STORE64(void *p, uint64 v) { inline void UNALIGNED_STORE64(void *p, uint64 v) {
memcpy(p, &v, sizeof v); std::memcpy(p, &v, sizeof v);
} }
#endif #endif

View File

@@ -71,6 +71,7 @@
#include <stdio.h> #include <stdio.h>
#include <algorithm> #include <algorithm>
#include <cstring>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -124,17 +125,17 @@ namespace {
void UnalignedCopy64(const void* src, void* dst) { void UnalignedCopy64(const void* src, void* dst) {
char tmp[8]; char tmp[8];
memcpy(tmp, src, 8); std::memcpy(tmp, src, 8);
memcpy(dst, tmp, 8); std::memcpy(dst, tmp, 8);
} }
void UnalignedCopy128(const void* src, void* dst) { void UnalignedCopy128(const void* src, void* dst) {
// memcpy gets vectorized when the appropriate compiler options are used. // std::memcpy() gets vectorized when the appropriate compiler options are
// For example, x86 compilers targeting SSE2+ will optimize to an SSE2 load // used. For example, x86 compilers targeting SSE2+ will optimize to an SSE2
// and store. // load and store.
char tmp[16]; char tmp[16];
memcpy(tmp, src, 16); std::memcpy(tmp, src, 16);
memcpy(dst, tmp, 16); std::memcpy(dst, tmp, 16);
} }
// Copy [src, src+(op_limit-op)) to [op, (op_limit-op)) a byte at a time. Used // Copy [src, src+(op_limit-op)) to [op, (op_limit-op)) a byte at a time. Used
@@ -146,7 +147,8 @@ void UnalignedCopy128(const void* src, void* dst) {
// After IncrementalCopySlow(src, op, op_limit), the result will have eleven // After IncrementalCopySlow(src, op, op_limit), the result will have eleven
// copies of "ab" // copies of "ab"
// ababababababababababab // ababababababababababab
// Note that this does not match the semantics of either memcpy() or memmove(). // Note that this does not match the semantics of either std::memcpy() or
// std::memmove().
inline char* IncrementalCopySlow(const char* src, char* op, inline char* IncrementalCopySlow(const char* src, char* op,
char* const op_limit) { char* const op_limit) {
// TODO: Remove pragma when LLVM is aware this // TODO: Remove pragma when LLVM is aware this
@@ -340,7 +342,7 @@ static inline char* EmitLiteral(char* op,
const char* literal, const char* literal,
int len) { int len) {
// The vast majority of copies are below 16 bytes, for which a // The vast majority of copies are below 16 bytes, for which a
// call to memcpy is overkill. This fast path can sometimes // call to std::memcpy() is overkill. This fast path can sometimes
// copy up to 15 bytes too much, but that is okay in the // copy up to 15 bytes too much, but that is okay in the
// main loop, since we have a bit to go on for both sides: // main loop, since we have a bit to go on for both sides:
// //
@@ -370,11 +372,11 @@ static inline char* EmitLiteral(char* op,
// Encode in upcoming bytes. // Encode in upcoming bytes.
// Write 4 bytes, though we may care about only 1 of them. The output buffer // Write 4 bytes, though we may care about only 1 of them. The output buffer
// is guaranteed to have at least 3 more spaces left as 'len >= 61' holds // is guaranteed to have at least 3 more spaces left as 'len >= 61' holds
// here and there is a memcpy of size 'len' below. // here and there is a std::memcpy() of size 'len' below.
LittleEndian::Store32(op, n); LittleEndian::Store32(op, n);
op += count; op += count;
} }
memcpy(op, literal, len); std::memcpy(op, literal, len);
return op + len; return op + len;
} }
@@ -970,7 +972,7 @@ bool SnappyDecompressor::RefillTag() {
// contents. We store the needed bytes in "scratch_". They // contents. We store the needed bytes in "scratch_". They
// will be consumed immediately by the caller since we do not // will be consumed immediately by the caller since we do not
// read more than we need. // read more than we need.
memmove(scratch_, ip, nbuf); std::memmove(scratch_, ip, nbuf);
reader_->Skip(peeked_); // All peeked bytes are used up reader_->Skip(peeked_); // All peeked bytes are used up
peeked_ = 0; peeked_ = 0;
while (nbuf < needed) { while (nbuf < needed) {
@@ -978,7 +980,7 @@ bool SnappyDecompressor::RefillTag() {
const char* src = reader_->Peek(&length); const char* src = reader_->Peek(&length);
if (length == 0) return false; if (length == 0) return false;
uint32 to_add = std::min<uint32>(needed - nbuf, length); uint32 to_add = std::min<uint32>(needed - nbuf, length);
memcpy(scratch_ + nbuf, src, to_add); std::memcpy(scratch_ + nbuf, src, to_add);
nbuf += to_add; nbuf += to_add;
reader_->Skip(to_add); reader_->Skip(to_add);
} }
@@ -988,7 +990,7 @@ bool SnappyDecompressor::RefillTag() {
} else if (nbuf < kMaximumTagLength) { } else if (nbuf < kMaximumTagLength) {
// Have enough bytes, but move into scratch_ so that we do not // Have enough bytes, but move into scratch_ so that we do not
// read past end of input // read past end of input
memmove(scratch_, ip, nbuf); std::memmove(scratch_, ip, nbuf);
reader_->Skip(peeked_); // All peeked bytes are used up reader_->Skip(peeked_); // All peeked bytes are used up
peeked_ = 0; peeked_ = 0;
ip_ = scratch_; ip_ = scratch_;
@@ -1057,13 +1059,13 @@ size_t Compress(Source* reader, Sink* writer) {
fragment_size = num_to_read; fragment_size = num_to_read;
} else { } else {
char* scratch = wmem.GetScratchInput(); char* scratch = wmem.GetScratchInput();
memcpy(scratch, fragment, bytes_read); std::memcpy(scratch, fragment, bytes_read);
reader->Skip(bytes_read); reader->Skip(bytes_read);
while (bytes_read < num_to_read) { while (bytes_read < num_to_read) {
fragment = reader->Peek(&fragment_size); fragment = reader->Peek(&fragment_size);
size_t n = std::min<size_t>(fragment_size, num_to_read - bytes_read); size_t n = std::min<size_t>(fragment_size, num_to_read - bytes_read);
memcpy(scratch + bytes_read, fragment, n); std::memcpy(scratch + bytes_read, fragment, n);
bytes_read += n; bytes_read += n;
reader->Skip(n); reader->Skip(n);
} }
@@ -1184,7 +1186,7 @@ class SnappyIOVecWriter {
} }
const size_t to_write = std::min(len, curr_iov_remaining_); const size_t to_write = std::min(len, curr_iov_remaining_);
memcpy(curr_iov_output_, ip, to_write); std::memcpy(curr_iov_output_, ip, to_write);
curr_iov_output_ += to_write; curr_iov_output_ += to_write;
curr_iov_remaining_ -= to_write; curr_iov_remaining_ -= to_write;
total_written_ += to_write; total_written_ += to_write;
@@ -1337,7 +1339,7 @@ class SnappyArrayWriter {
char* op = *op_p; char* op = *op_p;
const size_t space_left = op_limit_ - op; const size_t space_left = op_limit_ - op;
if (space_left < len) return false; if (space_left < len) return false;
memcpy(op, ip, len); std::memcpy(op, ip, len);
*op_p = op + len; *op_p = op + len;
return true; return true;
} }
@@ -1538,7 +1540,7 @@ class SnappyScatteredWriter {
size_t avail = op_limit_ - op; size_t avail = op_limit_ - op;
if (len <= avail) { if (len <= avail) {
// Fast path // Fast path
memcpy(op, ip, len); std::memcpy(op, ip, len);
*op_p = op + len; *op_p = op + len;
return true; return true;
} else { } else {
@@ -1598,7 +1600,7 @@ bool SnappyScatteredWriter<Allocator>::SlowAppend(const char* ip, size_t len) {
size_t avail = op_limit_ - op_ptr_; size_t avail = op_limit_ - op_ptr_;
while (len > avail) { while (len > avail) {
// Completely fill this block // Completely fill this block
memcpy(op_ptr_, ip, avail); std::memcpy(op_ptr_, ip, avail);
op_ptr_ += avail; op_ptr_ += avail;
assert(op_limit_ - op_ptr_ == 0); assert(op_limit_ - op_ptr_ == 0);
full_size_ += (op_ptr_ - op_base_); full_size_ += (op_ptr_ - op_base_);
@@ -1619,7 +1621,7 @@ bool SnappyScatteredWriter<Allocator>::SlowAppend(const char* ip, size_t len) {
avail = bsize; avail = bsize;
} }
memcpy(op_ptr_, ip, len); std::memcpy(op_ptr_, ip, len);
op_ptr_ += len; op_ptr_ += len;
return true; return true;
} }

View File

@@ -84,7 +84,7 @@ class DataEndingAtUnreadablePage {
CHECK_NE(MAP_FAILED, mem_); CHECK_NE(MAP_FAILED, mem_);
protected_page_ = reinterpret_cast<char*>(mem_) + space_for_string; protected_page_ = reinterpret_cast<char*>(mem_) + space_for_string;
char* dst = protected_page_ - size; char* dst = protected_page_ - size;
memcpy(dst, s.data(), size); std::memcpy(dst, s.data(), size);
data_ = dst; data_ = dst;
size_ = size; size_ = size;
// Make guard page unreadable. // Make guard page unreadable.