Asterisk - The Open Source Telephony Project  GIT-master-4a4f1a5
Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
stringfields.h File Reference
#include "asterisk/inline_api.h"
#include "asterisk/vector.h"

Go to the source code of this file.

Data Structures

struct  ast_string_field_mgr
 
struct  ast_string_field_pool
 

Macros

#define __ast_string_field_ptr_set_by_fields(field_mgr_pool, field_mgr, ptr, data, file, lineno, func)
 
#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...)
 Set a field to a complex (built) value. More...
 
#define ast_string_field_build_va(x, field, fmt, args)
 Set a field to a complex (built) value. More...
 
#define AST_STRING_FIELD_EXTENDED(name)   AST_STRING_FIELD(name)
 Declare an extended string field. More...
 
#define ast_string_field_free_memory(x)
 free all memory - to be called before destroying the object More...
 
#define ast_string_field_init(x, size)
 Initialize a field pool and fields. More...
 
#define ast_string_field_init_extended(x, field)
 Initialize an extended string field. More...
 
#define ast_string_field_ptr_build(x, ptr, fmt, args...)
 Set a field to a complex (built) value. More...
 
#define ast_string_field_ptr_build_va(x, ptr, fmt, args)
 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_ptr_set_by_fields(field_mgr_pool, field_mgr, ptr, data)    __ast_string_field_ptr_set_by_fields(field_mgr_pool, field_mgr, ptr, data, __FILE__, __LINE__, __PRETTY_FUNCTION__)
 
#define ast_string_field_set(x, field, data)
 Set a field to a simple string value. More...
 
#define ast_string_fields_cmp(instance1, instance2)
 Compare the string fields in two instances of the same structure. More...
 
#define ast_string_fields_copy(copy, orig)
 Copy all string fields from one instance to another of the same structure. More...
 

Typedefs

typedef const char * ast_string_field
 
typedef uint16_t ast_string_field_allocation
 

Enumerations

enum  ast_stringfield_cleanup_type { AST_STRINGFIELD_RESET = 0 , AST_STRINGFIELD_DESTROY = -1 }
 

Functions

void * __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, const char *file, int lineno, const char *func)
 
int __ast_string_field_free_memory (struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head, enum ast_stringfield_cleanup_type cleanup_type, const char *file, int lineno, const char *func)
 Internal cleanup function. More...
 
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)
 Internal initialization function. More...
 
void __ast_string_field_ptr_build (const char *file, int lineno, const char *func, 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 ap, const char *file, int lineno, const char *func)
 
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)
 
int __ast_string_fields_cmp (struct ast_string_field_vector *left, struct ast_string_field_vector *right)
 
int __ast_string_fields_copy (struct ast_string_field_pool *copy_pool, struct ast_string_field_mgr *copy_mgr, struct ast_string_field_mgr *orig_mgr, const char *file, int lineno, const char *func)
 
 AST_VECTOR (ast_string_field_vector, const char **)
 

Variables

const char * __ast_string_field_empty
 

Macro Definition Documentation

◆ __ast_string_field_ptr_set_by_fields

#define __ast_string_field_ptr_set_by_fields (   field_mgr_pool,
  field_mgr,
  ptr,
  data,
  file,
  lineno,
  func 
)

Definition at line 479 of file stringfields.h.

◆ ast_calloc_with_stringfields

#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__)
static const char type[]
Definition: chan_ooh323.c:109
void * __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: stringfields.c:380

Allocate a structure with embedded stringfields in a single allocation.

Parameters
nCurrent implementation only allows 1 structure to be allocated
typeThe type of structure to allocate
sizeThe number of bytes of space (minimum) to allocate for stringfields to use in each structure

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.

Since
1.8

Definition at line 426 of file stringfields.h.

◆ AST_DECLARE_STRING_FIELDS

#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

Declare the fields needed in a structure.

Parameters
field_listThe 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.
Examples
app_skel.c.

Definition at line 337 of file stringfields.h.

◆ AST_STRING_FIELD

