text
stringlengths
0
357
array = (dc_array_t*) calloc(1, sizeof(dc_array_t));
if (array==NULL) {
exit(47);
}
array->magic = DC_ARRAY_MAGIC;
array->context = context;
array->count = 0;
array->allocated = initsize<1? 1 : initsize;
array->type = type;
array->array = malloc(array->allocated * sizeof(uintptr_t));
if (array->array==NULL) {
exit(48);
}
return array;
}
dc_array_t* dc_array_new(dc_context_t* context, size_t initsize)
{
return dc_array_new_typed(context, 0, initsize);
}
/**
* Free an array object. Does not free any data items.
*
* @memberof dc_array_t
* @param array The array object to free,
* created eg. by dc_get_chatlist(), dc_get_contacts() and so on.
* If NULL is given, nothing is done.
* @return None.
*/
void dc_array_unref(dc_array_t* array)
{
if (array==NULL || array->magic!=DC_ARRAY_MAGIC) {
return;
}
if (array->type==DC_ARRAY_LOCATIONS) {
dc_array_free_ptr(array);
}
free(array->array);
array->magic = 0;
free(array);
}
/**
* Calls free() for each item and sets the item to 0 afterwards.
* The array object itself is not deleted and the size of the array stays the same.
*
* @private @memberof dc_array_t
* @param array The array object.
* @return None.
*/
void dc_array_free_ptr(dc_array_t* array)
{
if (array==NULL || array->magic!=DC_ARRAY_MAGIC) {
return;
}
for (size_t i = 0; i<array->count; i++) {
if (array->type==DC_ARRAY_LOCATIONS) {
free(((struct _dc_location*)array->array[i])->marker);
}
free((void*)array->array[i]);
array->array[i] = 0;
}
}
/**
* Duplicates the array, take care if the array contains pointers to objects, take care to free them only once afterwards!
* If the array only contains integers, you are always save.
*
* @private @memberof dc_array_t
* @param array The array object.
* @return The duplicated array.
*/
dc_array_t* dc_array_duplicate(const dc_array_t* array)
{
dc_array_t* ret = NULL;
if (array==NULL || array->magic!=DC_ARRAY_MAGIC) {
return NULL;
}
ret = dc_array_new(array->context, array->allocated);
ret->count = array->count;
memcpy(ret->array, array->array, array->count * sizeof(uintptr_t));
return ret;
}
static int cmp_intptr_t(const void* p1, const void* p2)
{