Blame src/common/oscap_queue.c

Packit 517ee8
/*
Packit 517ee8
 * Copyright 2018 Red Hat Inc., Durham, North Carolina.
Packit 517ee8
 * All Rights Reserved.
Packit 517ee8
 *
Packit 517ee8
 * This library is free software; you can redistribute it and/or
Packit 517ee8
 * modify it under the terms of the GNU Lesser General Public
Packit 517ee8
 * License as published by the Free Software Foundation; either
Packit 517ee8
 * version 2.1 of the License, or (at your option) any later version.
Packit 517ee8
 *
Packit 517ee8
 * This library is distributed in the hope that it will be useful,
Packit 517ee8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 517ee8
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 517ee8
 * Lesser General Public License for more details.
Packit 517ee8
 *
Packit 517ee8
 * You should have received a copy of the GNU Lesser General Public
Packit 517ee8
 * License along with this library; if not, write to the Free Software
Packit 517ee8
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Packit 517ee8
 *
Packit 517ee8
 * Authors:
Packit 517ee8
 *      Jan Černý <jcerny@redhat.com>
Packit 517ee8
 */
Packit 517ee8
Packit 517ee8
Packit 517ee8
#ifdef HAVE_CONFIG_H
Packit 517ee8
#include <config.h>
Packit 517ee8
#endif
Packit 517ee8
Packit 517ee8
#include <stdio.h>
Packit 517ee8
#include <stdlib.h>
Packit 517ee8
#include "util.h"
Packit 517ee8
#include "oscap_queue.h"
Packit 517ee8
Packit 517ee8
struct oscap_queue_item {
Packit 517ee8
	void *data;
Packit 517ee8
	struct oscap_queue_item *next;
Packit 517ee8
};
Packit 517ee8
Packit 517ee8
struct oscap_queue {
Packit 517ee8
	struct oscap_queue_item *begin;
Packit 517ee8
	struct oscap_queue_item *end;
Packit 517ee8
};
Packit 517ee8
Packit 517ee8
struct oscap_queue *oscap_queue_new()
Packit 517ee8
{
Packit 517ee8
	struct oscap_queue *queue = malloc(sizeof(struct oscap_queue));
Packit 517ee8
	queue->begin = NULL;
Packit 517ee8
	queue->end = NULL;
Packit 517ee8
	return queue;
Packit 517ee8
}
Packit 517ee8
Packit 517ee8
void oscap_queue_add(struct oscap_queue *queue, void *data)
Packit 517ee8
{
Packit 517ee8
	struct oscap_queue_item *temp = malloc(sizeof(struct oscap_queue_item));
Packit 517ee8
	temp->data = data;
Packit 517ee8
	temp->next = NULL;
Packit 517ee8
	if (queue->begin == NULL) {
Packit 517ee8
		queue->begin = temp;
Packit 517ee8
	} else {
Packit 517ee8
		queue->end->next = temp;
Packit 517ee8
	}
Packit 517ee8
	queue->end = temp;
Packit 517ee8
}
Packit 517ee8
Packit 517ee8
void *oscap_queue_remove(struct oscap_queue *queue)
Packit 517ee8
{
Packit 517ee8
	if (queue->begin == NULL) {
Packit 517ee8
		return NULL;
Packit 517ee8
	}
Packit 517ee8
	void *data = queue->begin->data;
Packit 517ee8
	struct oscap_queue_item *temp = queue->begin;
Packit 517ee8
	if (queue->begin == queue->end) {
Packit 517ee8
		queue->end = NULL;
Packit 517ee8
	}
Packit 517ee8
	queue->begin = queue->begin->next;
Packit 517ee8
	free(temp);
Packit 517ee8
	return data;
Packit 517ee8
}
Packit 517ee8
Packit 517ee8
void oscap_queue_free(struct oscap_queue *queue, oscap_destruct_func destructor)
Packit 517ee8
{
Packit 517ee8
	if (queue == NULL) {
Packit 517ee8
		return;
Packit 517ee8
	}
Packit 517ee8
	struct oscap_queue_item *current, *temp;
Packit 517ee8
	current = queue->begin;
Packit 517ee8
	while (current != NULL) {
Packit 517ee8
		temp = current;
Packit 517ee8
		current = current->next;
Packit 517ee8
		if (destructor != NULL) {
Packit 517ee8
			free(temp->data);
Packit 517ee8
		}
Packit 517ee8
		free(temp);
Packit 517ee8
	}
Packit 517ee8
	free(queue);
Packit 517ee8
}