Eina_List and memory allocation
If you don't know how to create lists see Adding elements to Eina_List. In this example we also use Stringshare, however it should be possible to understand the code regardless of previous knowledge about it.
Here we have the usual list creation code with a twist, now we are using as data for the list memory that has to be freed later on.
#include <stdio.h>
#include <Eina.h>
int
main(int argc, char **argv)
{
Eina_List *list = NULL;
Eina_List *l;
void *list_data;
eina_init();
Eina Utility library.
struct _Eina_List Eina_List
Type for a generic double linked list.
Definition eina_list.h:307
Eina_List * eina_list_append(Eina_List *list, const void *data)
Append the given data to the given linked list.
Definition eina_list.c:530
This time we are going to iterate over our list in a different way:
for(l = list; l; l = eina_list_next(l))
static Eina_List * eina_list_next(const Eina_List *list)
Get the next list node after the specified list node.
And now we are going to iterate over the list backwards:
static Eina_List * eina_list_prev(const Eina_List *list)
Get the previous list node before the specified list node.
static void * eina_list_data_get(const Eina_List *list)
Get the list node data member.
static Eina_List * eina_list_last(const Eina_List *list)
Get the last list node in the list.
And now we need to free up the memory allocated during creation of the list:
EINA_LIST_FREE(list, list_data)
eina_stringshare_del(list_data);
#define EINA_LIST_FREE(list, data)
Macro to remove each list node while having access to each node's data.
Definition eina_list.h:1609
- Note
- We don't need to use eina_list_free() since EINA_LIST_FREE takes care of that.
And shut everything down:
return 0;
}
The full source code can be found on the examples folder on the eina_list_04.c file.