Compare commits
No commits in common. "35e2f241c18d248cc98f450d5c570b3b98dea6d4" and "ea7f7453133cb3ce231db074986820cbba86c0a6" have entirely different histories.
35e2f241c1
...
ea7f745313
|
|
@ -1,7 +0,0 @@
|
|||
gcc
|
||||
# Headers
|
||||
-Iheaders
|
||||
|
||||
# Macros
|
||||
-DDEBUG=2
|
||||
-DASSET_FILE_NAME="$(make print-ASSET_OUTPUT)"
|
||||
|
|
@ -14,7 +14,7 @@ CORE_CC = gcc
|
|||
ifeq ($(RELEASE), 1)
|
||||
CORE_CFLAGS = --std=c17 --pedantic -O3 -DASSET_FILE_NAME=\"$(notdir $(ASSET_OUTPUT))\" -D_GNU_SOURCE=1 $(shell pkg-config --cflags sdl3) # RELEASE
|
||||
else
|
||||
CORE_CFLAGS = --std=c17 --pedantic -O1 -g -DDEBUG=2 -DASSET_FILE_NAME=\"$(notdir $(ASSET_OUTPUT))\" -D_GNU_SOURCE=1 $(shell pkg-config --cflags sdl3) \
|
||||
CORE_CFLAGS = --std=c17 --pedantic -O0 -g -DDEBUG=2 -DASSET_FILE_NAME=\"$(notdir $(ASSET_OUTPUT))\" -D_GNU_SOURCE=1 $(shell pkg-config --cflags sdl3) \
|
||||
-Wall -Wno-missing-braces -Wextra -Wno-missing-field-initializers \
|
||||
-Wformat=2 -Wswitch-default -Wswitch-enum -Wcast-align \
|
||||
-Wpointer-arith -Wbad-function-cast -Wstrict-overflow=5 \
|
||||
|
|
|
|||
|
|
@ -63,12 +63,12 @@ int asset_manager_init(asset_manager_t *asset_manager)
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if(linked_list_reserve(&asset_manager->assets_header_defs, nb_assets + 1)) return_failure_int;
|
||||
if(linked_list_reserve(&asset_manager->assets_header_defs, nb_assets + 1)) return EXIT_FAILURE;
|
||||
|
||||
for(size_t i = 0; i < nb_assets; i++)
|
||||
{
|
||||
elem_t *asset_header_def_elem = create_elem(&asset_manager->assets_header_defs);
|
||||
if(!asset_header_def_elem) return_failure_int;
|
||||
if(!asset_header_def_elem) return EXIT_FAILURE;
|
||||
asset_header_def_t *asset_header_def = asset_header_def_elem->data;
|
||||
|
||||
size_t cap = 0;
|
||||
|
|
@ -87,7 +87,7 @@ int asset_manager_init(asset_manager_t *asset_manager)
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
linked_list_push_back(&asset_manager->assets_header_defs, asset_header_def_elem);
|
||||
if(linked_list_push_back(&asset_manager->assets_header_defs, asset_header_def_elem)) return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
debug_printf("Initialized asset manager.");
|
||||
|
|
@ -98,8 +98,8 @@ int asset_manager_exit(asset_manager_t *asset_manager)
|
|||
{
|
||||
assert("Asset manager cannot be NULL" && asset_manager);
|
||||
|
||||
if(linked_list_exit(&asset_manager->assets_header_defs)) return_failure_int;
|
||||
if(linked_list_exit(&asset_manager->assets)) return_failure_int;
|
||||
if(linked_list_exit(&asset_manager->assets_header_defs)) return EXIT_FAILURE;
|
||||
if(linked_list_exit(&asset_manager->assets)) return EXIT_FAILURE;
|
||||
|
||||
if(fclose(asset_manager->asset_file))
|
||||
{
|
||||
|
|
@ -122,7 +122,7 @@ inline int asset_manager_collect_garbage(asset_manager_t *asset_manager)
|
|||
{
|
||||
assert("Asset manager cannot be NULL" && asset_manager);
|
||||
|
||||
if(linked_list_remove_if(&asset_manager->assets, condition_is_asset_unused)) return_failure_int;
|
||||
if(linked_list_remove_if(&asset_manager->assets, condition_is_asset_unused)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
@ -230,7 +230,7 @@ int asset_manager_load_asset(asset_manager_t *asset_manager, const char *asset_n
|
|||
asset_header_def_t *asset_header_def = asset_header_def_elem->data;
|
||||
|
||||
asset_elem = create_elem(&asset_manager->assets, asset_type);
|
||||
if(!asset_elem) return_failure_int;
|
||||
if(!asset_elem) return EXIT_FAILURE;
|
||||
asset_t *asset = asset_elem->data;
|
||||
|
||||
asset->name = calloc(strlen(asset_name) + 1, sizeof(char));
|
||||
|
|
@ -246,7 +246,7 @@ int asset_manager_load_asset(asset_manager_t *asset_manager, const char *asset_n
|
|||
switch(asset_type)
|
||||
{
|
||||
case ASSET_TEXTURE:
|
||||
if(load_texture(asset, asset_header_def, asset_manager->asset_file)) return_failure_int;
|
||||
if(load_texture(asset, asset_header_def, asset_manager->asset_file)) return EXIT_FAILURE;
|
||||
break;
|
||||
|
||||
default:
|
||||
|
|
|
|||
34
src/ecs.c
34
src/ecs.c
|
|
@ -35,7 +35,7 @@ int create_component(component_t *component, va_list args)
|
|||
|
||||
inline int destroy_component(component_t *component)
|
||||
{
|
||||
if(component->deleter(component)) return_failure_int;
|
||||
if(component->deleter(component)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
@ -52,7 +52,7 @@ int create_entity(entity_t *entity, va_list args)
|
|||
|
||||
inline int destroy_entity(entity_t *entity)
|
||||
{
|
||||
if(linked_list_exit(&entity->components)) return_failure_int;
|
||||
if(linked_list_exit(&entity->components)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
@ -60,15 +60,15 @@ inline int destroy_entity(entity_t *entity)
|
|||
component_t *entity_new_component(entity_t *entity, component_type_t component_type)
|
||||
{
|
||||
elem_t *component_elem = create_elem(&entity->components, component_type);
|
||||
if(!component_elem) return_failure_ptr;
|
||||
if(!component_elem) return NULL;
|
||||
|
||||
component_t *component = component_elem->data;
|
||||
|
||||
component->entity = entity;
|
||||
|
||||
if(component->init(component)) return_failure_ptr;
|
||||
if(component->init(component)) return NULL;
|
||||
|
||||
linked_list_push_front(&entity->components, component_elem);
|
||||
if(linked_list_push_front(&entity->components, component_elem)) return NULL;
|
||||
|
||||
return component;
|
||||
}
|
||||
|
|
@ -96,32 +96,33 @@ static inline int action_update_component(elem_t *elem, va_list args __attribute
|
|||
{
|
||||
component_t *component = elem->data;
|
||||
|
||||
if(component->update(component)) return_failure_int;
|
||||
if(component->update(component)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
inline int update_entity(entity_t *entity)
|
||||
{
|
||||
if(linked_list_for_each(&entity->components, action_update_component)) return_failure_int;
|
||||
if(linked_list_for_each(&entity->components, action_update_component)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// Only for this file
|
||||
// No args
|
||||
static inline int action_draw_component(elem_t *elem, va_list args __attribute__((unused)))
|
||||
{
|
||||
component_t *component = elem->data;
|
||||
|
||||
if(component->draw)
|
||||
if(component->draw(component)) return_failure_int;
|
||||
if(component->draw(component)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
inline int draw_entity(entity_t *entity)
|
||||
{
|
||||
if(linked_list_for_each(&entity->components, action_draw_component)) return_failure_int;
|
||||
if(linked_list_for_each(&entity->components, action_draw_component)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
@ -140,15 +141,16 @@ inline entity_t *entity_manager_new_entity(entity_manager_t *entity_manager, siz
|
|||
assert("Entity manager cannot be NULL" && entity_manager);
|
||||
|
||||
elem_t *entity_elem = create_elem(&entity_manager->entities, id);
|
||||
if(!entity_elem) return_failure_ptr;
|
||||
if(!entity_elem) return NULL;
|
||||
|
||||
linked_list_push_back(&entity_manager->entities, entity_elem);
|
||||
if(linked_list_push_back(&entity_manager->entities, entity_elem)) return NULL;
|
||||
|
||||
debug_printf("Added entity %lu to entity manager.", id);
|
||||
|
||||
return entity_elem->data;
|
||||
}
|
||||
|
||||
// Only for this file
|
||||
// No args
|
||||
static inline int action_update_entity(elem_t *elem, va_list args __attribute__((unused)))
|
||||
{
|
||||
|
|
@ -159,7 +161,7 @@ inline int entity_manager_update(entity_manager_t *entity_manager)
|
|||
{
|
||||
assert("Entity manager cannot be NULL" && entity_manager);
|
||||
|
||||
if(linked_list_for_each(&entity_manager->entities, action_update_entity)) return_failure_int;
|
||||
if(linked_list_for_each(&entity_manager->entities, action_update_entity)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
@ -172,7 +174,7 @@ static inline int action_draw_entity(elem_t *elem, va_list args __attribute__((u
|
|||
}
|
||||
|
||||
// No args
|
||||
static inline bool condition_is_draw_priority_higher(elem_t *elem, va_list args __attribute__((unused)))
|
||||
static inline bool condition_is_draw_priority_higher(elem_t *elem, va_list args)
|
||||
{
|
||||
return ((entity_t *)elem->data)->draw_priority > ((entity_t *)elem->next->data)->draw_priority;
|
||||
}
|
||||
|
|
@ -183,7 +185,7 @@ inline int entity_manager_draw(entity_manager_t *entity_manager)
|
|||
|
||||
linked_list_insertion_sort(&entity_manager->entities, condition_is_draw_priority_higher);
|
||||
|
||||
if(linked_list_for_each(&entity_manager->entities, action_draw_entity)) return_failure_int;
|
||||
if(linked_list_for_each(&entity_manager->entities, action_draw_entity)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
@ -199,7 +201,7 @@ inline int entity_manager_remove_entity(entity_manager_t *entity_manager, unsign
|
|||
{
|
||||
assert("Entity manager cannot be NULL" && entity_manager);
|
||||
|
||||
if(linked_list_remove_if(&entity_manager->entities, condition_is_entity_name, id)) return_failure_int;
|
||||
if(linked_list_remove_if(&entity_manager->entities, condition_is_entity_name, id)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
@ -208,7 +210,7 @@ inline int entity_manager_exit(entity_manager_t *entity_manager)
|
|||
{
|
||||
assert("Entity manager cannot be NULL" && entity_manager);
|
||||
|
||||
if(linked_list_exit(&entity_manager->entities)) return_failure_int;
|
||||
if(linked_list_exit(&entity_manager->entities)) return EXIT_FAILURE;
|
||||
|
||||
debug_printf("Exited entity manager.");
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
#include "errors.h"
|
||||
|
||||
// Arg : callback_t callback
|
||||
inline int create_subscription(subscription_t *subscription, va_list args)
|
||||
int create_subscription(subscription_t *subscription, va_list args)
|
||||
{
|
||||
subscription->callback = va_arg(args, callback_t);
|
||||
subscription->data = va_arg(args, void *);
|
||||
|
|
@ -15,7 +15,7 @@ inline int create_subscription(subscription_t *subscription, va_list args)
|
|||
}
|
||||
|
||||
// Arg : size_t id
|
||||
inline int create_topic(topic_t *topic, va_list args)
|
||||
int create_topic(topic_t *topic, va_list args)
|
||||
{
|
||||
topic->id = va_arg(args, size_t);
|
||||
|
||||
|
|
@ -24,23 +24,23 @@ inline int create_topic(topic_t *topic, va_list args)
|
|||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
inline int destroy_topic(topic_t *topic)
|
||||
int destroy_topic(topic_t *topic)
|
||||
{
|
||||
if(linked_list_exit(&topic->subscriptions)) return_failure_int;
|
||||
if(linked_list_exit(&topic->subscriptions)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
inline void event_bus_init(event_bus_t *event_bus)
|
||||
void event_bus_init(event_bus_t *event_bus)
|
||||
{
|
||||
linked_list_init(&event_bus->topics, sizeof(topic_t), (allocator_t)create_topic, (deleter_t)destroy_topic);
|
||||
|
||||
debug_printf("Initialized event bus.");
|
||||
}
|
||||
|
||||
inline int event_bus_exit(event_bus_t *event_bus)
|
||||
int event_bus_exit(event_bus_t *event_bus)
|
||||
{
|
||||
if(linked_list_exit(&event_bus->topics)) return_failure_int;
|
||||
if(linked_list_exit(&event_bus->topics)) return EXIT_FAILURE;
|
||||
|
||||
debug_printf("Exited event bus.");
|
||||
return EXIT_SUCCESS;
|
||||
|
|
@ -49,9 +49,9 @@ inline int event_bus_exit(event_bus_t *event_bus)
|
|||
int event_bus_new_topic(event_bus_t *event_bus, size_t id)
|
||||
{
|
||||
elem_t *topic_elem = create_elem(&event_bus->topics, id);
|
||||
if(!topic_elem) return_failure_int;
|
||||
if(!topic_elem) return EXIT_FAILURE;
|
||||
|
||||
linked_list_push_back(&event_bus->topics, topic_elem);
|
||||
if(linked_list_push_back(&event_bus->topics, topic_elem)) return EXIT_FAILURE;
|
||||
|
||||
debug_printf("Created new topic with id : %lu", id);
|
||||
|
||||
|
|
@ -64,9 +64,9 @@ static inline bool condition_is_topic_id(elem_t *elem, va_list args)
|
|||
return ((topic_t *)elem->data)->id == va_arg(args, size_t);
|
||||
}
|
||||
|
||||
inline int event_bus_remove_topic(event_bus_t *event_bus, size_t id)
|
||||
int event_bus_remove_topic(event_bus_t *event_bus, size_t id)
|
||||
{
|
||||
if(linked_list_remove_if(&event_bus->topics, condition_is_topic_id, id)) return_failure_int;
|
||||
if(linked_list_remove_if(&event_bus->topics, condition_is_topic_id, id)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
@ -74,7 +74,11 @@ inline int event_bus_remove_topic(event_bus_t *event_bus, size_t id)
|
|||
int event_bus_change_topic_id(event_bus_t *event_bus, size_t old_id, size_t new_id)
|
||||
{
|
||||
elem_t *topic_elem = linked_list_get_if(&event_bus->topics, condition_is_topic_id, old_id);
|
||||
if(!topic_elem) return_failure_int;
|
||||
if(!topic_elem)
|
||||
{
|
||||
error_printf("No topics with id = %lu found.", old_id);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
topic_t *topic = topic_elem->data;
|
||||
topic->id = new_id;
|
||||
|
|
@ -82,15 +86,15 @@ int event_bus_change_topic_id(event_bus_t *event_bus, size_t old_id, size_t new_
|
|||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// Args : va_list args
|
||||
static int action_execute_callback(elem_t *elem, va_list args)
|
||||
// Args : va_list_args
|
||||
static inline int action_execute_callback(elem_t *elem, va_list args)
|
||||
{
|
||||
subscription_t *subscription = elem->data;
|
||||
|
||||
va_list args_copy;
|
||||
va_copy(args_copy, args);
|
||||
|
||||
if(subscription->callback(args_copy, subscription->data)) return_failure_int;
|
||||
if(subscription->callback(args_copy, subscription->data)) return EXIT_FAILURE;
|
||||
|
||||
va_end(args_copy);
|
||||
|
||||
|
|
@ -100,7 +104,11 @@ static int action_execute_callback(elem_t *elem, va_list args)
|
|||
int event_bus_publish(event_bus_t *event_bus, size_t id, ...)
|
||||
{
|
||||
elem_t *topic_elem = linked_list_get_if(&event_bus->topics, condition_is_topic_id, id);
|
||||
if(!topic_elem) return_failure_int;
|
||||
if(!topic_elem)
|
||||
{
|
||||
error_printf("No topics with id = %lu found.", id);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
topic_t *topic = topic_elem->data;
|
||||
|
||||
|
|
@ -108,7 +116,7 @@ int event_bus_publish(event_bus_t *event_bus, size_t id, ...)
|
|||
va_start(args, id);
|
||||
va_copy(args_copy, args);
|
||||
|
||||
if(linked_list_for_eachv(&topic->subscriptions, action_execute_callback, args_copy)) return_failure_int;
|
||||
if(linked_list_for_eachv(&topic->subscriptions, action_execute_callback, args_copy)) return EXIT_FAILURE;
|
||||
|
||||
va_end(args_copy);
|
||||
va_end(args);
|
||||
|
|
@ -121,14 +129,18 @@ int event_bus_publish(event_bus_t *event_bus, size_t id, ...)
|
|||
int event_bus_subscribe(event_bus_t *event_bus, size_t id, callback_t callback, void *data)
|
||||
{
|
||||
elem_t *topic_elem = linked_list_get_if(&event_bus->topics, condition_is_topic_id, id);
|
||||
if(!topic_elem) return_failure_int;
|
||||
if(!topic_elem)
|
||||
{
|
||||
error_printf("No topics with id = %lu found.", id);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
topic_t *topic = topic_elem->data;
|
||||
|
||||
elem_t *subscription_elem = create_elem(&topic->subscriptions, callback, data);
|
||||
if(!subscription_elem) return_failure_int;
|
||||
if(!subscription_elem) return EXIT_FAILURE;
|
||||
|
||||
linked_list_push_back(&topic->subscriptions, subscription_elem);
|
||||
if(linked_list_push_back(&topic->subscriptions, subscription_elem)) return EXIT_FAILURE;
|
||||
|
||||
debug_printf("Subscribed to topic with id : %lu", id);
|
||||
|
||||
|
|
|
|||
|
|
@ -32,16 +32,4 @@
|
|||
fputc('\n', stderr); \
|
||||
} while (0)
|
||||
|
||||
#define return_failure_int \
|
||||
do { \
|
||||
fprintf(stderr, " in %s:%d:%s()\n", __FILE__, __LINE__, __func__); \
|
||||
return EXIT_FAILURE; \
|
||||
} while (0);
|
||||
|
||||
#define return_failure_ptr \
|
||||
do { \
|
||||
fprintf(stderr, " in %s:%d:%s()\n", __FILE__, __LINE__, __func__); \
|
||||
return NULL; \
|
||||
} while (0);
|
||||
|
||||
#endif // ERRORS_H
|
||||
|
|
@ -172,16 +172,20 @@ int linked_list_clear(linked_list_t *linked_list);
|
|||
*
|
||||
* @param linked_list Pointer to a linked list
|
||||
* @param elem Pointer to an element
|
||||
*
|
||||
* @return EXIT_SUCCESS, EXIT_FAILURE on error.
|
||||
*/
|
||||
void linked_list_push_back(linked_list_t *linked_list, elem_t *elem);
|
||||
int linked_list_push_back(linked_list_t *linked_list, elem_t *elem);
|
||||
|
||||
/**
|
||||
* @brief Insert an element from the front in a linked list, do nothing on error.
|
||||
*
|
||||
* @param linked_list Pointer to a linked list
|
||||
* @param elem Pointer to an element
|
||||
*
|
||||
* @return EXIT_SUCCESS, EXIT_FAILURE on error.
|
||||
*/
|
||||
void linked_list_push_front(linked_list_t *linked_list, elem_t *elem);
|
||||
int linked_list_push_front(linked_list_t *linked_list, elem_t *elem);
|
||||
|
||||
/**
|
||||
* @brief Remove and return the last element in the given linked list, do nothing on error.
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ elem_t *create_elem(linked_list_t *linked_list, ...)
|
|||
va_start(args, linked_list);
|
||||
va_copy(args_copy, args);
|
||||
|
||||
if(linked_list->allocator(elem->data, args_copy)) return_failure_ptr;
|
||||
if(linked_list->allocator(elem->data, args_copy)) return NULL;
|
||||
|
||||
va_end(args_copy);
|
||||
va_end(args);
|
||||
|
|
@ -51,8 +51,14 @@ inline int destroy_elem(linked_list_t *linked_list, elem_t *elem)
|
|||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
if(!elem)
|
||||
{
|
||||
error_printf("Elem cannot be NULL.");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if(linked_list->deleter)
|
||||
if(linked_list->deleter(elem->data)) return_failure_int;
|
||||
if(linked_list->deleter(elem->data)) return EXIT_FAILURE;
|
||||
|
||||
if(linked_list->size < linked_list->target_max_size)
|
||||
{
|
||||
|
|
@ -68,7 +74,7 @@ inline int destroy_elem(linked_list_t *linked_list, elem_t *elem)
|
|||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void linked_list_init(linked_list_t *linked_list, size_t data_size, const allocator_t allocator, const deleter_t elem_deleter)
|
||||
void linked_list_init(linked_list_t *linked_list, const size_t data_size, const allocator_t allocator, const deleter_t elem_deleter)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
|
|
@ -88,8 +94,6 @@ int linked_list_exit(linked_list_t *linked_list)
|
|||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
linked_list->target_max_size = 0;
|
||||
|
||||
elem_t *current_elem = linked_list->first;
|
||||
elem_t *tmp;
|
||||
|
||||
|
|
@ -99,7 +103,11 @@ int linked_list_exit(linked_list_t *linked_list)
|
|||
|
||||
current_elem = current_elem->next;
|
||||
|
||||
if(destroy_elem(linked_list, tmp)) return_failure_int;
|
||||
if(linked_list->deleter)
|
||||
if(linked_list->deleter(tmp->data)) return EXIT_FAILURE;
|
||||
|
||||
free(tmp->data);
|
||||
free(tmp);
|
||||
}
|
||||
|
||||
current_elem = linked_list->reserved;
|
||||
|
|
@ -130,7 +138,7 @@ int linked_list_clear(linked_list_t *linked_list)
|
|||
|
||||
current_elem = current_elem->next;
|
||||
|
||||
if(destroy_elem(linked_list, tmp)) return_failure_int;
|
||||
if(destroy_elem(linked_list, tmp)) return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
linked_list->first = NULL;
|
||||
|
|
@ -159,8 +167,8 @@ int linked_list_reserve(linked_list_t *linked_list, size_t target_max_size)
|
|||
elem->data = malloc(linked_list->data_size);
|
||||
if(!elem->data)
|
||||
{
|
||||
free(elem);
|
||||
error_printf("Failed to allocate memory : %d", errno);
|
||||
free(elem);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
|
@ -171,10 +179,16 @@ int linked_list_reserve(linked_list_t *linked_list, size_t target_max_size)
|
|||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void linked_list_push_back(linked_list_t *linked_list, elem_t *elem)
|
||||
int linked_list_push_back(linked_list_t *linked_list, elem_t *elem)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
if(!elem)
|
||||
{
|
||||
error_printf("Elem cannot be NULL.");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
elem->prev = linked_list->last;
|
||||
elem->next = NULL;
|
||||
|
||||
|
|
@ -186,12 +200,20 @@ void linked_list_push_back(linked_list_t *linked_list, elem_t *elem)
|
|||
linked_list->last = elem;
|
||||
|
||||
linked_list->size++;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void linked_list_push_front(linked_list_t *linked_list, elem_t *elem)
|
||||
int linked_list_push_front(linked_list_t *linked_list, elem_t *elem)
|
||||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
if(!elem)
|
||||
{
|
||||
error_printf("Elem cannot be NULL.");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
elem->next = linked_list->first;
|
||||
elem->prev = NULL;
|
||||
|
||||
|
|
@ -203,6 +225,8 @@ void linked_list_push_front(linked_list_t *linked_list, elem_t *elem)
|
|||
linked_list->first = elem;
|
||||
|
||||
linked_list->size++;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
elem_t *linked_list_pop_back(linked_list_t *linked_list)
|
||||
|
|
@ -263,16 +287,11 @@ elem_t *linked_list_get(const linked_list_t *linked_list, size_t index)
|
|||
|
||||
if(index == 0)
|
||||
return linked_list->first;
|
||||
|
||||
if(index == linked_list->size - 1)
|
||||
return linked_list->last;
|
||||
|
||||
elem_t *current_elem = linked_list->first;
|
||||
if(!current_elem)
|
||||
{
|
||||
error_printf("Failed to get element : linked list is empty.");
|
||||
return NULL;
|
||||
}
|
||||
if(!current_elem) return NULL;
|
||||
|
||||
for (size_t i = 0; i < index; i++)
|
||||
{
|
||||
|
|
@ -286,19 +305,29 @@ int linked_list_insert(linked_list_t *linked_list, elem_t *elem, size_t index) /
|
|||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
if(!elem)
|
||||
{
|
||||
error_printf("Elem cannot be NULL.");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if(index > linked_list->size)
|
||||
{
|
||||
error_printf("Insert index out of range.");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if(index == 0)
|
||||
{
|
||||
linked_list_push_front(linked_list, elem);
|
||||
return EXIT_SUCCESS;
|
||||
return linked_list_push_front(linked_list, elem);
|
||||
}
|
||||
else if(index == linked_list->size)
|
||||
{
|
||||
linked_list_push_back(linked_list, elem);
|
||||
return EXIT_SUCCESS;
|
||||
return linked_list_push_back(linked_list, elem);
|
||||
}
|
||||
|
||||
elem_t *next_insert_elem = linked_list_get(linked_list, index);
|
||||
if(!next_insert_elem) return_failure_int;
|
||||
if(!next_insert_elem) return EXIT_FAILURE;
|
||||
elem_t *prev_insert_elem = next_insert_elem->prev;
|
||||
|
||||
elem->prev = prev_insert_elem;
|
||||
|
|
@ -316,13 +345,19 @@ int linked_list_remove(linked_list_t *linked_list, elem_t *elem)
|
|||
{
|
||||
assert("Linked list cannot be NULL" && linked_list);
|
||||
|
||||
if(!elem)
|
||||
{
|
||||
error_printf("Elem cannot be NULL.");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
elem_t *prev_elem = elem->prev;
|
||||
elem_t *next_elem = elem->next;
|
||||
|
||||
if(prev_elem) prev_elem->next = next_elem;
|
||||
if(next_elem) next_elem->prev = prev_elem;
|
||||
|
||||
if(destroy_elem(linked_list, elem)) return_failure_int;
|
||||
if(destroy_elem(linked_list, elem)) return EXIT_FAILURE;
|
||||
|
||||
linked_list->size--;
|
||||
|
||||
|
|
@ -390,7 +425,7 @@ int linked_list_remove_if(linked_list_t *linked_list, const condition_t conditio
|
|||
va_end(args_copy);
|
||||
va_end(args);
|
||||
|
||||
return_failure_int;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -424,7 +459,7 @@ int linked_list_for_each(const linked_list_t *linked_list, const action_t action
|
|||
va_end(args_copy);
|
||||
va_end(args);
|
||||
|
||||
return_failure_int;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
va_end(args_copy);
|
||||
|
|
@ -454,7 +489,7 @@ int linked_list_for_eachv(const linked_list_t *linked_list, const action_t actio
|
|||
va_end(args_copy);
|
||||
va_end(args);
|
||||
|
||||
return_failure_int;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
va_end(args_copy);
|
||||
|
|
|
|||
Loading…
Reference in New Issue