FreeSWITCH API Documentation  1.7.0
Data Structures | Enumerations | Functions | Variables
switch_buffer.c File Reference
#include <switch.h>
#include <switch_buffer.h>
+ Include dependency graph for switch_buffer.c:

Go to the source code of this file.

Data Structures

struct  switch_buffer
 

Enumerations

enum  switch_buffer_flag_t { SWITCH_BUFFER_FLAG_DYNAMIC = (1 << 0), SWITCH_BUFFER_FLAG_PARTITION = (1 << 1) }
 

Functions

switch_status_t switch_buffer_reset_partition_data (switch_buffer_t *buffer)
 
switch_status_t switch_buffer_set_partition_data (switch_buffer_t *buffer, void *data, switch_size_t datalen)
 
switch_status_t switch_buffer_create_partition (switch_memory_pool_t *pool, switch_buffer_t **buffer, void *data, switch_size_t datalen)
 
switch_status_t switch_buffer_create (switch_memory_pool_t *pool, switch_buffer_t **buffer, switch_size_t max_len)
 
switch_status_t switch_buffer_create_dynamic (switch_buffer_t **buffer, switch_size_t blocksize, switch_size_t start_len, switch_size_t max_len)
 
void switch_buffer_add_mutex (switch_buffer_t *buffer, switch_mutex_t *mutex)
 
void switch_buffer_lock (switch_buffer_t *buffer)
 
switch_status_t switch_buffer_trylock (switch_buffer_t *buffer)
 
void switch_buffer_unlock (switch_buffer_t *buffer)
 
switch_size_t switch_buffer_len (switch_buffer_t *buffer)
 
switch_size_t switch_buffer_freespace (switch_buffer_t *buffer)
 
switch_size_t switch_buffer_inuse (switch_buffer_t *buffer)
 
switch_size_t switch_buffer_toss (switch_buffer_t *buffer, switch_size_t datalen)
 
void switch_buffer_set_loops (switch_buffer_t *buffer, int32_t loops)
 
switch_size_t switch_buffer_read_loop (switch_buffer_t *buffer, void *data, switch_size_t datalen)
 
switch_size_t switch_buffer_read (switch_buffer_t *buffer, void *data, switch_size_t datalen)
 
switch_size_t switch_buffer_peek (switch_buffer_t *buffer, void *data, switch_size_t datalen)
 
switch_size_t switch_buffer_peek_zerocopy (switch_buffer_t *buffer, const void **ptr)
 
switch_size_t switch_buffer_write (switch_buffer_t *buffer, const void *data, switch_size_t datalen)
 
void switch_buffer_zero (switch_buffer_t *buffer)
 
switch_size_t switch_buffer_zwrite (switch_buffer_t *buffer, const void *data, switch_size_t datalen)
 
switch_size_t switch_buffer_slide_write (switch_buffer_t *buffer, const void *data, switch_size_t datalen)
 
void switch_buffer_destroy (switch_buffer_t **buffer)
 Destroy the buffer. More...
 

Variables

static uint32_t buffer_id = 0
 

Enumeration Type Documentation

Enumerator
SWITCH_BUFFER_FLAG_DYNAMIC 
SWITCH_BUFFER_FLAG_PARTITION 

Definition at line 37 of file switch_buffer.c.

Function Documentation

void switch_buffer_add_mutex ( switch_buffer_t buffer,
switch_mutex_t mutex 
)

Definition at line 138 of file switch_buffer.c.

References mutex.

139 {
140  buffer->mutex = mutex;
141 }
switch_mutex_t * mutex
Definition: switch_buffer.c:50
switch_mutex_t * mutex
switch_status_t switch_buffer_create ( switch_memory_pool_t pool,
switch_buffer_t **  buffer,
switch_size_t  max_len 
)

Definition at line 93 of file switch_buffer.c.

References buffer_id, switch_buffer::data, switch_buffer::datalen, switch_buffer::head, switch_buffer::id, switch_core_alloc, SWITCH_STATUS_MEMERR, and SWITCH_STATUS_SUCCESS.