#define AST_STRING_FIELD (   name)    const ast_string_field name

Declare a string field.

Parameters
nameThe field name
Examples
app_skel.c.

Definition at line 299 of file stringfields.h.

◆ AST_STRING_FIELD_ALLOCATION

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

Parameters
xPointer 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 460 of file stringfields.h.

◆ ast_string_field_build

#define ast_string_field_build (   x,
  field,
  fmt,
  args... 
)

Set a field to a complex (built) value.

Parameters
xPointer to a structure containing fields
fieldName of the field to set
fmtprintf-style format string
argsArguments for format string
Returns
nothing

Definition at line 550 of file stringfields.h.

◆ ast_string_field_build_va

#define ast_string_field_build_va (   x,
  field,
  fmt,
  args 
)

Set a field to a complex (built) value.

Parameters
xPointer to a structure containing fields
fieldName of the field to set
fmtprintf-style format string
argsArguments for format string in va_list format
Returns
nothing

Definition at line 588 of file stringfields.h.

◆ AST_STRING_FIELD_EXTENDED

#define AST_STRING_FIELD_EXTENDED (   name)    AST_STRING_FIELD(name)

Declare an extended string field.

Since
13.9.0
Parameters
nameThe field name

Definition at line 307 of file stringfields.h.

◆ ast_string_field_free_memory

#define ast_string_field_free_memory (   x)

free all memory - to be called before destroying the object

Parameters
x
Examples
app_skel.c.

Definition at line 368 of file stringfields.h.

◆ ast_string_field_init

#define ast_string_field_init (   x,
  size 
)

Initialize a field pool and fields.

Parameters
xPointer to a structure containing fields
sizeAmount of storage to allocate. Use AST_STRINGFIELD_RESET to reset fields to the default value, and release all but the most recent pool. AST_STRINGFIELD_DESTROY (used internally) means free all pools which is equivalent to calling ast_string_field_free_memory.
Returns
0 on success, non-zero on failure
Examples
app_skel.c.

Definition at line 353 of file stringfields.h.

◆ ast_string_field_init_extended

#define ast_string_field_init_extended (   x,
  field 
)

Initialize an extended string field.

Since
13.9.0
Parameters
xPointer to a structure containing the field
fieldThe extended field to initialize
Return values
zeroon success
non-zeroon error
Note
This macro must be called on ALL fields defined with AST_STRING_FIELD_EXTENDED after ast_string_field_init has been called.

Definition at line 395 of file stringfields.h.

◆ ast_string_field_ptr_build

#define ast_string_field_ptr_build (   x,
  ptr,
  fmt,
  args... 
)

Set a field to a complex (built) value.

Parameters
xPointer to a structure containing fields
ptrPointer to a field within the structure
fmtprintf-style format string
argsArguments for format string
Returns
nothing

Definition at line 531 of file stringfields.h.

◆ ast_string_field_ptr_build_va

#define ast_string_field_ptr_build_va (   x,
  ptr,
  fmt,
  args 
)

Set a field to a complex (built) value with prebuilt va_lists.

Parameters
xPointer to a structure containing fields
ptrPointer to a field within the structure
fmtprintf-style format string
argsArguments for format string in va_list format
Returns
nothing

Definition at line 569 of file stringfields.h.

◆ ast_string_field_ptr_set

#define ast_string_field_ptr_set (   x,
  ptr,
  data 
)

Set a field to a simple string value.

Parameters
xPointer to a structure containing fields
ptrPointer to a field within the structure
dataString value to be copied into the field
Return values
zeroon success
non-zeroon error

Definition at line 470 of file stringfields.h.

◆ ast_string_field_ptr_set_by_fields

#define ast_string_field_ptr_set_by_fields (   field_mgr_pool,
  field_mgr,
  ptr,
  data 
)     __ast_string_field_ptr_set_by_fields(field_mgr_pool, field_mgr, ptr, data, __FILE__, __LINE__, __PRETTY_FUNCTION__)

Definition at line 503 of file stringfields.h.

◆ ast_string_field_set

