#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 |
Defines | |
#define | ast_calloc_with_stringfields(n, type, size) |
Allocate a structure with embedded stringfields in a single allocation. | |
#define | AST_DECLARE_STRING_FIELDS(field_list) |
Declare the fields needed in a structure. | |
#define | AST_STRING_FIELD(name) const ast_string_field name |
Declare a string field. | |
#define | AST_STRING_FIELD_ALLOCATION(x) *((ast_string_field_allocation *) (x - sizeof(ast_string_field_allocation))) |
Macro to provide access to the allocation field that lives immediately in front of a string field. | |
#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. | |
#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. | |
#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 | |
#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. | |
#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. | |
#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. | |
#define | ast_string_field_ptr_set(x, ptr, data) |
Set a field to a simple string value. | |
#define | ast_string_field_set(x, field, data) |
Set a field to a simple string value. | |
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 |
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:
struct sample_fields { int x1; AST_DECLARE_STRING_FIELDS( AST_STRING_FIELD(foo); AST_STRING_FIELD(bar); AST_STRING_FIELD(blah); ); long x2; };
When an instance of this structure is allocated (either statically or dynamically), the fields and the pool of storage for them must be initialized:
struct sample_fields *x; x = ast_calloc(1, sizeof(*x)); if (x == NULL || ast_string_field_init(x, 252)) { if (x) ast_free(x); x = NULL; ... handle error }
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.
ast_string_field_set(x, foo, "infinite loop"); ast_string_field_set(x, foo, NULL); // set to an empty string ast_string_field_ptr_set(x, &x->bar, "right way"); ast_string_field_build(x, blah, "%d %s", zipcode, city); ast_string_field_ptr_build(x, &x->blah, "%d %s", zipcode, city); ast_string_field_build_va(x, bar, fmt, args1, args2) ast_string_field_ptr_build_va(x, &x->bar, fmt, args1, args2)
When the structure instance is no longer needed, the fields and their storage pool must be freed:
ast_string_field_free_memory(x); ast_free(x);
This completes the API description.
Definition in file stringfields.h.
#define ast_calloc_with_stringfields | ( | n, | |||
type, | |||||
size | ) |
Value:
__ast_calloc_with_stringfields(n, sizeof(type), offsetof(type, __field_mgr), offsetof(type, __field_mgr_pool), \ size, __FILE__, __LINE__, __PRETTY_FUNCTION__)
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 |
Definition at line 269 of file stringfields.h.
Referenced by append_mailbox_mapping(), ast_log(), ast_manager_register2(), build_extension(), jack_data_alloc(), load_config(), load_module(), pbx_builtin_raise_exception(), register_group(), register_group_feature(), sip_register(), sip_subscribe_mwi(), sla_build_station(), and sla_build_trunk().
#define AST_DECLARE_STRING_FIELDS | ( | field_list | ) |
Value:
struct ast_string_field_pool *__field_mgr_pool; \ field_list \ struct ast_string_field_mgr __field_mgr
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 229 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 214 of file stringfields.h.
#define AST_STRING_FIELD_ALLOCATION | ( | x | ) | *((ast_string_field_allocation *) (x - sizeof(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 |
Definition at line 304 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 360 of file stringfields.h.
Referenced by __ast_channel_alloc_ap(), build_callid_pvt(), build_callid_registry(), build_contact(), build_user(), caldav_write_event(), create_addr_from_peer(), handle_request_subscribe(), init_acf_query(), load_config(), 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 384 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 247 of file stringfields.h.
Referenced by __sip_destroy(), ast_channel_destructor(), ast_custom_function_unregister(), ast_dummy_channel_destructor(), ast_manager_unregister(), ast_unregister_application(), ast_unregister_groups(), build_user(), caldav_destructor(), calendar_destructor(), calendar_event_destructor(), delete_extension(), delete_file(), destroy_jack_data(), destroy_mailbox_mapping(), destroy_queue(), destroy_session_details(), destroy_station(), destroy_trunk(), ewscal_destructor(), exception_store_free(), exchangecal_destructor(), free_acf_query(), free_outgoing(), icalendar_destructor(), init_acf_query(), load_module(), peer_destructor(), profile_destructor(), pvt_destructor(), realtime_multi_odbc(), realtime_odbc(), route_destructor(), sip_destroy_peer(), sip_monitor_instance_destructor(), sip_registry_destroy(), sip_subscribe_mwi_destroy(), 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 243 of file stringfields.h.
Referenced by __ast_channel_alloc_ap(), acf_retrieve_docs(), alloc_queue(), ast_calendar_event_alloc(), ast_dummy_channel_alloc(), ast_register_application2(), build_calendar(), build_peer(), build_profile(), build_route(), build_user(), caldav_load_calendar(), ewscal_load_calendar(), exchangecal_load_calendar(), ical_load_calendar(), init_acf_query(), init_outgoing(), init_pvt(), new_iax(), realtime_multi_odbc(), realtime_odbc(), session_details_new(), sip_alloc(), sip_monitor_instance_init(), 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 349 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 372 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 313 of file stringfields.h.
Referenced by reply_digest().
#define ast_string_field_set | ( | x, | |||
field, | |||||
data | ) |
Value:
do { \ ast_string_field_ptr_set(x, &(x)->field, data); \ } while (0)
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 337 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(), 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_register_application2(), ast_set_hangupsource(), ast_set_owners_and_peers(), authenticate_reply(), authenticate_request(), authenticate_verify(), begin_dial_channel(), build_calendar(), build_extension(), build_peer(), build_profile(), build_route(), build_user(), cache_get_callno_locked(), caldav_add_event(), caldav_load_calendar(), calendar_write_exec(), 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(), 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_subscribe(), iax2_call(), iax2_request(), ical_load_calendar(), icalendar_add_event(), init_acf_query(), init_pvt(), init_queue(), initreqprep(), jingle_new(), load_config(), local_call(), logger_print_normal(), mgcp_new(), misdn_facility_ie_handler(), moh_handle_digit(), monitor_dial(), nbs_new(), new_iax(), oss_new(), parse_moved_contact(), parse_ok_contact(), parse_register_contact(), pbx_builtin_raise_exception(), phone_new(), queue_set_param(), 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_poke_peer(), 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(), usbradio_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 298 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 1777 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.
01780 { 01781 struct ast_string_field_mgr *mgr; 01782 struct ast_string_field_pool *pool; 01783 struct ast_string_field_pool **pool_head; 01784 size_t pool_size_needed = sizeof(*pool) + pool_size; 01785 size_t size_to_alloc = optimal_alloc_size(struct_size + pool_size_needed); 01786 void *allocation; 01787 unsigned int x; 01788 01789 #if defined(__AST_DEBUG_MALLOC) 01790 if (!(allocation = __ast_calloc(num_structs, size_to_alloc, file, lineno, func))) { 01791 return NULL; 01792 } 01793 #else 01794 if (!(allocation = ast_calloc(num_structs, size_to_alloc))) { 01795 return NULL; 01796 } 01797 #endif 01798 01799 for (x = 0; x < num_structs; x++) { 01800 void *base = allocation + (size_to_alloc * x); 01801 const char **p; 01802 01803 mgr = base + field_mgr_offset; 01804 pool_head = base + field_mgr_pool_offset; 01805 pool = base + struct_size; 01806 01807 p = (const char **) pool_head + 1; 01808 while ((struct ast_string_field_mgr *) p != mgr) { 01809 *p++ = __ast_string_field_empty; 01810 } 01811 01812 mgr->embedded_pool = pool; 01813 *pool_head = pool; 01814 pool->size = size_to_alloc - struct_size - sizeof(*pool); 01815 #if defined(__AST_DEBUG_MALLOC) 01816 mgr->owner_file = file; 01817 mgr->owner_func = func; 01818 mgr->owner_line = lineno; 01819 #endif 01820 } 01821 01822 return allocation; 01823 }
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 1613 of file utils.c.
References add_string_pool(), AST_STRING_FIELD_ALLOCATION, and ast_string_field_mgr::last_alloc.
Referenced by __ast_string_field_ptr_build_va().
01615 { 01616 char *result = NULL; 01617 size_t space = (*pool_head)->size - (*pool_head)->used; 01618 size_t to_alloc = needed + sizeof(ast_string_field_allocation); 01619 01620 /* This +1 accounts for alignment on SPARC */ 01621 if (__builtin_expect(to_alloc + 1 > space, 0)) { 01622 size_t new_size = (*pool_head)->size; 01623 01624 while (new_size < to_alloc) { 01625 new_size *= 2; 01626 } 01627 01628 #if defined(__AST_DEBUG_MALLOC) 01629 if (add_string_pool(mgr, pool_head, new_size, mgr->owner_file, mgr->owner_line, mgr->owner_func)) 01630 return NULL; 01631 #else 01632 if (add_string_pool(mgr, pool_head, new_size, __FILE__, __LINE__, __FUNCTION__)) 01633 return NULL; 01634 #endif 01635 } 01636 01637 result = (*pool_head)->base + (*pool_head)->used; 01638 #ifdef __sparc__ 01639 /* SPARC requires that the allocation field be aligned. */ 01640 if ((long) result % sizeof(ast_string_field_allocation)) { 01641 result++; 01642 (*pool_head)->used++; 01643 } 01644 #endif 01645 (*pool_head)->used += to_alloc; 01646 (*pool_head)->active += needed; 01647 result += sizeof(ast_string_field_allocation); 01648 AST_STRING_FIELD_ALLOCATION(result) = needed; 01649 mgr->last_alloc = result; 01650 01651 return result; 01652 }
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 1546 of file utils.c.
References 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.
01548 { 01549 const char **p = (const char **) pool_head + 1; 01550 struct ast_string_field_pool *cur = NULL; 01551 struct ast_string_field_pool *preserve = NULL; 01552 01553 /* clear fields - this is always necessary */ 01554 while ((struct ast_string_field_mgr *) p != mgr) { 01555 *p++ = __ast_string_field_empty; 01556 } 01557 01558 mgr->last_alloc = NULL; 01559 #if defined(__AST_DEBUG_MALLOC) 01560 mgr->owner_file = file; 01561 mgr->owner_func = func; 01562 mgr->owner_line = lineno; 01563 #endif 01564 if (needed > 0) { /* allocate the initial pool */ 01565 *pool_head = NULL; 01566 mgr->embedded_pool = NULL; 01567 return add_string_pool(mgr, pool_head, needed, file, lineno, func); 01568 } 01569 01570 /* if there is an embedded pool, we can't actually release *all* 01571 * pools, we must keep the embedded one. if the caller is about 01572 * to free the structure that contains the stringfield manager 01573 * and embedded pool anyway, it will be freed as part of that 01574 * operation. 01575 */ 01576 if ((needed < 0) && mgr->embedded_pool) { 01577 needed = 0; 01578 } 01579 01580 if (needed < 0) { /* reset all pools */ 01581 cur = *pool_head; 01582 } else if (mgr->embedded_pool) { /* preserve the embedded pool */ 01583 preserve = mgr->embedded_pool; 01584 cur = *pool_head; 01585 } else { /* preserve the last pool */ 01586 if (*pool_head == NULL) { 01587 ast_log(LOG_WARNING, "trying to reset empty pool\n"); 01588 return -1; 01589 } 01590 preserve = *pool_head; 01591 cur = preserve->prev; 01592 } 01593 01594 if (preserve) { 01595 preserve->prev = NULL; 01596 preserve->used = preserve->active = 0; 01597 } 01598 01599 while (cur) { 01600 struct ast_string_field_pool *prev = cur->prev; 01601 01602 if (cur != preserve) { 01603 ast_free(cur); 01604 } 01605 cur = prev; 01606 } 01607 01608 *pool_head = preserve; 01609 01610 return 0; 01611 }
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 1762 of file utils.c.
References __ast_string_field_ptr_build_va().
01765 { 01766 va_list ap1, ap2; 01767 01768 va_start(ap1, format); 01769 va_start(ap2, format); /* va_copy does not exist on FreeBSD */ 01770 01771 __ast_string_field_ptr_build_va(mgr, pool_head, ptr, format, ap1, ap2); 01772 01773 va_end(ap1); 01774 va_end(ap2); 01775 }
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 1697 of file utils.c.
References __ast_string_field_alloc_space(), __ast_string_field_empty, __ast_string_field_release_active(), AST_STRING_FIELD_ALLOCATION, available(), and ast_string_field_mgr::last_alloc.
Referenced by __ast_string_field_ptr_build().
01700 { 01701 size_t needed; 01702 size_t available; 01703 size_t space = (*pool_head)->size - (*pool_head)->used; 01704 ssize_t grow; 01705 char *target; 01706 01707 /* if the field already has space allocated, try to reuse it; 01708 otherwise, try to use the empty space at the end of the current 01709 pool 01710 */ 01711 if (*ptr != __ast_string_field_empty) { 01712 target = (char *) *ptr; 01713 available = AST_STRING_FIELD_ALLOCATION(*ptr); 01714 if (*ptr == mgr->last_alloc) { 01715 available += space; 01716 } 01717 } else { 01718 target = (*pool_head)->base + (*pool_head)->used + sizeof(ast_string_field_allocation); 01719 #ifdef __sparc__ 01720 if ((long) target % sizeof(ast_string_field_allocation)) { 01721 target++; 01722 space--; 01723 } 01724 #endif 01725 available = space - sizeof(ast_string_field_allocation); 01726 } 01727 01728 needed = vsnprintf(target, available, format, ap1) + 1; 01729 01730 va_end(ap1); 01731 01732 if (needed > available) { 01733 /* the allocation could not be satisfied using the field's current allocation 01734 (if it has one), or the space available in the pool (if it does not). allocate 01735 space for it, adding a new string pool if necessary. 01736 */ 01737 if (!(target = (char *) __ast_string_field_alloc_space(mgr, pool_head, needed))) { 01738 return; 01739 } 01740 vsprintf(target, format, ap2); 01741 __ast_string_field_release_active(*pool_head, *ptr); 01742 *ptr = target; 01743 } else if (*ptr != target) { 01744 /* the allocation was satisfied using available space in the pool, but not 01745 using the space already allocated to the field 01746 */ 01747 __ast_string_field_release_active(*pool_head, *ptr); 01748 mgr->last_alloc = *ptr = target; 01749 AST_STRING_FIELD_ALLOCATION(target) = needed; 01750 (*pool_head)->used += needed + sizeof(ast_string_field_allocation); 01751 (*pool_head)->active += needed; 01752 } else if ((grow = (needed - AST_STRING_FIELD_ALLOCATION(*ptr))) > 0) { 01753 /* the allocation was satisfied by using available space in the pool *and* 01754 the field was the last allocated field from the pool, so it grew 01755 */ 01756 (*pool_head)->used += grow; 01757 (*pool_head)->active += grow; 01758 AST_STRING_FIELD_ALLOCATION(*ptr) += grow; 01759 } 01760 }
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 1654 of file utils.c.
References AST_STRING_FIELD_ALLOCATION, and ast_string_field_mgr::last_alloc.
01657 { 01658 ssize_t grow = needed - AST_STRING_FIELD_ALLOCATION(*ptr); 01659 size_t space = (*pool_head)->size - (*pool_head)->used; 01660 01661 if (*ptr != mgr->last_alloc) { 01662 return 1; 01663 } 01664 01665 if (space < grow) { 01666 return 1; 01667 } 01668 01669 (*pool_head)->used += grow; 01670 (*pool_head)->active += grow; 01671 AST_STRING_FIELD_ALLOCATION(*ptr) += grow; 01672 01673 return 0; 01674 }
void __ast_string_field_release_active | ( | struct ast_string_field_pool * | pool_head, | |
const ast_string_field | ptr | |||
) |
Definition at line 1676 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().
01678 { 01679 struct ast_string_field_pool *pool, *prev; 01680 01681 if (ptr == __ast_string_field_empty) { 01682 return; 01683 } 01684 01685 for (pool = pool_head, prev = NULL; pool; prev = pool, pool = pool->prev) { 01686 if ((ptr >= pool->base) && (ptr <= (pool->base + pool->size))) { 01687 pool->active -= AST_STRING_FIELD_ALLOCATION(ptr); 01688 if ((pool->active == 0) && prev) { 01689 prev->prev = pool->prev; 01690 ast_free(pool); 01691 } 01692 break; 01693 } 01694 } 01695 }
const char* __ast_string_field_empty |
Definition at line 1491 of file utils.c.
Referenced by __ast_calloc_with_stringfields(), and __ast_string_field_ptr_build_va().