Skip to content

Commit 2f73cef

Browse files
committed
rustc_codegen_llvm: use safe references for MemoryBuffer and ObjectFile.
1 parent ebec156 commit 2f73cef

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

src/librustc_codegen_llvm/llvm/ffi.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -390,13 +390,11 @@ extern { pub type Metadata; }
390390
extern { pub type BasicBlock; }
391391
extern { pub type Builder; }
392392
extern { pub type MemoryBuffer; }
393-
pub type MemoryBufferRef = *mut MemoryBuffer;
394393
extern { pub type PassManager; }
395394
pub type PassManagerRef = *mut PassManager;
396395
extern { pub type PassManagerBuilder; }
397396
pub type PassManagerBuilderRef = *mut PassManagerBuilder;
398397
extern { pub type ObjectFile; }
399-
pub type ObjectFileRef = *mut ObjectFile;
400398
extern { pub type SectionIterator; }
401399
pub type SectionIteratorRef = *mut SectionIterator;
402400
extern { pub type Pass; }
@@ -1143,17 +1141,19 @@ extern "C" {
11431141
// Stuff that's in rustllvm/ because it's not upstream yet.
11441142

11451143
/// Opens an object file.
1146-
pub fn LLVMCreateObjectFile(MemBuf: MemoryBufferRef) -> ObjectFileRef;
1144+
pub fn LLVMCreateObjectFile(
1145+
MemBuf: &'static mut MemoryBuffer,
1146+
) -> Option<&'static mut ObjectFile>;
11471147
/// Closes an object file.
1148-
pub fn LLVMDisposeObjectFile(ObjFile: ObjectFileRef);
1148+
pub fn LLVMDisposeObjectFile(ObjFile: &'static mut ObjectFile);
11491149

11501150
/// Enumerates the sections in an object file.
1151-
pub fn LLVMGetSections(ObjFile: ObjectFileRef) -> SectionIteratorRef;
1151+
pub fn LLVMGetSections(ObjFile: &ObjectFile) -> SectionIteratorRef;
11521152
/// Destroys a section iterator.
11531153
pub fn LLVMDisposeSectionIterator(SI: SectionIteratorRef);
11541154
/// Returns true if the section iterator is at the end of the section
11551155
/// list:
1156-
pub fn LLVMIsSectionIteratorAtEnd(ObjFile: ObjectFileRef, SI: SectionIteratorRef) -> Bool;
1156+
pub fn LLVMIsSectionIteratorAtEnd(ObjFile: &ObjectFile, SI: SectionIteratorRef) -> Bool;
11571157
/// Moves the section iterator to point to the next section.
11581158
pub fn LLVMMoveToNextSection(SI: SectionIteratorRef);
11591159
/// Returns the current section size.
@@ -1163,7 +1163,9 @@ extern "C" {
11631163

11641164
/// Reads the given file and returns it as a memory buffer. Use
11651165
/// LLVMDisposeMemoryBuffer() to get rid of it.
1166-
pub fn LLVMRustCreateMemoryBufferWithContentsOfFile(Path: *const c_char) -> MemoryBufferRef;
1166+
pub fn LLVMRustCreateMemoryBufferWithContentsOfFile(
1167+
Path: *const c_char,
1168+
) -> Option<&'static mut MemoryBuffer>;
11671169

11681170
pub fn LLVMStartMultithreaded() -> Bool;
11691171

src/librustc_codegen_llvm/llvm/mod.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,16 @@ impl Attribute {
179179
// Memory-managed interface to object files.
180180

181181
pub struct ObjectFile {
182-
pub llof: ObjectFileRef,
182+
pub llof: &'static mut ffi::ObjectFile,
183183
}
184184

185185
unsafe impl Send for ObjectFile {}
186186

187187
impl ObjectFile {
188188
// This will take ownership of llmb
189-
pub fn new(llmb: MemoryBufferRef) -> Option<ObjectFile> {
189+
pub fn new(llmb: &'static mut MemoryBuffer) -> Option<ObjectFile> {
190190
unsafe {
191-
let llof = LLVMCreateObjectFile(llmb);
192-
if llof as isize == 0 {
193-
// LLVMCreateObjectFile took ownership of llmb
194-
return None;
195-
}
196-
191+
let llof = LLVMCreateObjectFile(llmb)?;
197192
Some(ObjectFile { llof: llof })
198193
}
199194
}
@@ -202,7 +197,7 @@ impl ObjectFile {
202197
impl Drop for ObjectFile {
203198
fn drop(&mut self) {
204199
unsafe {
205-
LLVMDisposeObjectFile(self.llof);
200+
LLVMDisposeObjectFile(&mut *(self.llof as *mut _));
206201
}
207202
}
208203
}
@@ -221,7 +216,7 @@ impl Drop for SectionIter {
221216
}
222217
}
223218

224-
pub fn mk_section_iter(llof: ObjectFileRef) -> SectionIter {
219+
pub fn mk_section_iter(llof: &ffi::ObjectFile) -> SectionIter {
225220
unsafe { SectionIter { llsi: LLVMGetSections(llof) } }
226221
}
227222

src/librustc_codegen_llvm/metadata.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,8 @@ impl MetadataLoader for LlvmMetadataLoader {
5858
-> Result<MetadataRef, String> {
5959
unsafe {
6060
let buf = common::path2cstr(filename);
61-
let mb = llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf.as_ptr());
62-
if mb as isize == 0 {
63-
return Err(format!("error reading library: '{}'", filename.display()));
64-
}
61+
let mb = llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf.as_ptr())
62+
.ok_or_else(|| format!("error reading library: '{}'", filename.display()))?;
6563
let of = ObjectFile::new(mb)
6664
.map(|of| OwningRef::new(box of))
6765
.ok_or_else(|| format!("provided path not an object file: '{}'",

0 commit comments

Comments
 (0)