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:
parent
a9f09b529e
commit
964e9c5a11
@ -19,9 +19,7 @@ edition = "2018"
|
|||||||
maintenance = { status = "actively-developed" }
|
maintenance = { status = "actively-developed" }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
libc = "0.2"
|
|
||||||
thread-id = "3.3"
|
thread-id = "3.3"
|
||||||
|
|
||||||
qadapt-macro = { version = "1.0.0", path = "./qadapt-macro" }
|
qadapt-macro = { version = "1.0.0", path = "./qadapt-macro" }
|
||||||
qadapt-spin = { version = "1.0.0", path = "./qadapt-spin" }
|
qadapt-spin = { version = "1.0.0", path = "./qadapt-spin" }
|
||||||
|
|
||||||
|
12
appveyor.yml
12
appveyor.yml
@ -1,14 +1,14 @@
|
|||||||
environment:
|
environment:
|
||||||
matrix:
|
matrix:
|
||||||
# MinGW
|
# MinGW
|
||||||
# - TARGET: i686-pc-windows-gnu
|
- TARGET: i686-pc-windows-gnu
|
||||||
# RUST_VERSION: nightly
|
RUST_VERSION: nightly
|
||||||
# - TARGET: x86_64-pc-windows-gnu
|
- TARGET: x86_64-pc-windows-gnu
|
||||||
# RUST_VERSION: nightly
|
RUST_VERSION: nightly
|
||||||
|
|
||||||
# MSVC
|
# MSVC
|
||||||
# - TARGET: i686-pc-windows-msvc
|
- TARGET: i686-pc-windows-msvc
|
||||||
# RUST_VERSION: nightly
|
RUST_VERSION: nightly
|
||||||
- TARGET: x86_64-pc-windows-msvc
|
- TARGET: x86_64-pc-windows-msvc
|
||||||
RUST_VERSION: nightly
|
RUST_VERSION: nightly
|
||||||
- TARGET: x86_64-pc-windows-msvc
|
- TARGET: x86_64-pc-windows-msvc
|
||||||
|
14
src/lib.rs
14
src/lib.rs
@ -69,12 +69,10 @@ use thread_id;
|
|||||||
// Re-export the proc macros to use by other code
|
// Re-export the proc macros to use by other code
|
||||||
pub use qadapt_macro::*;
|
pub use qadapt_macro::*;
|
||||||
|
|
||||||
use libc::c_void;
|
|
||||||
use libc::free;
|
|
||||||
use libc::malloc;
|
|
||||||
use qadapt_spin::RwLock;
|
use qadapt_spin::RwLock;
|
||||||
use std::alloc::GlobalAlloc;
|
use std::alloc::GlobalAlloc;
|
||||||
use std::alloc::Layout;
|
use std::alloc::Layout;
|
||||||
|
use std::alloc::System;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
thread_local! {
|
thread_local! {
|
||||||
@ -94,6 +92,8 @@ thread_local! {
|
|||||||
/// ```
|
/// ```
|
||||||
pub struct QADAPT;
|
pub struct QADAPT;
|
||||||
|
|
||||||
|
static SYSTEM_ALLOC: System = System;
|
||||||
|
|
||||||
/// Let QADAPT know that we are now entering a protected region and that
|
/// 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.
|
/// 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,
|
// If we're attempting to allocate our PROTECTION_LEVEL thread local,
|
||||||
// just allow it through
|
// just allow it through
|
||||||
if alloc_immediate() {
|
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,
|
// Because accessing PROTECTION_LEVEL has the potential to trigger an allocation,
|
||||||
@ -297,7 +297,7 @@ unsafe impl GlobalAlloc for QADAPT {
|
|||||||
release_internal_alloc();
|
release_internal_alloc();
|
||||||
|
|
||||||
match protection_level {
|
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) => {
|
Ok(v) => {
|
||||||
// Tripped a bad allocation, but make sure further memory access during unwind
|
// Tripped a bad allocation, but make sure further memory access during unwind
|
||||||
// doesn't have issues
|
// doesn't have issues
|
||||||
@ -314,7 +314,7 @@ unsafe impl GlobalAlloc for QADAPT {
|
|||||||
|
|
||||||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
||||||
if alloc_immediate() {
|
if alloc_immediate() {
|
||||||
return free(ptr as *mut c_void);
|
return SYSTEM_ALLOC.dealloc(ptr, layout);
|
||||||
}
|
}
|
||||||
|
|
||||||
claim_internal_alloc();
|
claim_internal_alloc();
|
||||||
@ -323,7 +323,7 @@ unsafe impl GlobalAlloc for QADAPT {
|
|||||||
release_internal_alloc();
|
release_internal_alloc();
|
||||||
|
|
||||||
// Free before checking panic to make sure we avoid leaks
|
// Free before checking panic to make sure we avoid leaks
|
||||||
free(ptr as *mut c_void);
|
SYSTEM_ALLOC.dealloc(ptr, layout);
|
||||||
match protection_level {
|
match protection_level {
|
||||||
Ok(v) if v > 0 => {
|
Ok(v) if v > 0 => {
|
||||||
// Tripped a bad drop, but make sure further memory access during unwind
|
// Tripped a bad drop, but make sure further memory access during unwind
|
||||||
|
Loading…
Reference in New Issue
Block a user