/src/test/run-pass/allocator/auxiliary/custom.rs

https://gitlab.com/jianglu/rust · Rust · 31 lines · 15 code · 6 blank · 10 comment · 1 complexity · c21d6abf31ae32dcb07129080f732985 MD5 · raw file

  1. // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // http://rust-lang.org/COPYRIGHT.
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. // no-prefer-dynamic
  11. #![feature(heap_api, allocator_api)]
  12. #![crate_type = "rlib"]
  13. use std::alloc::{GlobalAlloc, System, Layout, Opaque};
  14. use std::sync::atomic::{AtomicUsize, Ordering};
  15. pub struct A(pub AtomicUsize);
  16. unsafe impl GlobalAlloc for A {
  17. unsafe fn alloc(&self, layout: Layout) -> *mut Opaque {
  18. self.0.fetch_add(1, Ordering::SeqCst);
  19. System.alloc(layout)
  20. }
  21. unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout) {
  22. self.0.fetch_add(1, Ordering::SeqCst);
  23. System.dealloc(ptr, layout)
  24. }
  25. }