Skip to content

Commit

Permalink
rustc_codegen_llvm: use safe references for SectionIterator.
Browse files Browse the repository at this point in the history
  • Loading branch information
irinagpopa committed Jul 30, 2018
1 parent e22eeba commit 2c1d7fb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
17 changes: 8 additions & 9 deletions src/librustc_codegen_llvm/llvm/ffi.rs
Expand Up @@ -400,8 +400,7 @@ extern { pub type MemoryBuffer; }
pub struct PassManager<'a>(InvariantOpaque<'a>);
extern { pub type PassManagerBuilder; }
extern { pub type ObjectFile; }
extern { pub type SectionIterator; }
pub type SectionIteratorRef = *mut SectionIterator;
pub struct SectionIterator<'a>(InvariantOpaque<'a>);
extern { pub type Pass; }
extern { pub type TargetMachine; }
extern { pub type Archive; }
Expand Down Expand Up @@ -1146,18 +1145,18 @@ extern "C" {
pub fn LLVMDisposeObjectFile(ObjFile: &'static mut ObjectFile);

/// Enumerates the sections in an object file.
pub fn LLVMGetSections(ObjFile: &ObjectFile) -> SectionIteratorRef;
pub fn LLVMGetSections(ObjFile: &'a ObjectFile) -> &'a mut SectionIterator<'a>;
/// Destroys a section iterator.
pub fn LLVMDisposeSectionIterator(SI: SectionIteratorRef);
pub fn LLVMDisposeSectionIterator(SI: &'a mut SectionIterator<'a>);
/// Returns true if the section iterator is at the end of the section
/// list:
pub fn LLVMIsSectionIteratorAtEnd(ObjFile: &ObjectFile, SI: SectionIteratorRef) -> Bool;
pub fn LLVMIsSectionIteratorAtEnd(ObjFile: &'a ObjectFile, SI: &SectionIterator<'a>) -> Bool;
/// Moves the section iterator to point to the next section.
pub fn LLVMMoveToNextSection(SI: SectionIteratorRef);
pub fn LLVMMoveToNextSection(SI: &SectionIterator);
/// Returns the current section size.
pub fn LLVMGetSectionSize(SI: SectionIteratorRef) -> c_ulonglong;
pub fn LLVMGetSectionSize(SI: &SectionIterator) -> c_ulonglong;
/// Returns the current section contents as a string buffer.
pub fn LLVMGetSectionContents(SI: SectionIteratorRef) -> *const c_char;
pub fn LLVMGetSectionContents(SI: &SectionIterator) -> *const c_char;

/// Reads the given file and returns it as a memory buffer. Use
/// LLVMDisposeMemoryBuffer() to get rid of it.
Expand Down Expand Up @@ -1481,7 +1480,7 @@ extern "C" {
pub fn LLVMRustArchiveIteratorFree(AIR: ArchiveIteratorRef);
pub fn LLVMRustDestroyArchive(AR: &'static mut Archive);

pub fn LLVMRustGetSectionName(SI: SectionIteratorRef, data: &mut *const c_char) -> size_t;
pub fn LLVMRustGetSectionName(SI: &SectionIterator, data: &mut *const c_char) -> size_t;

pub fn LLVMRustWriteTwineToString(T: &Twine, s: &RustString);

Expand Down
10 changes: 5 additions & 5 deletions src/librustc_codegen_llvm/llvm/mod.rs
Expand Up @@ -204,19 +204,19 @@ impl Drop for ObjectFile {

// Memory-managed interface to section iterators.

pub struct SectionIter {
pub llsi: SectionIteratorRef,
pub struct SectionIter<'a> {
pub llsi: &'a mut SectionIterator<'a>,
}

impl Drop for SectionIter {
impl Drop for SectionIter<'a> {
fn drop(&mut self) {
unsafe {
LLVMDisposeSectionIterator(self.llsi);
LLVMDisposeSectionIterator(&mut *(self.llsi as *mut _));
}
}
}

pub fn mk_section_iter(llof: &ffi::ObjectFile) -> SectionIter {
pub fn mk_section_iter(llof: &'a ffi::ObjectFile) -> SectionIter<'a> {
unsafe { SectionIter { llsi: LLVMGetSections(llof) } }
}

Expand Down

0 comments on commit 2c1d7fb

Please sign in to comment.