![]() |
Home page |
Mailing list |
Docs
Asterisk developer's documentation :: Codename Pineapple
stringfields.h File Reference
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(name); AST_STRING_FIELD(address); AST_STRING_FIELD(password); ); long x2; };
When an instance of this structure is allocated, the fields (and the pool of storage for them) must be initialized:
struct sample_fields *sample; sample = calloc(1, sizeof(*sample)); if (sample) { if (ast_string_field_init(sample, 256)) { free(sample); sample = NULL; } } if (!sample) { ... }
Fields will default to pointing to an empty string, and will revert to that when ast_string_field_free() is called. This means that a string field will never contain NULL.
Using the fields is much like using regular 'char *' fields in the structure, except that writing into them must be done using wrapper macros defined in this file.
Storing simple values into fields can be done using ast_string_field_set(); more complex values (using printf-style format strings) can be stored using ast_string_field_build().
When the structure instance is no longer needed, the fields and their storage pool must be freed:
ast_string_field_free_all(sample); free(sample);
Definition in file stringfields.h.
#include <string.h>
#include <stdarg.h>
#include <stddef.h>
#include "asterisk/inline_api.h"
#include "asterisk/compiler.h"
#include "asterisk/compat.h"
Include dependency graph for stringfields.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.
Data Structures | |
| struct | ast_string_field_mgr |
| struct | ast_string_field_pool |
Defines | |
| #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_build(x, field, fmt, args...) ast_string_field_index_build(x, ast_string_field_index(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_index_build_va(x, ast_string_field_index(x, field), fmt, args1, args2) |
| Set a field to a complex (built) value. | |
| #define | ast_string_field_count(x) (offsetof(typeof(*(x)), __end_field) - offsetof(typeof(*(x)), __begin_field)) / sizeof(ast_string_field) |
| Get the number of string fields in a structure. | |
| #define | ast_string_field_free(x, field) ast_string_field_index_free(x, ast_string_field_index(x, field)) |
| Free a field's value. | |
| #define | ast_string_field_free_all(x) |
| Free the stringfields in a structure. | |
| #define | ast_string_field_free_pools(x) |
| Free the stringfield storage pools attached to a structure. | |
| #define | ast_string_field_index(x, field) (offsetof(typeof(*x), field) - offsetof(typeof(*x), __begin_field)) / sizeof(ast_string_field) |
| Get the index of a field in a structure. | |
| #define | ast_string_field_index_build(x, index, fmt, args...) __ast_string_field_index_build(&(x)->__field_mgr, &(x)->__begin_field[0], ast_string_field_count(x), index, fmt, args) |
| Set a field to a complex (built) value. | |
| #define | ast_string_field_index_build_va(x, index, fmt, args1, args2) __ast_string_field_index_build_va(&(x)->__field_mgr, &(x)->__begin_field[0], ast_string_field_count(x), index, fmt, args1, args2) |
| Set a field to a complex (built) value with prebuilt va_lists. | |
| #define | ast_string_field_index_free(x, index) |
| Free a field's value. | |
| #define | ast_string_field_index_set(x, index, data) |
| Set a field to a simple string value. | |
| #define | ast_string_field_init(x, size) __ast_string_field_init(&(x)->__field_mgr, size, &(x)->__begin_field[0], ast_string_field_count(x)) |
| Initialize a field pool and fields. | |
| #define | ast_string_field_set(x, field, data) ast_string_field_index_set(x, ast_string_field_index(x, field), data) |
| Set a field to a simple string value. | |
Typedefs | |
| typedef const char * | ast_string_field |
Functions | |
| ast_string_field | __ast_string_field_alloc_space (struct ast_string_field_mgr *mgr, size_t needed, ast_string_field *fields, int num_fields) |
| void | __ast_string_field_index_build (struct ast_string_field_mgr *mgr, ast_string_field *fields, int num_fields, int index, const char *format,...) |
| void | __ast_string_field_index_build_va (struct ast_string_field_mgr *mgr, ast_string_field *fields, int num_fields, int index, const char *format, va_list a1, va_list a2) |
| int | __ast_string_field_init (struct ast_string_field_mgr *mgr, size_t size, ast_string_field *fields, int num_fields) |
Variables | |
| const char | __ast_string_field_empty [] |
|
|
Value: ast_string_field __begin_field[0]; \ field_list \ ast_string_field __end_field[0]; \ struct ast_string_field_mgr __field_mgr
Definition at line 196 of file stringfields.h. |
|
|
Declare a string field.
Definition at line 190 of file stringfields.h. |
|
|
Set a field to a complex (built) value.
Definition at line 314 of file stringfields.h. Referenced by build_callid_pvt(), build_callid_registry(), build_contact(), build_rpid(), build_user(), build_via(), check_auth(), create_addr_from_peer(), handle_request_subscribe(), handle_response(), handle_response_invite(), mgcp_new(), parse_moved_contact(), parse_register_contact(), sip_register(), sip_sipredirect(), and transmit_fake_auth_response(). |
|
|
Set a field to a complex (built) value.
Definition at line 325 of file stringfields.h. |
|
|
Get the number of string fields in a structure.
Definition at line 207 of file stringfields.h. |
|
|
Free a field's value.
Definition at line 352 of file stringfields.h. Referenced by check_peer_ok(), check_user_full(), handle_request_invite(), handle_response_invite(), parse_register_contact(), sip_request_call(), and transmit_register(). |
|
|
Free the stringfields in a structure.
Definition at line 381 of file stringfields.h. Referenced by transmit_response_using_temp(). |
|
|
Free the stringfield storage pools attached to a structure.
Definition at line 364 of file stringfields.h. Referenced by ast_channel_free(), build_peer(), build_user(), destroy_peer(), destroy_user(), iax2_destroy(), sip_destroy_device(), sip_registry_destroy(), and temp_pvt_cleanup(). |
|
|
Get the index of a field in a structure.
Definition at line 217 of file stringfields.h. Referenced by reply_digest(). |
|
|
Set a field to a complex (built) value.
Definition at line 291 of file stringfields.h. |
|
|
Set a field to a complex (built) value with prebuilt va_lists.
Definition at line 303 of file stringfields.h. |
|
|
Value: do { \ (x)->__begin_field[index] = __ast_string_field_empty; \ } while(0)
Definition at line 338 of file stringfields.h. |
|
|
Set a field to a simple string value.
Definition at line 236 of file stringfields.h. Referenced by reply_digest(). |
|
|
Initialize a field pool and fields.
Definition at line 226 of file stringfields.h. Referenced by ast_channel_alloc(), build_device(), build_peer(), build_user(), new_iax(), sip_alloc(), sip_register(), temp_pvt_init(), and transmit_response_using_temp(). |
|
|
|
Definition at line 100 of file stringfields.h. |
|
||||||||||||||||||||
|
Definition at line 874 of file utils.c. References add_string_pool(), ast_string_field_pool::base, ast_string_field_mgr::pool, ast_string_field_mgr::size, ast_string_field_mgr::space, and ast_string_field_mgr::used. 00876 { 00877 char *result = NULL; 00878 00879 if (__builtin_expect(needed > mgr->space, 0)) { 00880 size_t new_size = mgr->size * 2; 00881 00882 while (new_size < needed) 00883 new_size *= 2; 00884 00885 if (add_string_pool(mgr, new_size)) 00886 return NULL; 00887 } 00888 00889 result = mgr->pool->base + mgr->used; 00890 mgr->used += needed; 00891 mgr->space -= needed; 00892 return result; 00893 }
|
|
||||||||||||||||||||||||||||
|
Definition at line 922 of file utils.c. References __ast_string_field_index_build_va(). 00925 { 00926 va_list ap1, ap2; 00927 00928 va_start(ap1, format); 00929 va_start(ap2, format); /* va_copy does not exist on FreeBSD */ 00930 00931 __ast_string_field_index_build_va(mgr, fields, num_fields, index, format, ap1, ap2); 00932 00933 va_end(ap1); 00934 va_end(ap2); 00935 }
|
|
||||||||||||||||||||||||||||||||
|
Definition at line 895 of file utils.c. References add_string_pool(), ast_string_field_pool::base, ast_string_field_mgr::pool, ast_string_field_mgr::size, ast_string_field_mgr::space, and ast_string_field_mgr::used. Referenced by __ast_string_field_index_build(). 00898 { 00899 size_t needed; 00900 00901 needed = vsnprintf(mgr->pool->base + mgr->used, mgr->space, format, ap1) + 1; 00902 00903 va_end(ap1); 00904 00905 if (needed > mgr->space) { 00906 size_t new_size = mgr->size * 2; 00907 00908 while (new_size < needed) 00909 new_size *= 2; 00910 00911 if (add_string_pool(mgr, new_size)) 00912 return; 00913 00914 vsprintf(mgr->pool->base + mgr->used, format, ap2); 00915 } 00916 00917 fields[index] = mgr->pool->base + mgr->used; 00918 mgr->used += needed; 00919 mgr->space -= needed; 00920 }
|
|
||||||||||||||||||||
|
Definition at line 860 of file utils.c. References add_string_pool(). 00862 { 00863 int index; 00864 00865 if (add_string_pool(mgr, size)) 00866 return -1; 00867 00868 for (index = 0; index < num_fields; index++) 00869 fields[index] = __ast_string_field_empty; 00870 00871 return 0; 00872 }
|
|
|
|