source: roaraudio/libroar/buffer.c @ 46:9cbcc36848f9

Last change on this file since 46:9cbcc36848f9 was 46:9cbcc36848f9, checked in by phi, 16 years ago

added roar_buffer_duplicate()

File size: 3.2 KB
Line 
1//buffer.c:
2
3#include "libroar.h"
4
5int roar_buffer_new      (struct roar_buffer ** buf, size_t len) {
6 struct roar_buffer * new;
7
8 ROAR_DBG("buffer_new(buf=%p, len=%i) = ?", buf, len);
9
10 if ((new = malloc(sizeof(struct roar_buffer))) == NULL) {
11  *buf = NULL;
12  return -1;
13 }
14
15 if ((new->data = malloc(len)) == NULL) {
16  free(new);
17  *buf = NULL;
18  return -1;
19 }
20
21 new->user_data = new->data;
22
23 new->next      = NULL;
24
25 new->len       = len;
26 new->user_len  = len;
27 *buf           = new;
28
29 ROAR_DBG("buffer_new(buf=%p, len=%i): New buffer at %p", buf, len, new);
30
31 return 0;
32}
33
34int roar_buffer_free     (struct roar_buffer * buf) {
35 struct roar_buffer * next;
36
37 if ( buf == NULL )
38  return -1;
39
40 while ((next = buf->next)) {
41  free(buf->data);
42  free(buf);
43  buf = next;
44 }
45
46 free(buf->data);
47 free(buf);
48
49 return 0;
50}
51
52int roar_buffer_delete   (struct roar_buffer * buf, struct roar_buffer ** next) {
53 if ( buf == NULL ) {
54  if ( next != NULL )
55   *next = NULL;
56  return -1;
57 }
58
59 ROAR_DBG("buffer_delete(buf=%p, next=%p) = ?", buf, next);
60
61 if ( next != NULL )
62  *next = buf->next;
63
64 free(buf->data);
65 free(buf);
66
67 ROAR_DBG("buffer_delete(buf=%p, next=%p) = 0", buf, next);
68 return 0;
69}
70
71int roar_buffer_add      (struct roar_buffer * buf, struct roar_buffer *  next) {
72 if ( buf == NULL )
73  return -1;
74
75 ROAR_DBG("buffer_add(buf=%p, next=%p) = ?", buf, next);
76
77 while ( buf->next != NULL ) {
78//  ROAR_DBG("buffer_add(): buf=%p, buf->next=%p", buf, buf->next);
79  buf = buf->next;
80 }
81
82 buf->next = next;
83
84 return 0;
85}
86
87int roar_buffer_get_next (struct roar_buffer *  buf, struct roar_buffer ** next) {
88 if ( buf == NULL )
89  return -1;
90
91 *next = buf->next;
92
93 return 0;
94}
95
96int roar_buffer_get_data (struct roar_buffer *  buf, void   ** data) {
97 if ( buf == NULL )
98  return -1;
99
100 *data = buf->user_data;
101
102 return 0;
103}
104
105int roar_buffer_set_offset (struct roar_buffer *  buf, size_t off) {
106 if ( buf == NULL )
107  return -1;
108
109 buf->user_len  -= off;
110 buf->user_data += off;
111
112 return 0;
113}
114
115int roar_buffer_set_meta (struct roar_buffer * buf, void *  meta) {
116 if ( buf == NULL )
117  return -1;
118
119 buf->meta = meta;
120
121 return 0;
122}
123
124int roar_buffer_get_meta (struct roar_buffer * buf, void ** meta) {
125 if ( buf == NULL )
126  return -1;
127
128 *meta = buf->meta;
129
130 return 0;
131}
132
133int roar_buffer_set_len  (struct roar_buffer *  buf, size_t    len) {
134 if ( buf == NULL )
135  return -1;
136
137 buf->user_len = len;
138
139 return 0;
140}
141
142int roar_buffer_get_len  (struct roar_buffer *  buf, size_t *  len) {
143 if ( buf == NULL )
144  return -1;
145
146 *len = buf->user_len;
147
148 return 0;
149}
150
151int roar_buffer_duplicate (struct roar_buffer *  buf, struct roar_buffer ** copy) {
152 struct roar_buffer *  cur = buf;
153 struct roar_buffer *  new;
154
155 *copy = NULL;
156
157 while (cur) {
158  if ( roar_buffer_new(&new, cur->user_len) == -1 ) {
159   roar_buffer_free(*copy);
160   return -1;
161  }
162
163  if ( *copy == NULL )
164   *copy = new;
165
166  roar_buffer_add(*copy, new);
167
168  cur = cur->next;
169 }
170 return 0;
171}
172
173int roar_buffer_ring_stats (struct roar_buffer *  buf, struct roar_buffer_stats * stats) {
174 if ( buf == NULL )
175  return -1;
176
177 stats->parts        = 0;
178 stats->bytes        = 0;
179 stats->memory_usage = 0;
180
181 while (buf) {
182  stats->parts++;
183  stats->bytes        += buf->user_len;
184  stats->memory_usage += buf->len + sizeof(struct roar_buffer);
185  buf = buf->next;
186 }
187
188 return 0;
189}
190
191//ll
Note: See TracBrowser for help on using the repository browser.