Fledasty/Src/Core/Queue.c

97 lines
3 KiB
C
Raw Normal View History

/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* -----------------------------------------------------------------------------
* File: Main.c
* Description:
* Executes all tests.
*
* Author: Mineplay
* -----------------------------------------------------------------------------
*/
#include "../../Include/Fledasty/Core/Queue.h"
#include <Hallocy/Core/Allocator.h>
#include <Hallocy/Utils/Error.h>
#include <string.h>
FledastyError fledasty_queue_initialize(FledastyQueue *new_queue) {
if (new_queue == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
new_queue->size = 0;
new_queue->start = NULL;
new_queue->end = NULL;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_queue_destroy(FledastyQueue *current_queue) {
if (current_queue == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
FledastyQueueNode *previousNode = NULL;
FledastyQueueNode *current_node = current_queue->start;
while (current_node != NULL) {
previousNode = current_node;
current_node = current_node->next;
HallocyError error = hallocy_free(previousNode->value);
if (error != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
error = hallocy_free(previousNode);
if (error != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
}
current_queue->size = 0;
current_queue->start = NULL;
current_queue->end = NULL;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value, const size_t size) {
if (current_queue == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
FledastyQueueNode *new_queue_node = (FledastyQueueNode*)hallocy_malloc(sizeof(FledastyQueueNode));
if (new_queue_node == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
new_queue_node->value = (FledastyQueueNode*)hallocy_malloc(size);
hallocy_copy_memory(new_queue_node->value, value, size);
new_queue_node->next = NULL;
if (current_queue->start == NULL) {
current_queue->start = new_queue_node;
current_queue->end = new_queue_node;
new_queue_node->previous = NULL;
} else {
new_queue_node->previous = current_queue->end;
current_queue->end->next = new_queue_node;
current_queue->end = new_queue_node;
}
current_queue->size += 1;
return FLEDASTY_ERROR_NONE;
}