-
Notifications
You must be signed in to change notification settings - Fork 255
add implementations of memcpy et al #128
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| #[no_mangle] | ||
| pub unsafe extern "C" fn memcpy(dest: *mut u8, | ||
| src: *const u8, | ||
| n: usize) | ||
| -> *mut u8 { | ||
| let mut i = 0; | ||
| while i < n { | ||
| *dest.offset(i as isize) = *src.offset(i as isize); | ||
| i += 1; | ||
| } | ||
| dest | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub unsafe extern "C" fn memmove(dest: *mut u8, | ||
| src: *const u8, | ||
| n: usize) | ||
| -> *mut u8 { | ||
| if src < dest as *const u8 { | ||
| // copy from end | ||
| let mut i = n; | ||
| while i != 0 { | ||
| i -= 1; | ||
| *dest.offset(i as isize) = *src.offset(i as isize); | ||
| } | ||
| } else { | ||
| // copy from beginning | ||
| let mut i = 0; | ||
| while i < n { | ||
| *dest.offset(i as isize) = *src.offset(i as isize); | ||
| i += 1; | ||
| } | ||
| } | ||
| dest | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub unsafe extern "C" fn memset(s: *mut u8, c: i32, n: usize) -> *mut u8 { | ||
| let mut i = 0; | ||
| while i < n { | ||
| *s.offset(i as isize) = c as u8; | ||
| i += 1; | ||
| } | ||
| s | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 { | ||
| let mut i = 0; | ||
| while i < n { | ||
| let a = *s1.offset(i as isize); | ||
| let b = *s2.offset(i as isize); | ||
| if a != b { | ||
| return a as i32 - b as i32; | ||
| } | ||
| i += 1; | ||
| } | ||
| 0 | ||
| } | ||
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.
Why
cisi32? Will it work on all architectures? maybe it should beisizeorusize?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.
It should be
c_intbut it's better not to depend onlibc, which is wherec_intis defined. Right now,c_intis defined astype c_int = i32inlibc.That's a good question. AVR seems to define
intas 16-bit. I don't know about MSP430. You probably know that better. It's likely that all these implementations don't work correctly on architectures wheresizeof(usize)is neither 32 or 64.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.
Yes, on MSP430
intis 16bit. And I would like to have a working memset on MSP430. My point is that if you make itisizeinstead ofi32it will be compatible with all architectures that rust supports AFAIK.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.
But 64-bit architectures expect
cto bei32(c_intis 32-bit on 64 and 32 bit architectures).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.
Both x86_64 and AArch64 do not care if you pass a 64bit or 32bit value inside their 64bit registers. And the receiving code does cast to i8 anyway.