#define ast_string_field_set (   x,
  field,
  data 
)

Set a field to a simple string value.

Parameters
xPointer to a structure containing fields
fieldName of the field to set
dataString value to be copied into the field
Return values
zeroon success
non-zeroon error
Examples
app_skel.c.

Definition at line 514 of file stringfields.h.

◆ ast_string_fields_cmp

#define ast_string_fields_cmp (   instance1,
  instance2 
)

Compare the string fields in two instances of the same structure.

Since
12
Parameters
instance1The first instance of the structure to be compared
instance2The second instance of the structure to be compared
Return values
zeroif all string fields are equal (does not compare non-string field data)
non-zeroif the values of the string fields differ

Definition at line 607 of file stringfields.h.

◆ ast_string_fields_copy

#define ast_string_fields_copy (   copy,
  orig 
)

Copy all string fields from one instance to another of the same structure.

Since
12
Parameters
copyThe instance of the structure to be copied into
origThe instance of the structure to be copied from
Return values
zeroon success
non-zeroon error

Definition at line 627 of file stringfields.h.

Typedef Documentation

◆ ast_string_field

typedef const char* ast_string_field

Definition at line 190 of file stringfields.h.

◆ ast_string_field_allocation

typedef uint16_t ast_string_field_allocation

Definition at line 194 of file stringfields.h.

Enumeration Type Documentation

◆ ast_stringfield_cleanup_type

Enumerator
AST_STRINGFIELD_RESET 

Reset all string fields and free all extra pools that may have been created The allocation or structure can be reused as is.

AST_STRINGFIELD_DESTROY 

Reset all string fields and free all pools. If the pointer was returned by ast_calloc_with_stringfields, it can NOT be reused and should be immediately freed. Otherwise, you must call ast_string_field_init again if you want to reuse it.

Definition at line 309 of file stringfields.h.

309  {
310  /*!
311  * Reset all string fields and free all extra pools that may have been created
312  * The allocation or structure can be reused as is.
313  */
315  /*!
316  * Reset all string fields and free all pools.
317  * If the pointer was returned by ast_calloc_with_stringfields, it can NOT be reused
318  * and should be immediately freed. Otherwise, you must call ast_string_field_init
319  * again if you want to reuse it.
320  */
322 };
@ AST_STRINGFIELD_DESTROY
Definition: stringfields.h:321
@ AST_STRINGFIELD_RESET
Definition: stringfields.h:314

Function Documentation

◆ __ast_calloc_with_stringfields()

void* __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 380 of file stringfields.c.

383 {
384  struct ast_string_field_mgr *mgr;
385  struct ast_string_field_pool *pool;
386  struct ast_string_field_pool **pool_head;
387  size_t pool_size_needed = sizeof(*pool) + pool_size;
388  size_t size_to_alloc = optimal_alloc_size(struct_size + pool_size_needed);
389  void *allocation;
390  const char **p;
391  size_t initial_vector_size;
392 
393  ast_assert(num_structs == 1);
394 
395  allocation = __ast_calloc(num_structs, size_to_alloc, file, lineno, func);
396  if (!allocation) {
397  return NULL;
398  }
399 
400  mgr = allocation + field_mgr_offset;
401 
402  pool = allocation + struct_size;
403  pool_head = allocation + field_mgr_pool_offset;
404  p = (const char **) pool_head + 1;
405  initial_vector_size = ((size_t) (((char *)mgr) - ((char *)p))) / sizeof(*p);
406 
407  if (AST_VECTOR_INIT(&mgr->string_fields, initial_vector_size)) {
409  return NULL;
410  }
411 
412  while ((struct ast_string_field_mgr *) p != mgr) {
415  }
416 
417  mgr->embedded_pool = pool;
418  *pool_head = pool;
419  pool->size = size_to_alloc - struct_size - sizeof(*pool);
420 
421  return allocation;
422 }
#define ast_free(a)
Definition: astmm.h:182
void * __ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func) attribute_malloc
Definition: astmm.c:1635
#define NULL
Definition: resample.c:96
ast_string_field_allocation allocation
Definition: stringfields.c:39
ast_string_field __ast_string_field_empty
Definition: stringfields.c:43
static size_t optimal_alloc_size(size_t size)
Definition: stringfields.c:47
struct ast_string_field_vector string_fields
Definition: stringfields.h:230
struct ast_string_field_pool * embedded_pool
Definition: stringfields.h:229
#define ast_assert(a)
Definition: utils.h:710
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
Definition: vector.h:113
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition: vector.h:256

