Skip to content

Commit 80878ff

Browse files
committed
auto merge of #9809 : fhahn/rust/remove-old-cratemap-code, r=alexcrichton
This patch removes the code responsible for handling older CrateMap versions (as discussed during #9593). Only the new (safer) layout is supported now.
2 parents c147ec6 + f3b1f79 commit 80878ff

File tree

1 file changed

+10
-170
lines changed

1 file changed

+10
-170
lines changed

src/libstd/rt/crate_map.rs

Lines changed: 10 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,13 @@ use hashmap::HashSet;
1313
use option::{Some, None, Option};
1414
use vec::ImmutableVector;
1515

16-
/// Imports for old crate map versions
17-
use cast::transmute;
18-
use libc::c_char;
19-
use ptr;
20-
use str::raw::from_c_str;
21-
use vec;
22-
2316
// Need to tell the linker on OS X to not barf on undefined symbols
2417
// and instead look them up at runtime, which we need to resolve
2518
// the crate_map properly.
2619
#[cfg(target_os = "macos")]
2720
#[link_args = "-undefined dynamic_lookup"]
2821
extern {}
2922

30-
#[cfg(not(windows))]
31-
extern {
32-
#[weak_linkage]
33-
#[link_name = "_rust_crate_map_toplevel"]
34-
static CRATE_MAP: CrateMap<'static>;
35-
}
36-
37-
/// structs for old crate map versions
38-
pub struct ModEntryV0 {
39-
name: *c_char,
40-
log_level: *mut u32
41-
}
42-
pub struct CrateMapV0 {
43-
entries: *ModEntryV0,
44-
children: [*CrateMapV0, ..1]
45-
}
46-
47-
pub struct CrateMapV1 {
48-
version: i32,
49-
entries: *ModEntryV0,
50-
/// a dynamically sized struct, where all pointers to children are listed adjacent
51-
/// to the struct, terminated with NULL
52-
children: [*CrateMapV1, ..1]
53-
}
54-
5523
pub struct ModEntry<'self> {
5624
name: &'self str,
5725
log_level: *mut u32
@@ -65,6 +33,12 @@ pub struct CrateMap<'self> {
6533

6634
#[cfg(not(windows))]
6735
pub fn get_crate_map() -> Option<&'static CrateMap<'static>> {
36+
extern {
37+
#[weak_linkage]
38+
#[link_name = "_rust_crate_map_toplevel"]
39+
static CRATE_MAP: CrateMap<'static>;
40+
}
41+
6842
let ptr: (*CrateMap) = &'static CRATE_MAP;
6943
if ptr.is_null() {
7044
return None;
@@ -77,6 +51,7 @@ pub fn get_crate_map() -> Option<&'static CrateMap<'static>> {
7751
#[fixed_stack_segment]
7852
#[inline(never)]
7953
pub fn get_crate_map() -> Option<&'static CrateMap<'static>> {
54+
use cast::transmute;
8055
use c_str::ToCStr;
8156
use unstable::dynamic_lib::dl;
8257

@@ -101,54 +76,23 @@ pub fn get_crate_map() -> Option<&'static CrateMap<'static>> {
10176
fn version(crate_map: &CrateMap) -> i32 {
10277
match crate_map.version {
10378
2 => return 2,
104-
1 => return 1,
10579
_ => return 0
10680
}
10781
}
10882

109-
fn iter_module_map(mod_entries: &[ModEntry], f: &fn(&ModEntry)) {
110-
for entry in mod_entries.iter() {
111-
f(entry);
112-
}
113-
}
114-
115-
unsafe fn iter_module_map_v0(entries: *ModEntryV0, f: &fn(&ModEntry)) {
116-
let mut curr = entries;
117-
while !(*curr).name.is_null() {
118-
let mod_entry = ModEntry { name: from_c_str((*curr).name), log_level: (*curr).log_level };
119-
f(&mod_entry);
120-
curr = curr.offset(1);
121-
}
122-
}
123-
12483
fn do_iter_crate_map<'a>(crate_map: &'a CrateMap<'a>, f: &fn(&ModEntry),
12584
visited: &mut HashSet<*CrateMap<'a>>) {
12685
if visited.insert(crate_map as *CrateMap) {
12786
match version(crate_map) {
12887
2 => {
12988
let (entries, children) = (crate_map.entries, crate_map.children);
130-
iter_module_map(entries, |x| f(x));
89+
for entry in entries.iter() {
90+
f(entry);
91+
}
13192
for child in children.iter() {
13293
do_iter_crate_map(*child, |x| f(x), visited);
13394
}
13495
},
135-
// code for old crate map versions
136-
1 => unsafe {
137-
let v1: *CrateMapV1 = transmute(crate_map);
138-
iter_module_map_v0((*v1).entries, |x| f(x));
139-
let children = vec::raw::to_ptr((*v1).children);
140-
do ptr::array_each(children) |child| {
141-
do_iter_crate_map(transmute(child), |x| f(x), visited);
142-
}
143-
},
144-
0 => unsafe {
145-
let v0: *CrateMapV0 = transmute(crate_map);
146-
iter_module_map_v0((*v0).entries, |x| f(x));
147-
let children = vec::raw::to_ptr((*v0).children);
148-
do ptr::array_each(children) |child| {
149-
do_iter_crate_map(transmute(child), |x| f(x), visited);
150-
}
151-
},
15296
_ => fail2!("invalid crate map version")
15397
}
15498
}
@@ -234,108 +178,4 @@ mod tests {
234178
assert!(cnt == 4);
235179
}
236180
}
237-
238-
239-
/// Tests for old crate map versions
240-
#[test]
241-
fn iter_crate_map_duplicates_v1() {
242-
use c_str::ToCStr;
243-
use cast::transmute;
244-
use ptr;
245-
use rt::crate_map::{CrateMapV1, ModEntryV0, iter_crate_map};
246-
use vec;
247-
248-
struct CrateMapT3 {
249-
version: i32,
250-
entries: *ModEntryV0,
251-
children: [*CrateMapV1, ..3]
252-
}
253-
254-
unsafe {
255-
let mod_name1 = "c::m1".to_c_str();
256-
let mut level3: u32 = 3;
257-
258-
let entries: ~[ModEntryV0] = ~[
259-
ModEntryV0 { name: mod_name1.with_ref(|buf| buf), log_level: &mut level3},
260-
ModEntryV0 { name: ptr::null(), log_level: ptr::mut_null()}
261-
];
262-
let child_crate = CrateMapV1 {
263-
version: 1,
264-
entries: vec::raw::to_ptr(entries),
265-
children: [ptr::null()]
266-
};
267-
268-
let root_crate = CrateMapT3 {
269-
version: 1,
270-
entries: vec::raw::to_ptr([
271-
ModEntryV0 { name: ptr::null(), log_level: ptr::mut_null()}
272-
]),
273-
children: [&child_crate as *CrateMapV1, &child_crate as *CrateMapV1, ptr::null()]
274-
};
275-
276-
let mut cnt = 0;
277-
do iter_crate_map(transmute(&root_crate)) |entry| {
278-
assert!(*(*entry).log_level == 3);
279-
cnt += 1;
280-
}
281-
assert!(cnt == 1);
282-
}
283-
}
284-
285-
#[test]
286-
fn iter_crate_map_follow_children_v1() {
287-
use c_str::ToCStr;
288-
use cast::transmute;
289-
use ptr;
290-
use rt::crate_map::{CrateMapV1, ModEntryV0, iter_crate_map};
291-
use vec;
292-
293-
struct CrateMapT2 {
294-
version: i32,
295-
entries: *ModEntryV0,
296-
children: [*CrateMapV1, ..2]
297-
}
298-
299-
unsafe {
300-
let mod_name1 = "c::m1".to_c_str();
301-
let mod_name2 = "c::m2".to_c_str();
302-
let mut level2: u32 = 2;
303-
let mut level3: u32 = 3;
304-
let child_crate2 = CrateMapV1 {
305-
version: 1,
306-
entries: vec::raw::to_ptr([
307-
ModEntryV0 { name: mod_name1.with_ref(|buf| buf), log_level: &mut level2},
308-
ModEntryV0 { name: mod_name2.with_ref(|buf| buf), log_level: &mut level3},
309-
ModEntryV0 { name: ptr::null(), log_level: ptr::mut_null()}
310-
]),
311-
children: [ptr::null()]
312-
};
313-
314-
let child_crate1 = CrateMapT2 {
315-
version: 1,
316-
entries: vec::raw::to_ptr([
317-
ModEntryV0 { name: "t::f1".with_c_str(|buf| buf), log_level: &mut 1},
318-
ModEntryV0 { name: ptr::null(), log_level: ptr::mut_null()}
319-
]),
320-
children: [&child_crate2 as *CrateMapV1, ptr::null()]
321-
};
322-
323-
let child_crate1_ptr: *CrateMapV1 = transmute(&child_crate1);
324-
let root_crate = CrateMapT2 {
325-
version: 1,
326-
entries: vec::raw::to_ptr([
327-
ModEntryV0 { name: "t::f1".with_c_str(|buf| buf), log_level: &mut 0},
328-
ModEntryV0 { name: ptr::null(), log_level: ptr::mut_null()}
329-
]),
330-
children: [child_crate1_ptr, ptr::null()]
331-
};
332-
333-
let mut cnt = 0;
334-
do iter_crate_map(transmute(&root_crate)) |entry| {
335-
assert!(*(*entry).log_level == cnt);
336-
cnt += 1;
337-
}
338-
assert!(cnt == 4);
339-
}
340-
}
341181
}

0 commit comments

Comments
 (0)