RTS API Documentation  1.10.11
Macros | Functions
Buffer Routines
+ Collaboration diagram for Buffer Routines:

Macros

#define switch_buffer_zero_fill(_buffer, _datalen)   switch_buffer_write(_buffer, 0, _datalen);
 

Functions

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_set_partition_data (switch_buffer_t *buffer, void *data, switch_size_t datalen)
 
switch_status_t switch_buffer_reset_partition_data (switch_buffer_t *buffer)
 
switch_status_t switch_buffer_create (_In_ switch_memory_pool_t *pool, _Out_ switch_buffer_t **buffer, _In_ switch_size_t max_len)
 Allocate a new switch_buffer. More...
 
switch_status_t switch_buffer_create_dynamic (_Out_ switch_buffer_t **buffer, _In_ switch_size_t blocksize, _In_ switch_size_t start_len, _In_ switch_size_t max_len)
 Allocate a new dynamic switch_buffer. More...
 
void switch_buffer_add_mutex (_In_ switch_buffer_t *buffer, _In_ switch_mutex_t *mutex)
 
void switch_buffer_lock (_In_ switch_buffer_t *buffer)
 
switch_status_t switch_buffer_trylock (_In_ switch_buffer_t *buffer)
 
void switch_buffer_unlock (_In_ switch_buffer_t *buffer)
 
switch_size_t switch_buffer_len (_In_ switch_buffer_t *buffer)
 Get the length of a switch_buffer_t. More...
 
switch_size_t switch_buffer_freespace (_In_ switch_buffer_t *buffer)
 Get the freespace of a switch_buffer_t. More...
 
switch_size_t switch_buffer_inuse (_In_ switch_buffer_t *buffer)
 Get the in use amount of a switch_buffer_t. More...
 
switch_size_t switch_buffer_read (_In_ switch_buffer_t *buffer, _In_ void *data, _In_ switch_size_t datalen)
 Read data from a switch_buffer_t up to the ammount of datalen if it is available. Remove read data from buffer. More...
 
switch_size_t switch_buffer_peek (_In_ switch_buffer_t *buffer, _In_ void *data, _In_ switch_size_t datalen)
 Read data from a switch_buffer_t up to the ammount of datalen if it is available, without removing read data from buffer. More...
 
switch_size_t switch_buffer_peek_zerocopy (_In_ switch_buffer_t *buffer, _Out_ const void **ptr)
 
switch_size_t switch_buffer_read_loop (_In_ switch_buffer_t *buffer, _In_ void *data, _In_ switch_size_t datalen)
 Read data endlessly from a switch_buffer_t. More...
 
void switch_buffer_set_loops (_In_ switch_buffer_t *buffer, _In_ int32_t loops)
 Assign a number of loops to read. More...
 
switch_size_t switch_buffer_write (_In_ switch_buffer_t *buffer, _In_bytecount_(datalen) const void *data, _In_ switch_size_t datalen)
 Write data into a switch_buffer_t up to the length of datalen. More...
 
switch_size_t switch_buffer_toss (_In_ switch_buffer_t *buffer, _In_ switch_size_t datalen)
 Remove data from the buffer. More...
 
void switch_buffer_zero (_In_ switch_buffer_t *buffer)
 Remove all data from the buffer. More...
 
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...
 
switch_size_t switch_buffer_zwrite (_In_ switch_buffer_t *buffer, _In_bytecount_(datalen) const void *data, _In_ switch_size_t datalen)
 
void * switch_buffer_get_head_pointer (switch_buffer_t *buffer)
 

Detailed Description

The purpose of this module is to make a plain buffering interface that can be used for read/write buffers throughout the application. The first implementation was done to provide the functionality and the interface and I think it can be optimized under the hood as we go using bucket brigades and/or ring buffering techniques.

Macro Definition Documentation

◆ switch_buffer_zero_fill

#define switch_buffer_zero_fill (   _buffer,
  _datalen 
)    switch_buffer_write(_buffer, 0, _datalen);

Definition at line 142 of file switch_buffer.h.

Function Documentation

◆ switch_buffer_add_mutex()

void switch_buffer_add_mutex ( _In_ switch_buffer_t buffer,
_In_ switch_mutex_t mutex 
)

◆ switch_buffer_create()

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

Allocate a new switch_buffer.

Parameters
poolPool to allocate the buffer from
bufferreturned pointer to the new buffer
max_lenlength required by the buffer
Returns
status

◆ switch_buffer_create_dynamic()

switch_status_t switch_buffer_create_dynamic ( _Out_ switch_buffer_t **  buffer,
_In_ switch_size_t  blocksize,
_In_ switch_size_t  start_len,
_In_ switch_size_t  max_len 
)

