Blame src/common/oscap_queue.c

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