String fields in structures. More...
#include "asterisk/inline_api.h"
Go to the source code of this file.
Data Structures | |
struct | ast_string_field_mgr |
struct | ast_string_field_pool |
Macros | |
#define | ast_calloc_with_stringfields(n, type, size) |
Allocate a structure with embedded stringfields in a single allocation. More... | |
#define | AST_DECLARE_STRING_FIELDS(field_list) |
Declare the fields needed in a structure. More... | |
#define | AST_STRING_FIELD(name) const ast_string_field name |
Declare a string field. More... | |
#define | AST_STRING_FIELD_ALLOCATION(x) *((ast_string_field_allocation *) (x - __alignof__(ast_string_field_allocation))) |
Macro to provide access to the allocation field that lives immediately in front of a string field. More... | |
#define | ast_string_field_build(x, field, fmt, args...) __ast_string_field_ptr_build(&(x)->__field_mgr, &(x)->__field_mgr_pool, (ast_string_field *) &(x)->field, fmt, args) |
Set a field to a complex (built) value. More... | |
#define | ast_string_field_build_va(x, field, fmt, args1, args2) __ast_string_field_ptr_build_va(&(x)->__field_mgr, &(x)->__field_mgr_pool, (ast_string_field *) &(x)->field, fmt, args1, args2) |
Set a field to a complex (built) value. More... | |
#define | ast_string_field_free_memory(x) __ast_string_field_init(&(x)->__field_mgr, &(x)->__field_mgr_pool, -1, __FILE__, __LINE__, __PRETTY_FUNCTION__) |
free all memory - to be called before destroying the object More... | |
#define | ast_string_field_init(x, size) __ast_string_field_init(&(x)->__field_mgr, &(x)->__field_mgr_pool, size, __FILE__, __LINE__, __PRETTY_FUNCTION__) |
Initialize a field pool and fields. More... | |
#define | ast_string_field_ptr_build(x, ptr, fmt, args...) __ast_string_field_ptr_build(&(x)->__field_mgr, &(x)->__field_mgr_pool, (ast_string_field *) ptr, fmt, args) |
Set a field to a complex (built) value. More... | |
#define | ast_string_field_ptr_build_va(x, ptr, fmt, args1, args2) __ast_string_field_ptr_build_va(&(x)->__field_mgr, &(x)->__field_mgr_pool, (ast_string_field *) ptr, fmt, args1, args2) |
Set a field to a complex (built) value with prebuilt va_lists. More... | |
#define | ast_string_field_ptr_set(x, ptr, data) |
Set a field to a simple string value. More... | |
#define | ast_string_field_set(x, field, data) |
Set a field to a simple string value. More... | |
Typedefs | |
typedef const char * | ast_string_field |
typedef uint16_t | ast_string_field_allocation |
Functions | |
void *attribute_malloc | __ast_calloc_with_stringfields (unsigned int num_structs, size_t struct_size, size_t field_mgr_offset, size_t field_mgr_pool_offset, size_t pool_size, const char *file, int lineno, const char *func) |
ast_string_field | __ast_string_field_alloc_space (struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head, size_t needed) |
int | __ast_string_field_init (struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head, int needed, const char *file, int lineno, const char *func) |
void | __ast_string_field_ptr_build (struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head, ast_string_field *ptr, const char *format,...) |
void | __ast_string_field_ptr_build_va (struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head, ast_string_field *ptr, const char *format, va_list a1, va_list a2) |
int | __ast_string_field_ptr_grow (struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head, size_t needed, const ast_string_field *ptr) |
void | __ast_string_field_release_active (struct ast_string_field_pool *pool_head, const ast_string_field ptr) |
Variables | |
const char * | __ast_string_field_empty |
String fields in structures.
This file contains objects and macros used to manage string fields in structures without requiring them to be allocated as fixed-size buffers or requiring individual allocations for for each field.
Using this functionality is quite simple. An example structure with three fields is defined like this:
When an instance of this structure is allocated (either statically or dynamically), the fields and the pool of storage for them must be initialized:
Fields will default to pointing to an empty string, and will revert to that when ast_string_field_set() is called with a NULL argument. A string field will never contain NULL.
ast_string_field_init(x, 0) will reset fields to the initial value while keeping the pool allocated.
Reading the fields is much like using 'const char * const' fields in the structure: you cannot write to the field or to the memory it points to.
Writing to the fields must be done using the wrapper macros listed below; and assignments are always by value (i.e. strings are copied): ast_string_field_set() stores a simple value; ast_string_field_build() builds the string using a printf-style format; ast_string_field_build_va() is the varargs version of the above (for portability reasons it uses two vararg arguments); variants of these function allow passing a pointer to the field as an argument.
When the structure instance is no longer needed, the fields and their storage pool must be freed:
This completes the API description.
Definition in file stringfields.h.
#define ast_calloc_with_stringfields | ( | n, | |
type, | |||
size | |||
) |
Allocate a structure with embedded stringfields in a single allocation.
n | Number of structures to allocate (see ast_calloc) |
type | The type of structure to allocate |
size | The number of bytes of space (minimum) to allocate for stringfields to use |
This function will allocate memory for one or more structures that use stringfields, and also allocate space for the stringfields and initialize the stringfield management structure embedded in the outer structure.
Definition at line 275 of file stringfields.h.
Referenced by append_mailbox_mapping(), ast_log(), AST_TEST_DEFINE(), jack_data_alloc(), load_config(), load_module(), raise_exception(), register_group(), register_group_feature(), sip_register(), and sip_subscribe_mwi().
#define AST_DECLARE_STRING_FIELDS | ( | field_list | ) |
Declare the fields needed in a structure.
field_list | The list of fields to declare, using AST_STRING_FIELD() for each one. Internally, string fields are stored as a pointer to the head of the pool, followed by individual string fields, and then a struct ast_string_field_mgr which describes the space allocated. We split the two variables so they can be used as markers around the field_list, and this allows us to determine how many entries are in the field, and play with them. In particular, for writing to the fields, we rely on __field_mgr_pool to be a non-const pointer, so we know it has the same size as ast_string_field, and we can use it to locate the fields. |
Definition at line 235 of file stringfields.h.
#define AST_STRING_FIELD | ( | name | ) | const ast_string_field name |
Declare a string field.
name | The field name |
Definition at line 220 of file stringfields.h.
#define AST_STRING_FIELD_ALLOCATION | ( | x | ) | *((ast_string_field_allocation *) (x - __alignof__(ast_string_field_allocation))) |
Macro to provide access to the allocation field that lives immediately in front of a string field.
x | Pointer to the string field |
Note that x must be a pointer to a byte-sized type – normally (char *) – or this calculation would break horribly
Definition at line 309 of file stringfields.h.
Referenced by __ast_string_field_alloc_space(), __ast_string_field_ptr_build_va(), __ast_string_field_ptr_grow(), and __ast_string_field_release_active().
#define ast_string_field_build | ( | x, | |
field, | |||
fmt, | |||
args... | |||
) | __ast_string_field_ptr_build(&(x)->__field_mgr, &(x)->__field_mgr_pool, (ast_string_field *) &(x)->field, fmt, args) |
Set a field to a complex (built) value.
x | Pointer to a structure containing fields |
field | Name of the field to set |
fmt | printf-style format string |
args | Arguments for format string |
Definition at line 367 of file stringfields.h.
Referenced by __ast_channel_alloc_ap(), action_originate(), build_callid_pvt(), build_callid_registry(), build_contact(), build_localtag_registry(), build_user(), caldav_write_event(), handle_request_subscribe(), init_acf_query(), load_config(), make_our_tag(), parse_cdata(), parse_moved_contact(), parse_register_contact(), set_nonce_randdata(), sip_sendhtml(), sip_sipredirect(), and t30_phase_e_handler().
#define ast_string_field_build_va | ( | x, | |
field, | |||
fmt, | |||
args1, | |||
args2 | |||
) | __ast_string_field_ptr_build_va(&(x)->__field_mgr, &(x)->__field_mgr_pool, (ast_string_field *) &(x)->field, fmt, args1, args2) |
Set a field to a complex (built) value.
x | Pointer to a structure containing fields |
field | Name of the field to set |
fmt | printf-style format string |
args1 | argument one |
args2 | argument two |
Definition at line 391 of file stringfields.h.
Referenced by __ast_channel_alloc_ap().
#define ast_string_field_free_memory | ( | x | ) | __ast_string_field_init(&(x)->__field_mgr, &(x)->__field_mgr_pool, -1, __FILE__, __LINE__, __PRETTY_FUNCTION__) |
free all memory - to be called before destroying the object
Definition at line 253 of file stringfields.h.
Referenced by __sip_destroy(), action_destroy(), ast_channel_destructor(), ast_custom_function_unregister(), ast_dummy_channel_destructor(), ast_pbx_outgoing_app(), ast_pbx_run_app(), AST_TEST_DEFINE(), ast_unregister_application(), ast_unregister_groups(), auth_http_callback(), build_user(), caldav_destructor(), calendar_destructor(), calendar_event_destructor(), destroy_fast_originate_helper(), destroy_jack_data(), destroy_mailbox_mapping(), destroy_odbc(), destroy_queue(), destroy_session_details(), ewscal_destructor(), exception_store_free(), exchangecal_destructor(), free_acf_query(), free_outgoing(), icalendar_destructor(), load_module(), peer_destructor(), profile_destructor(), pvt_destructor(), realtime_multi_odbc(), realtime_odbc(), sip_destroy_peer(), sip_monitor_instance_destructor(), sip_registry_destroy(), sip_subscribe_mwi_destroy(), sla_station_destructor(), sla_trunk_destructor(), store_odbc(), tds_unload_module(), temp_pvt_cleanup(), update_odbc(), and user_destructor().
#define ast_string_field_init | ( | x, | |
size | |||
) | __ast_string_field_init(&(x)->__field_mgr, &(x)->__field_mgr_pool, size, __FILE__, __LINE__, __PRETTY_FUNCTION__) |
Initialize a field pool and fields.
x | Pointer to a structure containing fields |
size | Amount of storage to allocate. Use 0 to reset fields to the default value, and release all but the most recent pool. size<0 (used internally) means free all pools. |
Definition at line 249 of file stringfields.h.
Referenced by __ast_channel_alloc_ap(), acf_retrieve_docs(), action_originate(), alloc_queue(), ast_calendar_event_alloc(), ast_dummy_channel_alloc(), ast_manager_register2(), ast_pbx_outgoing_app(), ast_register_application2(), auth_http_callback(), build_calendar(), build_peer(), build_route(), build_user(), caldav_load_calendar(), destroy_odbc(), ewscal_load_calendar(), exchangecal_load_calendar(), ical_load_calendar(), init_acf_query(), init_pvt(), new_iax(), new_outgoing(), realtime_multi_odbc(), realtime_odbc(), session_details_new(), sip_alloc(), sip_monitor_instance_init(), sla_build_station(), sla_build_trunk(), store_odbc(), tds_load_module(), temp_peer(), temp_pvt_init(), transmit_response_using_temp(), and update_odbc().
#define ast_string_field_ptr_build | ( | x, | |
ptr, | |||
fmt, | |||
args... | |||
) | __ast_string_field_ptr_build(&(x)->__field_mgr, &(x)->__field_mgr_pool, (ast_string_field *) ptr, fmt, args) |
Set a field to a complex (built) value.
x | Pointer to a structure containing fields |
ptr | Pointer to a field within the structure |
fmt | printf-style format string |
args | Arguments for format string |
Definition at line 356 of file stringfields.h.
#define ast_string_field_ptr_build_va | ( | x, | |
ptr, | |||
fmt, | |||
args1, | |||
args2 | |||
) | __ast_string_field_ptr_build_va(&(x)->__field_mgr, &(x)->__field_mgr_pool, (ast_string_field *) ptr, fmt, args1, args2) |
Set a field to a complex (built) value with prebuilt va_lists.
x | Pointer to a structure containing fields |
ptr | Pointer to a field within the structure |
fmt | printf-style format string |
args1 | Arguments for format string in va_list format |
args2 | a second copy of the va_list for the sake of bsd, with no va_list copy operation |
Definition at line 379 of file stringfields.h.
#define ast_string_field_ptr_set | ( | x, | |
ptr, | |||
data | |||
) |
Set a field to a simple string value.
x | Pointer to a structure containing fields |
ptr | Pointer to a field within the structure |
data | String value to be copied into the field |
Definition at line 318 of file stringfields.h.
Referenced by ast_parse_digest(), and reply_digest().
#define ast_string_field_set | ( | x, | |
field, | |||
data | |||
) |
Set a field to a simple string value.
x | Pointer to a structure containing fields |
field | Name of the field to set |
data | String value to be copied into the field |
Definition at line 344 of file stringfields.h.
Referenced by __ast_change_name_nolink(), __ast_channel_alloc_ap(), __find_callno(), __oh323_new(), __sip_subscribe_mwi_do(), acf_faxopt_write(), acf_retrieve_docs(), action_originate(), agent_new(), alloc_queue(), alsa_new(), analog_new_ast_channel(), append_mailbox_mapping(), apply_outgoing(), ast_call_forward(), ast_cdr_setaccount(), ast_cdr_setpeeraccount(), ast_cel_fabricate_channel_from_event(), ast_channel_change_linkedid(), ast_do_masquerade(), ast_iax2_new(), ast_log(), ast_manager_register2(), ast_parse_digest(), ast_pbx_outgoing_app(), ast_register_application2(), ast_set_hangupsource(), ast_set_owners_and_peers(), authenticate_reply(), authenticate_request(), authenticate_verify(), begin_dial_channel(), build_calendar(), build_peer(), build_route(), build_user(), cache_get_callno_locked(), caldav_add_event(), caldav_load_calendar(), calendar_write_exec(), change_callid_pvt(), check_access(), check_peer_ok(), check_user_full(), conf_start_moh(), console_new(), copy_event_data(), create_addr(), create_addr_from_peer(), custom_prepare(), dahdi_new(), dial_exec_full(), disa_exec(), do_forward(), endelm(), ewscal_load_calendar(), exchangecal_load_calendar(), extract_uri(), feature_request_and_dial(), findmeexec(), generic_fax_exec(), get_also_info(), get_destination(), get_pai(), get_realm(), get_rpid(), gtalk_new(), handle_cc_notify(), handle_incoming(), handle_options(), handle_request_bye(), handle_request_invite(), handle_request_options(), handle_request_publish(), handle_request_refer(), handle_response(), handle_response_invite(), handle_response_notify(), handle_response_publish(), handle_response_register(), handle_response_subscribe(), iax2_call(), iax2_request(), iax_park(), ical_load_calendar(), icalendar_add_event(), init_acf_query(), init_pvt(), init_queue(), initreqprep(), jingle_new(), load_config(), load_module(), local_call(), logger_print_normal(), mgcp_new(), misdn_facility_ie_handler(), moh_handle_digit(), monitor_dial(), nbs_new(), new_iax(), new_outgoing(), oss_new(), parse_moved_contact(), parse_ok_contact(), parse_register_contact(), phone_new(), queue_set_param(), raise_exception(), read_config(), receivefax_exec(), reg_source_db(), register_group(), register_group_feature(), register_verify(), registry_authrequest(), reply_digest(), reqprep(), respprep(), ring_entry(), save_osptoken(), sendfax_exec(), set_moh_exec(), set_peer_defaults(), set_pvt_defaults(), sig_pri_handle_subcmds(), sip_alloc(), sip_call(), sip_monitor_instance_init(), sip_new(), sip_park(), sip_parse_register_line(), sip_poke_peer(), sip_reload(), sip_request_call(), sip_send_mwi_to_peer(), sip_set_redirstr(), sip_subscribe_mwi(), skinny_new(), sla_build_station(), sla_build_trunk(), socket_process(), startelm(), store_callerid(), t30_phase_e_handler(), tds_load_module(), transmit_refer(), transmit_register(), transmit_response_using_temp(), unistim_new(), vm_execmain(), and wait_for_answer().
typedef const char* ast_string_field |
Definition at line 115 of file stringfields.h.
typedef uint16_t ast_string_field_allocation |
Definition at line 119 of file stringfields.h.
void* attribute_malloc __ast_calloc_with_stringfields | ( | unsigned int | num_structs, |
size_t | struct_size, | ||
size_t | field_mgr_offset, | ||
size_t | field_mgr_pool_offset, | ||
size_t | pool_size, | ||
const char * | file, | ||
int | lineno, | ||
const char * | func | ||
) |
Definition at line 2026 of file utils.c.
References __ast_calloc(), __ast_string_field_empty, allocation, ast_calloc, ast_string_field_pool::base, ast_string_field_mgr::embedded_pool, optimal_alloc_size(), and ast_string_field_pool::size.
ast_string_field __ast_string_field_alloc_space | ( | struct ast_string_field_mgr * | mgr, |
struct ast_string_field_pool ** | pool_head, | ||
size_t | needed | ||
) |
Definition at line 1847 of file utils.c.
References add_string_pool(), ast_alignof, ast_assert, ast_make_room_for, AST_STRING_FIELD_ALLOCATION, and ast_string_field_mgr::last_alloc.
Referenced by __ast_string_field_ptr_build_va().
int __ast_string_field_init | ( | struct ast_string_field_mgr * | mgr, |
struct ast_string_field_pool ** | pool_head, | ||
int | needed, | ||
const char * | file, | ||
int | lineno, | ||
const char * | func | ||
) |
Definition at line 1780 of file utils.c.
References __ast_string_field_empty, ast_string_field_pool::active, add_string_pool(), ast_free, ast_log(), ast_string_field_mgr::embedded_pool, ast_string_field_mgr::last_alloc, LOG_WARNING, ast_string_field_pool::prev, and ast_string_field_pool::used.
void __ast_string_field_ptr_build | ( | struct ast_string_field_mgr * | mgr, |
struct ast_string_field_pool ** | pool_head, | ||
ast_string_field * | ptr, | ||
const char * | format, | ||
... | |||
) |
Definition at line 2011 of file utils.c.
References __ast_string_field_ptr_build_va().
void __ast_string_field_ptr_build_va | ( | struct ast_string_field_mgr * | mgr, |
struct ast_string_field_pool ** | pool_head, | ||
ast_string_field * | ptr, | ||
const char * | format, | ||
va_list | a1, | ||
va_list | a2 | ||
) |
Definition at line 1935 of file utils.c.
References __ast_string_field_alloc_space(), __ast_string_field_empty, __ast_string_field_release_active(), ast_align_for, ast_alignof, ast_assert, ast_make_room_for, AST_STRING_FIELD_ALLOCATION, available(), and ast_string_field_mgr::last_alloc.
Referenced by __ast_string_field_ptr_build().
int __ast_string_field_ptr_grow | ( | struct ast_string_field_mgr * | mgr, |
struct ast_string_field_pool ** | pool_head, | ||
size_t | needed, | ||
const ast_string_field * | ptr | ||
) |
Definition at line 1888 of file utils.c.
References AST_STRING_FIELD_ALLOCATION, and ast_string_field_mgr::last_alloc.
void __ast_string_field_release_active | ( | struct ast_string_field_pool * | pool_head, |
const ast_string_field | ptr | ||
) |
Definition at line 1910 of file utils.c.
References ast_string_field_pool::active, ast_free, AST_STRING_FIELD_ALLOCATION, and ast_string_field_pool::prev.
Referenced by __ast_string_field_ptr_build_va().
const char* __ast_string_field_empty |
Definition at line 1725 of file utils.c.
Referenced by __ast_calloc_with_stringfields(), __ast_string_field_init(), and __ast_string_field_ptr_build_va().