RTS API Documentation  1.10.11
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

void * switch_buffer_get_head_pointer (switch_buffer_t *buffer)
 
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

◆ switch_buffer_flag_t

Enumerator
SWITCH_BUFFER_FLAG_DYNAMIC 
SWITCH_BUFFER_FLAG_PARTITION 

Definition at line 37 of file switch_buffer.c.

Function Documentation

◆ switch_buffer_add_mutex()

void switch_buffer_add_mutex ( switch_buffer_t buffer,
switch_mutex_t mutex 
)

Definition at line 151 of file switch_buffer.c.

References switch_buffer::mutex.

152 {
153  buffer->mutex = mutex;
154 }
switch_mutex_t * mutex
Definition: switch_buffer.c:50
switch_mutex_t * mutex

◆ switch_buffer_create()

switch_status_t switch_buffer_create ( switch_memory_pool_t pool,
switch_buffer_t **  buffer,
switch_size_t  max_len 
)

Definition at line 99 of file switch_buffer.c.

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

100 {
101  switch_buffer_t *new_buffer;
102 
103  if ((new_buffer = switch_core_alloc(pool, sizeof(switch_buffer_t))) != 0 && (new_buffer->data = switch_core_alloc(pool, max_len)) != 0) {
104  new_buffer->datalen = max_len;
105  new_buffer->id = buffer_id++;
106  new_buffer->head = new_buffer->data;
107  *buffer = new_buffer;
108  return SWITCH_STATUS_SUCCESS;
109  }
110  return SWITCH_STATUS_MEMERR;
111 }
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:684

◆ switch_buffer_create_dynamic()

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 113 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.

115 {
116  switch_buffer_t *new_buffer;
117 
118  if ((new_buffer = malloc(sizeof(*new_buffer)))) {
119  memset(new_buffer, 0, sizeof(*new_buffer));
120 
121  if (!start_len) {
122  start_len = 250;
123  }
124 
125  if (!blocksize) {
126  blocksize = start_len;
127  }
128 
129  if (!(new_buffer->data = malloc(start_len))) {
130  free(new_buffer);
131  *buffer = NULL;
132  return SWITCH_STATUS_MEMERR;
133  }
134 
135  memset(new_buffer->data, 0, start_len);
136 
137  new_buffer->max_len = max_len;
138  new_buffer->datalen = start_len;
139  new_buffer->id = buffer_id++;
140  new_buffer->blocksize = blocksize;
141  new_buffer->head = new_buffer->data;
143 
144  *buffer = new_buffer;
145  return SWITCH_STATUS_SUCCESS;
146  }
147  *buffer = NULL;
148  return SWITCH_STATUS_MEMERR;
149 }
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:700
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_buffer_freespace()

switch_size_t switch_buffer_freespace ( switch_buffer_t buffer)

Definition at line 183 of file switch_buffer.c.

References switch_buffer::datalen, switch_buffer::max_len, SWITCH_BUFFER_FLAG_DYNAMIC, switch_test_flag, and switch_buffer::used.

184 {
186  if (buffer->max_len) {
187  return (switch_size_t) (buffer->max_len - buffer->used);
188  }
189  return 1000000;
190  }
191 
192  return (switch_size_t) (buffer->datalen - buffer->used);
193 }
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:693

◆ switch_buffer_inuse()

switch_size_t switch_buffer_inuse ( switch_buffer_t buffer)

Definition at line 195 of file switch_buffer.c.

References switch_buffer::used.

196 {
197  return buffer->used;
198 }
switch_size_t used
Definition: switch_buffer.c:45

◆ switch_buffer_len()

switch_size_t switch_buffer_len ( switch_buffer_t buffer)

Definition at line 178 of file switch_buffer.c.

References switch_buffer::datalen.

179 {
180  return buffer->datalen;
181 }
switch_size_t datalen
Definition: switch_buffer.c:47

◆ switch_buffer_lock()

void switch_buffer_lock ( switch_buffer_t buffer)

Definition at line 156 of file switch_buffer.c.

References switch_buffer::mutex, and switch_mutex_lock().

157 {
158  if (buffer->mutex) {
159  switch_mutex_lock(buffer->mutex);
160  }
161 }
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
switch_mutex_t * mutex
Definition: switch_buffer.c:50

◆ switch_buffer_peek()

