From 9c1c1b785176475140f3537633a79adaad4ab99f Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Wed, 25 Apr 2018 12:00:50 -0400 Subject: [PATCH] Debug Handle tracker --- .travis.yml | 1 + Cargo.lock | 17 +++++++++-------- Makefile | 3 +++ libportability-gfx/Cargo.toml | 2 +- libportability-gfx/src/handle.rs | 31 ++++++++++++++++++++++++++++++- libportability-gfx/src/impls.rs | 4 ++++ libportability-gfx/src/lib.rs | 1 + libportability/Cargo.toml | 1 + 8 files changed, 50 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index 855552a..8257cc0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -52,3 +52,4 @@ script: - if [[ $TRAVIS_RUST_VERSION == "nightly" && $TRAVIS_BRANCH == "staging" ]]; then exit; fi - export PATH=$PATH:$HOME/deps/bin - make + - if [[ $TRAVIS_RUST_VERSION == "nightly" ]]; then make debug release; fi diff --git a/Cargo.lock b/Cargo.lock index 8155939..f8e643b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -215,14 +215,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "gfx-backend-dx12" version = "0.1.0" -source = "git+https://github.com/gfx-rs/gfx#1083ce9ab7f320a746668c06e79964f2514b745c" +source = "git+https://github.com/gfx-rs/gfx#0fe18fa91300a63267e6e19cb3b0f77ca734ea2d" dependencies = [ "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "derivative 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "gfx-hal 0.1.0 (git+https://github.com/gfx-rs/gfx)", "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "smallvec 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", - "spirv_cross 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)", + "spirv_cross 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "winit 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "wio 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -231,7 +231,7 @@ dependencies = [ [[package]] name = "gfx-backend-metal" version = "0.1.0" -source = "git+https://github.com/gfx-rs/gfx#1083ce9ab7f320a746668c06e79964f2514b745c" +source = "git+https://github.com/gfx-rs/gfx#0fe18fa91300a63267e6e19cb3b0f77ca734ea2d" dependencies = [ "block 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "cocoa 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -243,14 +243,15 @@ dependencies = [ "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "metal-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "objc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "spirv_cross 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "spirv_cross 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "winit 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "gfx-backend-vulkan" version = "0.1.0" -source = "git+https://github.com/gfx-rs/gfx#1083ce9ab7f320a746668c06e79964f2514b745c" +source = "git+https://github.com/gfx-rs/gfx#0fe18fa91300a63267e6e19cb3b0f77ca734ea2d" dependencies = [ "ash 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -268,7 +269,7 @@ dependencies = [ [[package]] name = "gfx-hal" version = "0.1.0" -source = "git+https://github.com/gfx-rs/gfx#1083ce9ab7f320a746668c06e79964f2514b745c" +source = "git+https://github.com/gfx-rs/gfx#0fe18fa91300a63267e6e19cb3b0f77ca734ea2d" dependencies = [ "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -566,7 +567,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "spirv_cross" -version = "0.7.4" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cc 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)", @@ -901,7 +902,7 @@ dependencies = [ "checksum rustc-demangle 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "11fb43a206a04116ffd7cfcf9bcb941f8eb6cc7ff667272246b0a1c74259a3cb" "checksum shared_library 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8254bf098ce4d8d7cc7cc6de438c5488adc5297e5b7ffef88816c0a91bd289c1" "checksum smallvec 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "44db0ecb22921ef790d17ae13a3f6d15784183ff5f2a01aa32098c7498d2b4b9" -"checksum spirv_cross 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)" = "e0a28a50f7633f82624147f7c9524fe31002fb7e24e897272eb244c61ef74178" +"checksum spirv_cross 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "794a3850a6d16be4a4ea1536ff0f416697a95598e1fe7d926537ac78dc10750c" "checksum syn 0.10.8 (registry+https://github.com/rust-lang/crates.io-index)" = "58fd09df59565db3399efbba34ba8a2fec1307511ebd245d0061ff9d42691673" "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" diff --git a/Makefile b/Makefile index 61e9582..9cf95b2 100644 --- a/Makefile +++ b/Makefile @@ -45,6 +45,9 @@ LIBRARY_FAST=target/release/libportability.$(LIB_EXTENSION) all: $(TARGET) +debug: + cargo build --manifest-path libportability/Cargo.toml --features "$(BACKEND) debug" + release: $(LIBRARY_FAST) binding: $(BINDING) diff --git a/libportability-gfx/Cargo.toml b/libportability-gfx/Cargo.toml index 0c7e15a..b884c65 100644 --- a/libportability-gfx/Cargo.toml +++ b/libportability-gfx/Cargo.toml @@ -13,7 +13,7 @@ name = "portability_gfx" [features] default = [] dispatch = [] -#default = ["env_logger"] # uncomment for debugging +nightly = [] [dependencies] lazy_static = "1.0" diff --git a/libportability-gfx/src/handle.rs b/libportability-gfx/src/handle.rs index f75739b..234919d 100644 --- a/libportability-gfx/src/handle.rs +++ b/libportability-gfx/src/handle.rs @@ -1,13 +1,38 @@ use VK_NULL_HANDLE; use std::{borrow, cmp, fmt, ops}; +#[cfg(feature = "nightly")] +use std::collections::HashMap; +#[cfg(feature = "nightly")] +use std::sync::{Arc, Mutex}; +#[cfg(feature = "nightly")] +lazy_static! { + static ref REGISTRY: Arc>> = Arc::new(Mutex::new(HashMap::new())); +} #[repr(C)] pub struct Handle(*mut T); -impl Handle { +#[cfg(feature = "nightly")] +impl Handle<()> { + pub fn report_leaks() { + println!("Leaked handles:"); + let mut map = REGISTRY.lock().unwrap(); + for (_, type_id) in map.drain() { + println!("\t{:?}", type_id); + } + } +} + +impl Handle { pub fn new(value: T) -> Self { let ptr = Box::into_raw(Box::new(value)); + #[cfg(feature = "nightly")] + { + use std::intrinsics::type_name; + let name = unsafe { type_name::() }; + REGISTRY.lock().unwrap().insert(ptr as _, name); + } Handle(ptr) } @@ -19,6 +44,10 @@ impl Handle { if self.0 == VK_NULL_HANDLE as *mut T { None } else { + #[cfg(feature = "nightly")] + { + REGISTRY.lock().unwrap().remove(&(self.0 as _)).unwrap(); + } Some(*unsafe { Box::from_raw(self.0) }) } } diff --git a/libportability-gfx/src/impls.rs b/libportability-gfx/src/impls.rs index 7d403f6..d1c2859 100644 --- a/libportability-gfx/src/impls.rs +++ b/libportability-gfx/src/impls.rs @@ -60,6 +60,10 @@ pub extern "C" fn gfxDestroyInstance( instance: VkInstance, _pAllocator: *const VkAllocationCallbacks, ) { + #[cfg(feature = "nightly")] + { + Handle::report_leaks(); + } for adapter in instance.unbox().unwrap().adapters { let _ = adapter.unbox(); } diff --git a/libportability-gfx/src/lib.rs b/libportability-gfx/src/lib.rs index eb053bd..3d932ca 100644 --- a/libportability-gfx/src/lib.rs +++ b/libportability-gfx/src/lib.rs @@ -2,6 +2,7 @@ #![allow(non_camel_case_types)] #![allow(non_upper_case_globals)] #![allow(improper_ctypes)] //TEMP: buggy Rustc FFI analysis +#![cfg_attr(feature = "nightly", feature(core_intrinsics))] extern crate gfx_hal as hal; #[cfg(feature = "gfx-backend-dx12")] diff --git a/libportability/Cargo.toml b/libportability/Cargo.toml index c5db3b2..0e3ba3b 100644 --- a/libportability/Cargo.toml +++ b/libportability/Cargo.toml @@ -13,6 +13,7 @@ crate-type = ["cdylib"] [features] default = [] +debug = ["portability-gfx/env_logger", "portability-gfx/nightly"] dx12 = ["portability-gfx/gfx-backend-dx12"] metal = ["portability-gfx/gfx-backend-metal"] vulkan = ["portability-gfx/gfx-backend-vulkan"]