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)
{
(void)argc;
(void)argv;
Eina_List *list = NULL;
Eina_List *l;
void *list_data;
eina_init();
list = eina_list_append(list, eina_stringshare_add("calvin"));
list = eina_list_append(list, eina_stringshare_add("Leoben"));
list = eina_list_append(list, eina_stringshare_add("D'Anna"));
list = eina_list_append(list, eina_stringshare_add("Simon"));
list = eina_list_append(list, eina_stringshare_add("Doral"));
list = eina_list_append(list, eina_stringshare_add("Six"));
list = eina_list_append(list, eina_stringshare_add("Sharon"));
Eina.h
Eina Utility library.
eina_list_append
EINA_API Eina_List * eina_list_append(Eina_List *list, const void *data)
Appends the given data to the given linked list.
Definition: eina_list.c:584
eina_init
EINA_API int eina_init(void)
Initializes the Eina library.
Definition: eina_main.c:291
eina_stringshare_add
EINA_API Eina_Stringshare * eina_stringshare_add(const char *str)
Retrieves an instance of a string for use in a program.
Definition: eina_stringshare.c:606
_Eina_List
Type for a generic double linked list.
Definition: eina_list.h:318

This time we are going to iterate over our list in a different way:

for(l = list; l; l = eina_list_next(l))
printf("%s\n", (char*)l->data);
eina_list_next
static Eina_List * eina_list_next(const Eina_List *list)
Gets the next list node after the specified list node.
_Eina_List::data
void * data
Pointer to list element payload.
Definition: eina_list.h:319

And now we are going to iterate over the list backwards:

for(l = eina_list_last(list); l; l = eina_list_prev(l))
printf("%s\n", (char*)eina_list_data_get(l));
eina_list_last
static Eina_List * eina_list_last(const Eina_List *list)
Gets the last list node in the list.
eina_list_prev
static Eina_List * eina_list_prev(const Eina_List *list)
Gets the list node prior to the specified list node.
eina_list_data_get
static void * eina_list_data_get(const Eina_List *list)
Gets the list node data member.

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);
EINA_LIST_FREE
#define EINA_LIST_FREE(list, data)
Definition for the macro to remove each list node while having access to each node's data.
Definition: eina_list.h:1629
eina_stringshare_del
EINA_API void eina_stringshare_del(Eina_Stringshare *str)
Notes that the given string has lost an instance.
Definition: eina_stringshare.c:533

Note
  We don't need to use eina_list_free() since EINA_LIST_FREE takes care of that.

And shut everything down:

eina_shutdown();
return 0;
}
eina_shutdown
EINA_API int eina_shutdown(void)
Shuts down the Eina library.
Definition: eina_main.c:379

The full source code can be found on the examples folder on the eina_list_04.c file.