1
0
mirror of https://github.com/bspeice/qadapt synced 2024-11-21 13:28:11 -05:00

Use the system allocator

This commit is contained in:
Bradlee Speice 2018-12-16 16:29:25 -05:00
parent a9f09b529e
commit 964e9c5a11
3 changed files with 13 additions and 15 deletions

View File

@ -19,9 +19,7 @@ edition = "2018"
maintenance = { status = "actively-developed" }
[dependencies]
libc = "0.2"
thread-id = "3.3"
qadapt-macro = { version = "1.0.0", path = "./qadapt-macro" }
qadapt-spin = { version = "1.0.0", path = "./qadapt-spin" }

View File

@ -1,14 +1,14 @@
environment:
matrix:
# MinGW
# - TARGET: i686-pc-windows-gnu
# RUST_VERSION: nightly
# - TARGET: x86_64-pc-windows-gnu
# RUST_VERSION: nightly
- TARGET: i686-pc-windows-gnu
RUST_VERSION: nightly
- TARGET: x86_64-pc-windows-gnu
RUST_VERSION: nightly
# MSVC
# - TARGET: i686-pc-windows-msvc
# RUST_VERSION: nightly
- TARGET: i686-pc-windows-msvc
RUST_VERSION: nightly
- TARGET: x86_64-pc-windows-msvc
RUST_VERSION: nightly
- TARGET: x86_64-pc-windows-msvc

View File

@ -69,12 +69,10 @@ use thread_id;
// Re-export the proc macros to use by other code
pub use qadapt_macro::*;
use libc::c_void;
use libc::free;
use libc::malloc;
use qadapt_spin::RwLock;
use std::alloc::GlobalAlloc;
use std::alloc::Layout;
use std::alloc::System;
use std::thread;
thread_local! {
@ -94,6 +92,8 @@ thread_local! {
/// ```
pub struct QADAPT;
static SYSTEM_ALLOC: System = System;
/// Let QADAPT know that we are now entering a protected region and that
/// panics should be triggered if allocations/drops happen while we are running.
///
@ -286,7 +286,7 @@ unsafe impl GlobalAlloc for QADAPT {
// If we're attempting to allocate our PROTECTION_LEVEL thread local,
// just allow it through
if alloc_immediate() {
return malloc(layout.size()) as *mut u8;
return SYSTEM_ALLOC.alloc(layout);
}
// Because accessing PROTECTION_LEVEL has the potential to trigger an allocation,
@ -297,7 +297,7 @@ unsafe impl GlobalAlloc for QADAPT {
release_internal_alloc();
match protection_level {
Ok(v) if v == 0 => malloc(layout.size()) as *mut u8,
Ok(v) if v == 0 => SYSTEM_ALLOC.alloc(layout),
Ok(v) => {
// Tripped a bad allocation, but make sure further memory access during unwind
// doesn't have issues
@ -314,7 +314,7 @@ unsafe impl GlobalAlloc for QADAPT {
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
if alloc_immediate() {
return free(ptr as *mut c_void);
return SYSTEM_ALLOC.dealloc(ptr, layout);
}
claim_internal_alloc();
@ -323,7 +323,7 @@ unsafe impl GlobalAlloc for QADAPT {
release_internal_alloc();
// Free before checking panic to make sure we avoid leaks
free(ptr as *mut c_void);
SYSTEM_ALLOC.dealloc(ptr, layout);
match protection_level {
Ok(v) if v > 0 => {
// Tripped a bad drop, but make sure further memory access during unwind