94 {
95  switch_buffer_t *new_buffer;
96 
97  if ((new_buffer = switch_core_alloc(pool, sizeof(switch_buffer_t))) != 0 && (new_buffer->data = switch_core_alloc(pool, max_len)) != 0) {
98  new_buffer->datalen = max_len;
99  new_buffer->id = buffer_id++;
100  new_buffer->head = new_buffer->data;
101  *buffer = new_buffer;
102  return SWITCH_STATUS_SUCCESS;
103  }
104  return SWITCH_STATUS_MEMERR;
105 }
switch_byte_t * head
Definition: switch_buffer.c:44
switch_memory_pool_t * pool
switch_size_t datalen
Definition: switch_buffer.c:47
switch_byte_t * data
Definition: switch_buffer.c:43
static uint32_t buffer_id
Definition: switch_buffer.c:35
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:682
switch_status_t switch_buffer_create_dynamic ( switch_buffer_t **  buffer,
switch_size_t  blocksize,
switch_size_t  start_len,
switch_size_t  max_len 
)

Definition at line 107 of file switch_buffer.c.

References switch_buffer::blocksize, buffer_id, switch_buffer::data, switch_buffer::datalen, switch_buffer::head, switch_buffer::id, switch_buffer::max_len, memset(), SWITCH_BUFFER_FLAG_DYNAMIC, switch_set_flag, SWITCH_STATUS_MEMERR, and SWITCH_STATUS_SUCCESS.

109 {
110  switch_buffer_t *new_buffer;
111 
112  if ((new_buffer = malloc(sizeof(*new_buffer)))) {
113  memset(new_buffer, 0, sizeof(*new_buffer));
114 
115  if (start_len) {
116  if (!(new_buffer->data = malloc(start_len))) {
117  free(new_buffer);
118  *buffer = NULL;
119  return SWITCH_STATUS_MEMERR;
120  }
121  memset(new_buffer->data, 0, start_len);
122  }
123 
124  new_buffer->max_len = max_len;
125  new_buffer->datalen = start_len;
126  new_buffer->id = buffer_id++;
127  new_buffer->blocksize = blocksize;
128  new_buffer->head = new_buffer->data;
130 
131  *buffer = new_buffer;
132  return SWITCH_STATUS_SUCCESS;
133  }
134  *buffer = NULL;
135  return SWITCH_STATUS_MEMERR;
136 }
switch_byte_t * head
Definition: switch_buffer.c:44
#define switch_set_flag(obj, flag)
Set a flag on an arbitrary object.
Definition: switch_utils.h:631
switch_size_t datalen
Definition: switch_buffer.c:47
switch_byte_t * data
Definition: switch_buffer.c:43
static uint32_t buffer_id
Definition: switch_buffer.c:35
switch_size_t blocksize
Definition: switch_buffer.c:49
switch_size_t max_len
Definition: switch_buffer.c:48
memset(buf, 0, buflen)
switch_size_t switch_buffer_freespace ( switch_buffer_t buffer)

Definition at line 170 of file switch_buffer.c.

References SWITCH_BUFFER_FLAG_DYNAMIC, and switch_test_flag.

171 {
173  if (buffer->max_len) {
174  return (switch_size_t) (buffer->max_len - buffer->used);
175  }
176  return 1000000;
177  }
178 
179  return (switch_size_t) (buffer->datalen - buffer->used);
180 }
switch_size_t datalen
Definition: switch_buffer.c:47
switch_size_t used
Definition: switch_buffer.c:45
uintptr_t switch_size_t
switch_size_t max_len
Definition: switch_buffer.c:48
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
switch_size_t switch_buffer_inuse ( switch_buffer_t buffer)

Definition at line 182 of file switch_buffer.c.

183 {
184  return buffer->used;
185 }
switch_size_t used
Definition: switch_buffer.c:45
switch_size_t switch_buffer_len ( switch_buffer_t buffer)

Definition at line 165 of file switch_buffer.c.

166 {
167  return buffer->datalen;
168 }
switch_size_t datalen
Definition: switch_buffer.c:47
void switch_buffer_lock ( switch_buffer_t buffer)

Definition at line 143 of file switch_buffer.c.

References switch_mutex_lock().

144 {
145  if (buffer->mutex) {
146  switch_mutex_lock(buffer->mutex);
147  }
148 }
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
switch_mutex_t * mutex
Definition: switch_buffer.c:50
switch_size_t switch_buffer_peek ( switch_buffer_t buffer,
void *  data,
switch_size_t  datalen 
)

Definition at line 248 of file switch_buffer.c.

