FreeSWITCH API Documentation  1.7.0
switch_buffer.h
Go to the documentation of this file.
1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is
20  * Anthony Minessale II <anthm@freeswitch.org>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Anthony Minessale II <anthm@freeswitch.org>
27  *
28  *
29  * switch_buffer.h -- Data Buffering Code
30  *
31  */
32 /**
33  * @file switch_buffer.h
34  * @brief Data Buffering Code
35  * @see switch_buffer
36  */
37 
38 #ifndef SWITCH_BUFFER_H
39 #define SWITCH_BUFFER_H
40 
41 #include <switch.h>
42 
44 /**
45  * @defgroup switch_buffer Buffer Routines
46  * @ingroup core1
47  * The purpose of this module is to make a plain buffering interface that can be used for read/write buffers
48  * throughout the application. The first implementation was done to provide the functionality and the interface
49  * and I think it can be optimized under the hood as we go using bucket brigades and/or ring buffering techniques.
50  * @{
51  */
52  struct switch_buffer;
53 
54 
58 
59 /*! \brief Allocate a new switch_buffer
60  * \param pool Pool to allocate the buffer from
61  * \param buffer returned pointer to the new buffer
62  * \param max_len length required by the buffer
63  * \return status
64  */
66 
67 /*! \brief Allocate a new dynamic switch_buffer
68  * \param buffer returned pointer to the new buffer
69  * \param blocksize length to realloc by as data is added
70  * \param start_len ammount of memory to reserve initially
71  * \param max_len length the buffer is allowed to grow to
72  * \return status
73  */
75  _In_ switch_size_t max_len);
76 
81 
82 /*! \brief Get the length of a switch_buffer_t
83  * \param buffer any buffer of type switch_buffer_t
84  * \return int size of the buffer.
85  */
87 
88 /*! \brief Get the freespace of a switch_buffer_t
89  * \param buffer any buffer of type switch_buffer_t
90  * \return int freespace in the buffer.
91  */
93 
94 /*! \brief Get the in use amount of a switch_buffer_t
95  * \param buffer any buffer of type switch_buffer_t
96  * \return int ammount of buffer curently in use
97  */
99 
100 /*! \brief Read data from a switch_buffer_t up to the ammount of datalen if it is available. Remove read data from buffer.
101  * \param buffer any buffer of type switch_buffer_t
102  * \param data pointer to the read data to be returned
103  * \param datalen amount of data to be returned
104  * \return int ammount of data actually read
105  */
107 
108 /*! \brief Read data from a switch_buffer_t up to the ammount of datalen if it is available, without removing read data from buffer.
109  * \param buffer any buffer of type switch_buffer_t
110  * \param data pointer to the read data to be returned
111  * \param datalen amount of data to be returned
112  * \return int ammount of data actually read
113  */
115 
117 
118 /*! \brief Read data endlessly from a switch_buffer_t
119  * \param buffer any buffer of type switch_buffer_t
120  * \param data pointer to the read data to be returned
121  * \param datalen amount of data to be returned
122  * \return int ammount of data actually read
123  * \note Once you have read all the data from the buffer it will loop around.
124  */
126 
127 /*! \brief Assign a number of loops to read
128  * \param buffer any buffer of type switch_buffer_t
129  * \param loops the number of loops (-1 for infinite)
130  */
132 
133 /*! \brief Write data into a switch_buffer_t up to the length of datalen
134  * \param buffer any buffer of type switch_buffer_t
135  * \param data pointer to the data to be written
136  * \param datalen amount of data to be written
137  * \return int amount of buffer used after the write, or 0 if no space available
138  */
140  const void *data, _In_ switch_size_t datalen);
141 
142 /*! \brief Remove data from the buffer
143  * \param buffer any buffer of type switch_buffer_t
144  * \param datalen amount of data to be removed
145  * \return int size of buffer, or 0 if unable to toss that much data
146  */
148 
149 /*! \brief Remove all data from the buffer
150  * \param buffer any buffer of type switch_buffer_t
151  */
153 
155 
156 /*! \brief Destroy the buffer
157  * \param buffer buffer to destroy
158  * \note only neccessary on dynamic buffers (noop on pooled ones)
159  */
161 
163  const void *data, _In_ switch_size_t datalen);
164 
165 /** @} */
166 
168 #endif
169 /* For Emacs:
170  * Local Variables:
171  * mode:c
172  * indent-tabs-mode:t
173  * tab-width:4
174  * c-basic-offset:4
175  * End:
176  * For VIM:
177  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
178  */
switch_size_t switch_buffer_freespace(_In_ switch_buffer_t *buffer)
Get the freespace of a switch_buffer_t.
switch_size_t switch_buffer_len(_In_ switch_buffer_t *buffer)
Get the length of a switch_buffer_t.
switch_size_t switch_buffer_zwrite(_In_ switch_buffer_t *buffer, _In_bytecount_(datalen) const void *data, _In_ switch_size_t datalen)
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 fr...
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.
#define SWITCH_END_EXTERN_C
Definition: switch.h:43
switch_status_t switch_buffer_trylock(_In_ switch_buffer_t *buffer)
switch_memory_pool_t * pool
switch_size_t datalen
Definition: switch_buffer.c:47
switch_size_t switch_buffer_slide_write(switch_buffer_t *buffer, const void *data, switch_size_t datalen)
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.
switch_status_t switch_buffer_create_partition(switch_memory_pool_t *pool, switch_buffer_t **buffer, void *data, switch_size_t datalen)
Definition: switch_buffer.c:79
switch_size_t switch_buffer_toss(_In_ switch_buffer_t *buffer, _In_ switch_size_t datalen)
Remove data from the buffer.
switch_byte_t * data
Definition: switch_buffer.c:43
void switch_buffer_lock(_In_ switch_buffer_t *buffer)
void switch_buffer_set_loops(_In_ switch_buffer_t *buffer, _In_ int32_t loops)
Assign a number of loops to read.
switch_size_t blocksize
Definition: switch_buffer.c:49
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.
void switch_buffer_zero(_In_ switch_buffer_t *buffer)
Remove all data from the buffer.
uintptr_t switch_size_t
#define _In_bytecount_(x)
void switch_buffer_add_mutex(_In_ switch_buffer_t *buffer, _In_ switch_mutex_t *mutex)
switch_mutex_t * mutex
#define _Out_
switch_size_t max_len
Definition: switch_buffer.c:48
struct apr_thread_mutex_t switch_mutex_t
Definition: switch_apr.h:314
switch_status_t
Common return values.
switch_status_t switch_buffer_reset_partition_data(switch_buffer_t *buffer)
Definition: switch_buffer.c:56
void switch_buffer_unlock(_In_ switch_buffer_t *buffer)
switch_size_t switch_buffer_peek_zerocopy(_In_ switch_buffer_t *buffer, _Out_ const void **ptr)
Main Library Header.
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 re...
#define SWITCH_DECLARE(type)
struct apr_pool_t switch_memory_pool_t
switch_status_t switch_buffer_set_partition_data(switch_buffer_t *buffer, void *data, switch_size_t datalen)
Definition: switch_buffer.c:68
switch_size_t switch_buffer_inuse(_In_ switch_buffer_t *buffer)
Get the in use amount of a switch_buffer_t.
void switch_buffer_destroy(switch_buffer_t **buffer)
Destroy the 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.
#define _In_
#define SWITCH_BEGIN_EXTERN_C
Definition: switch.h:42