1/* 2 * Bag of pointers 3 * Copyright (c) 2001 by Abramo Bagnara <abramo@alsa-project.org> 4 * 5 * 6 * This library is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU Lesser General Public License as 8 * published by the Free Software Foundation; either version 2.1 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * 20 */ 21 22#include "mixer_local.h" 23 24int bag_new(bag_t **bag) 25{ 26 bag_t *b = malloc(sizeof(*b)); 27 if (!b) 28 return -ENOMEM; 29 INIT_LIST_HEAD(b); 30 *bag = b; 31 return 0; 32} 33 34void bag_free(bag_t *bag) 35{ 36 assert(list_empty(bag)); 37 free(bag); 38} 39 40int bag_empty(bag_t *bag) 41{ 42 return list_empty(bag); 43} 44 45int bag_add(bag_t *bag, void *ptr) 46{ 47 bag1_t *b = malloc(sizeof(*b)); 48 if (!b) 49 return -ENOMEM; 50 b->ptr = ptr; 51 list_add_tail(&b->list, bag); 52 return 0; 53} 54 55int bag_del(bag_t *bag, void *ptr) 56{ 57 struct list_head *pos; 58 list_for_each(pos, bag) { 59 bag1_t *b = list_entry(pos, bag1_t, list); 60 if (b->ptr == ptr) { 61 list_del(&b->list); 62 free(b); 63 return 0; 64 } 65 } 66 return -ENOENT; 67} 68 69void bag_del_all(bag_t *bag) 70{ 71 while (!list_empty(bag)) 72 list_del(bag->next); 73} 74