switch_size_t switch_buffer_peek ( switch_buffer_t buffer,
void *  data,
switch_size_t  datalen 
)

Definition at line 261 of file switch_buffer.c.

References switch_buffer::datalen, switch_buffer::head, and switch_buffer::used.

262 {
263  switch_size_t reading = 0;
264 
265  if (buffer->used < 1) {
266  buffer->used = 0;
267  return 0;
268  } else if (buffer->used >= datalen) {
269  reading = datalen;
270  } else {
271  reading = buffer->used;
272  }
273 
274  memcpy(data, buffer->head, reading);
275 
276  return reading;
277 }
switch_byte_t * head
Definition: switch_buffer.c:44
switch_size_t used
Definition: switch_buffer.c:45
uintptr_t switch_size_t

◆ switch_buffer_peek_zerocopy()

switch_size_t switch_buffer_peek_zerocopy ( switch_buffer_t buffer,
const void **  ptr 
)

Definition at line 279 of file switch_buffer.c.

References switch_buffer::head, and switch_buffer::used.

280 {
281  switch_size_t reading = 0;
282 
283  if (buffer->used < 1) {
284  buffer->used = 0;
285  *ptr = NULL;
286  return 0;
287  } else {
288  reading = buffer->used;
289  }
290 
291  *ptr = buffer->head;
292 
293  return reading;
294 }
switch_byte_t * head
Definition: switch_buffer.c:44
switch_size_t used
Definition: switch_buffer.c:45
uintptr_t switch_size_t

◆ switch_buffer_read()

switch_size_t switch_buffer_read ( switch_buffer_t buffer,
void *  data,
switch_size_t  datalen 
)

Definition at line 241 of file switch_buffer.c.

References switch_buffer::datalen, switch_buffer::head, and switch_buffer::used.

Referenced by switch_buffer_read_loop().

242 {
243  switch_size_t reading = 0;
244 
245  if (buffer->used < 1) {
246  buffer->used = 0;
247  return 0;
248  } else if (buffer->used >= datalen) {
249  reading = datalen;
250  } else {
251  reading = buffer->used;
252  }
253 
254  memcpy(data, buffer->head, reading);
255  buffer->used -= reading;
256  buffer->head += reading;
257 
258  return reading;
259 }
switch_byte_t * head
Definition: switch_buffer.c:44
switch_size_t used
Definition: switch_buffer.c:45
uintptr_t switch_size_t

◆ switch_buffer_read_loop()

switch_size_t switch_buffer_read_loop ( switch_buffer_t buffer,
void *  data,
switch_size_t  datalen 
)

Definition at line 224 of file switch_buffer.c.

References switch_buffer::actually_used, switch_buffer::data, switch_buffer::head, switch_buffer::loops, switch_buffer_read(), and switch_buffer::used.

225 {
226  switch_size_t len;
227  if ((len = switch_buffer_read(buffer, data, datalen)) == 0) {
228  if (buffer->loops > 0) {
229  buffer->loops--;
230  }
231  if (buffer->loops == 0) {
232  return 0;
233  }
234  buffer->head = buffer->data;
235  buffer->used = buffer->actually_used;
236  len = switch_buffer_read(buffer, data, datalen);
237  }
238  return len;
239 }
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)

◆ switch_buffer_set_loops()

void switch_buffer_set_loops ( switch_buffer_t buffer,
int32_t  loops 
)

Definition at line 219 of file switch_buffer.c.

References switch_buffer::loops.

220 {
221  buffer->loops = loops;
222 }

◆ switch_buffer_toss()

switch_size_t switch_buffer_toss ( switch_buffer_t buffer,
switch_size_t  datalen 
)

Definition at line 200 of file switch_buffer.c.

References switch_buffer::datalen, switch_buffer::head, and switch_buffer::used.

Referenced by switch_buffer_slide_write().

201 {
202  switch_size_t reading = 0;
203 
204  if (buffer->used < 1) {
205  buffer->used = 0;
206  return 0;
207  } else if (buffer->used >= datalen) {
208  reading = datalen;
209  } else {
210  reading = buffer->used;
211  }
212 
213  buffer->used -= reading;
214  buffer->head += reading;
215 
216  return buffer->used;
217 }
switch_byte_t * head
Definition: switch_buffer.c:44
switch_size_t used
Definition: switch_buffer.c:45
uintptr_t switch_size_t

◆ switch_buffer_trylock()

