rustc_codegen_llvm: use safe references for MemoryBuffer and ObjectFile.

This commit is contained in:
Irina Popa 2018-07-10 19:19:17 +03:00
parent ebec156abf
commit 2f73cef5a3
3 changed files with 16 additions and 21 deletions

View File

@ -390,13 +390,11 @@ extern { pub type Metadata; }
extern { pub type BasicBlock; }
extern { pub type Builder; }
extern { pub type MemoryBuffer; }
pub type MemoryBufferRef = *mut MemoryBuffer;
extern { pub type PassManager; }
pub type PassManagerRef = *mut PassManager;
extern { pub type PassManagerBuilder; }
pub type PassManagerBuilderRef = *mut PassManagerBuilder;
extern { pub type ObjectFile; }
pub type ObjectFileRef = *mut ObjectFile;
extern { pub type SectionIterator; }
pub type SectionIteratorRef = *mut SectionIterator;
extern { pub type Pass; }
@ -1143,17 +1141,19 @@ extern "C" {
// Stuff that's in rustllvm/ because it's not upstream yet.
/// Opens an object file.
pub fn LLVMCreateObjectFile(MemBuf: MemoryBufferRef) -> ObjectFileRef;
pub fn LLVMCreateObjectFile(
MemBuf: &'static mut MemoryBuffer,
) -> Option<&'static mut ObjectFile>;
/// Closes an object file.
pub fn LLVMDisposeObjectFile(ObjFile: ObjectFileRef);
pub fn LLVMDisposeObjectFile(ObjFile: &'static mut ObjectFile);
/// Enumerates the sections in an object file.
pub fn LLVMGetSections(ObjFile: ObjectFileRef) -> SectionIteratorRef;
pub fn LLVMGetSections(ObjFile: &ObjectFile) -> SectionIteratorRef;
/// Destroys a section iterator.
pub fn LLVMDisposeSectionIterator(SI: SectionIteratorRef);
/// Returns true if the section iterator is at the end of the section
/// list:
pub fn LLVMIsSectionIteratorAtEnd(ObjFile: ObjectFileRef, SI: SectionIteratorRef) -> Bool;
pub fn LLVMIsSectionIteratorAtEnd(ObjFile: &ObjectFile, SI: SectionIteratorRef) -> Bool;
/// Moves the section iterator to point to the next section.
pub fn LLVMMoveToNextSection(SI: SectionIteratorRef);
/// Returns the current section size.
@ -1163,7 +1163,9 @@ extern "C" {
/// Reads the given file and returns it as a memory buffer. Use
/// LLVMDisposeMemoryBuffer() to get rid of it.
pub fn LLVMRustCreateMemoryBufferWithContentsOfFile(Path: *const c_char) -> MemoryBufferRef;
pub fn LLVMRustCreateMemoryBufferWithContentsOfFile(
Path: *const c_char,
) -> Option<&'static mut MemoryBuffer>;
pub fn LLVMStartMultithreaded() -> Bool;

View File

@ -179,21 +179,16 @@ impl Attribute {
// Memory-managed interface to object files.
pub struct ObjectFile {
pub llof: ObjectFileRef,
pub llof: &'static mut ffi::ObjectFile,
}
unsafe impl Send for ObjectFile {}
impl ObjectFile {
// This will take ownership of llmb
pub fn new(llmb: MemoryBufferRef) -> Option<ObjectFile> {
pub fn new(llmb: &'static mut MemoryBuffer) -> Option<ObjectFile> {
unsafe {
let llof = LLVMCreateObjectFile(llmb);
if llof as isize == 0 {
// LLVMCreateObjectFile took ownership of llmb
return None;
}
let llof = LLVMCreateObjectFile(llmb)?;
Some(ObjectFile { llof: llof })
}
}
@ -202,7 +197,7 @@ impl ObjectFile {
impl Drop for ObjectFile {
fn drop(&mut self) {
unsafe {
LLVMDisposeObjectFile(self.llof);
LLVMDisposeObjectFile(&mut *(self.llof as *mut _));
}
}
}
@ -221,7 +216,7 @@ impl Drop for SectionIter {
}
}
pub fn mk_section_iter(llof: ObjectFileRef) -> SectionIter {
pub fn mk_section_iter(llof: &ffi::ObjectFile) -> SectionIter {
unsafe { SectionIter { llsi: LLVMGetSections(llof) } }
}

View File

@ -58,10 +58,8 @@ impl MetadataLoader for LlvmMetadataLoader {
-> Result<MetadataRef, String> {
unsafe {
let buf = common::path2cstr(filename);
let mb = llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf.as_ptr());
if mb as isize == 0 {
return Err(format!("error reading library: '{}'", filename.display()));
}
let mb = llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf.as_ptr())
.ok_or_else(|| format!("error reading library: '{}'", filename.display()))?;
let of = ObjectFile::new(mb)
.map(|of| OwningRef::new(box of))
.ok_or_else(|| format!("provided path not an object file: '{}'",