Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
f168c12c
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f168c12c
编写于
10月 25, 2014
作者:
B
bors
浏览文件
操作
浏览文件
下载
差异文件
auto merge of #18293 : thestinger/rust/heap, r=cmr
上级
80e5fe1a
a9e85100
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
61 addition
and
103 deletion
+61
-103
src/liballoc/heap.rs
src/liballoc/heap.rs
+49
-40
src/liballoc/lib.rs
src/liballoc/lib.rs
+2
-6
src/liballoc/libc_heap.rs
src/liballoc/libc_heap.rs
+0
-48
src/librustrt/c_str.rs
src/librustrt/c_str.rs
+4
-3
src/librustrt/mutex.rs
src/librustrt/mutex.rs
+3
-3
src/libstd/c_vec.rs
src/libstd/c_vec.rs
+2
-2
src/libstd/rt/mod.rs
src/libstd/rt/mod.rs
+1
-1
未找到文件。
src/liballoc/heap.rs
浏览文件 @
f168c12c
...
...
@@ -28,8 +28,8 @@ pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
/// size on the platform.
///
/// The `old_size` and `align` parameters are the parameters that were used to
/// create the allocation referenced by `ptr`. The `old_size` parameter may
also
///
be the value returned by `usable_size` for the requested size
.
/// create the allocation referenced by `ptr`. The `old_size` parameter may
be
///
any value in range_inclusive(requested_size, usable_size)
.
#[inline]
pub
unsafe
fn
reallocate
(
ptr
:
*
mut
u8
,
old_size
:
uint
,
size
:
uint
,
align
:
uint
)
->
*
mut
u8
{
imp
::
reallocate
(
ptr
,
old_size
,
size
,
align
)
...
...
@@ -38,8 +38,8 @@ pub unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint)
/// Extends or shrinks the allocation referenced by `ptr` to `size` bytes of
/// memory in-place.
///
///
Returns true if successful, otherwise false if the allocation was no
t
///
altered
.
///
If the operation succeeds, it returns `usable_size(size, align)` and if i
t
///
fails (or is a no-op) it returns `usable_size(old_size, align)`
.
///
/// Behavior is undefined if the requested size is 0 or the alignment is not a
/// power of 2. The alignment must be no larger than the largest supported page
...
...
@@ -49,7 +49,7 @@ pub unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint)
/// create the allocation referenced by `ptr`. The `old_size` parameter may be
/// any value in range_inclusive(requested_size, usable_size).
#[inline]
pub
unsafe
fn
reallocate_inplace
(
ptr
:
*
mut
u8
,
old_size
:
uint
,
size
:
uint
,
align
:
uint
)
->
bool
{
pub
unsafe
fn
reallocate_inplace
(
ptr
:
*
mut
u8
,
old_size
:
uint
,
size
:
uint
,
align
:
uint
)
->
uint
{
imp
::
reallocate_inplace
(
ptr
,
old_size
,
size
,
align
)
}
...
...
@@ -57,12 +57,12 @@ pub unsafe fn reallocate_inplace(ptr: *mut u8, old_size: uint, size: uint, align
///
/// The `ptr` parameter must not be null.
///
/// The `size` and `align` parameters are the parameters that were used to
/// create the allocation referenced by `ptr`. The `
size` parameter may also
be
///
the value returned by `usable_size` for the requested size
.
/// The `
old_
size` and `align` parameters are the parameters that were used to
/// create the allocation referenced by `ptr`. The `
old_size` parameter may
be
///
any value in range_inclusive(requested_size, usable_size)
.
#[inline]
pub
unsafe
fn
deallocate
(
ptr
:
*
mut
u8
,
size
:
uint
,
align
:
uint
)
{
imp
::
deallocate
(
ptr
,
size
,
align
)
pub
unsafe
fn
deallocate
(
ptr
:
*
mut
u8
,
old_
size
:
uint
,
align
:
uint
)
{
imp
::
deallocate
(
ptr
,
old_
size
,
align
)
}
/// Returns the usable size of an allocation created with the specified the
...
...
@@ -102,8 +102,8 @@ unsafe fn exchange_malloc(size: uint, align: uint) -> *mut u8 {
#[cfg(not(test))]
#[lang=
"exchange_free"
]
#[inline]
unsafe
fn
exchange_free
(
ptr
:
*
mut
u8
,
size
:
uint
,
align
:
uint
)
{
deallocate
(
ptr
,
size
,
align
);
unsafe
fn
exchange_free
(
ptr
:
*
mut
u8
,
old_
size
:
uint
,
align
:
uint
)
{
deallocate
(
ptr
,
old_
size
,
align
);
}
// The minimum alignment guaranteed by the architecture. This value is used to
...
...
@@ -112,10 +112,10 @@ unsafe fn exchange_free(ptr: *mut u8, size: uint, align: uint) {
#[cfg(any(target_arch
=
"arm"
,
target_arch
=
"mips"
,
target_arch
=
"mipsel"
))]
static
MIN_ALIGN
:
uint
=
8
;
const
MIN_ALIGN
:
uint
=
8
;
#[cfg(any(target_arch
=
"x86"
,
target_arch
=
"x86_64"
))]
static
MIN_ALIGN
:
uint
=
16
;
const
MIN_ALIGN
:
uint
=
16
;
#[cfg(jemalloc)]
mod
imp
{
...
...
@@ -178,22 +178,16 @@ pub unsafe fn reallocate(ptr: *mut u8, _old_size: uint, size: uint, align: uint)
}
#[inline]
pub
unsafe
fn
reallocate_inplace
(
ptr
:
*
mut
u8
,
old_size
:
uint
,
size
:
uint
,
align
:
uint
)
->
bool
{
pub
unsafe
fn
reallocate_inplace
(
ptr
:
*
mut
u8
,
_
old_size
:
uint
,
size
:
uint
,
align
:
uint
)
->
uint
{
let
flags
=
align_to_flags
(
align
);
let
new_size
=
je_xallocx
(
ptr
as
*
mut
c_void
,
size
as
size_t
,
0
,
flags
)
as
uint
;
// checking for failure to shrink is tricky
if
size
<
old_size
{
usable_size
(
size
,
align
)
==
new_size
as
uint
}
else
{
new_size
>=
size
}
je_xallocx
(
ptr
as
*
mut
c_void
,
size
as
size_t
,
0
,
flags
)
as
uint
}
#[inline]
pub
unsafe
fn
deallocate
(
ptr
:
*
mut
u8
,
size
:
uint
,
align
:
uint
)
{
pub
unsafe
fn
deallocate
(
ptr
:
*
mut
u8
,
old_
size
:
uint
,
align
:
uint
)
{
let
flags
=
align_to_flags
(
align
);
je_sdallocx
(
ptr
as
*
mut
c_void
,
size
as
size_t
,
flags
)
je_sdallocx
(
ptr
as
*
mut
c_void
,
old_
size
as
size_t
,
flags
)
}
#[inline]
...
...
@@ -213,8 +207,8 @@ pub fn stats_print() {
mod
imp
{
use
core
::
cmp
;
use
core
::
ptr
;
use
core
::
ptr
::
RawPtr
;
use
libc
;
use
libc_heap
;
use
super
::
MIN_ALIGN
;
extern
{
...
...
@@ -226,7 +220,11 @@ fn posix_memalign(memptr: *mut *mut libc::c_void,
#[inline]
pub
unsafe
fn
allocate
(
size
:
uint
,
align
:
uint
)
->
*
mut
u8
{
if
align
<=
MIN_ALIGN
{
libc_heap
::
malloc_raw
(
size
)
let
ptr
=
libc
::
malloc
(
size
as
libc
::
size_t
);
if
ptr
.is_null
()
{
::
oom
();
}
ptr
as
*
mut
u8
}
else
{
let
mut
out
=
0
as
*
mut
libc
::
c_void
;
let
ret
=
posix_memalign
(
&
mut
out
,
...
...
@@ -242,7 +240,11 @@ pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
#[inline]
pub
unsafe
fn
reallocate
(
ptr
:
*
mut
u8
,
old_size
:
uint
,
size
:
uint
,
align
:
uint
)
->
*
mut
u8
{
if
align
<=
MIN_ALIGN
{
libc_heap
::
realloc_raw
(
ptr
,
size
)
let
ptr
=
libc
::
realloc
(
ptr
as
*
mut
libc
::
c_void
,
size
as
libc
::
size_t
);
if
ptr
.is_null
()
{
::
oom
();
}
ptr
as
*
mut
u8
}
else
{
let
new_ptr
=
allocate
(
size
,
align
);
ptr
::
copy_memory
(
new_ptr
,
ptr
as
*
const
u8
,
cmp
::
min
(
size
,
old_size
));
...
...
@@ -252,13 +254,13 @@ pub unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint)
}
#[inline]
pub
unsafe
fn
reallocate_inplace
(
_
ptr
:
*
mut
u8
,
old_size
:
uint
,
size
:
uint
,
_
align
:
uint
)
->
bool
{
size
==
old_size
pub
unsafe
fn
reallocate_inplace
(
_
ptr
:
*
mut
u8
,
old_size
:
uint
,
_
size
:
uint
,
_
align
:
uint
)
->
uint
{
old_size
}
#[inline]
pub
unsafe
fn
deallocate
(
ptr
:
*
mut
u8
,
_
size
:
uint
,
_
align
:
uint
)
{
pub
unsafe
fn
deallocate
(
ptr
:
*
mut
u8
,
_
old_
size
:
uint
,
_
align
:
uint
)
{
libc
::
free
(
ptr
as
*
mut
libc
::
c_void
)
}
...
...
@@ -274,7 +276,6 @@ pub fn stats_print() {}
mod
imp
{
use
libc
::{
c_void
,
size_t
};
use
libc
;
use
libc_heap
;
use
core
::
ptr
::
RawPtr
;
use
super
::
MIN_ALIGN
;
...
...
@@ -288,7 +289,11 @@ fn _aligned_realloc(block: *mut c_void, size: size_t,
#[inline]
pub
unsafe
fn
allocate
(
size
:
uint
,
align
:
uint
)
->
*
mut
u8
{
if
align
<=
MIN_ALIGN
{
libc_heap
::
malloc_raw
(
size
)
let
ptr
=
libc
::
malloc
(
size
as
size_t
);
if
ptr
.is_null
()
{
::
oom
();
}
ptr
as
*
mut
u8
}
else
{
let
ptr
=
_
aligned_malloc
(
size
as
size_t
,
align
as
size_t
);
if
ptr
.is_null
()
{
...
...
@@ -301,7 +306,11 @@ pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
#[inline]
pub
unsafe
fn
reallocate
(
ptr
:
*
mut
u8
,
_
old_size
:
uint
,
size
:
uint
,
align
:
uint
)
->
*
mut
u8
{
if
align
<=
MIN_ALIGN
{
libc_heap
::
realloc_raw
(
ptr
,
size
)
let
ptr
=
libc
::
realloc
(
ptr
as
*
mut
c_void
,
size
as
size_t
);
if
ptr
.is_null
()
{
::
oom
();
}
ptr
as
*
mut
u8
}
else
{
let
ptr
=
_
aligned_realloc
(
ptr
as
*
mut
c_void
,
size
as
size_t
,
align
as
size_t
);
...
...
@@ -313,13 +322,13 @@ pub unsafe fn reallocate(ptr: *mut u8, _old_size: uint, size: uint, align: uint)
}
#[inline]
pub
unsafe
fn
reallocate_inplace
(
_
ptr
:
*
mut
u8
,
old_size
:
uint
,
size
:
uint
,
_
align
:
uint
)
->
bool
{
size
==
old_size
pub
unsafe
fn
reallocate_inplace
(
_
ptr
:
*
mut
u8
,
old_size
:
uint
,
_
size
:
uint
,
_
align
:
uint
)
->
uint
{
old_size
}
#[inline]
pub
unsafe
fn
deallocate
(
ptr
:
*
mut
u8
,
_
size
:
uint
,
align
:
uint
)
{
pub
unsafe
fn
deallocate
(
ptr
:
*
mut
u8
,
_
old_
size
:
uint
,
align
:
uint
)
{
if
align
<=
MIN_ALIGN
{
libc
::
free
(
ptr
as
*
mut
libc
::
c_void
)
}
else
{
...
...
@@ -348,7 +357,7 @@ fn basic_reallocate_inplace_noop() {
let
ptr
=
heap
::
allocate
(
size
,
8
);
let
ret
=
heap
::
reallocate_inplace
(
ptr
,
size
,
size
,
8
);
heap
::
deallocate
(
ptr
,
size
,
8
);
assert
!
(
ret
);
assert
_eq!
(
ret
,
heap
::
usable_size
(
size
,
8
)
);
}
}
...
...
src/liballoc/lib.rs
浏览文件 @
f168c12c
...
...
@@ -54,11 +54,8 @@
//!
//! ## Heap interfaces
//!
//! The [`heap`](heap/index.html) and [`libc_heap`](libc_heap/index.html)
//! modules are the unsafe interfaces to the underlying allocation systems. The
//! `heap` module is considered the default heap, and is not necessarily backed
//! by libc malloc/free. The `libc_heap` module is defined to be wired up to
//! the system malloc/free.
//! The [`heap`](heap/index.html) module defines the low-level interface to the
//! default global allocator. It is not compatible with the libc allocator API.
#![crate_name
=
"alloc"
]
#![experimental]
...
...
@@ -90,7 +87,6 @@
// Heaps provided for low-level allocation strategies
pub
mod
heap
;
pub
mod
libc_heap
;
// Primitive types using the heaps above
...
...
src/liballoc/libc_heap.rs
已删除
100644 → 0
浏览文件 @
80e5fe1a
// Copyright 2012-2014 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.
//! The global (exchange) heap.
use
libc
::{
c_void
,
size_t
,
free
,
malloc
,
realloc
};
use
core
::
ptr
::{
RawPtr
,
null_mut
};
/// A wrapper around libc::malloc, aborting on out-of-memory.
#[inline]
pub
unsafe
fn
malloc_raw
(
size
:
uint
)
->
*
mut
u8
{
// `malloc(0)` may allocate, but it may also return a null pointer
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/malloc.html
if
size
==
0
{
null_mut
()
}
else
{
let
p
=
malloc
(
size
as
size_t
);
if
p
.is_null
()
{
::
oom
();
}
p
as
*
mut
u8
}
}
/// A wrapper around libc::realloc, aborting on out-of-memory.
#[inline]
pub
unsafe
fn
realloc_raw
(
ptr
:
*
mut
u8
,
size
:
uint
)
->
*
mut
u8
{
// `realloc(ptr, 0)` may allocate, but it may also return a null pointer
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html
if
size
==
0
{
free
(
ptr
as
*
mut
c_void
);
null_mut
()
}
else
{
let
p
=
realloc
(
ptr
as
*
mut
c_void
,
size
as
size_t
);
if
p
.is_null
()
{
::
oom
();
}
p
as
*
mut
u8
}
}
src/librustrt/c_str.rs
浏览文件 @
f168c12c
...
...
@@ -71,7 +71,6 @@ fn main() {
*/
use
alloc
::
libc_heap
::
malloc_raw
;
use
collections
::
string
::
String
;
use
collections
::
hash
;
use
core
::
fmt
;
...
...
@@ -101,7 +100,8 @@ impl Clone for CString {
/// with C's allocator API, rather than the usual shallow clone.
fn
clone
(
&
self
)
->
CString
{
let
len
=
self
.len
()
+
1
;
let
buf
=
unsafe
{
malloc_raw
(
len
)
}
as
*
mut
libc
::
c_char
;
let
buf
=
unsafe
{
libc
::
malloc
(
len
as
libc
::
size_t
)
}
as
*
mut
libc
::
c_char
;
if
buf
.is_null
()
{
fail
!
(
"out of memory"
)
}
unsafe
{
ptr
::
copy_nonoverlapping_memory
(
buf
,
self
.buf
,
len
);
}
CString
{
buf
:
buf
as
*
const
libc
::
c_char
,
owns_buffer_
:
true
}
}
...
...
@@ -393,7 +393,8 @@ fn to_c_str(&self) -> CString {
unsafe
fn
to_c_str_unchecked
(
&
self
)
->
CString
{
let
self_len
=
self
.len
();
let
buf
=
malloc_raw
(
self_len
+
1
);
let
buf
=
libc
::
malloc
(
self_len
as
libc
::
size_t
+
1
)
as
*
mut
u8
;
if
buf
.is_null
()
{
fail
!
(
"out of memory"
)
}
ptr
::
copy_memory
(
buf
,
self
.as_ptr
(),
self_len
);
*
buf
.offset
(
self_len
as
int
)
=
0
;
...
...
src/librustrt/mutex.rs
浏览文件 @
f168c12c
...
...
@@ -516,7 +516,7 @@ fn pthread_cond_wait(cond: *mut pthread_cond_t,
#[cfg(windows)]
mod
imp
{
use
alloc
::
libc_heap
::
malloc_raw
;
use
alloc
::
heap
;
use
core
::
atomic
;
use
core
::
ptr
;
use
libc
::{
HANDLE
,
BOOL
,
LPSECURITY_ATTRIBUTES
,
c_void
,
DWORD
,
LPCSTR
};
...
...
@@ -607,7 +607,7 @@ unsafe fn getcond(&self) -> *mut c_void {
}
pub
unsafe
fn
init_lock
()
->
uint
{
let
block
=
malloc_raw
(
CRIT_SECTION_SIZE
as
uint
)
as
*
mut
c_void
;
let
block
=
heap
::
allocate
(
CRIT_SECTION_SIZE
,
8
)
as
*
mut
c_void
;
InitializeCriticalSectionAndSpinCount
(
block
,
SPIN_COUNT
);
return
block
as
uint
;
}
...
...
@@ -619,7 +619,7 @@ pub unsafe fn init_cond() -> uint {
pub
unsafe
fn
free_lock
(
h
:
uint
)
{
DeleteCriticalSection
(
h
as
LPCRITICAL_SECTION
);
libc
::
free
(
h
as
*
mut
c_void
);
heap
::
deallocate
(
h
as
*
mut
u8
,
CRIT_SECTION_SIZE
,
8
);
}
pub
unsafe
fn
free_cond
(
h
:
uint
)
{
...
...
src/libstd/c_vec.rs
浏览文件 @
f168c12c
...
...
@@ -165,11 +165,11 @@ mod tests {
use
super
::
CVec
;
use
libc
;
use
ptr
;
use
rt
::
libc_heap
::
malloc_raw
;
fn
malloc
(
n
:
uint
)
->
CVec
<
u8
>
{
unsafe
{
let
mem
=
malloc_raw
(
n
);
let
mem
=
libc
::
malloc
(
n
as
libc
::
size_t
);
if
mem
.is_null
()
{
fail
!
(
"out of memory"
)
}
CVec
::
new_with_dtor
(
mem
as
*
mut
u8
,
n
,
proc
()
{
libc
::
free
(
mem
as
*
mut
libc
::
c_void
);
})
...
...
src/libstd/rt/mod.rs
浏览文件 @
f168c12c
...
...
@@ -62,7 +62,7 @@
// Reexport functionality from librustrt and other crates underneath the
// standard library which work together to create the entire runtime.
pub
use
alloc
::
{
heap
,
libc_heap
}
;
pub
use
alloc
::
heap
;
pub
use
rustrt
::{
task
,
local
,
mutex
,
exclusive
,
stack
,
args
,
rtio
,
thread
};
pub
use
rustrt
::{
Stdio
,
Stdout
,
Stderr
,
begin_unwind
,
begin_unwind_fmt
};
pub
use
rustrt
::{
bookkeeping
,
at_exit
,
unwind
,
DEFAULT_ERROR_CODE
,
Runtime
};
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录