Skip to content

Commit d18e321

Browse files
committed
Merge pull request rust-lang#33996 from alexcrichton/beta-next
Backport two small Android patches
2 parents 39f3c16 + 18cb3ed commit d18e321

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

src/liballoc_system/lib.rs

+34-7
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,40 @@ mod imp {
8080
if align <= MIN_ALIGN {
8181
libc::malloc(size as libc::size_t) as *mut u8
8282
} else {
83-
let mut out = ptr::null_mut();
84-
let ret = libc::posix_memalign(&mut out, align as libc::size_t, size as libc::size_t);
85-
if ret != 0 {
86-
ptr::null_mut()
87-
} else {
88-
out as *mut u8
89-
}
83+
aligned_malloc(size, align)
84+
}
85+
}
86+
87+
#[cfg(target_os = "android")]
88+
unsafe fn aligned_malloc(size: usize, align: usize) -> *mut u8 {
89+
// On android we currently target API level 9 which unfortunately
90+
// doesn't have the `posix_memalign` API used below. Instead we use
91+
// `memalign`, but this unfortunately has the property on some systems
92+
// where the memory returned cannot be deallocated by `free`!
93+
//
94+
// Upon closer inspection, however, this appears to work just fine with
95+
// Android, so for this platform we should be fine to call `memalign`
96+
// (which is present in API level 9). Some helpful references could
97+
// possibly be chromium using memalign [1], attempts at documenting that
98+
// memalign + free is ok [2] [3], or the current source of chromium
99+
// which still uses memalign on android [4].
100+
//
101+
// [1]: https://codereview.chromium.org/10796020/
102+
// [2]: https://code.google.com/p/android/issues/detail?id=35391
103+
// [3]: https://bugs.chromium.org/p/chromium/issues/detail?id=138579
104+
// [4]: https://chromium.googlesource.com/chromium/src/base/+/master/
105+
// /memory/aligned_memory.cc
106+
libc::memalign(align as libc::size_t, size as libc::size_t) as *mut u8
107+
}
108+
109+
#[cfg(not(target_os = "android"))]
110+
unsafe fn aligned_malloc(size: usize, align: usize) -> *mut u8 {
111+
let mut out = ptr::null_mut();
112+
let ret = libc::posix_memalign(&mut out, align as libc::size_t, size as libc::size_t);
113+
if ret != 0 {
114+
ptr::null_mut()
115+
} else {
116+
out as *mut u8
90117
}
91118
}
92119

src/tools/compiletest/src/runtest.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,15 @@ actual:\n\
433433

434434
cmds = cmds.replace("run", "continue");
435435

436+
let tool_path = match self.config.android_cross_path.to_str() {
437+
Some(x) => x.to_owned(),
438+
None => self.fatal("cannot find android cross path")
439+
};
440+
436441
// write debugger script
437442
let mut script_str = String::with_capacity(2048);
438443
script_str.push_str(&format!("set charset {}\n", Self::charset()));
444+
script_str.push_str(&format!("set sysroot {}\n", tool_path));
439445
script_str.push_str(&format!("file {}\n", exe_file.to_str().unwrap()));
440446
script_str.push_str("target remote :5039\n");
441447
script_str.push_str(&format!("set solib-search-path \
@@ -508,11 +514,6 @@ actual:\n\
508514
}
509515
}
510516

511-
let tool_path = match self.config.android_cross_path.to_str() {
512-
Some(x) => x.to_owned(),
513-
None => self.fatal("cannot find android cross path")
514-
};
515-
516517
let debugger_script = self.make_out_name("debugger.script");
517518
// FIXME (#9639): This needs to handle non-utf8 paths
518519
let debugger_opts =

0 commit comments

Comments
 (0)