[1280] | 1 | //stack.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[5381] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2012 |
---|
[1280] | 5 | * |
---|
| 6 | * This file is part of libroar a part of RoarAudio, |
---|
| 7 | * a cross-platform sound system for both, home and professional use. |
---|
| 8 | * See README for details. |
---|
| 9 | * |
---|
| 10 | * This file is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License version 3 |
---|
| 12 | * as published by the Free Software Foundation. |
---|
| 13 | * |
---|
| 14 | * libroar is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this software; see the file COPYING. If not, write to |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[1280] | 23 | * |
---|
| 24 | * NOTE for everyone want's to change something and send patches: |
---|
| 25 | * read README and HACKING! There a addition information on |
---|
| 26 | * the license of this document you need to read before you send |
---|
| 27 | * any patches. |
---|
| 28 | * |
---|
| 29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 30 | * or libpulse*: |
---|
| 31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 34 | */ |
---|
| 35 | |
---|
| 36 | #include "libroar.h" |
---|
| 37 | |
---|
[5272] | 38 | static void __free(void * v) { |
---|
| 39 | roar_mm_free(v); |
---|
| 40 | } |
---|
| 41 | |
---|
[1282] | 42 | int roar_stack_new(struct roar_stack * stack) { |
---|
| 43 | if ( stack == NULL ) |
---|
| 44 | return -1; |
---|
| 45 | |
---|
| 46 | memset(stack, 0, sizeof(struct roar_stack)); |
---|
| 47 | |
---|
| 48 | stack->flags = ROAR_STACK_FLAG_FREE_DATA; |
---|
[5272] | 49 | stack->free = __free; |
---|
[1282] | 50 | |
---|
| 51 | return 0; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | struct roar_stack * roar_stack_newalloc(void) { |
---|
[3063] | 55 | struct roar_stack * stack = roar_mm_malloc(sizeof(struct roar_stack)); |
---|
[1282] | 56 | |
---|
| 57 | if ( stack == NULL ) |
---|
| 58 | return NULL; |
---|
| 59 | |
---|
| 60 | if ( roar_stack_new(stack) == -1 ) { |
---|
[4904] | 61 | roar_mm_free(stack); |
---|
[1282] | 62 | return NULL; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | if ( roar_stack_set_flag(stack, ROAR_STACK_FLAG_FREE_SELF, 0) == -1 ) { |
---|
[4904] | 66 | roar_mm_free(stack); |
---|
[1282] | 67 | return NULL; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | return stack; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | int roar_stack_free(struct roar_stack * stack) { |
---|
| 74 | int i; |
---|
| 75 | |
---|
| 76 | if ( stack == NULL ) |
---|
| 77 | return -1; |
---|
| 78 | |
---|
| 79 | if ( (stack->flags & ROAR_STACK_FLAG_FREE_DATA) && stack->free != NULL ) { |
---|
| 80 | for (i = 0; i < stack->next; i++) |
---|
| 81 | stack->free(stack->slot[i]); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | if ( stack->flags & ROAR_STACK_FLAG_FREE_SELF ) { |
---|
[4904] | 85 | roar_mm_free(stack); |
---|
[1282] | 86 | } else { |
---|
| 87 | memset(stack, 0, sizeof(struct roar_stack)); // just to be sure... |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | return 0; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | int roar_stack_set_free(struct roar_stack * stack, void (*func)(void*)) { |
---|
| 94 | if ( stack == NULL ) |
---|
| 95 | return -1; |
---|
| 96 | |
---|
| 97 | stack->free = func; |
---|
| 98 | |
---|
| 99 | return 0; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | int roar_stack_set_flag(struct roar_stack * stack, int flag, int reset) { |
---|
| 103 | if ( stack == NULL ) |
---|
| 104 | return -1; |
---|
| 105 | |
---|
| 106 | stack->flags |= flag; |
---|
| 107 | |
---|
| 108 | if ( reset ) |
---|
| 109 | stack->flags -= flag; |
---|
| 110 | |
---|
| 111 | return 0; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | int roar_stack_push (struct roar_stack * stack, void * ptr) { |
---|
| 115 | if ( stack == NULL ) |
---|
| 116 | return -1; |
---|
| 117 | |
---|
| 118 | if ( stack->next == ROAR_STACK_SIZE ) /* stack is full */ |
---|
| 119 | return -1; |
---|
| 120 | |
---|
| 121 | stack->slot[stack->next++] = ptr; |
---|
| 122 | |
---|
| 123 | return 0; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | int roar_stack_pop (struct roar_stack * stack, void ** ptr) { |
---|
| 127 | if ( stack == NULL ) /* it is valid to have ptr = NULL, */ |
---|
| 128 | return -1; /* to just pop a value without want to take it */ |
---|
| 129 | |
---|
| 130 | if ( ptr != NULL ) /* just to be sure */ |
---|
| 131 | *ptr = NULL; |
---|
| 132 | |
---|
| 133 | if ( stack->next == 0 ) /* nothing in stack */ |
---|
| 134 | return -1; |
---|
| 135 | |
---|
| 136 | stack->next--; |
---|
| 137 | |
---|
| 138 | if ( ptr != NULL ) |
---|
| 139 | *ptr = stack->slot[stack->next]; |
---|
| 140 | |
---|
| 141 | return 0; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | int roar_stack_get_cur (struct roar_stack * stack, void ** ptr) { |
---|
| 145 | if ( stack == NULL || ptr == NULL ) |
---|
| 146 | return -1; |
---|
| 147 | |
---|
| 148 | *ptr = NULL; /* just to be sure */ |
---|
| 149 | |
---|
| 150 | if ( stack->next == 0 ) /* nothing in the stack */ |
---|
| 151 | return -1; |
---|
| 152 | |
---|
| 153 | *ptr = stack->slot[stack->next - 1]; |
---|
| 154 | |
---|
| 155 | return 0; |
---|
| 156 | } |
---|
| 157 | |
---|
[1280] | 158 | //ll |
---|