Fixed semantics of tox_array (should also be more memory efficient).

This commit is contained in:
Anony Moose 2013-08-24 20:34:38 +00:00
parent 05167a9294
commit 4326c4c8b5
No known key found for this signature in database
GPG Key ID: A567AC285089C602

View File

@ -26,6 +26,7 @@
#include <stdlib.h>
#include <stdint.h>
#include <string.h> /* for memcpy() */
unsigned char *hex_string_to_bin(char hex_string[]);
@ -67,6 +68,7 @@ unsigned char *hex_string_to_bin(char hex_string[]);
/************************Linked List***********************
* http://wiki.tox.im/index.php/Internal_functions_and_data_structures#Linked_List
* TODO: Update wiki.
**********************************************************/
#define MEMBER_OFFSET(var_name_in_parent, parent_type) \
@ -76,22 +78,22 @@ unsigned char *hex_string_to_bin(char hex_string[]);
((parent_type*)((uint64_t)(&(var)) - (uint64_t)(MEMBER_OFFSET(var_name_in_parent, parent_type))))
#define TOX_LIST_FOR_EACH(lst, tmp_name) \
for (tox_list_t* tmp_name = lst.next; tmp_name != &lst; tmp_name = tmp_name->next)
for (tox_list* tmp_name = lst.next; tmp_name != &lst; tmp_name = tmp_name->next)
#define TOX_LIST_GET_VALUE(tmp_name, name_in_parent, parent_type) GET_PARENT(tmp_name, name_in_parent, parent_type)
typedef struct tox_list {
struct tox_list *prev, *next;
} tox_list_t;
} tox_list;
/* Returns a new tox_list_t. */
static inline void tox_list_new(tox_list_t *lst)
static inline void tox_list_new(tox_list *lst)
{
lst->prev = lst->next = lst;
}
/* Inserts a new tox_lst after lst and returns it. */
static inline void tox_list_add(tox_list_t *lst, tox_list_t *new_lst)
static inline void tox_list_add(tox_list *lst, tox_list *new_lst)
{
tox_list_new(new_lst);
@ -102,61 +104,60 @@ static inline void tox_list_add(tox_list_t *lst, tox_list_t *new_lst)
new_lst->prev = lst;
}
static inline void tox_list_remove(tox_list_t *lst)
static inline void
tox_list_remove(tox_list *lst)
{
lst->prev->next = lst->next;
lst->next->prev = lst->prev;
}
/****************************Array***************************
* Array to store pointers which tracks it's own size.
* TODO: Figure out if it shold store values instead of
* pointers?
* Array which manages its own memory allocation.
* It stores copy of data (not pointers).
* TODO: Add wiki info usage.
************************************************************/
struct tox_array {
void **data;
uint32_t size, length;
};
typedef struct tox_array {
void *data;
uint32_t len;
size_t elem_size; /* in bytes */
} tox_array;
static inline void tox_array_init(struct tox_array *arr)
static inline void
tox_array_init(tox_array *arr, size_t elem_size)
{
arr->size = 1;
arr->length = 0;
arr->data = malloc(sizeof(void *));
arr->len = 0;
arr->elem_size = elem_size;
arr->data = NULL;
}
static inline void tox_array_delete(struct tox_array *arr)
static inline void
tox_array_delete(tox_array *arr)
{
free(arr->data);
arr->size = arr->length = 0;
arr->len = arr->elem_size = 0;
}
/* shrinks arr so it will not have unused space. If you want to have
* addtional space, extra species the amount of extra space.
static inline void
_tox_array_push(tox_array *arr, uint8_t *item)
{
arr->data = realloc(arr->data, arr->elem_size * (arr->len+1));
memcpy(arr->data + arr->elem_size*arr->len, item, arr->elem_size);
arr->len++;
}
#define tox_array_push(arr, item) _tox_array_push(arr, (void*)(&(item)))
/* Deletes num items from array.
* Not same as pop in stacks, because to access elements you use data.
*/
static inline void tox_array_shrink_to_fit(struct tox_array *arr, int32_t extra)
static inline void
tox_array_pop(tox_array *arr, uint32_t num)
{
arr->size = arr->length + extra;
arr->data = realloc(arr->data, arr->size * sizeof(void *));
arr->len--;
arr->data = realloc(arr->data, arr->elem_size*arr->len);
}
static inline void _tox_array_push(struct tox_array *arr, void *new)
{
if (arr->length + 1 >= arr->size)
tox_array_shrink_to_fit(arr, arr->size);
arr->data[arr->length++] = new;
}
#define tox_array_push(arr, new) _tox_array_push(arr, (void*)new)
static inline void *tox_array_pop(struct tox_array *arr)
{
if (arr->length - 1 < arr->size / 4)
tox_array_shrink_to_fit(arr, arr->length * 2);
return arr->data[arr->length--];
}
#define tox_array_get(arr, i, type) ((type*)(arr)->data)[i]
#endif // MISC_TOOLS_H