249 {
250  switch_size_t reading = 0;
251 
252  if (buffer->used < 1) {
253  buffer->used = 0;
254  return 0;
255  } else if (buffer->used >= datalen) {
256  reading = datalen;
257  } else {
258  reading = buffer->used;
259  }
260 
261  memcpy(data, buffer->head, reading);
262 
263  return reading;
264 }
switch_byte_t * head
Definition: switch_buffer.c:44
switch_size_t used
Definition: switch_buffer.c:45
uintptr_t switch_size_t
switch_size_t switch_buffer_peek_zerocopy ( switch_buffer_t buffer,
const void **  ptr 
)

Definition at line 266 of file switch_buffer.c.

267 {
268  switch_size_t reading = 0;
269 
270  if (buffer->used < 1) {
271  buffer->used = 0;
272  *ptr = NULL;
273  return 0;
274  } else {
275  reading = buffer->used;
276  }
277 
278  *ptr = buffer->head;
279 
280  return reading;
281 }
switch_byte_t * head
Definition: switch_buffer.c:44
switch_size_t used
Definition: switch_buffer.c:45
uintptr_t switch_size_t
switch_size_t switch_buffer_read ( switch_buffer_t buffer,
void *  data,
switch_size_t  datalen 
)

Definition at line 228 of file switch_buffer.c.

Referenced by switch_buffer_read_loop().

229 {
230  switch_size_t reading = 0;
231 
232  if (buffer->used < 1) {
233  buffer->used = 0;
234  return 0;
235  } else if (buffer->used >= datalen) {
236  reading = datalen;
237  } else {
238  reading = buffer->used;
239  }
240 
241  memcpy(data, buffer->head, reading);
242  buffer->used -= reading;
243  buffer->head += reading;
244 
245  return reading;
246 }
switch_byte_t * head
Definition: switch_buffer.c:44
switch_size_t used
Definition: switch_buffer.c:45
uintptr_t switch_size_t
switch_size_t switch_buffer_read_loop ( switch_buffer_t buffer,
void *  data,
switch_size_t  datalen 
)

Definition at line 211 of file switch_buffer.c.

References switch_buffer_read().

212 {
213  switch_size_t len;
214  if ((len = switch_buffer_read(buffer, data, datalen)) == 0) {
215  if (buffer->loops > 0) {
216  buffer->loops--;
217  }
218  if (buffer->loops == 0) {
219  return 0;
220  }
221  buffer->head = buffer->data;
222  buffer->used = buffer->actually_used;
223  len = switch_buffer_read(buffer, data, datalen);
224  }
225  return len;
226 }
switch_byte_t * head
Definition: switch_buffer.c:44
switch_size_t actually_used
Definition: switch_buffer.c:46
switch_size_t used
Definition: switch_buffer.c:45
switch_byte_t * data
Definition: switch_buffer.c:43
uintptr_t switch_size_t
switch_size_t switch_buffer_read(switch_buffer_t *buffer, void *data, switch_size_t datalen)
void switch_buffer_set_loops ( switch_buffer_t buffer,
int32_t  loops 
)

Definition at line 206 of file switch_buffer.c.

207 {
208  buffer->loops = loops;
209 }
switch_size_t switch_buffer_toss ( switch_buffer_t buffer,
switch_size_t  datalen 
)

Definition at line 187 of file switch_buffer.c.

Referenced by switch_buffer_slide_write().

188 {
189  switch_size_t reading = 0;
190 
191  if (buffer->used < 1) {
192  buffer->used = 0;
193  return 0;
194  } else if (buffer->used >= datalen) {
195  reading = datalen;
196  } else {
197  reading = buffer->used;
198  }
199 
200  buffer->used -= reading;
201  buffer->head += reading;
202 
203  return buffer->used;
204 }
switch_byte_t * head
Definition: switch_buffer.c:44
switch_size_t used
Definition: switch_buffer.c:45
uintptr_t switch_size_t
switch_status_t switch_buffer_trylock ( switch_buffer_t buffer)

Definition at line 150 of file switch_buffer.c.

References switch_mutex_lock(), and SWITCH_STATUS_FALSE.

151 {
152  if (buffer->mutex) {
153  return switch_mutex_lock(buffer->mutex);
154  }
155  return SWITCH_STATUS_FALSE;
156 }
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
switch_mutex_t * mutex
Definition: switch_buffer.c:50
void switch_buffer_unlock ( switch_buffer_t buffer)

