]> git.lizzy.rs Git - rust.git/commitdiff
rustc_codegen_llvm: use safe references for MemoryBuffer and ObjectFile.
authorIrina Popa <irinagpopa@gmail.com>
Tue, 10 Jul 2018 16:19:17 +0000 (19:19 +0300)
committerIrina Popa <irinagpopa@gmail.com>
Mon, 30 Jul 2018 17:10:28 +0000 (20:10 +0300)
src/librustc_codegen_llvm/llvm/ffi.rs
src/librustc_codegen_llvm/llvm/mod.rs
src/librustc_codegen_llvm/metadata.rs

index d67cc773bbcc8c1a367b4661cd38cfbfd5d8a306..422eb32bab8f8b8243238858e9a9f6345dd42974 100644 (file)
@@ -390,13 +390,11 @@ pub enum ThreadLocalMode {
 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 @@ pub fn LLVMRustPassManagerBuilderPopulateThinLTOPassManager(
     // 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 @@ pub fn LLVMRustPassManagerBuilderPopulateThinLTOPassManager(
 
     /// 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;
 
index b4b5ae42d02bd0b95199df1663cdae3801abb7f2..6bca2a162214cb2b57f5f67b7ccf4e9e95bdfe9f 100644 (file)
@@ -179,21 +179,16 @@ pub fn toggle_llfn(&self, idx: AttributePlace, llfn: &Value, set: bool) {
 // 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 @@ pub fn new(llmb: MemoryBufferRef) -> Option<ObjectFile> {
 impl Drop for ObjectFile {
     fn drop(&mut self) {
         unsafe {
-            LLVMDisposeObjectFile(self.llof);
+            LLVMDisposeObjectFile(&mut *(self.llof as *mut _));
         }
     }
 }
@@ -221,7 +216,7 @@ fn drop(&mut self) {
     }
 }
 
-pub fn mk_section_iter(llof: ObjectFileRef) -> SectionIter {
+pub fn mk_section_iter(llof: &ffi::ObjectFile) -> SectionIter {
     unsafe { SectionIter { llsi: LLVMGetSections(llof) } }
 }
 
index 144baa65c1bfa32a6b19b29391799d5811de5f5d..fcb704413ef116c1944e486083f4a995dcbffaba 100644 (file)
@@ -58,10 +58,8 @@ fn get_dylib_metadata(&self,
                           -> 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: '{}'",