Blame src/mixer/bag.c

Packit 4a16fb
/*
Packit 4a16fb
 *  Bag of pointers
Packit 4a16fb
 *  Copyright (c) 2001 by Abramo Bagnara <abramo@alsa-project.org>
Packit 4a16fb
 *
Packit 4a16fb
 *
Packit 4a16fb
 *   This library is free software; you can redistribute it and/or modify
Packit 4a16fb
 *   it under the terms of the GNU Lesser General Public License as
Packit 4a16fb
 *   published by the Free Software Foundation; either version 2.1 of
Packit 4a16fb
 *   the License, or (at your option) any later version.
Packit 4a16fb
 *
Packit 4a16fb
 *   This program is distributed in the hope that it will be useful,
Packit 4a16fb
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4a16fb
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 4a16fb
 *   GNU Lesser General Public License for more details.
Packit 4a16fb
 *
Packit 4a16fb
 *   You should have received a copy of the GNU Lesser General Public
Packit 4a16fb
 *   License along with this library; if not, write to the Free Software
Packit 4a16fb
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 4a16fb
 *
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
#include "mixer_local.h"
Packit 4a16fb
Packit 4a16fb
int bag_new(bag_t **bag)
Packit 4a16fb
{
Packit 4a16fb
	bag_t *b = malloc(sizeof(*b));
Packit 4a16fb
	if (!b)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	INIT_LIST_HEAD(b);
Packit 4a16fb
	*bag = b;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void bag_free(bag_t *bag)
Packit 4a16fb
{
Packit 4a16fb
	assert(list_empty(bag));
Packit 4a16fb
	free(bag);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int bag_empty(bag_t *bag)
Packit 4a16fb
{
Packit 4a16fb
	return list_empty(bag);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int bag_add(bag_t *bag, void *ptr)
Packit 4a16fb
{
Packit 4a16fb
	bag1_t *b = malloc(sizeof(*b));
Packit 4a16fb
	if (!b)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	b->ptr = ptr;
Packit 4a16fb
	list_add_tail(&b->list, bag);
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int bag_del(bag_t *bag, void *ptr)
Packit 4a16fb
{
Packit 4a16fb
	struct list_head *pos;
Packit 4a16fb
	list_for_each(pos, bag) {
Packit 4a16fb
		bag1_t *b = list_entry(pos, bag1_t, list);
Packit 4a16fb
		if (b->ptr == ptr) {
Packit 4a16fb
			list_del(&b->list);
Packit 4a16fb
			free(b);
Packit 4a16fb
			return 0;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	return -ENOENT;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
void bag_del_all(bag_t *bag)
Packit 4a16fb
{
Packit 4a16fb
	while (!list_empty(bag))
Packit 4a16fb
		list_del(bag->next);
Packit 4a16fb
}