Definition at line 158 of file switch_buffer.c.

References switch_mutex_unlock().

159 {
160  if (buffer->mutex) {
161  switch_mutex_unlock(buffer->mutex);
162  }
163 }
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_mutex_t * mutex
Definition: switch_buffer.c:50
switch_size_t switch_buffer_write ( switch_buffer_t buffer,
const void *  data,
switch_size_t  datalen 
)

Definition at line 283 of file switch_buffer.c.

References switch_assert, SWITCH_BUFFER_FLAG_DYNAMIC, SWITCH_BUFFER_FLAG_PARTITION, and switch_test_flag.

Referenced by switch_buffer_slide_write(), and switch_buffer_zwrite().

284 {
285  switch_size_t freespace, actual_freespace;
286 
288  return 0;
289  }
290 
291  switch_assert(buffer->data != NULL);
292 
293  if (!datalen) {
294  return buffer->used;
295  }
296 
297  actual_freespace = buffer->datalen - buffer->actually_used;
298 
299  if (actual_freespace < datalen) {
300  memmove(buffer->data, buffer->head, buffer->used);
301  buffer->head = buffer->data;
302  buffer->actually_used = buffer->used;
303  }
304 
305  freespace = buffer->datalen - buffer->used;
306 
308  if (freespace < datalen && (!buffer->max_len || (buffer->used + datalen <= buffer->max_len))) {
309  switch_size_t new_size, new_block_size;
310  void *tmp;
311 
312  new_size = buffer->datalen + datalen;
313  new_block_size = buffer->datalen + buffer->blocksize;
314 
315  if (new_block_size > new_size) {
316  new_size = new_block_size;
317  }
318  buffer->head = buffer->data;
319  if (!(tmp = realloc(buffer->data, new_size))) {
320  return 0;
321  }
322  buffer->data = tmp;
323  buffer->head = buffer->data;
324  buffer->datalen = new_size;
325  }
326  }
327 
328  freespace = buffer->datalen - buffer->used;
329 
330  if (freespace < datalen) {
331  return 0;
332  }
333 
334  memcpy(buffer->head + buffer->used, data, datalen);
335  buffer->used += datalen;
336  buffer->actually_used += datalen;
337  return buffer->used;
338 }
switch_byte_t * head
Definition: switch_buffer.c:44
switch_size_t actually_used
Definition: switch_buffer.c:46
switch_size_t datalen
Definition: switch_buffer.c:47
switch_size_t used
Definition: switch_buffer.c:45
switch_byte_t * data
Definition: switch_buffer.c:43
switch_size_t blocksize
Definition: switch_buffer.c:49
uintptr_t switch_size_t
switch_size_t max_len
Definition: switch_buffer.c:48
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
#define switch_assert(expr)
void switch_buffer_zero ( switch_buffer_t buffer)

Definition at line 340 of file switch_buffer.c.

References switch_assert.

Referenced by switch_buffer_zwrite().

341 {
342  switch_assert(buffer->data != NULL);
343 
344  buffer->used = 0;
345  buffer->actually_used = 0;
346  buffer->head = buffer->data;
347 }
switch_byte_t * head
Definition: switch_buffer.c:44
switch_size_t actually_used
Definition: switch_buffer.c:46
switch_size_t used
Definition: switch_buffer.c:45
switch_byte_t * data
Definition: switch_buffer.c:43
#define switch_assert(expr)
switch_size_t switch_buffer_zwrite ( switch_buffer_t buffer,
const void *  data,
switch_size_t  datalen 
)

Definition at line 349 of file switch_buffer.c.

References SWITCH_BUFFER_FLAG_PARTITION, switch_buffer_write(), switch_buffer_zero(), and switch_test_flag.

350 {
351  switch_size_t w;
352 
354  return 0;
355  }
356 
357  if (!(w = switch_buffer_write(buffer, data, datalen))) {
358  switch_buffer_zero(buffer);
359  return switch_buffer_write(buffer, data, datalen);
360  }
361 
362  return w;
363 }
uintptr_t switch_size_t
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
switch_size_t switch_buffer_write(switch_buffer_t *buffer, const void *data, switch_size_t datalen)
void switch_buffer_zero(switch_buffer_t *buffer)

Variable Documentation

uint32_t buffer_id = 0
static