source: roaraudio/libroar/stack.c @ 3516:2becb65c23b6

Last change on this file since 3516:2becb65c23b6 was 3063:955233719a84, checked in by phi, 14 years ago

use memory functions from libroar, not libc, fixed a small memory leak

File size: 3.6 KB
Line 
1//stack.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009
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, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *  NOTE for everyone want's to change something and send patches:
24 *  read README and HACKING! There a addition information on
25 *  the license of this document you need to read before you send
26 *  any patches.
27 *
28 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
29 *  or libpulse*:
30 *  The libs libroaresd, libroararts and libroarpulse link this lib
31 *  and are therefore GPL. Because of this it may be illigal to use
32 *  them with any software that uses libesd, libartsc or libpulse*.
33 */
34
35#include "libroar.h"
36
37int roar_stack_new(struct roar_stack * stack) {
38 if ( stack == NULL )
39  return -1;
40
41 memset(stack, 0, sizeof(struct roar_stack));
42
43 stack->flags = ROAR_STACK_FLAG_FREE_DATA;
44 stack->free  = free;
45
46 return 0;
47}
48
49struct roar_stack * roar_stack_newalloc(void) {
50 struct roar_stack * stack = roar_mm_malloc(sizeof(struct roar_stack));
51
52 if ( stack == NULL )
53  return NULL;
54
55 if ( roar_stack_new(stack) == -1 ) {
56  free(stack);
57  return NULL;
58 }
59
60 if ( roar_stack_set_flag(stack, ROAR_STACK_FLAG_FREE_SELF, 0) == -1 ) {
61  free(stack);
62  return NULL;
63 }
64
65 return stack;
66}
67
68int roar_stack_free(struct roar_stack * stack) {
69 int i;
70
71 if ( stack == NULL )
72  return -1;
73
74 if ( (stack->flags & ROAR_STACK_FLAG_FREE_DATA) && stack->free != NULL ) {
75  for (i = 0; i < stack->next; i++)
76   stack->free(stack->slot[i]);
77 }
78
79 if ( stack->flags & ROAR_STACK_FLAG_FREE_SELF ) {
80  free(stack);
81 } else {
82  memset(stack, 0, sizeof(struct roar_stack)); // just to be sure...
83 }
84
85 return 0;
86}
87
88int roar_stack_set_free(struct roar_stack * stack, void (*func)(void*)) {
89 if ( stack == NULL )
90  return -1;
91
92 stack->free = func;
93
94 return 0;
95}
96
97int roar_stack_set_flag(struct roar_stack * stack, int flag, int reset) {
98 if ( stack == NULL )
99  return -1;
100
101 stack->flags  |= flag;
102
103 if ( reset )
104  stack->flags -= flag;
105
106 return 0;
107}
108
109int roar_stack_push    (struct roar_stack * stack, void *  ptr) {
110 if ( stack == NULL )
111  return -1;
112
113 if ( stack->next == ROAR_STACK_SIZE ) /* stack is full */
114  return -1;
115
116 stack->slot[stack->next++] = ptr;
117
118 return 0;
119}
120
121int roar_stack_pop     (struct roar_stack * stack, void ** ptr) {
122 if ( stack == NULL ) /* it is valid to have ptr = NULL, */
123  return -1;          /* to just pop a value without want to take it */
124
125 if ( ptr != NULL ) /* just to be sure */
126  *ptr = NULL;
127
128 if ( stack->next == 0 ) /* nothing in stack */
129  return -1;
130
131 stack->next--;
132
133 if ( ptr != NULL )
134  *ptr = stack->slot[stack->next];
135
136 return 0;
137}
138
139int roar_stack_get_cur (struct roar_stack * stack, void ** ptr) {
140 if ( stack == NULL || ptr == NULL )
141  return -1;
142
143 *ptr = NULL; /* just to be sure */
144
145 if ( stack->next == 0 ) /* nothing in the stack */
146  return -1;
147
148 *ptr = stack->slot[stack->next - 1];
149
150 return 0;
151}
152
153//ll
Note: See TracBrowser for help on using the repository browser.