Skip to content

Commit

Permalink
Add more wasm shims to enable zdict builder
Browse files Browse the repository at this point in the history
  • Loading branch information
gyscos committed Oct 10, 2023
1 parent 86c5f6c commit ffb7a9b
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 0 deletions.
3 changes: 3 additions & 0 deletions zstd-safe/zstd-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#[cfg(target_arch = "wasm32")]
extern crate alloc;

#[cfg(target_arch = "wasm32")]
extern crate std;

#[cfg(target_arch = "wasm32")]
mod wasm_shim;

Expand Down
59 changes: 59 additions & 0 deletions zstd-safe/zstd-sys/src/wasm_shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,70 @@ use core::ffi::{c_int, c_void};
const USIZE_ALIGN: usize = core::mem::align_of::<usize>();
const USIZE_SIZE: usize = core::mem::size_of::<usize>();

#[no_mangle]
pub extern "C" fn rust_zstd_wasm_shim_clock() -> u64 {
std::time::UNIX_EPOCH.elapsed().unwrap().as_millis() as u64
}

#[no_mangle]
pub extern "C" fn rust_zstd_wasm_shim_qsort(
base: *mut c_void,
n_items: usize,
size: usize,
compar: extern "C" fn(*const c_void, *const c_void) -> c_int,
) {
unsafe {
match size {
1 => qsort::<1>(base, n_items, compar),
2 => qsort::<2>(base, n_items, compar),
4 => qsort::<4>(base, n_items, compar),
8 => qsort::<8>(base, n_items, compar),
16 => qsort::<16>(base, n_items, compar),
_ => panic!("Unsupported qsort item size"),
}
}
}

unsafe fn qsort<const N: usize>(
base: *mut c_void,
n_items: usize,
compar: extern "C" fn(*const c_void, *const c_void) -> c_int,
) {
let base: &mut [[u8; N]] =
core::slice::from_raw_parts_mut(base as *mut [u8; N], n_items);
base.sort_unstable_by(|a, b| {
match compar(a.as_ptr() as *const c_void, b.as_ptr() as *const c_void)
{
..=-1 => core::cmp::Ordering::Less,
0 => core::cmp::Ordering::Equal,
1.. => core::cmp::Ordering::Greater,
}
});
}

#[no_mangle]
pub extern "C" fn rust_zstd_wasm_shim_malloc(size: usize) -> *mut c_void {
wasm_shim_alloc::<false>(size)
}

#[no_mangle]
pub extern "C" fn rust_zstd_wasm_shim_memcmp(
str1: *const c_void,
str2: *const c_void,
n: usize,
) -> i32 {
// Safety: function contracts requires str1 and str2 at least `n`-long.
unsafe {
let str1: &[u8] = core::slice::from_raw_parts(str1 as *const u8, n);
let str2: &[u8] = core::slice::from_raw_parts(str2 as *const u8, n);
match str1.cmp(str2) {
core::cmp::Ordering::Less => -1,
core::cmp::Ordering::Equal => 0,
core::cmp::Ordering::Greater => 1,
}
}
}

#[no_mangle]
pub extern "C" fn rust_zstd_wasm_shim_calloc(
nmemb: usize,
Expand Down
6 changes: 6 additions & 0 deletions zstd-safe/zstd-sys/wasm-shim/assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef _ASSERT_H
#define _ASSERT_H

#define assert(expr)

#endif // _ASSERT_H
10 changes: 10 additions & 0 deletions zstd-safe/zstd-sys/wasm-shim/stdio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <stddef.h>

#ifndef _STDIO_H
#define _STDIO_H 1

#define fprintf(expr, ...)
#define fflush(expr)

#endif // _STDIO_H

6 changes: 6 additions & 0 deletions zstd-safe/zstd-sys/wasm-shim/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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);
void rust_zstd_wasm_shim_qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*));

inline void *malloc(size_t size) {
return rust_zstd_wasm_shim_malloc(size);
Expand All @@ -19,4 +20,9 @@ inline void free(void *ptr) {
rust_zstd_wasm_shim_free(ptr);
}

inline void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))
{
return rust_zstd_wasm_shim_qsort(base, nitems, size, compar);
}

#endif // _STDLIB_H
5 changes: 5 additions & 0 deletions zstd-safe/zstd-sys/wasm-shim/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
#ifndef _STRING_H
#define _STRING_H 1

int rust_zstd_wasm_shim_memcmp(const void *str1, const void *str2, size_t n);
void *rust_zstd_wasm_shim_memcpy(void *restrict dest, const void *restrict src, size_t n);
void *rust_zstd_wasm_shim_memmove(void *dest, const void *src, size_t n);
void *rust_zstd_wasm_shim_memset(void *dest, int c, size_t n);

inline int memcmp(const void *str1, const void *str2, size_t n) {
return rust_zstd_wasm_shim_memcmp(str1, str2, n);
}

inline void *memcpy(void *restrict dest, const void *restrict src, size_t n) {
return rust_zstd_wasm_shim_memcpy(dest, src, n);
}
Expand Down
13 changes: 13 additions & 0 deletions zstd-safe/zstd-sys/wasm-shim/time.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _TIME_H
#define _TIME_H

#define CLOCKS_PER_SEC 1000
typedef unsigned long long clock_t;

clock_t rust_zstd_wasm_shim_clock();

inline clock_t clock() {
return rust_zstd_wasm_shim_clock();
}

#endif // _TIME_H

0 comments on commit ffb7a9b

Please sign in to comment.