|
| 1 | +// Copyright 2012 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 | + |
| 11 | +use sys::{TypeDesc, size_of}; |
| 12 | +use libc::{c_void, size_t, uintptr_t}; |
| 13 | +use c_malloc = libc::malloc; |
| 14 | +use c_free = libc::free; |
| 15 | +use managed::raw::{BoxHeaderRepr, BoxRepr}; |
| 16 | +use cast::transmute; |
| 17 | +use ptr::{set_memory, null}; |
| 18 | +use intrinsic::TyDesc; |
| 19 | + |
| 20 | +pub unsafe fn malloc(td: *TypeDesc, size: uint) -> *c_void { |
| 21 | + unsafe { |
| 22 | + assert td.is_not_null(); |
| 23 | + |
| 24 | + let total_size = get_box_size(size, (*td).align); |
| 25 | + let p = c_malloc(total_size as size_t); |
| 26 | + assert p.is_not_null(); |
| 27 | + |
| 28 | + // FIXME #4761: Would be very nice to not memset all allocations |
| 29 | + let p: *mut u8 = transmute(p); |
| 30 | + set_memory(p, 0, total_size); |
| 31 | + |
| 32 | + // FIXME #3475: Converting between our two different tydesc types |
| 33 | + let td: *TyDesc = transmute(td); |
| 34 | + |
| 35 | + let box: &mut BoxRepr = transmute(p); |
| 36 | + box.header.ref_count = -1; // Exchange values not ref counted |
| 37 | + box.header.type_desc = td; |
| 38 | + box.header.prev = null(); |
| 39 | + box.header.next = null(); |
| 40 | + |
| 41 | + let exchange_count = &mut *rust_get_exchange_count_ptr(); |
| 42 | + rusti::atomic_xadd(exchange_count, 1); |
| 43 | + |
| 44 | + return transmute(box); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +pub unsafe fn free(ptr: *c_void) { |
| 49 | + let exchange_count = &mut *rust_get_exchange_count_ptr(); |
| 50 | + rusti::atomic_xsub(exchange_count, 1); |
| 51 | + |
| 52 | + assert ptr.is_not_null(); |
| 53 | + c_free(ptr); |
| 54 | +} |
| 55 | + |
| 56 | +fn get_box_size(body_size: uint, body_align: uint) -> uint { |
| 57 | + let header_size = size_of::<BoxHeaderRepr>(); |
| 58 | + // FIXME (#2699): This alignment calculation is suspicious. Is it right? |
| 59 | + let total_size = align_to(header_size, body_align) + body_size; |
| 60 | + return total_size; |
| 61 | +} |
| 62 | + |
| 63 | +// Rounds |size| to the nearest |alignment|. Invariant: |alignment| is a power |
| 64 | +// of two. |
| 65 | +fn align_to(size: uint, align: uint) -> uint { |
| 66 | + assert align != 0; |
| 67 | + (size + align - 1) & !(align - 1) |
| 68 | +} |
| 69 | + |
| 70 | +extern { |
| 71 | + #[rust_stack] |
| 72 | + fn rust_get_exchange_count_ptr() -> *mut int; |
| 73 | +} |
| 74 | + |
| 75 | +#[abi = "rust-intrinsic"] |
| 76 | +extern mod rusti { |
| 77 | + fn atomic_xadd(dst: &mut int, src: int) -> int; |
| 78 | + fn atomic_xsub(dst: &mut int, src: int) -> int; |
| 79 | +} |
0 commit comments