forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rust-lang#4619 from brson/exchange
Some work on freestanding Rust: foreign calls, exchange allocator
- Loading branch information
Showing
17 changed files
with
306 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
use sys::{TypeDesc, size_of}; | ||
use libc::{c_void, size_t, uintptr_t}; | ||
use c_malloc = libc::malloc; | ||
use c_free = libc::free; | ||
use managed::raw::{BoxHeaderRepr, BoxRepr}; | ||
use cast::transmute; | ||
use ptr::{set_memory, null}; | ||
use intrinsic::TyDesc; | ||
|
||
pub unsafe fn malloc(td: *TypeDesc, size: uint) -> *c_void { | ||
unsafe { | ||
assert td.is_not_null(); | ||
|
||
let total_size = get_box_size(size, (*td).align); | ||
let p = c_malloc(total_size as size_t); | ||
assert p.is_not_null(); | ||
|
||
// FIXME #4761: Would be very nice to not memset all allocations | ||
let p: *mut u8 = transmute(p); | ||
set_memory(p, 0, total_size); | ||
|
||
// FIXME #3475: Converting between our two different tydesc types | ||
let td: *TyDesc = transmute(td); | ||
|
||
let box: &mut BoxRepr = transmute(p); | ||
box.header.ref_count = -1; // Exchange values not ref counted | ||
box.header.type_desc = td; | ||
box.header.prev = null(); | ||
box.header.next = null(); | ||
|
||
let exchange_count = &mut *rust_get_exchange_count_ptr(); | ||
rusti::atomic_xadd(exchange_count, 1); | ||
|
||
return transmute(box); | ||
} | ||
} | ||
|
||
pub unsafe fn free(ptr: *c_void) { | ||
let exchange_count = &mut *rust_get_exchange_count_ptr(); | ||
rusti::atomic_xsub(exchange_count, 1); | ||
|
||
assert ptr.is_not_null(); | ||
c_free(ptr); | ||
} | ||
|
||
fn get_box_size(body_size: uint, body_align: uint) -> uint { | ||
let header_size = size_of::<BoxHeaderRepr>(); | ||
// FIXME (#2699): This alignment calculation is suspicious. Is it right? | ||
let total_size = align_to(header_size, body_align) + body_size; | ||
return total_size; | ||
} | ||
|
||
// Rounds |size| to the nearest |alignment|. Invariant: |alignment| is a power | ||
// of two. | ||
fn align_to(size: uint, align: uint) -> uint { | ||
assert align != 0; | ||
(size + align - 1) & !(align - 1) | ||
} | ||
|
||
extern { | ||
#[rust_stack] | ||
fn rust_get_exchange_count_ptr() -> *mut int; | ||
} | ||
|
||
#[abi = "rust-intrinsic"] | ||
extern mod rusti { | ||
fn atomic_xadd(dst: &mut int, src: int) -> int; | ||
fn atomic_xsub(dst: &mut int, src: int) -> int; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#include "rust_exchange_alloc.h" | ||
#include "sync/sync.h" | ||
#include <stdlib.h> | ||
#include <assert.h> | ||
#include <string.h> | ||
#include <stdio.h> | ||
|
||
uintptr_t exchange_count = 0; | ||
|
||
void * | ||
rust_exchange_alloc::malloc(size_t size, bool zero) { | ||
void *value = ::malloc(size); | ||
assert(value); | ||
if (zero) { | ||
memset(value, 0, size); | ||
} | ||
|
||
sync::increment(exchange_count); | ||
|
||
return value; | ||
} | ||
|
||
void * | ||
rust_exchange_alloc::calloc(size_t size) { | ||
return this->malloc(size); | ||
} | ||
|
||
void * | ||
rust_exchange_alloc::realloc(void *ptr, size_t size) { | ||
void *new_ptr = ::realloc(ptr, size); | ||
assert(new_ptr); | ||
return new_ptr; | ||
} | ||
|
||
void | ||
rust_exchange_alloc::free(void *ptr) { | ||
sync::decrement(exchange_count); | ||
::free(ptr); | ||
} | ||
|
||
extern "C" uintptr_t * | ||
rust_get_exchange_count_ptr() { | ||
return &exchange_count; | ||
} | ||
|
||
void | ||
rust_check_exchange_count_on_exit() { | ||
if (exchange_count != 0) { | ||
printf("exchange heap not empty on on exit"); | ||
printf("%d dangling allocations", (int)exchange_count); | ||
abort(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#ifndef RUST_EXCHANGE_ALLOC_H | ||
#define RUST_EXCHANGE_ALLOC_H | ||
|
||
#include <stddef.h> | ||
#include <stdint.h> | ||
|
||
class rust_exchange_alloc { | ||
public: | ||
void *malloc(size_t size, bool zero = true); | ||
void *calloc(size_t size); | ||
void *realloc(void *mem, size_t size); | ||
void free(void *mem); | ||
}; | ||
|
||
extern "C" uintptr_t * | ||
rust_get_exchange_count_ptr(); | ||
|
||
void | ||
rust_check_exchange_count_on_exit(); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.