◆ switch_buffer_create_partition()

switch_status_t switch_buffer_create_partition ( switch_memory_pool_t pool,
switch_buffer_t **  buffer,
void *  data,
switch_size_t  datalen 
)

Definition at line 85 of file switch_buffer.c.

References buffer_id, id, SWITCH_BUFFER_FLAG_PARTITION, switch_buffer_set_partition_data(), switch_core_alloc, switch_set_flag, SWITCH_STATUS_MEMERR, and SWITCH_STATUS_SUCCESS.

86 {
87  switch_buffer_t *new_buffer;
88 
89  if ((new_buffer = switch_core_alloc(pool, sizeof(switch_buffer_t))) != 0) {
90  new_buffer->id = buffer_id++;
92  switch_buffer_set_partition_data(new_buffer, data, datalen);
93  *buffer = new_buffer;
94  return SWITCH_STATUS_SUCCESS;
95  }
96  return SWITCH_STATUS_MEMERR;
97 }
#define switch_set_flag(obj, flag)
Set a flag on an arbitrary object.
Definition: switch_utils.h:700
switch_memory_pool_t * pool
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_status_t switch_buffer_set_partition_data(switch_buffer_t *buffer, void *data, switch_size_t datalen)
Definition: switch_buffer.c:74

◆ switch_buffer_destroy()

void switch_buffer_destroy ( switch_buffer_t **  buffer)

Destroy the buffer.

Parameters
bufferbuffer to destroy
Note
only neccessary on dynamic buffers (noop on pooled ones)

Definition at line 399 of file switch_buffer.c.

References SWITCH_BUFFER_FLAG_DYNAMIC, switch_safe_free, and switch_test_flag.

Referenced by inband_dtmf_generate_callback(), read_displace_callback(), record_callback(), setup_ringback(), switch_core_file_close(), switch_core_file_pre_close(), switch_core_media_bug_destroy(), switch_core_session_perform_destroy(), switch_core_session_reset(), switch_core_speech_close(), switch_img_data_url(), switch_ivr_eavesdrop_session(), switch_ivr_gentones(), switch_ivr_originate(), switch_ivr_play_file(), switch_ivr_wait_for_answer(), switch_rtp_text_factory_destroy(), and text_bridge_thread().

400 {
401  if (buffer && *buffer) {
402  if ((switch_test_flag((*buffer), SWITCH_BUFFER_FLAG_DYNAMIC))) {
403  switch_safe_free((*buffer)->data);
404  free(*buffer);
405  }
406  *buffer = NULL;
407  }
408 }
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:693

◆ switch_buffer_freespace()

switch_size_t switch_buffer_freespace ( _In_ switch_buffer_t buffer)

Get the freespace of a switch_buffer_t.

Parameters
bufferany buffer of type switch_buffer_t
Returns
int freespace in the buffer.

◆ switch_buffer_get_head_pointer()

void* switch_buffer_get_head_pointer ( switch_buffer_t buffer)

Definition at line 57 of file switch_buffer.c.

References head.

Referenced by switch_core_session_get_text_buffer(), switch_core_session_read_text_frame(), and switch_img_data_url().

58 {
59  return buffer->head;
60 }
switch_byte_t * head
Definition: switch_buffer.c:44

◆ switch_buffer_inuse()

switch_size_t switch_buffer_inuse ( _In_ switch_buffer_t buffer)

◆ switch_buffer_len()

switch_size_t switch_buffer_len ( _In_ switch_buffer_t buffer)

Get the length of a switch_buffer_t.

Parameters
bufferany buffer of type switch_buffer_t
Returns
int size of the buffer.

◆ switch_buffer_lock()

void switch_buffer_lock ( _In_ switch_buffer_t buffer)

◆ switch_buffer_peek()

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

Read data from a switch_buffer_t up to the ammount of datalen if it is available, without removing read data from buffer.

Parameters
bufferany buffer of type switch_buffer_t
datapointer to the read data to be returned
datalenamount of data to be returned
Returns
int ammount of data actually read

Referenced by switch_ivr_originate().

◆ switch_buffer_peek_zerocopy()

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

◆ switch_buffer_read()

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

Read data from a switch_buffer_t up to the ammount of datalen if it is available. Remove read data from buffer.

Parameters
bufferany buffer of type switch_buffer_t
datapointer to the read data to be returned
datalenamount of data to be returned
Returns
int ammount of data actually read

Referenced by eavesdrop_callback(), inband_dtmf_generate_callback(), read_displace_callback(), recording_thread(), switch_core_file_pre_close(), switch_core_file_read(), switch_core_file_write(), switch_core_media_bug_read(), switch_core_session_read_frame(), switch_core_session_read_text_frame(), switch_core_session_write_frame(), switch_core_session_write_text_frame(), switch_core_speech_read_tts(), switch_ivr_eavesdrop_session(), switch_ivr_originate(), switch_ivr_play_file(), and text_bridge_thread().

