Debian Codegraph @

rplayd/buffer.c

raw symbols SCIP
1 /* $Id: buffer.c,v 1.4 1999/03/10 07:58:02 boyns Exp $ */
2
3 /*
4 * Copyright (C) 1993-99 Mark R. Boyns <[email protected]>
5 *
6 * This file is part of rplay.
7 *
8 * rplay is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * rplay is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with rplay; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include "rplayd.h"
32 #include "buffer.h"
33
34 static BUFFER *buffers = NULL;
35 static int nbuffers = 0; /* number of buffers on the free list */
36 static int next_id = 0; /* next buffer id number */
37
38 #ifdef __STDC__
39 BUFFER *
40 buffer_create(void)
41 #else
42 BUFFER *
43 buffer_create()
44 #endif
45 {
46 BUFFER *b;
47
48 if (buffers == NULL)
49 {
50 b = (BUFFER *) malloc(sizeof(BUFFER));
51 if (b == NULL)
52 {
53 report(REPORT_ERROR, "buffer_create: out of memory\n");
54 done(1);
55 }
56 b->id = ++next_id;
57 }
58 else
59 {
60 nbuffers--;
61 b = buffers;
62 buffers = buffers->next;
63 }
64
65 b->nbytes = 0;
66 b->buf[0] = '\0';
67 b->status = BUFFER_FREE;
68 b->offset = 0;
69 b->next = NULL;
70
71 return b;
72 }
73
74 #ifdef __STDC__
75 void
76 buffer_destroy(BUFFER *b)
77 #else
78 void
79 buffer_destroy(b)
80 BUFFER *b;
81 #endif
82 {
83 if (b == NULL || b->status == BUFFER_KEEP)
84 {
85 /* Do nothing. */
86 return;
87 }
88 else if (b->status == BUFFER_REUSE)
89 {
90 /* Always put REUSE buffers on the free list. This will
91 probably cause rplayd to grow bigger, but it prevents
92 problems with calling free() inside the timer
93 interrupt. */
94 nbuffers++;
95 b->next = buffers;
96 buffers = b;
97 }
98 else
99 {
100 /* Free the buffer. */
101 free((char *) b);
102 }
103 }
104
105 /*
106 * Allocate enough buffers to hold nbytes.
107 */
108 #ifdef __STDC__
109 BUFFER *
110 buffer_alloc(int nbytes, int type)
111 #else
112 BUFFER *
113 buffer_alloc(nbytes, type)
114 int nbytes;
115 int type;
116 #endif
117 {
118 BUFFER *head, **next = &head;
119
120 do
121 {
122 *next = buffer_create();
123 nbytes -= BUFFER_SIZE;
124 (*next)->status = type;
125 next = &(*next)->next;
126 }
127 while (nbytes > 0);
128
129 *next = NULL;
130
131 return head;
132 }
133
134 #ifdef __STDC__
135 void
136 buffer_dealloc(BUFFER *b, int force)
137 #else
138 void
139 buffer_dealloc(b, force)
140 BUFFER *b;
141 int force;
142 #endif
143 {
144 BUFFER *next;
145
146 while (b)
147 {
148 next = b->next;
149 if (force)
150 {
151 b->status = BUFFER_FREE;
152 }
153 buffer_destroy(b);
154 b = next;
155 }
156 }
157
158 #ifdef __STDC__
159 void
160 buffer_cleanup(void)
161 #else
162 void
163 buffer_cleanup()
164 #endif
165 {
166 BUFFER *b;
167
168 report(REPORT_DEBUG, "cleaning up buffers - %d bytes\n", nbuffers * BUFFER_SIZE);
169
170 while (buffers)
171 {
172 b = buffers;
173 buffers = buffers->next;
174 free((char *) b);
175 }
176
177 buffers = NULL;
178 nbuffers = 0;
179 next_id = 0;
180 }
181
182 #ifdef __STDC__
183 int
184 buffer_nbytes(BUFFER *b)
185 #else
186 int
187 buffer_nbytes(b)
188 BUFFER *b;
189 #endif
190 {
191 int n = 0;
192
193 while (b)
194 {
195 n += b->nbytes;
196 b = b->next;
197 }
198
199 return n;
200 }