switch_status_t switch_buffer_trylock ( switch_buffer_t buffer)

Definition at line 163 of file switch_buffer.c.

References switch_buffer::mutex, switch_mutex_lock(), and SWITCH_STATUS_FALSE.

164 {
165  if (buffer->mutex) {
166  return switch_mutex_lock(buffer->mutex);
167  }
168  return SWITCH_STATUS_FALSE;
169 }
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
switch_mutex_t * mutex
Definition: switch_buffer.c:50

◆ switch_buffer_unlock()

void switch_buffer_unlock ( switch_buffer_t buffer)

Definition at line 171 of file switch_buffer.c.

References switch_buffer::mutex, and switch_mutex_unlock().

172 {
173  if (buffer->mutex) {
174  switch_mutex_unlock(buffer->mutex);
175  }
176 }
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_mutex_t * mutex
Definition: switch_buffer.c:50

◆ switch_buffer_write()

switch_size_t switch_buffer_write ( switch_buffer_t buffer,
const void *  data,
switch_size_t  datalen 
)

Definition at line 296 of file switch_buffer.c.

References switch_buffer::actually_used, switch_buffer::blocksize, switch_buffer::data, switch_buffer::datalen, switch_buffer::head, switch_buffer::max_len, memset(), switch_assert, SWITCH_BUFFER_FLAG_DYNAMIC, SWITCH_BUFFER_FLAG_PARTITION, switch_test_flag, and switch_buffer::used.

Referenced by switch_buffer_slide_write(), and switch_buffer_zwrite().

297 {
298  switch_size_t freespace, actual_freespace;
299 
301  return 0;
302  }
303 
304  switch_assert(buffer->data != NULL);
305 
306  if (!datalen) {
307  return buffer->used;
308  }
309 
310  actual_freespace = buffer->datalen - buffer->actually_used;
311 
312  if (actual_freespace < datalen) {
313  memmove(buffer->data, buffer->head, buffer->used);
314  buffer->head = buffer->data;
315  buffer->actually_used = buffer->used;
316  }
317 
318  freespace = buffer->datalen - buffer->used;
319 
321  if (freespace < datalen && (!buffer->max_len || (buffer->used + datalen <= buffer->max_len))) {
322  switch_size_t new_size, new_block_size;
323  void *tmp;
324 
325  new_size = buffer->datalen + datalen;
326  new_block_size = buffer->datalen + buffer->blocksize;
327 
328  if (new_block_size > new_size) {
329  new_size = new_block_size;
330  }
331  buffer->head = buffer->data;
332  if (!(tmp = realloc(buffer->data, new_size))) {
333  return 0;
334  }
335  buffer->data = tmp;
336  buffer->head = buffer->data;
337  buffer->datalen = new_size;
338  }
339  }
340 
341  freespace = buffer->datalen - buffer->used;
342 
343  if (freespace < datalen) {
344  return 0;
345  }
346 
347  if (data) {
348  memcpy(buffer->head + buffer->used, data, datalen);
349  } else {
350  memset(buffer->head + buffer->used, 0, datalen);
351  }
352 
353  buffer->used += datalen;
354  buffer->actually_used += datalen;
355  return buffer->used;
356 }
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:693
#define switch_assert(expr)
memset(buf, 0, buflen)

◆ switch_buffer_zero()

void switch_buffer_zero ( switch_buffer_t buffer)

Definition at line 358 of file switch_buffer.c.

References switch_buffer::actually_used, switch_buffer::data, switch_buffer::head, switch_assert, and switch_buffer::used.

Referenced by switch_buffer_zwrite().

359 {
360  switch_assert(buffer->data != NULL);
361 
362  buffer->used = 0;
363  buffer->actually_used = 0;
364  buffer->head = buffer->data;
365 }
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_buffer_zwrite()

switch_size_t switch_buffer_zwrite ( switch_buffer_t buffer,
const void *  data,
switch_size_t  datalen 
)

Definition at line 367 of file switch_buffer.c.

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

368 {
369  switch_size_t w;
370 
372  return 0;
373  }
374 
375  if (!(w = switch_buffer_write(buffer, data, datalen))) {
376  switch_buffer_zero(buffer);
377  return switch_buffer_write(buffer, data, datalen);
378  }
379 
380  return w;
381 }
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:693
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

◆ buffer_id

uint32_t buffer_id = 0
static