source: roaraudio/libroar/stack.c @ 3811:49db840fb4f4

Last change on this file since 3811:49db840fb4f4 was 3811:49db840fb4f4, checked in by phi, 14 years ago

fixed some copyright statements

File size: 3.6 KB
Line 
1//stack.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2010
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
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
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
38int roar_stack_new(struct roar_stack * stack) {
39 if ( stack == NULL )
40  return -1;
41
42 memset(stack, 0, sizeof(struct roar_stack));
43
44 stack->flags = ROAR_STACK_FLAG_FREE_DATA;
45 stack->free  = free;
46
47 return 0;
48}
49
50struct roar_stack * roar_stack_newalloc(void) {
51 struct roar_stack * stack = roar_mm_malloc(sizeof(struct roar_stack));
52
53 if ( stack == NULL )
54  return NULL;
55
56 if ( roar_stack_new(stack) == -1 ) {
57  free(stack);
58  return NULL;
59 }
60
61 if ( roar_stack_set_flag(stack, ROAR_STACK_FLAG_FREE_SELF, 0) == -1 ) {
62  free(stack);
63  return NULL;
64 }
65
66 return stack;
67}
68
69int roar_stack_free(struct roar_stack * stack) {
70 int i;
71
72 if ( stack == NULL )
73  return -1;
74
75 if ( (stack->flags & ROAR_STACK_FLAG_FREE_DATA) && stack->free != NULL ) {
76  for (i = 0; i < stack->next; i++)
77   stack->free(stack->slot[i]);
78 }
79
80 if ( stack->flags & ROAR_STACK_FLAG_FREE_SELF ) {
81  free(stack);
82 } else {
83  memset(stack, 0, sizeof(struct roar_stack)); // just to be sure...
84 }
85
86 return 0;
87}
88
89int roar_stack_set_free(struct roar_stack * stack, void (*func)(void*)) {
90 if ( stack == NULL )
91  return -1;
92
93 stack->free = func;
94
95 return 0;
96}
97
98int roar_stack_set_flag(struct roar_stack * stack, int flag, int reset) {
99 if ( stack == NULL )
100  return -1;
101
102 stack->flags  |= flag;
103
104 if ( reset )
105  stack->flags -= flag;
106
107 return 0;
108}
109
110int roar_stack_push    (struct roar_stack * stack, void *  ptr) {
111 if ( stack == NULL )
112  return -1;
113
114 if ( stack->next == ROAR_STACK_SIZE ) /* stack is full */
115  return -1;
116
117 stack->slot[stack->next++] = ptr;
118
119 return 0;
120}
121
122int roar_stack_pop     (struct roar_stack * stack, void ** ptr) {
123 if ( stack == NULL ) /* it is valid to have ptr = NULL, */
124  return -1;          /* to just pop a value without want to take it */
125
126 if ( ptr != NULL ) /* just to be sure */
127  *ptr = NULL;
128
129 if ( stack->next == 0 ) /* nothing in stack */
130  return -1;
131
132 stack->next--;
133
134 if ( ptr != NULL )
135  *ptr = stack->slot[stack->next];
136
137 return 0;
138}
139
140int roar_stack_get_cur (struct roar_stack * stack, void ** ptr) {
141 if ( stack == NULL || ptr == NULL )
142  return -1;
143
144 *ptr = NULL; /* just to be sure */
145
146 if ( stack->next == 0 ) /* nothing in the stack */
147  return -1;
148
149 *ptr = stack->slot[stack->next - 1];
150
151 return 0;
152}
153
154//ll
Note: See TracBrowser for help on using the repository browser.