◆ switch_buffer_read_loop()

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

Read data endlessly from a switch_buffer_t.

Parameters
bufferany buffer of type switch_buffer_t
datapointer to the read data to be returned
datalenamount of data to be returned
Returns
int ammount of data actually read
Note
Once you have read all the data from the buffer it will loop around.

Referenced by switch_ivr_gentones(), switch_ivr_originate(), and switch_ivr_wait_for_answer().

◆ switch_buffer_reset_partition_data()

switch_status_t switch_buffer_reset_partition_data ( switch_buffer_t buffer)

Definition at line 62 of file switch_buffer.c.

References actually_used, data, datalen, head, SWITCH_BUFFER_FLAG_PARTITION, SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, switch_test_flag, and used.

Referenced by switch_buffer_set_partition_data().

63 {
65  return SWITCH_STATUS_FALSE;
66  }
67 
68  buffer->head = buffer->data;
69  buffer->used = buffer->actually_used = buffer->datalen;
70 
71  return SWITCH_STATUS_SUCCESS;
72 }
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
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:693

◆ switch_buffer_set_loops()

void switch_buffer_set_loops ( _In_ switch_buffer_t buffer,
_In_ int32_t  loops 
)

Assign a number of loops to read.

Parameters
bufferany buffer of type switch_buffer_t
loopsthe number of loops (-1 for infinite)

Referenced by setup_ringback(), switch_ivr_gentones(), and switch_ivr_wait_for_answer().

◆ switch_buffer_set_partition_data()

switch_status_t switch_buffer_set_partition_data ( switch_buffer_t buffer,
void *  data,
switch_size_t  datalen 
)

Definition at line 74 of file switch_buffer.c.

References data, datalen, SWITCH_BUFFER_FLAG_PARTITION, switch_buffer_reset_partition_data(), SWITCH_STATUS_FALSE, and switch_test_flag.

Referenced by switch_buffer_create_partition().

75 {
77  return SWITCH_STATUS_FALSE;
78  }
79 
80  buffer->data = data;
81  buffer->datalen = datalen;
83 }
switch_size_t datalen
Definition: switch_buffer.c:47
switch_byte_t * data
Definition: switch_buffer.c:43
switch_status_t switch_buffer_reset_partition_data(switch_buffer_t *buffer)
Definition: switch_buffer.c:62
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:693

◆ switch_buffer_slide_write()

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

Definition at line 383 of file switch_buffer.c.

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

384 {
385  switch_size_t w;
386 
388  return 0;
389  }
390 
391  if (!(w = switch_buffer_write(buffer, data, datalen))) {
392  switch_buffer_toss(buffer, datalen);
393  return switch_buffer_write(buffer, data, datalen);
394  }
395 
396  return w;
397 }
uintptr_t switch_size_t
switch_size_t switch_buffer_toss(switch_buffer_t *buffer, switch_size_t datalen)
#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)

◆ switch_buffer_toss()

switch_size_t switch_buffer_toss ( _In_ switch_buffer_t buffer,
_In_ switch_size_t  datalen 
)

Remove data from the buffer.

Parameters
bufferany buffer of type switch_buffer_t
datalenamount of data to be removed
Returns
int size of buffer, or 0 if unable to toss that much data

Referenced by switch_core_media_bug_read(), and switch_ivr_originate().

◆ switch_buffer_trylock()

switch_status_t switch_buffer_trylock ( _In_ switch_buffer_t buffer)

◆ switch_buffer_unlock()

void switch_buffer_unlock ( _In_ switch_buffer_t buffer)

◆ switch_buffer_write()

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

Write data into a switch_buffer_t up to the length of datalen.

Parameters
bufferany buffer of type switch_buffer_t
datapointer to the data to be written or 0 to fill in with datalen zeros
datalenamount of data to be written
Returns
int amount of buffer used after the write, or 0 if no space available

Referenced by early_thread_run(), file_callback(), read_displace_callback(), record_callback(), switch_core_file_read(), switch_core_file_write(), switch_core_session_read_frame(), switch_core_session_read_text_frame(), switch_core_session_write_frame(), switch_core_session_write_text_frame(), switch_core_speech_read_tts(), switch_ivr_play_file(), teletone_dtmf_generate_handler(), teletone_handler(), and text_bridge_thread().

◆ switch_buffer_zero()

void switch_buffer_zero ( _In_ switch_buffer_t buffer)

◆ switch_buffer_zwrite()

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