References __ast_calloc(), __ast_string_field_empty, allocation, ast_assert, ast_free, AST_VECTOR_APPEND, AST_VECTOR_INIT, ast_string_field_mgr::embedded_pool, make_ari_stubs::file, NULL, optimal_alloc_size(), ast_string_field_pool::size, and ast_string_field_mgr::string_fields.

◆ __ast_string_field_alloc_space()

ast_string_field __ast_string_field_alloc_space ( struct ast_string_field_mgr mgr,
struct ast_string_field_pool **  pool_head,
size_t  needed,
const char *  file,
int  lineno,
const char *  func 
)

Definition at line 202 of file stringfields.c.

205 {
206  char *result = NULL;
207  size_t space = (*pool_head)->size - (*pool_head)->used;
208  size_t to_alloc;
209 
210  /* Make room for ast_string_field_allocation and make it a multiple of that. */
213 
214  if (__builtin_expect(to_alloc > space, 0)) {
215  size_t new_size = (*pool_head)->size;
216 
217  while (new_size < to_alloc) {
218  new_size *= 2;
219  }
220 
221  if (add_string_pool(mgr, pool_head, new_size, file, lineno, func)) {
222  return NULL;
223  }
224  }
225 
226  /* pool->base is always aligned (gcc aligned attribute). We ensure that
227  * to_alloc is also a multiple of ast_alignof(ast_string_field_allocation)
228  * causing result to always be aligned as well; which in turn fixes that
229  * AST_STRING_FIELD_ALLOCATION(result) is aligned. */
230  result = (*pool_head)->base + (*pool_head)->used;
231  (*pool_head)->used += to_alloc;
232  (*pool_head)->active += needed;
235  mgr->last_alloc = result;
236 
237  return result;
238 }
static PGresult * result
Definition: cel_pgsql.c:88
static int add_string_pool(struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head, size_t size, const char *file, int lineno, const char *func)
add a new block to the pool. We can only allocate from the topmost pool, so the fields in *mgr reflec...
Definition: stringfields.c:62
uint16_t ast_string_field_allocation
Definition: stringfields.h:194
#define AST_STRING_FIELD_ALLOCATION(x)
Macro to provide access to the allocation field that lives immediately in front of a string field.
Definition: stringfields.h:460
ast_string_field last_alloc
Definition: stringfields.h:228
#define ast_make_room_for(offset, type)
Increase offset by the required alignment of type and make sure it is a multiple of said alignment.
Definition: utils.h:781
#define ast_alignof(type)
Return the number of bytes used in the alignment of type.
Definition: utils.h:738

References add_string_pool(), ast_alignof, ast_assert, ast_make_room_for, AST_STRING_FIELD_ALLOCATION, make_ari_stubs::file, ast_string_field_mgr::last_alloc, NULL, and result.

Referenced by __ast_string_field_ptr_build_va().

◆ __ast_string_field_free_memory()

int __ast_string_field_free_memory ( struct ast_string_field_mgr mgr,
struct ast_string_field_pool **  pool_head,
enum ast_stringfield_cleanup_type  cleanup_type,
const char *  file,
int  lineno,
const char *  func 
)

Internal cleanup function.

Definition at line 103 of file stringfields.c.

