Skip to content

Commit

Permalink
Replace mimalloc patch with stubs (envoyproxy#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga authored Dec 15, 2022
1 parent feae078 commit d191eaa
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 42 deletions.
6 changes: 4 additions & 2 deletions buildtools/mimalloc/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ FROM ghcr.io/corazawaf/coraza-proxy-wasm/buildtools-wasi-sdk:main

RUN apt-get install -y cmake patch

# Enables export of aligned_alloc
# TODO(anuraaga): It seems like this should be set automatically, consider debugging further.
ENV CFLAGS -D__USE_ISOC11 ${CFLAGS}

RUN mkdir -p /mimalloc && curl -L https://github.com/microsoft/mimalloc/archive/refs/tags/v2.0.6.tar.gz | tar -xz --strip-components 1 -C /mimalloc
WORKDIR /mimalloc
ADD mimalloc.patch mimalloc.patch
RUN patch -p1 < mimalloc.patch
RUN mkdir -p out/release && cd out/release && cmake ../.. -DMI_BUILD_SHARED=off -DMI_BUILD_TESTS=off && make && ${RANLIB} libmimalloc.a

CMD ["cp", "./out/release/libmimalloc.a", "/out/libmimalloc.a"]
34 changes: 0 additions & 34 deletions buildtools/mimalloc/mimalloc.patch

This file was deleted.

40 changes: 40 additions & 0 deletions internal/gc/mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,43 @@ import "unsafe"

//go:linkname memzero runtime.memzero
func memzero(ptr unsafe.Pointer, size uintptr)

//export mi_aligned_alloc
func mi_aligned_alloc(alignment uintptr, size uintptr) unsafe.Pointer

//export mi_zalloc_aligned
func mi_zalloc_aligned(size uintptr, alignment uintptr) unsafe.Pointer

//export mi_malloc
func mi_malloc(size uintptr) unsafe.Pointer

//export mi_calloc
func mi_calloc(count uintptr, size uintptr) unsafe.Pointer

//export mi_free
func mi_free(ptr unsafe.Pointer)

// Not exported by mimalloc on __wasi__ by default so we implement here.

//export __libc_malloc
func __libc_malloc(size uintptr) unsafe.Pointer {
return mi_malloc(size)
}

//export __libc_calloc
func __libc_calloc(count uintptr, size uintptr) unsafe.Pointer {
return mi_calloc(count, size)
}

//export __libc_free
func __libc_free(ptr unsafe.Pointer) {
mi_free(ptr)
}

// Used by mimalloc for delayed free but Envoy doesn't stub it yet.
// We don't use delayed free, so it's fine to stub it out.

//export sched_yield
func sched_yield() int32 {
return 0
}
6 changes: 0 additions & 6 deletions internal/gc/mmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ import "C"
// Must match bdwgc value of HBLKSIZE
const hBlkSize = 4096

//export mi_zalloc_aligned
func mi_zalloc_aligned(size uintptr, alignment uintptr) unsafe.Pointer

//export mi_free
func mi_free(ptr unsafe.Pointer)

//export mmap
func mmap(_ unsafe.Pointer, length uintptr, _ int32, _ int32, _ int32, _ uint64) unsafe.Pointer {
buf := mi_zalloc_aligned(length, hBlkSize)
Expand Down
Binary file modified lib/libmimalloc.a
Binary file not shown.

0 comments on commit d191eaa

Please sign in to comment.