Skip to content

Commit

Permalink
Return result from malloc builder functions
Browse files Browse the repository at this point in the history
  • Loading branch information
willtunnels committed Jan 23, 2020
1 parent f9e23fc commit 78f9e53
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
17 changes: 11 additions & 6 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ impl<'ctx> Builder<'ctx> {
}

// TODOC: Heap allocation
pub fn build_malloc<T: BasicType<'ctx>>(&self, ty: T, name: &str) -> PointerValue<'ctx> {
pub fn build_malloc<T: BasicType<'ctx>>(&self, ty: T, name: &str) -> Result<PointerValue<'ctx>, &'static str> {
// LLVMBulidMalloc segfaults if ty is unsized
let is_sized = match ty.as_basic_type_enum() {
BasicTypeEnum::ArrayType(ty) => ty.is_sized(),
Expand All @@ -359,7 +359,7 @@ impl<'ctx> Builder<'ctx> {
BasicTypeEnum::VectorType(ty) => ty.is_sized(),
};
if !is_sized {
panic!("Cannot build malloc call for an unsized type {:?}", ty);
return Err("Cannot build malloc call for an unsized type");
}

let c_string = CString::new(name).expect("Conversion to CString failed unexpectedly");
Expand All @@ -368,11 +368,16 @@ impl<'ctx> Builder<'ctx> {
LLVMBuildMalloc(self.builder, ty.as_type_ref(), c_string.as_ptr())
};

PointerValue::new(value)
Ok(PointerValue::new(value))
}

// TODOC: Heap allocation
pub fn build_array_malloc<T: BasicType<'ctx>>(&self, ty: T, size: IntValue<'ctx>, name: &str) -> PointerValue<'ctx> {
pub fn build_array_malloc<T: BasicType<'ctx>>(
&self,
ty: T,
size: IntValue<'ctx>,
name: &str
) -> Result<PointerValue<'ctx>, &'static str> {
// LLVMBulidArrayMalloc segfaults if ty is unsized
let is_sized = match ty.as_basic_type_enum() {
BasicTypeEnum::ArrayType(ty) => ty.is_sized(),
Expand All @@ -383,7 +388,7 @@ impl<'ctx> Builder<'ctx> {
BasicTypeEnum::VectorType(ty) => ty.is_sized(),
};
if !is_sized {
panic!("Cannot build malloc call for an unsized type {:?}", ty);
return Err("Cannot build array malloc call for an unsized type");
}

let c_string = CString::new(name).expect("Conversion to CString failed unexpectedly");
Expand All @@ -392,7 +397,7 @@ impl<'ctx> Builder<'ctx> {
LLVMBuildArrayMalloc(self.builder, ty.as_type_ref(), size.as_value_ref(), c_string.as_ptr())
};

PointerValue::new(value)
Ok(PointerValue::new(value))
}

// SubType: <P>(&self, ptr: PointerValue<P>) -> InstructionValue {
Expand Down
10 changes: 6 additions & 4 deletions tests/all/test_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -928,17 +928,19 @@ fn test_allocations() {

let heap_ptr = builder.build_malloc(i32_type, "heap_ptr");

assert_eq!(*heap_ptr.get_type().print_to_string(), *CString::new("i32*").unwrap());
assert!(heap_ptr.is_ok());
assert_eq!(*heap_ptr.unwrap().get_type().print_to_string(), *CString::new("i32*").unwrap());

let heap_array = builder.build_array_malloc(i32_type, i32_three, "heap_array");

assert_eq!(*heap_array.get_type().print_to_string(), *CString::new("i32*").unwrap());
assert!(heap_array.is_ok());
assert_eq!(*heap_array.unwrap().get_type().print_to_string(), *CString::new("i32*").unwrap());

let bad_malloc_res = std::panic::catch_unwind(|| builder.build_malloc(unsized_type, ""));
let bad_malloc_res = builder.build_malloc(unsized_type, "");

assert!(bad_malloc_res.is_err());

let bad_array_malloc_res = std::panic::catch_unwind(|| builder.build_array_malloc(unsized_type, i32_three, ""));
let bad_array_malloc_res = builder.build_array_malloc(unsized_type, i32_three, "");

assert!(bad_array_malloc_res.is_err());
}
Expand Down

0 comments on commit 78f9e53

Please sign in to comment.