diff --git a/src/dc_array.rs b/src/dc_array.rs index 46ded0c27..d850c0533 100644 --- a/src/dc_array.rs +++ b/src/dc_array.rs @@ -6,23 +6,19 @@ const DC_ARRAY_MAGIC: uint32_t = 0x000a11aa; const DC_ARRAY_LOCATIONS: libc::c_int = 1; /* * the structure behind dc_array_t */ -#[derive(Copy, Clone)] +#[derive(Clone)] pub struct dc_array_t { pub magic: uint32_t, - pub allocated: size_t, - pub count: size_t, pub type_0: libc::c_int, - pub array: *mut uintptr_t, + pub array: Vec, } impl dc_array_t { - pub fn new() -> Self { + pub fn new(capacity: usize) -> Self { dc_array_t { magic: DC_ARRAY_MAGIC, - allocated: 0, - count: 0, type_0: 0, - array: 0 as *mut uintptr_t, + array: Vec::with_capacity(capacity), } } @@ -46,7 +42,6 @@ pub unsafe fn dc_array_unref(mut array: *mut dc_array_t) { if (*array).type_0 == DC_ARRAY_LOCATIONS { dc_array_free_ptr(array); } - free((*array).array as *mut libc::c_void); (*array).magic = 0i32 as uint32_t; Box::from_raw(array); } @@ -55,31 +50,21 @@ pub unsafe fn dc_array_free_ptr(array: *mut dc_array_t) { if array.is_null() || (*array).magic != DC_ARRAY_MAGIC { return; } - for i in 0..(*array).count { + for i in 0..(*array).array.len() { if (*array).type_0 == DC_ARRAY_LOCATIONS { - Box::from_raw(*(*array).array.offset(i as isize) as *mut dc_location); + Box::from_raw((*array).array[i] as *mut dc_location); } else { - free(*(*array).array.offset(i as isize) as *mut libc::c_void); + free((*array).array[i] as *mut libc::c_void); } - *(*array).array.offset(i as isize) = 0i32 as uintptr_t; + (*array).array[i] = 0i32 as uintptr_t; } } -pub unsafe fn dc_array_add_uint(mut array: *mut dc_array_t, item: uintptr_t) { +pub unsafe fn dc_array_add_uint(array: *mut dc_array_t, item: uintptr_t) { if array.is_null() || (*array).magic != DC_ARRAY_MAGIC { return; } - if (*array).count == (*array).allocated { - let newsize = (*array).allocated.wrapping_mul(2).wrapping_add(10); - (*array).array = realloc( - (*array).array as *mut libc::c_void, - (newsize).wrapping_mul(::std::mem::size_of::()), - ) as *mut uintptr_t; - assert!(!(*array).array.is_null()); - (*array).allocated = newsize as size_t - } - *(*array).array.offset((*array).count as isize) = item; - (*array).count = (*array).count.wrapping_add(1); + (*array).array.push(item); } pub unsafe fn dc_array_add_id(array: *mut dc_array_t, item: uint32_t) { @@ -94,127 +79,127 @@ pub unsafe fn dc_array_get_cnt(array: *const dc_array_t) -> size_t { if array.is_null() || (*array).magic != DC_ARRAY_MAGIC { return 0i32 as size_t; } - (*array).count + (*array).array.len() } pub unsafe fn dc_array_get_uint(array: *const dc_array_t, index: size_t) -> uintptr_t { - if array.is_null() || (*array).magic != DC_ARRAY_MAGIC || index >= (*array).count { + if array.is_null() || (*array).magic != DC_ARRAY_MAGIC || index >= (*array).array.len() { return 0i32 as uintptr_t; } - *(*array).array.offset(index as isize) + (*array).array[index] } pub unsafe fn dc_array_get_id(array: *const dc_array_t, index: size_t) -> uint32_t { - if array.is_null() || (*array).magic != DC_ARRAY_MAGIC || index >= (*array).count { + if array.is_null() || (*array).magic != DC_ARRAY_MAGIC || index >= (*array).array.len() { return 0i32 as uint32_t; } if (*array).type_0 == DC_ARRAY_LOCATIONS { - return (*(*(*array).array.offset(index as isize) as *mut dc_location)).location_id; + return (*((*array).array[index] as *mut dc_location)).location_id; } - *(*array).array.offset(index as isize) as uint32_t + (*array).array[index] as uint32_t } pub unsafe fn dc_array_get_ptr(array: *const dc_array_t, index: size_t) -> *mut libc::c_void { - if array.is_null() || (*array).magic != DC_ARRAY_MAGIC || index >= (*array).count { + if array.is_null() || (*array).magic != DC_ARRAY_MAGIC || index >= (*array).array.len() { return 0 as *mut libc::c_void; } - *(*array).array.offset(index as isize) as *mut libc::c_void + (*array).array[index] as *mut libc::c_void } pub unsafe fn dc_array_get_latitude(array: *const dc_array_t, index: size_t) -> libc::c_double { if array.is_null() || (*array).magic != DC_ARRAY_MAGIC - || index >= (*array).count + || index >= (*array).array.len() || (*array).type_0 != DC_ARRAY_LOCATIONS - || *(*array).array.offset(index as isize) == 0 + || (*array).array[index] == 0 { return 0i32 as libc::c_double; } - (*(*(*array).array.offset(index as isize) as *mut dc_location)).latitude + (*((*array).array[index] as *mut dc_location)).latitude } pub unsafe fn dc_array_get_longitude(array: *const dc_array_t, index: size_t) -> libc::c_double { if array.is_null() || (*array).magic != DC_ARRAY_MAGIC - || index >= (*array).count + || index >= (*array).array.len() || (*array).type_0 != DC_ARRAY_LOCATIONS - || *(*array).array.offset(index as isize) == 0 + || (*array).array[index] == 0 { return 0i32 as libc::c_double; } - (*(*(*array).array.offset(index as isize) as *mut dc_location)).longitude + (*((*array).array[index] as *mut dc_location)).longitude } pub unsafe fn dc_array_get_accuracy(array: *const dc_array_t, index: size_t) -> libc::c_double { if array.is_null() || (*array).magic != DC_ARRAY_MAGIC - || index >= (*array).count + || index >= (*array).array.len() || (*array).type_0 != DC_ARRAY_LOCATIONS - || *(*array).array.offset(index as isize) == 0 + || (*array).array[index] == 0 { return 0i32 as libc::c_double; } - (*(*(*array).array.offset(index as isize) as *mut dc_location)).accuracy + (*((*array).array[index] as *mut dc_location)).accuracy } pub unsafe fn dc_array_get_timestamp(array: *const dc_array_t, index: size_t) -> i64 { if array.is_null() || (*array).magic != DC_ARRAY_MAGIC - || index >= (*array).count + || index >= (*array).array.len() || (*array).type_0 != DC_ARRAY_LOCATIONS - || *(*array).array.offset(index as isize) == 0 + || (*array).array[index] == 0 { return 0; } - (*(*(*array).array.offset(index as isize) as *mut dc_location)).timestamp + (*((*array).array[index] as *mut dc_location)).timestamp } pub unsafe fn dc_array_get_chat_id(array: *const dc_array_t, index: size_t) -> uint32_t { if array.is_null() || (*array).magic != DC_ARRAY_MAGIC - || index >= (*array).count + || index >= (*array).array.len() || (*array).type_0 != DC_ARRAY_LOCATIONS - || *(*array).array.offset(index as isize) == 0 + || (*array).array[index] == 0 { return 0i32 as uint32_t; } - (*(*(*array).array.offset(index as isize) as *mut dc_location)).chat_id + (*((*array).array[index] as *mut dc_location)).chat_id } pub unsafe fn dc_array_get_contact_id(array: *const dc_array_t, index: size_t) -> uint32_t { if array.is_null() || (*array).magic != DC_ARRAY_MAGIC - || index >= (*array).count + || index >= (*array).array.len() || (*array).type_0 != DC_ARRAY_LOCATIONS - || *(*array).array.offset(index as isize) == 0 + || (*array).array[index] == 0 { return 0i32 as uint32_t; } - (*(*(*array).array.offset(index as isize) as *mut dc_location)).contact_id + (*((*array).array[index] as *mut dc_location)).contact_id } pub unsafe fn dc_array_get_msg_id(array: *const dc_array_t, index: size_t) -> uint32_t { if array.is_null() || (*array).magic != DC_ARRAY_MAGIC - || index >= (*array).count + || index >= (*array).array.len() || (*array).type_0 != DC_ARRAY_LOCATIONS - || *(*array).array.offset(index as isize) == 0 + || (*array).array[index] == 0 { return 0i32 as uint32_t; } - (*(*(*array).array.offset(index as isize) as *mut dc_location)).msg_id + (*((*array).array[index] as *mut dc_location)).msg_id } pub unsafe fn dc_array_get_marker(array: *const dc_array_t, index: size_t) -> *mut libc::c_char { if array.is_null() || (*array).magic != DC_ARRAY_MAGIC - || index >= (*array).count + || index >= (*array).array.len() || (*array).type_0 != DC_ARRAY_LOCATIONS - || *(*array).array.offset(index as isize) == 0 + || (*array).array[index] == 0 { return 0 as *mut libc::c_char; } - if let Some(s) = &(*(*(*array).array.offset(index as isize) as *mut dc_location)).marker { + if let Some(s) = &(*((*array).array[index] as *mut dc_location)).marker { to_cstring(s) } else { std::ptr::null_mut() @@ -234,14 +219,14 @@ pub unsafe fn dc_array_get_marker(array: *const dc_array_t, index: size_t) -> *m pub unsafe fn dc_array_is_independent(array: *const dc_array_t, index: size_t) -> libc::c_int { if array.is_null() || (*array).magic != DC_ARRAY_MAGIC - || index >= (*array).count + || index >= (*array).array.len() || (*array).type_0 != DC_ARRAY_LOCATIONS - || *(*array).array.offset(index as isize) == 0 + || (*array).array[index] == 0 { return 0; } - (*(*(*array).array.offset(index as isize) as *mut dc_location)).independent as libc::c_int + (*((*array).array[index] as *mut dc_location)).independent as libc::c_int } pub unsafe fn dc_array_search_id( @@ -252,9 +237,8 @@ pub unsafe fn dc_array_search_id( if array.is_null() || (*array).magic != DC_ARRAY_MAGIC { return false; } - let data: *mut uintptr_t = (*array).array; - for i in 0..(*array).count { - if *data.offset(i as isize) == needle as size_t { + for i in 0..(*array).array.len() { + if (*array).array[i] == needle as size_t { if !ret_index.is_null() { *ret_index = i } @@ -268,7 +252,7 @@ pub unsafe fn dc_array_get_raw(array: *const dc_array_t) -> *const uintptr_t { if array.is_null() || (*array).magic != DC_ARRAY_MAGIC { return 0 as *const uintptr_t; } - (*array).array + (*array).array.as_ptr() } pub unsafe fn dc_array_new(initsize: size_t) -> *mut dc_array_t { @@ -276,27 +260,17 @@ pub unsafe fn dc_array_new(initsize: size_t) -> *mut dc_array_t { } pub unsafe fn dc_array_new_typed(type_0: libc::c_int, initsize: size_t) -> *mut dc_array_t { - let mut array = dc_array_t::new(); - - array.allocated = if initsize < 1 { 1 } else { initsize }; + let capacity = if initsize < 1 { 1 } else { initsize as usize }; + let mut array = dc_array_t::new(capacity); array.type_0 = type_0; - array.array = malloc( - array - .allocated - .wrapping_mul(::std::mem::size_of::()), - ) as *mut uintptr_t; - if array.array.is_null() { - exit(48i32); - } - array.as_ptr() } -pub unsafe fn dc_array_empty(mut array: *mut dc_array_t) { +pub unsafe fn dc_array_empty(array: *mut dc_array_t) { if array.is_null() || (*array).magic != DC_ARRAY_MAGIC { return; } - (*array).count = 0i32 as size_t; + (*array).array.clear(); } pub unsafe fn dc_array_duplicate(array: *const dc_array_t) -> *mut dc_array_t { @@ -304,40 +278,16 @@ pub unsafe fn dc_array_duplicate(array: *const dc_array_t) -> *mut dc_array_t { if array.is_null() || (*array).magic != DC_ARRAY_MAGIC { return 0 as *mut dc_array_t; } - ret = dc_array_new((*array).allocated); - (*ret).count = (*array).count; - memcpy( - (*ret).array as *mut libc::c_void, - (*array).array as *const libc::c_void, - (*array) - .count - .wrapping_mul(::std::mem::size_of::()), - ); + ret = dc_array_new(1); + (*ret).array = (*array).array.clone(); ret } pub unsafe fn dc_array_sort_ids(array: *mut dc_array_t) { - if array.is_null() || (*array).magic != DC_ARRAY_MAGIC || (*array).count <= 1 { + if array.is_null() || (*array).magic != DC_ARRAY_MAGIC || (*array).array.len() <= 1 { return; } - qsort( - (*array).array as *mut libc::c_void, - (*array).count, - ::std::mem::size_of::(), - Some(cmp_intptr_t), - ); -} - -unsafe extern "C" fn cmp_intptr_t(p1: *const libc::c_void, p2: *const libc::c_void) -> libc::c_int { - let v1: uintptr_t = *(p1 as *mut uintptr_t); - let v2: uintptr_t = *(p2 as *mut uintptr_t); - return if v1 < v2 { - -1i32 - } else if v1 > v2 { - 1i32 - } else { - 0i32 - }; + (*array).array.sort(); } pub unsafe fn dc_array_get_string( @@ -347,14 +297,12 @@ pub unsafe fn dc_array_get_string( if array.is_null() || (*array).magic != DC_ARRAY_MAGIC || sep.is_null() { return dc_strdup(b"\x00" as *const u8 as *const libc::c_char); } - let cnt = (*array).count as usize; - let slice = std::slice::from_raw_parts((*array).array, cnt); + let cnt = (*array).array.len(); let sep = as_str(sep); - let res = slice - .iter() - .enumerate() - .fold(String::with_capacity(2 * cnt), |mut res, (i, n)| { + let res = (*array).array.iter().enumerate().fold( + String::with_capacity(2 * cnt), + |mut res, (i, n)| { if i == 0 { res += &n.to_string(); } else { diff --git a/src/dc_chat.rs b/src/dc_chat.rs index 8f7ac8ddf..aed692a96 100644 --- a/src/dc_chat.rs +++ b/src/dc_chat.rs @@ -2168,11 +2168,8 @@ pub unsafe fn dc_chat_get_profile_image(chat: *const Chat) -> *mut libc::c_char image_abs = dc_get_abs_path((*chat).context, image_rel) } else if (*chat).type_0 == 100i32 { contacts = dc_get_chat_contacts((*chat).context, (*chat).id); - if (*contacts).count >= 1 { - contact = dc_get_contact( - (*chat).context, - *(*contacts).array.offset(0isize) as uint32_t, - ); + if !(*contacts).array.is_empty() { + contact = dc_get_contact((*chat).context, (*contacts).array[0] as uint32_t); image_abs = dc_contact_get_profile_image(contact) } } @@ -2192,11 +2189,8 @@ pub unsafe fn dc_chat_get_color(chat: *const Chat) -> uint32_t { if !(chat.is_null() || (*chat).magic != 0xc4a7c4a7u32) { if (*chat).type_0 == 100i32 { contacts = dc_get_chat_contacts((*chat).context, (*chat).id); - if (*contacts).count >= 1 { - contact = dc_get_contact( - (*chat).context, - *(*contacts).array.offset(0isize) as uint32_t, - ); + if !(*contacts).array.is_empty() { + contact = dc_get_contact((*chat).context, (*contacts).array[0] as uint32_t); color = dc_str_to_color((*contact).addr) as uint32_t } } else {