On overlapping buffers use memmove

This commit is contained in:
Matt Ranney 2010-06-29 20:10:01 -07:00 committed by Ryan Dahl
parent 898afbaf34
commit 02ed0ec93b

View File

@ -323,9 +323,16 @@ Handle<Value> Buffer::Copy(const Arguments &args) {
ssize_t to_copy = MIN(source_end - source_start, ssize_t to_copy = MIN(source_end - source_start,
target->length() - target_start); target->length() - target_start);
if (target->blob_ == source->blob_) {
// need to use slightly slower memmove is the ranges might overlap
memmove((void*)(target->data() + target_start),
(const void*)(source->data() + source_start),
to_copy);
} else {
memcpy((void*)(target->data() + target_start), memcpy((void*)(target->data() + target_start),
(const void*)(source->data() + source_start), (const void*)(source->data() + source_start),
to_copy); to_copy);
}
return scope.Close(Integer::New(to_copy)); return scope.Close(Integer::New(to_copy));
} }