mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Fixed semantics of tox_array (should also be more memory efficient).
This commit is contained in:
parent
05167a9294
commit
4326c4c8b5
|
@ -26,6 +26,7 @@
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <string.h> /* for memcpy() */
|
||||||
|
|
||||||
unsigned char *hex_string_to_bin(char hex_string[]);
|
unsigned char *hex_string_to_bin(char hex_string[]);
|
||||||
|
|
||||||
|
@ -67,6 +68,7 @@ unsigned char *hex_string_to_bin(char hex_string[]);
|
||||||
|
|
||||||
/************************Linked List***********************
|
/************************Linked List***********************
|
||||||
* http://wiki.tox.im/index.php/Internal_functions_and_data_structures#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) \
|
#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))))
|
((parent_type*)((uint64_t)(&(var)) - (uint64_t)(MEMBER_OFFSET(var_name_in_parent, parent_type))))
|
||||||
|
|
||||||
#define TOX_LIST_FOR_EACH(lst, tmp_name) \
|
#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)
|
#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 {
|
typedef struct tox_list {
|
||||||
struct tox_list *prev, *next;
|
struct tox_list *prev, *next;
|
||||||
} tox_list_t;
|
} tox_list;
|
||||||
|
|
||||||
/* Returns a new tox_list_t. */
|
/* 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;
|
lst->prev = lst->next = lst;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Inserts a new tox_lst after lst and returns it. */
|
/* 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);
|
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;
|
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->prev->next = lst->next;
|
||||||
lst->next->prev = lst->prev;
|
lst->next->prev = lst->prev;
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************Array***************************
|
/****************************Array***************************
|
||||||
* Array to store pointers which tracks it's own size.
|
* Array which manages its own memory allocation.
|
||||||
* TODO: Figure out if it shold store values instead of
|
* It stores copy of data (not pointers).
|
||||||
* pointers?
|
|
||||||
* TODO: Add wiki info usage.
|
* TODO: Add wiki info usage.
|
||||||
************************************************************/
|
************************************************************/
|
||||||
|
|
||||||
struct tox_array {
|
typedef struct tox_array {
|
||||||
void **data;
|
void *data;
|
||||||
uint32_t size, length;
|
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->len = 0;
|
||||||
arr->length = 0;
|
arr->elem_size = elem_size;
|
||||||
arr->data = malloc(sizeof(void *));
|
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);
|
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
|
static inline void
|
||||||
* addtional space, extra species the amount of extra space.
|
_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->len--;
|
||||||
arr->data = realloc(arr->data, arr->size * sizeof(void *));
|
arr->data = realloc(arr->data, arr->elem_size*arr->len);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void _tox_array_push(struct tox_array *arr, void *new)
|
#define tox_array_get(arr, i, type) ((type*)(arr)->data)[i]
|
||||||
{
|
|
||||||
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--];
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // MISC_TOOLS_H
|
#endif // MISC_TOOLS_H
|
||||||
|
|
Loading…
Reference in New Issue
Block a user