Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced mem::uninitialized with mem::MaybeUninit #1

Merged
merged 2 commits into from
Jul 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ native_ref!(&MemoryBuffer = LLVMMemoryBufferRef);
impl MemoryBuffer {
pub fn new_from_file(path: &str) -> Result<CBox<MemoryBuffer>, CBox<str>> {
util::with_cstr(path, |path| unsafe {
let mut output = mem::uninitialized();
let mut error = mem::uninitialized();
if core::LLVMCreateMemoryBufferWithContentsOfFile(path, &mut output, &mut error) == 1 {
Err(CBox::new(error))
let mut output = mem::MaybeUninit::uninit();
let mut error = mem::MaybeUninit::uninit();
if core::LLVMCreateMemoryBufferWithContentsOfFile(path, output.as_mut_ptr(), error.as_mut_ptr()) == 1 {
Err(CBox::new(error.assume_init()))
} else {
Ok(CBox::new(output))
Ok(CBox::new(output.assume_init()))
}
})
}
Expand Down
18 changes: 9 additions & 9 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ pub trait ExecutionEngine<'a>:'a + Sized + DisposeRef where LLVMExecutionEngineR
/// Remove a module from the list of modules to interpret or compile.
fn remove_module(&'a self, module: &'a Module) -> &'a Module {
unsafe {
let mut out = mem::uninitialized();
engine::LLVMRemoveModule(self.into(), module.into(), &mut out, ptr::null_mut());
out.into()
let mut out = mem::MaybeUninit::uninit();
engine::LLVMRemoveModule(self.into(), module.into(), out.as_mut_ptr(), ptr::null_mut());
out.assume_init().into()
}
}
/// Execute all of the static constructors for this program.
Expand Down Expand Up @@ -134,7 +134,7 @@ impl<'a> ExecutionEngine<'a> for JitEngine {
type Options = JitOptions;
fn new(module: &'a Module, options: JitOptions) -> Result<CSemiBox<'a, JitEngine>, CBox<str>> {
unsafe {
let mut ee = mem::uninitialized();
let mut ee = mem::MaybeUninit::uninit();
let mut out = mem::zeroed();
engine::LLVMLinkInMCJIT();
if target::LLVM_InitializeNativeTarget() == 1 {
Expand All @@ -151,9 +151,9 @@ impl<'a> ExecutionEngine<'a> for JitEngine {
MCJMM: ptr::null_mut()
};
let size = mem::size_of::<LLVMMCJITCompilerOptions>();
let result = engine::LLVMCreateMCJITCompilerForModule(&mut ee, (&*module).into(), &mut options, size, &mut out);
let result = engine::LLVMCreateMCJITCompilerForModule(ee.as_mut_ptr(), (&*module).into(), &mut options, size, &mut out);
if result == 0 {
Ok(ee.into())
Ok(ee.assume_init().into())
} else {
Err(CBox::new(out))
}
Expand All @@ -168,12 +168,12 @@ impl<'a> ExecutionEngine<'a> for Interpreter {
type Options = ();
fn new(module: &'a Module, _: ()) -> Result<CSemiBox<'a, Interpreter>, CBox<str>> {
unsafe {
let mut ee = mem::uninitialized();
let mut ee = mem::MaybeUninit::uninit();
let mut out = mem::zeroed();
engine::LLVMLinkInInterpreter();
let result = engine::LLVMCreateInterpreterForModule(&mut ee, (&*module).into(), &mut out);
let result = engine::LLVMCreateInterpreterForModule(ee.as_mut_ptr(), (&*module).into(), &mut out);
if result == 0 {
Ok(ee.into())
Ok(ee.assume_init().into())
} else {
Err(CBox::new(out))
}
Expand Down
32 changes: 16 additions & 16 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ impl Module {
/// Parse this bitcode file into a module, or return an error string.
pub fn parse_bitcode<'a>(context: &'a Context, path: &str) -> Result<CSemiBox<'a, Module>, CBox<str>> {
unsafe {
let mut out = mem::uninitialized();
let mut err = mem::uninitialized();
let buf = try!(MemoryBuffer::new_from_file(path));
if reader::LLVMParseBitcodeInContext(context.into(), buf.as_ptr(), &mut out, &mut err) == 1 {
Err(CBox::new(err))
let mut out = mem::MaybeUninit::uninit();
let mut err = mem::MaybeUninit::uninit();
let buf = MemoryBuffer::new_from_file(path)?;
if reader::LLVMParseBitcodeInContext(context.into(), buf.as_ptr(), out.as_mut_ptr(), err.as_mut_ptr()) == 1 {
Err(CBox::new(err.assume_init()))
} else {
Ok(CSemiBox::new(out))
Ok(CSemiBox::new(out.assume_init()))
}
}
}
Expand Down Expand Up @@ -161,10 +161,10 @@ impl Module {
/// when an error occurs.
pub fn verify(&self) -> Result<(), CBox<str>> {
unsafe {
let mut error = mem::uninitialized();
let mut error = mem::MaybeUninit::uninit();
let action = LLVMVerifierFailureAction::LLVMReturnStatusAction;
if analysis::LLVMVerifyModule(self.into(), action, &mut error) == 1 {
Err(CBox::new(error))
if analysis::LLVMVerifyModule(self.into(), action, error.as_mut_ptr()) == 1 {
Err(CBox::new(error.assume_init()))
} else {
Ok(())
}
Expand All @@ -180,7 +180,7 @@ impl Module {
let path = path.to_str().unwrap();
let mod_path = dir.join("module.bc");
let mod_path = mod_path.to_str().unwrap();
try!(self.write_bitcode(mod_path));
self.write_bitcode(mod_path)?;
Command::new("llc")
.arg(&format!("-O={}", opt_level))
.arg("-filetype=obj")
Expand All @@ -197,9 +197,9 @@ impl Module {
unsafe {
let dest = self.into();
let src = src.into();
let mut message = mem::uninitialized();
if linker::LLVMLinkModules(dest, src, linker::LLVMLinkerMode::LLVMLinkerPreserveSource, &mut message) == 1 {
Err(CBox::new(message))
let mut message = mem::MaybeUninit::uninit();
if linker::LLVMLinkModules(dest, src, linker::LLVMLinkerMode::LLVMLinkerPreserveSource, message.as_mut_ptr()) == 1 {
Err(CBox::new(message.assume_init()))
} else {
Ok(())
}
Expand All @@ -213,9 +213,9 @@ impl Module {
unsafe {
let dest = self.into();
let src = src.as_ptr();
let mut message = mem::uninitialized();
if linker::LLVMLinkModules(dest, src, linker::LLVMLinkerMode::LLVMLinkerDestroySource, &mut message) == 1 {
Err(CBox::new(message))
let mut message = mem::MaybeUninit::uninit();
if linker::LLVMLinkModules(dest, src, linker::LLVMLinkerMode::LLVMLinkerDestroySource, message.as_mut_ptr()) == 1 {
Err(CBox::new(message.assume_init()))
} else {
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ native_ref!(ObjectFile, obj: LLVMObjectFileRef);
impl ObjectFile {
/// Parse the object file at the path given, or return an error string if an error occurs.
pub fn read(path: &str) -> Result<ObjectFile, CBox<str>> {
let buf = try!(MemoryBuffer::new_from_file(path));
let buf = MemoryBuffer::new_from_file(path)?;
unsafe {
let ptr = object::LLVMCreateObjectFile(buf.as_ptr());
if ptr.is_null() {
Expand Down
10 changes: 5 additions & 5 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ impl StructType {
pub fn get_elements(&self) -> Vec<&Type> {
unsafe {
let size = core::LLVMCountStructElementTypes(self.into());
let mut els:Vec<_> = (0..size).map(|_| mem::uninitialized()).collect();
let mut els:Vec<_> = (0..size).map(|_| mem::MaybeUninit::<&Type>::uninit()).collect();
core::LLVMGetStructElementTypes(self.into(), els.as_mut_ptr() as *mut LLVMTypeRef);
els
els.into_iter().map(|el| el.assume_init()).collect()
}
}
}
Expand Down Expand Up @@ -145,9 +145,9 @@ impl FunctionType {
pub fn get_params(&self) -> Vec<&Type> {
unsafe {
let count = core::LLVMCountParamTypes(self.into());
let mut types:Vec<_> = (0..count).map(|_| mem::uninitialized()).collect();
let mut types:Vec<_> = (0..count).map(|_| mem::MaybeUninit::<&Type>::uninit()).collect();
core::LLVMGetParamTypes(self.into(), types.as_mut_ptr() as *mut LLVMTypeRef);
types
types.into_iter().map(|type_| type_.assume_init()).collect()
}
}
/// Returns the type that this function returns.
Expand Down Expand Up @@ -229,4 +229,4 @@ impl ArrayType {
pub fn get_length(&self) -> usize {
unsafe { core::LLVMGetArrayLength(self.into()) as usize }
}
}
}