106 {
107  struct ast_string_field_pool *cur = NULL;
108  struct ast_string_field_pool *preserve = NULL;
109 
110  /* reset all the fields regardless of cleanup type */
112 
113  switch (cleanup_type) {
116 
117  if (mgr->embedded_pool) { /* ALWAYS preserve the embedded pool if there is one */
118  preserve = mgr->embedded_pool;
119  preserve->used = preserve->active = 0;
120  }
121 
122  break;
124  /* Preserve the embedded pool if there is one, otherwise the last pool */
125  if (mgr->embedded_pool) {
126  preserve = mgr->embedded_pool;
127  } else {
128  if (*pool_head == NULL) {
129  ast_log(LOG_WARNING, "trying to reset empty pool\n");
130  return -1;
131  }
132  preserve = *pool_head;
133  }
134  preserve->used = preserve->active = 0;
135  break;
136  default:
137  return -1;
138  }
139 
140  cur = *pool_head;
141  while (cur) {
142  struct ast_string_field_pool *prev = cur->prev;
143 
144  if (cur != preserve) {
145  ast_free(cur);
146  }
147  cur = prev;
148  }
149 
150  *pool_head = preserve;
151  if (preserve) {
152  preserve->prev = NULL;
153  }
154 
155  return 0;
156 }
#define ast_log
Definition: astobj2.c:42
#define LOG_WARNING
Definition: logger.h:275
static void reset_field(const char **p)
Definition: stringfields.c:81
struct ast_string_field_pool * prev
Definition: stringfields.h:209
#define AST_VECTOR_FREE(vec)
Deallocates this vector.
Definition: vector.h:174
#define AST_VECTOR_CALLBACK_VOID(vec, callback,...)
Execute a callback on every element in a vector disregarding callback return.
Definition: vector.h:865

References ast_string_field_pool::active, ast_free, ast_log, AST_STRINGFIELD_DESTROY, AST_STRINGFIELD_RESET, AST_VECTOR_CALLBACK_VOID, AST_VECTOR_FREE, ast_string_field_mgr::embedded_pool, LOG_WARNING, NULL, ast_string_field_pool::prev, reset_field(), ast_string_field_mgr::string_fields, and ast_string_field_pool::used.

Referenced by __ast_string_field_init().

◆ __ast_string_field_init()

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 
)

Internal initialization function.

Definition at line 171 of file stringfields.c.

173 {
174  const char **p = (const char **) pool_head + 1;
175  size_t initial_vector_size = ((size_t) (((char *)mgr) - ((char *)p))) / sizeof(*p);
176 
177  if (needed <= 0) {
178  return __ast_string_field_free_memory(mgr, pool_head, needed, file, lineno, func);
179  }
180 
181  mgr->last_alloc = NULL;
182 
183  if (AST_VECTOR_INIT(&mgr->string_fields, initial_vector_size)) {
184  return -1;
185  }
186 
187  while ((struct ast_string_field_mgr *) p != mgr) {
190  }
191 
192  *pool_head = NULL;
193  mgr->embedded_pool = NULL;
194  if (add_string_pool(mgr, pool_head, needed, file, lineno, func)) {
196  return -1;
197  }
198 
199  return 0;
200 }
int __ast_string_field_free_memory(struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head, enum ast_stringfield_cleanup_type cleanup_type, const char *file, int lineno, const char *func)
Internal cleanup function.
Definition: stringfields.c:103

References __ast_string_field_empty, __ast_string_field_free_memory(), add_string_pool(), AST_VECTOR_APPEND, AST_VECTOR_FREE, AST_VECTOR_INIT, ast_string_field_mgr::embedded_pool, make_ari_stubs::file, ast_string_field_mgr::last_alloc, NULL, and ast_string_field_mgr::string_fields.

◆ __ast_string_field_ptr_build()

void __ast_string_field_ptr_build ( const char *  file,
int  lineno,
const char *  func,
struct ast_string_field_mgr mgr,
struct ast_string_field_pool **  pool_head,
ast_string_field ptr,
const char *  format,
  ... 
)

Definition at line 369 of file stringfields.c.

