FreeSWITCH API Documentation  1.7.0
Functions
Buffer Routines
+ Collaboration diagram for Buffer Routines:

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)
 

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.

Function Documentation

void switch_buffer_add_mutex ( _In_ switch_buffer_t buffer,
_In_ switch_mutex_t mutex 
)
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_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.

Parameters
bufferreturned pointer to the new buffer
blocksizelength to realloc by as data is added
start_lenammount of memory to reserve initially
max_lenlength the buffer is allowed to grow to
Returns
status

Referenced by inband_dtmf_generate_callback(), recording_thread(), setup_ringback(), switch_core_file_read(), switch_core_media_bug_add(), switch_core_perform_file_open(), switch_core_session_read_frame(), switch_core_session_write_frame(), switch_core_speech_read_tts(), switch_ivr_eavesdrop_session(), switch_ivr_gentones(), switch_ivr_originate(), switch_ivr_play_file(), and switch_ivr_wait_for_answer().

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

80 {
81  switch_buffer_t *new_buffer;
82 
83  if ((new_buffer = switch_core_alloc(pool, sizeof(switch_buffer_t))) != 0) {
84  new_buffer->id = buffer_id++;
86  switch_buffer_set_partition_data(new_buffer, data, datalen);
87  *buffer = new_buffer;
88  return SWITCH_STATUS_SUCCESS;
89  }
90  return SWITCH_STATUS_MEMERR;
91 }
#define switch_set_flag(obj, flag)
Set a flag on an arbitrary object.
Definition: switch_utils.h:631
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:682
switch_status_t switch_buffer_set_partition_data(switch_buffer_t *buffer, void *data, switch_size_t datalen)
Definition: switch_buffer.c:68
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 381 of file switch_buffer.c.

References SWITCH_BUFFER_FLAG_DYNAMIC, switch_safe_free, and switch_test_flag.

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

382 {
383  if (buffer && *buffer) {
384  if ((switch_test_flag((*buffer), SWITCH_BUFFER_FLAG_DYNAMIC))) {
385  switch_safe_free((*buffer)->data);
386  free(*buffer);
387  }
388  *buffer = NULL;
389  }
390 }
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
#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_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_size_t switch_buffer_inuse ( _In_ switch_buffer_t buffer)
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.
void switch_buffer_lock ( _In_ switch_buffer_t buffer)
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_size_t switch_buffer_peek_zerocopy ( _In_ switch_buffer_t buffer,
_Out_ const void **  ptr 
)
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(), recording_thread(), switch_core_file_close(), switch_core_file_read(), switch_core_file_write(), switch_core_media_bug_read(), switch_core_session_read_frame(), switch_core_session_write_frame(), switch_core_speech_read_tts(), switch_ivr_eavesdrop_session(), switch_ivr_originate(), and switch_ivr_play_file().

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_status_t switch_buffer_reset_partition_data ( switch_buffer_t buffer)

Definition at line 56 of file switch_buffer.c.

References SWITCH_BUFFER_FLAG_PARTITION, SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, and switch_test_flag.

Referenced by switch_buffer_set_partition_data().

57 {
59  return SWITCH_STATUS_FALSE;
60  }
61 
62  buffer->head = buffer->data;
63  buffer->used = buffer->actually_used = buffer->datalen;
64 
65  return SWITCH_STATUS_SUCCESS;
66 }
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:624
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_status_t switch_buffer_set_partition_data ( switch_buffer_t buffer,
void *  data,
switch_size_t  datalen 
)

Definition at line 68 of file switch_buffer.c.

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

Referenced by switch_buffer_create_partition().

69 {
71  return SWITCH_STATUS_FALSE;
72  }
73 
74  buffer->data = data;
75  buffer->datalen = datalen;
77 }
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:56
#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_slide_write ( switch_buffer_t buffer,
const void *  data,
switch_size_t  datalen 
)

Definition at line 365 of file switch_buffer.c.

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

366 {
367  switch_size_t w;
368 
370  return 0;
371  }
372 
373  if (!(w = switch_buffer_write(buffer, data, datalen))) {
374  switch_buffer_toss(buffer, datalen);
375  return switch_buffer_write(buffer, data, datalen);
376  }
377 
378  return w;
379 }
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:624
switch_size_t switch_buffer_write(switch_buffer_t *buffer, const void *data, switch_size_t datalen)
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_status_t switch_buffer_trylock ( _In_ switch_buffer_t buffer)
void switch_buffer_unlock ( _In_ switch_buffer_t buffer)
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
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(), record_callback(), switch_core_file_read(), switch_core_file_write(), switch_core_session_read_frame(), switch_core_session_write_frame(), switch_core_speech_read_tts(), switch_ivr_play_file(), teletone_dtmf_generate_handler(), and teletone_handler().

void switch_buffer_zero ( _In_ switch_buffer_t buffer)

Remove all data from the buffer.

Parameters
bufferany buffer of type switch_buffer_t

Referenced by switch_core_file_command(), switch_core_file_seek(), switch_core_file_truncate(), switch_core_media_bug_flush(), switch_ivr_eavesdrop_session(), and switch_ivr_play_file().

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