-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
custom memmove implementation proposal. #593
Draft
devnexen
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
devnexen:memmove_impl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -65,15 +65,24 @@ extern "C" void abort() | |||||||
* fills one with a well-known pattern, and then copies subsets of this at | ||||||||
* one-byte increments to a target. This gives us unaligned starts. | ||||||||
*/ | ||||||||
template<bool overlap> | ||||||||
void check_size(size_t size) | ||||||||
{ | ||||||||
START_TEST("checking {}-byte memcpy", size); | ||||||||
if constexpr (!overlap) | ||||||||
{ | ||||||||
START_TEST("checking {}-byte memcpy", size); | ||||||||
} | ||||||||
else | ||||||||
{ | ||||||||
START_TEST("checking {}-byte memmove", size); | ||||||||
} | ||||||||
auto* s = static_cast<unsigned char*>(my_malloc(size + 1)); | ||||||||
auto* d = static_cast<unsigned char*>(my_malloc(size + 1)); | ||||||||
d[size] = 0; | ||||||||
s[size] = 255; | ||||||||
for (size_t start = 0; start < size; start++) | ||||||||
{ | ||||||||
void* ret; | ||||||||
unsigned char* src = s + start; | ||||||||
unsigned char* dst = d + start; | ||||||||
size_t sz = (size - start); | ||||||||
|
@@ -85,7 +94,14 @@ void check_size(size_t size) | |||||||
{ | ||||||||
dst[i] = 0; | ||||||||
} | ||||||||
void* ret = my_memcpy(dst, src, sz); | ||||||||
if constexpr (!overlap) | ||||||||
{ | ||||||||
ret = my_memcpy(dst, src, sz); | ||||||||
} | ||||||||
else | ||||||||
{ | ||||||||
ret = my_memmove(dst, src, sz); | ||||||||
} | ||||||||
EXPECT(ret == dst, "Return value should be {}, was {}", dst, ret); | ||||||||
for (size_t i = 0; i < sz; ++i) | ||||||||
{ | ||||||||
|
@@ -144,6 +160,50 @@ void check_bounds(size_t size, size_t out_of_bounds) | |||||||
my_free(d); | ||||||||
} | ||||||||
|
||||||||
void check_overlaps1() | ||||||||
{ | ||||||||
size_t size = 16; | ||||||||
START_TEST("memmove overlaps1"); | ||||||||
auto* s = static_cast<unsigned int*>(my_malloc(size * sizeof(unsigned int))); | ||||||||
for (size_t i = 0; i < size; ++i) | ||||||||
{ | ||||||||
s[i] = static_cast<unsigned int>(i); | ||||||||
} | ||||||||
my_memmove(&s[2], &s[4], sizeof(s[0])); | ||||||||
EXPECT(s[2] == s[4], "overlap error: {} {}", s[2], s[4]); | ||||||||
my_memmove(&s[15], &s[5], sizeof(s[0])); | ||||||||
EXPECT(s[15] == s[5], "overlap error: {} {}", s[15], s[5]); | ||||||||
auto ptr = s; | ||||||||
my_memmove(ptr, s, size * sizeof(s[0])); | ||||||||
EXPECT(ptr == s, "overlap error: {} {}", ptr, s); | ||||||||
my_free(s); | ||||||||
} | ||||||||
|
||||||||
template<bool after> | ||||||||
void check_overlaps2(size_t size) | ||||||||
{ | ||||||||
START_TEST("memmove overlaps2, size {}", size); | ||||||||
auto sz = size / 2; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if it would be good to separate the offset from the size being copied. The current test only covers very small overlaps.
Suggested change
Then use |
||||||||
auto offset = size / 2; | ||||||||
auto* s = static_cast<unsigned int*>(my_malloc(size * sizeof(unsigned int))); | ||||||||
for (size_t i = 0; i < size; ++i) | ||||||||
{ | ||||||||
s[i] = static_cast<unsigned int>(i); | ||||||||
} | ||||||||
auto dst = after ? s + offset : s; | ||||||||
auto src = after ? s : s + offset; | ||||||||
size_t i = after ? 0 : offset; | ||||||||
size_t u = 0; | ||||||||
my_memmove(dst, src, sz * sizeof(unsigned int)); | ||||||||
while (u < sz) | ||||||||
{ | ||||||||
EXPECT(dst[u] == i, "overlap error: {} {}", dst[u], i); | ||||||||
u++; | ||||||||
i++; | ||||||||
} | ||||||||
my_free(s); | ||||||||
} | ||||||||
|
||||||||
int main() | ||||||||
{ | ||||||||
// Skip the checks that expect bounds checks to fail when we are not the | ||||||||
|
@@ -168,7 +228,20 @@ int main() | |||||||
# endif | ||||||||
for (size_t x = 0; x < 2048; x++) | ||||||||
{ | ||||||||
check_size(x); | ||||||||
check_size<false>(x); | ||||||||
} | ||||||||
|
||||||||
for (size_t x = 0; x < 2048; x++) | ||||||||
{ | ||||||||
check_size<true>(x); | ||||||||
} | ||||||||
|
||||||||
check_overlaps1(); | ||||||||
|
||||||||
for (size_t x = 8; x < 256; x += 64) | ||||||||
{ | ||||||||
check_overlaps2<false>(x); | ||||||||
check_overlaps2<true>(x); | ||||||||
} | ||||||||
} | ||||||||
#endif |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't
s[15]
out of bounds whensize == 8
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah right I forgot to change in the beginning the sizes set were fixed :-)