-
Notifications
You must be signed in to change notification settings - Fork 114
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
zstd-sys: impl wasm32-unknown-unknown support #139
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ea28d3d
impl wasm32-unknown-unknown support
quininer fe55976
Add zdict feature for zstd-sys
quininer 57fa938
Fix comment
quininer 7cb7ecb
Fix wasm DEBUGLEVEL :'D
quininer ba4eee0
Add zdict_builder feature to zstd/zstd-safe
quininer b271195
Fix test
quininer 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
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
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use std::convert::TryInto; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn zstd_version() -> u32 { | ||
unsafe { | ||
zstd_sys::ZSTD_versionNumber() | ||
} | ||
} | ||
|
||
macro_rules! zstd_check { | ||
( $ret:expr ) => {{ | ||
let ret = $ret; | ||
let error_code = unsafe { | ||
zstd_sys::ZSTD_isError(ret) | ||
}; | ||
assert_eq!(error_code, 0); | ||
}} | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn test_compress() -> bool { | ||
let fbuf = include_bytes!("../Cargo.toml"); | ||
|
||
let cbufsize = unsafe { | ||
zstd_sys::ZSTD_compressBound(fbuf.len()) | ||
}; | ||
let mut cbuf = vec![0; cbufsize]; | ||
|
||
let csize = unsafe { | ||
zstd_sys::ZSTD_compress(cbuf.as_mut_ptr().cast(), cbuf.len(), fbuf.as_ptr().cast(), fbuf.len(), 1) | ||
}; | ||
zstd_check!(csize); | ||
let cbuf = &cbuf[..csize]; | ||
|
||
let rsize = unsafe { | ||
zstd_sys::ZSTD_getFrameContentSize(cbuf.as_ptr().cast(), cbuf.len()) | ||
}; | ||
let rsize = rsize.try_into().unwrap(); | ||
let mut rbuf = vec![0; rsize]; | ||
|
||
let dsize = unsafe { | ||
zstd_sys::ZSTD_decompress(rbuf.as_mut_ptr().cast(), rbuf.len(), cbuf.as_ptr().cast(), cbuf.len()) | ||
}; | ||
zstd_check!(dsize); | ||
assert_eq!(dsize, rsize); | ||
|
||
&fbuf[..] == &rbuf[..] | ||
} | ||
|
||
fn main() {} |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::os::raw::{c_void, c_int}; | ||
use std::alloc::{alloc, dealloc, Layout}; | ||
|
||
|
||
#[no_mangle] | ||
pub extern "C" fn rust_zstd_wasm_shim_malloc(size: usize) -> *mut c_void { | ||
unsafe { | ||
let layout = Layout::from_size_align_unchecked(size, 1); | ||
alloc(layout).cast() | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn rust_zstd_wasm_shim_calloc(nmemb: usize, size: usize) -> *mut c_void { | ||
unsafe { | ||
let layout = Layout::from_size_align_unchecked(size * nmemb, 1); | ||
alloc(layout).cast() | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn rust_zstd_wasm_shim_free(ptr: *mut c_void) { | ||
// layout is not actually used | ||
let layout = Layout::from_size_align_unchecked(1, 1); | ||
dealloc(ptr.cast(), layout); | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn rust_zstd_wasm_shim_memcpy(dest: *mut c_void, src: *const c_void, n: usize) -> *mut c_void { | ||
std::ptr::copy_nonoverlapping(src as *const u8, dest as *mut u8, n); | ||
dest | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn rust_zstd_wasm_shim_memmove(dest: *mut c_void, src: *const c_void, n: usize) -> *mut c_void { | ||
std::ptr::copy(src as *const u8, dest as *mut u8, n); | ||
dest | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn rust_zstd_wasm_shim_memset(dest: *mut c_void, c: c_int, n: usize) -> *mut c_void { | ||
std::ptr::write_bytes(dest as *mut u8, c as u8, n); | ||
dest | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <stddef.h> | ||
|
||
#ifndef _STDLIB_H | ||
#define _STDLIB_H 1 | ||
|
||
void *rust_zstd_wasm_shim_malloc(size_t size); | ||
void *rust_zstd_wasm_shim_calloc(size_t nmemb, size_t size); | ||
void rust_zstd_wasm_shim_free(void *ptr); | ||
|
||
inline void *malloc(size_t size) { | ||
return rust_zstd_wasm_shim_malloc(size); | ||
} | ||
|
||
inline void *calloc(size_t nmemb, size_t size) { | ||
return rust_zstd_wasm_shim_calloc(nmemb, size); | ||
} | ||
|
||
inline void free(void *ptr) { | ||
rust_zstd_wasm_shim_free(ptr); | ||
} | ||
|
||
#endif // _STDLIB_H |
Oops, something went wrong.
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.
You can specify that an example requires some features. In
Cargo.toml
:This way, in the example itself, you can just assume the feature is set.