372 {
373  va_list ap;
374 
375  va_start(ap, format);
376  __ast_string_field_ptr_build_va(mgr, pool_head, ptr, format, ap, file, lineno, func);
377  va_end(ap);
378 }
static snd_pcm_format_t format
Definition: chan_alsa.c:106
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 ap, const char *file, int lineno, const char *func)
Definition: stringfields.c:286

References __ast_string_field_ptr_build_va(), make_ari_stubs::file, and format.

◆ __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  ap,
const char *  file,
int  lineno,
const char *  func 
)

Definition at line 286 of file stringfields.c.

290 {
291  size_t needed;
292  size_t available;
293  size_t space = (*pool_head)->size - (*pool_head)->used;
294  int res;
295  ssize_t grow;
296  char *target;
297  va_list ap2;
298 
299  /* if the field already has space allocated, try to reuse it;
300  otherwise, try to use the empty space at the end of the current
301  pool
302  */
303  if (*ptr != __ast_string_field_empty) {
304  target = (char *) *ptr;
306  if (*ptr == mgr->last_alloc) {
307  available += space;
308  }
309  } else {
310  /* pool->used is always a multiple of ast_alignof(ast_string_field_allocation)
311  * so we don't need to re-align anything here.
312  */
313  target = (*pool_head)->base + (*pool_head)->used + ast_alignof(ast_string_field_allocation);
316  } else {
317  available = 0;
318  }
319  }
320 
321  va_copy(ap2, ap);
322  res = vsnprintf(target, available, format, ap2);
323  va_end(ap2);
324 
325  if (res < 0) {
326  /* Are we out of memory? */
327  return;
328  }
329  if (res == 0) {
330  __ast_string_field_release_active(*pool_head, *ptr);
332  return;
333  }
334  needed = (size_t)res + 1; /* NUL byte */
335 
336  if (needed > available) {
337  /* the allocation could not be satisfied using the field's current allocation
338  (if it has one), or the space available in the pool (if it does not). allocate
339  space for it, adding a new string pool if necessary.
340  */
341  target = (char *) __ast_string_field_alloc_space(mgr, pool_head, needed, file, lineno, func);
342  if (!target) {
343  return;
344  }
345  vsprintf(target, format, ap);
346  va_end(ap); /* XXX va_end without va_start? */
347  __ast_string_field_release_active(*pool_head, *ptr);
348  *ptr = target;
349  } else if (*ptr != target) {
350  /* the allocation was satisfied using available space in the pool, but not
351  using the space already allocated to the field
352  */
353  __ast_string_field_release_active(*pool_head, *ptr);
354  mgr->last_alloc = *ptr = target;
357  (*pool_head)->used += ast_make_room_for(needed, ast_string_field_allocation);
358  (*pool_head)->active += needed;
359  } else if ((grow = (needed - AST_STRING_FIELD_ALLOCATION(*ptr))) > 0) {
360  /* the allocation was satisfied by using available space in the pool *and*
361  the field was the last allocated field from the pool, so it grew
362  */
364  (*pool_head)->used += ast_align_for(grow, ast_string_field_allocation);
365  (*pool_head)->active += grow;
366  }
367 }
static int available(struct dahdi_pvt **pvt, int is_specific_channel)
Definition: chan_dahdi.c:13027
void __ast_string_field_release_active(struct ast_string_field_pool *pool_head, const ast_string_field ptr)
Definition: stringfields.c:261
ast_string_field __ast_string_field_alloc_space(struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head, size_t needed, const char *file, int lineno, const char *func)
Definition: stringfields.c:202
static void grow(struct ast_threadpool *pool, int delta)
Add threads to the threadpool.
Definition: threadpool.c:524
#define ast_align_for(offset, type)
Increase offset so it is a multiple of the required alignment of type.
Definition: utils.h:758

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(), make_ari_stubs::file, format, grow(), and ast_string_field_mgr::last_alloc.

Referenced by __ast_string_field_ptr_build().

◆ __ast_string_field_ptr_grow()

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 240 of file stringfields.c.

