mimalloc-0.1.29/.cargo_vcs_info.json0000644000000001360000000000100127220ustar { "git": { "sha1": "d5bfccf87f18ff0fe58faa2cd7903beee22f5ec6" }, "path_in_vcs": "" }mimalloc-0.1.29/.github/workflows/ci.yml000064400000000000000000000055410072674642500162620ustar 00000000000000name: CI on: push: pull_request: # Run daily to catch when Rust updates cause problems to happen. schedule: - cron: '00 01 * * *' jobs: rust: name: Rust strategy: matrix: os: [ubuntu-latest, macos-latest, windows-latest] toolchain: ["stable"] runs-on: ${{ matrix.os }} env: CARGO_INCREMENTAL: 0 RUST_BACKTRACE: 1 steps: - name: Checkout repository uses: actions/checkout@v1 with: submodules: recursive - name: Install Rust toolchain uses: actions-rs/toolchain@v1 with: toolchain: ${{ matrix.toolchain }} profile: minimal default: true - name: Build (secure) uses: actions-rs/cargo@v1 with: command: build - name: Test (secure) uses: actions-rs/cargo@v1 with: command: test args: --no-default-features - name: Build (no secure) uses: actions-rs/cargo@v1 with: command: build args: --no-default-features - name: Test (no secure) uses: actions-rs/cargo@v1 with: command: test args: --no-default-features - name: Test libmimalloc-sys crate bindings. uses: actions-rs/cargo@v1 with: command: run args: -p libmimalloc-sys-test lint: name: Rustfmt / Clippy runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 with: submodules: recursive - uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: stable components: rustfmt, clippy - name: Fmt uses: actions-rs/cargo@v1 with: command: fmt args: --all -- --check - name: Clippy uses: actions-rs/cargo@v1 with: command: clippy args: --workspace -- -D warnings # Detect cases where documentation links would be dead doc: name: Check documentation runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 with: submodules: recursive - uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: nightly override: true # Note: We need to use nightly rust, and `cargo rustdoc` (yes, not `cargo # doc`) to actually get it to respect -D warnings... Using nightly also # gets us the nicer syntax for linking to functions, and is in-line with # what docs.rs uses. - name: 'Check documentation links in `mimalloc`' uses: actions-rs/cargo@v1 with: command: rustdoc args: -- -D warnings - name: 'Check documentation links in `libmimalloc-sys`' uses: actions-rs/cargo@v1 with: command: rustdoc args: -p libmimalloc-sys -- -D warnings mimalloc-0.1.29/.gitignore000064400000000000000000000000350072674642500135300ustar 00000000000000/target **/*.rs.bk Cargo.lockmimalloc-0.1.29/.gitmodules000064400000000000000000000002010072674642500137100ustar 00000000000000[submodule "libmimalloc-sys/c_src/mimalloc"] path = libmimalloc-sys/c_src/mimalloc url = https://github.com/microsoft/mimalloc mimalloc-0.1.29/.travis.yml000064400000000000000000000023330072674642500136540ustar 00000000000000language: rust sudo: required rust: - stable - beta - nightly os: - windows - linux - osx dist: xenial matrix: allow_failures: - rust: nightly - os: windows addons: apt: packages: - cmake - gcc language: rust cache: cargo before_script: # Remove sh.exe from the path otherwise CMake will complain: # "sh.exe was found in your PATH, here: C:/Program Files/Git/usr/bin/sh.exe" # and the MinGW build will not work (the Visual Studio build does not care). # See http://help.appveyor.com/discussions/problems/3193-cmake-building-for-mingw-issue-with-git-shexe # The entire directory containing sh.exe could be removed from the PATH environment: # - set PATH=%PATH:C:\Program Files\Git\usr\bin;=% # However, this will also remove all other programs in this directory from the PATH. # In particular "patch" is still required. # So, just rename sh.exe: - if [ "$TRAVIS_OS_NAME" = "windows" ]; then mv "C:\Program Files\Git\usr\bin\sh.exe" "C:\Program Files\Git\usr\bin\_sh.exe" ; fi script: - cargo test --verbose --all - cargo test --verbose --all --no-default-features - cargo test --release --verbose --all - cargo test --release --verbose --all --no-default-features mimalloc-0.1.29/Cargo.toml0000644000000025050000000000100107220ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "mimalloc" version = "0.1.29" authors = ["Octavian Oncescu ", "Vincent Rouillé ", "Thom Chiovoloni "] description = "Performance and security oriented drop-in allocator" readme = "README.md" keywords = ["mimalloc", "allocator", "encrypted-heap", "performance"] categories = ["memory-management", "api-bindings"] license = "MIT" repository = "https://github.com/purpleprotocol/mimalloc_rust" [dependencies.libmimalloc-sys] version = "0.1.25" default-features = false [features] debug = ["libmimalloc-sys/debug"] debug_in_debug = ["libmimalloc-sys/debug_in_debug"] default = ["secure"] local_dynamic_tls = ["libmimalloc-sys/local_dynamic_tls"] override = ["libmimalloc-sys/override"] secure = ["libmimalloc-sys/secure"] [badges.travis-ci] repository = "purpleprotocol/mimalloc_rust" mimalloc-0.1.29/Cargo.toml.orig000064400000000000000000000017610072674642500144360ustar 00000000000000[package] name = "mimalloc" version = "0.1.29" authors = [ "Octavian Oncescu ", "Vincent Rouillé ", "Thom Chiovoloni ", ] edition = "2018" repository = "https://github.com/purpleprotocol/mimalloc_rust" keywords = ["mimalloc", "allocator", "encrypted-heap", "performance"] categories = ["memory-management", "api-bindings"] description = "Performance and security oriented drop-in allocator" license = "MIT" readme = "README.md" [workspace] members = ["libmimalloc-sys", "libmimalloc-sys/sys-test"] [badges] travis-ci = { repository = "purpleprotocol/mimalloc_rust" } [dependencies] libmimalloc-sys = { path = "libmimalloc-sys", version = "0.1.25", default-features = false } [features] default = ["secure"] secure = ["libmimalloc-sys/secure"] override = ["libmimalloc-sys/override"] debug = ["libmimalloc-sys/debug"] debug_in_debug = ["libmimalloc-sys/debug_in_debug"] local_dynamic_tls = ["libmimalloc-sys/local_dynamic_tls"] mimalloc-0.1.29/LICENSE.txt000064400000000000000000000020370072674642500133670ustar 00000000000000Copyright 2019 Octavian Oncescu Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.mimalloc-0.1.29/README.md000064400000000000000000000023330072674642500130220ustar 00000000000000# Mimalloc Rust [![Build Status](https://travis-ci.org/purpleprotocol/mimalloc_rust.svg?branch=master)](https://travis-ci.org/purpleprotocol/mimalloc_rust) [![Latest Version]][crates.io] [![Documentation]][docs.rs] A drop-in global allocator wrapper around the [mimalloc](https://github.com/microsoft/mimalloc) allocator. Mimalloc is a general purpose, performance oriented allocator built by Microsoft. ## Usage ```rust use mimalloc::MiMalloc; #[global_allocator] static GLOBAL: MiMalloc = MiMalloc; ``` ## Requirements A __C__ compiler is required for building [mimalloc](https://github.com/microsoft/mimalloc) with cargo. ## Usage without secure mode By default this library builds mimalloc in secure mode. This add guard pages, randomized allocation, encrypted free lists, etc. The performance penalty is usually around 10% according to [mimalloc](https://github.com/microsoft/mimalloc) own benchmarks. To disable secure mode, put in `Cargo.toml`: ```ini [dependencies] mimalloc = { version = "*", default-features = false } ``` [crates.io]: https://crates.io/crates/mimalloc [Latest Version]: https://img.shields.io/crates/v/mimalloc.svg [Documentation]: https://docs.rs/mimalloc/badge.svg [docs.rs]: https://docs.rs/mimalloc mimalloc-0.1.29/src/lib.rs000064400000000000000000000121520072674642500134460ustar 00000000000000// Copyright 2019 Octavian Oncescu #![no_std] //! A drop-in global allocator wrapper around the [mimalloc](https://github.com/microsoft/mimalloc) allocator. //! Mimalloc is a general purpose, performance oriented allocator built by Microsoft. //! //! ## Usage //! ```rust,ignore //! use mimalloc::MiMalloc; //! //! #[global_allocator] //! static GLOBAL: MiMalloc = MiMalloc; //! ``` //! //! ## Usage without secure mode //! By default this library builds mimalloc in secure mode. This means that //! heap allocations are encrypted, but this results in a 3% increase in overhead. //! //! To disable secure mode, in `Cargo.toml`: //! ```rust,ignore //! [dependencies] //! mimalloc = { version = "*", default-features = false } //! ``` extern crate libmimalloc_sys as ffi; use core::alloc::{GlobalAlloc, Layout}; use core::ffi::c_void; use ffi::*; // `MI_MAX_ALIGN_SIZE` is 16 unless manually overridden: // https://github.com/microsoft/mimalloc/blob/15220c68/include/mimalloc-types.h#L22 // // If it changes on us, we should consider either manually overriding it, or // expose it from the -sys crate (in order to catch updates) const MI_MAX_ALIGN_SIZE: usize = 16; // Note: this doesn't take a layout directly because doing so would be wrong for // reallocation #[inline] fn may_use_unaligned_api(size: usize, alignment: usize) -> bool { // Required by `GlobalAlloc`. Note that while allocators aren't allowed to // unwind in rust, this is only in debug mode, and can only happen if the // caller already caused UB by passing in an invalid layout. debug_assert!(size != 0 && alignment.is_power_of_two()); // This logic is based on the discussion [here]. We don't bother with the // 3rd suggested test due to it being high cost (calling `mi_good_size`) // compared to the other checks, and also feeling like it relies on too much // implementation-specific behavior. // // [here]: https://github.com/microsoft/mimalloc/issues/314#issuecomment-708541845 (alignment <= MI_MAX_ALIGN_SIZE && size >= alignment) || (alignment == size && alignment <= 4096) } /// Drop-in mimalloc global allocator. /// /// ## Usage /// ```rust,ignore /// use mimalloc::MiMalloc; /// /// #[global_allocator] /// static GLOBAL: MiMalloc = MiMalloc; /// ``` pub struct MiMalloc; unsafe impl GlobalAlloc for MiMalloc { #[inline] unsafe fn alloc(&self, layout: Layout) -> *mut u8 { if may_use_unaligned_api(layout.size(), layout.align()) { mi_malloc(layout.size()) as *mut u8 } else { mi_malloc_aligned(layout.size(), layout.align()) as *mut u8 } } #[inline] unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { if may_use_unaligned_api(layout.size(), layout.align()) { mi_zalloc(layout.size()) as *mut u8 } else { mi_zalloc_aligned(layout.size(), layout.align()) as *mut u8 } } #[inline] unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { mi_free(ptr as *mut c_void); } #[inline] unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { if may_use_unaligned_api(new_size, layout.align()) { mi_realloc(ptr as *mut c_void, new_size) as *mut u8 } else { mi_realloc_aligned(ptr as *mut c_void, new_size, layout.align()) as *mut u8 } } } #[cfg(test)] mod tests { use super::*; #[test] fn it_frees_allocated_memory() { unsafe { let layout = Layout::from_size_align(8, 8).unwrap(); let alloc = MiMalloc; let ptr = alloc.alloc(layout); alloc.dealloc(ptr, layout); } } #[test] fn it_frees_allocated_big_memory() { unsafe { let layout = Layout::from_size_align(1 << 20, 32).unwrap(); let alloc = MiMalloc; let ptr = alloc.alloc(layout); alloc.dealloc(ptr, layout); } } #[test] fn it_frees_zero_allocated_memory() { unsafe { let layout = Layout::from_size_align(8, 8).unwrap(); let alloc = MiMalloc; let ptr = alloc.alloc_zeroed(layout); alloc.dealloc(ptr, layout); } } #[test] fn it_frees_zero_allocated_big_memory() { unsafe { let layout = Layout::from_size_align(1 << 20, 32).unwrap(); let alloc = MiMalloc; let ptr = alloc.alloc_zeroed(layout); alloc.dealloc(ptr, layout); } } #[test] fn it_frees_reallocated_memory() { unsafe { let layout = Layout::from_size_align(8, 8).unwrap(); let alloc = MiMalloc; let ptr = alloc.alloc(layout); let ptr = alloc.realloc(ptr, layout, 16); alloc.dealloc(ptr, layout); } } #[test] fn it_frees_reallocated_big_memory() { unsafe { let layout = Layout::from_size_align(1 << 20, 32).unwrap(); let alloc = MiMalloc; let ptr = alloc.alloc(layout); let ptr = alloc.realloc(ptr, layout, 2 << 20); alloc.dealloc(ptr, layout); } } }