242 {
243  ssize_t grow = needed - AST_STRING_FIELD_ALLOCATION(*ptr);
244  size_t space = (*pool_head)->size - (*pool_head)->used;
245 
246  if (*ptr != mgr->last_alloc) {
247  return 1;
248  }
249 
250  if (space < grow) {
251  return 1;
252  }
253 
254  (*pool_head)->used += grow;
255  (*pool_head)->active += grow;
257 
258  return 0;
259 }

References AST_STRING_FIELD_ALLOCATION, grow(), and ast_string_field_mgr::last_alloc.

◆ __ast_string_field_release_active()

void __ast_string_field_release_active ( struct ast_string_field_pool pool_head,
const ast_string_field  ptr 
)

Definition at line 261 of file stringfields.c.

263 {
264  struct ast_string_field_pool *pool, *prev;
265 
266  if (ptr == __ast_string_field_empty) {
267  return;
268  }
269 
270  for (pool = pool_head, prev = NULL; pool; prev = pool, pool = pool->prev) {
271  if ((ptr >= pool->base) && (ptr <= (pool->base + pool->size))) {
272  pool->active -= AST_STRING_FIELD_ALLOCATION(ptr);
273  if (pool->active == 0) {
274  if (prev) {
275  prev->prev = pool->prev;
276  ast_free(pool);
277  } else {
278  pool->used = 0;
279  }
280  }
281  break;
282  }
283  }
284 }

References __ast_string_field_empty, ast_string_field_pool::active, ast_free, AST_STRING_FIELD_ALLOCATION, ast_string_field_pool::base, NULL, ast_string_field_pool::prev, ast_string_field_pool::size, and ast_string_field_pool::used.

Referenced by __ast_string_field_ptr_build_va(), and __ast_string_fields_copy().

◆ __ast_string_fields_cmp()

int __ast_string_fields_cmp ( struct ast_string_field_vector *  left,
struct ast_string_field_vector *  right 
)

Definition at line 424 of file stringfields.c.

426 {
427  int i;
428  int res = 0;
429 
430  ast_assert(AST_VECTOR_SIZE(left) == AST_VECTOR_SIZE(right));
431 
432  for (i = 0; i < AST_VECTOR_SIZE(left); i++) {
433  if ((res = strcmp(*AST_VECTOR_GET(left, i), *AST_VECTOR_GET(right, i)))) {
434  return res;
435  }
436  }
437 
438  return res;
439 }
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:611
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:682

References ast_assert, AST_VECTOR_GET, and AST_VECTOR_SIZE.

◆ __ast_string_fields_copy()

int __ast_string_fields_copy ( struct ast_string_field_pool copy_pool,
struct ast_string_field_mgr copy_mgr,
struct ast_string_field_mgr orig_mgr,
const char *  file,
int  lineno,
const char *  func 
)

Definition at line 441 of file stringfields.c.

444 {
445  int i;
446  struct ast_string_field_vector *dest = &(copy_mgr->string_fields);
447  struct ast_string_field_vector *src = &(orig_mgr->string_fields);
448 
450 
451  for (i = 0; i < AST_VECTOR_SIZE(dest); i++) {
454  }
455 
456  for (i = 0; i < AST_VECTOR_SIZE(dest); i++) {
457  if (__ast_string_field_ptr_set_by_fields(copy_pool, *copy_mgr, AST_VECTOR_GET(dest, i),
458  *AST_VECTOR_GET(src, i), file, lineno, func)) {
459  return -1;
460  }
461  }
462 
463  return 0;
464 }
#define __ast_string_field_ptr_set_by_fields(field_mgr_pool, field_mgr, ptr, data, file, lineno, func)
Definition: stringfields.h:479

References __ast_string_field_empty, __ast_string_field_ptr_set_by_fields, __ast_string_field_release_active(), ast_assert, AST_VECTOR_GET, AST_VECTOR_SIZE, make_ari_stubs::file, and ast_string_field_mgr::string_fields.

◆ AST_VECTOR()

AST_VECTOR ( ast_string_field_vector  ,
const char **   
)

Variable Documentation

◆ __ast_string_field_empty

const char* __ast_string_field_empty
extern