[asterisk-commits] rizzo: branch rizzo/video_v2 r87809 - in /team/rizzo/video_v2: channels/ incl...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Wed Oct 31 08:47:16 CDT 2007
Author: rizzo
Date: Wed Oct 31 08:47:15 2007
New Revision: 87809
URL: http://svn.digium.com/view/asterisk?view=rev&rev=87809
Log:
more stringfields simplifications
Modified:
team/rizzo/video_v2/channels/chan_sip.c
team/rizzo/video_v2/include/asterisk/stringfields.h
team/rizzo/video_v2/main/utils.c
Modified: team/rizzo/video_v2/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/rizzo/video_v2/channels/chan_sip.c?view=diff&rev=87809&r1=87808&r2=87809
==============================================================================
--- team/rizzo/video_v2/channels/chan_sip.c (original)
+++ team/rizzo/video_v2/channels/chan_sip.c Wed Oct 31 08:47:15 2007
@@ -6656,7 +6656,7 @@
__transmit_response(p, msg, req, XMIT_UNRELIABLE);
/* Free the string fields, but not the pool space */
- ast_string_field_free_strings(p);
+ ast_string_field_init(p, 0);
return 0;
}
Modified: team/rizzo/video_v2/include/asterisk/stringfields.h
URL: http://svn.digium.com/view/asterisk/team/rizzo/video_v2/include/asterisk/stringfields.h?view=diff&rev=87809&r1=87808&r2=87809
==============================================================================
--- team/rizzo/video_v2/include/asterisk/stringfields.h (original)
+++ team/rizzo/video_v2/include/asterisk/stringfields.h Wed Oct 31 08:47:15 2007
@@ -57,11 +57,13 @@
... handle error
}
\endcode
-
+
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 \b never contain NULL (this is not
needed in this code, but comes from external requirements).
+ ast_string_field_init(x, 0) can be used to reset fields to the
+ initial value while keeping the pool allocated.
Using the fields is much like using regular 'const char *' fields
in the structure, except that writing into them must be done
@@ -78,16 +80,13 @@
ast_string_field_build(sample, blah, "%d %s", zipcode, city);
ast_string_field_ptr_build(sample, &sample->blah, "%d %s", zipcode, city);
\endcode
- There are two more varargs versions of the 'build' functions,
- and ast_string_field_free_all() which is used to clear all stringfields
- to the default values (meant to be used when resetting a
- structure to its initial value).
+ There are two more varargs versions of the 'build' functions.
When the structure instance is no longer needed, the fields
and their storage pool must be freed:
\code
- ast_string_field_memory(sample);
+ ast_string_field_free_memory(sample);
ast_free(sample);
\endcode
*/
@@ -137,17 +136,6 @@
size_t size; /*!< the total size of the current pool */
size_t used; /*!< the space used in the current pool */
};
-
-/*!
- \internal
- \brief Initialize a field pool manager and fields
- \param mgr Pointer to the pool manager structure
- \param pool_head Pointer to the pool head (right before the fields)
- \param needed Amount of storage to allocate
- \return 0 on success, non-zero on failure
-*/
-int __ast_string_field_init(struct ast_string_field_mgr *mgr,
- struct ast_string_field_pool **pool_head, size_t needed);
/*!
\internal
@@ -219,11 +207,22 @@
/*!
\brief Initialize a field pool and fields
\param x Pointer to a structure containing fields
- \param size Amount of storage to allocate
+ \param 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.
\return 0 on success, non-zero on failure
*/
#define ast_string_field_init(x, size) \
__ast_string_field_init(&(x)->__field_mgr, &(x)->__field_mgr_pool, size)
+
+/*! \brief free all memory - to be called before destroying the object */
+#define ast_string_field_free_memory(x) \
+ __ast_string_field_init(&(x)->__field_mgr, &(x)->__field_mgr_pool, -1)
+
+/*! \internal \brief internal version of ast_string_field_init */
+int __ast_string_field_init(struct ast_string_field_mgr *mgr,
+ struct ast_string_field_pool **pool_head, size_t needed);
/*!
\brief Set a field to a simple string value
@@ -269,6 +268,17 @@
__ast_string_field_ptr_build(&(x)->__field_mgr, &(x)->__field_mgr_pool, ptr, fmt, args)
/*!
+ \brief Set a field to a complex (built) value
+ \param x Pointer to a structure containing fields
+ \param field Name of the field to set
+ \param fmt printf-style format string
+ \param args Arguments for format string
+ \return nothing
+*/
+#define ast_string_field_build(x, field, fmt, args...) \
+ __ast_string_field_ptr_build(&(x)->__field_mgr, &(x)->__field_mgr_pool, &(x)->field, fmt, args)
+
+/*!
\brief Set a field to a complex (built) value with prebuilt va_lists.
\param x Pointer to a structure containing fields
\param ptr Pointer to a field within the structure
@@ -279,17 +289,6 @@
*/
#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, ptr, fmt, args1, args2)
-
-/*!
- \brief Set a field to a complex (built) value
- \param x Pointer to a structure containing fields
- \param field Name of the field to set
- \param fmt printf-style format string
- \param args Arguments for format string
- \return nothing
-*/
-#define ast_string_field_build(x, field, fmt, args...) \
- __ast_string_field_ptr_build(&(x)->__field_mgr, &(x)->__field_mgr_pool, &(x)->field, fmt, args)
/*!
\brief Set a field to a complex (built) value
@@ -303,39 +302,4 @@
#define ast_string_field_build_va(x, field, fmt, args1, args2) \
__ast_string_field_ptr_build_va(&(x)->__field_mgr, &(x)->__field_mgr_pool, &(x)->field, fmt, args1, args2)
-/*!
- \brief Free the stringfield storage pools attached to a structure
- \param x Pointer to a structure containing fields
- \return nothing.
-
- After calling this macro, fields can no longer be accessed in
- structure; it should only be called immediately before freeing
- the structure itself.
-*/
-#define ast_string_field_free_memory(x) do { \
- struct ast_string_field_pool *this; \
- while ( (this = (x)->__field_mgr_pool)) { \
- (x)->__field_mgr_pool = this->prev; \
- ast_free(this); \
- } \
- } while(0)
-
-#if 1 /* deprecated but not deleted yet */
-/*!
- \brief Free the stringfields in a structure
- \param x Pointer to a structure containing fields
- \return nothing.
-
- After calling this macro, the most recently allocated pool
- attached to the structure will be available for use by
- stringfields again.
-*/
-#define ast_string_field_free_strings(x) do { \
- const char **__p__ = (const char **)&(x)->__field_mgr_pool + 1; \
- while ((struct ast_string_field_mgr *)__p__ != &(x)->__field_mgr) \
- *__p__++ = __ast_string_field_empty; \
- (x)->__field_mgr.used = 0; \
- } while(0)
-#endif
-
#endif /* _ASTERISK_STRINGFIELDS_H */
Modified: team/rizzo/video_v2/main/utils.c
URL: http://svn.digium.com/view/asterisk/team/rizzo/video_v2/main/utils.c?view=diff&rev=87809&r1=87808&r2=87809
==============================================================================
--- team/rizzo/video_v2/main/utils.c (original)
+++ team/rizzo/video_v2/main/utils.c Wed Oct 31 08:47:15 2007
@@ -1164,19 +1164,43 @@
/*
* This is an internal API, code should not use it directly.
- * Add a pool to the array, then initialize fields as empty.
+ * It initializes all fields as empty, then uses 'size' for 3 functions:
+ * size > 0 means initialize the pool list with a pool of given size.
+ * This must be called right after allocating the object.
+ * size = 0 means release all pools except the most recent one.
+ * This is useful to e.g. reset an object to the initial value.
+ * size < 0 means release all pools.
+ * This must be done before destroying the object.
*/
int __ast_string_field_init(struct ast_string_field_mgr *mgr,
struct ast_string_field_pool **pool_head, size_t size)
{
const char **p = (const char **)pool_head + 1;
-
- if (add_string_pool(mgr, pool_head, size))
- return -1;
-
+ struct ast_string_field_pool *cur = *pool_head;
+
+ /* clear fields - this is always necessary */
while ((struct ast_string_field_mgr *)p != mgr)
*p++ = __ast_string_field_empty;
-
+ if (size > 0) { /* allocate the initial pool */
+ *pool_head = NULL;
+ return add_string_pool(mgr, pool_head, size);
+ }
+ if (size < 0) { /* reset all pools */
+ *pool_head = NULL;
+ } else { /* preserve the first pool */
+ if (cur == NULL) {
+ ast_log(LOG_WARNING, "trying to reset empty pool\n");
+ return -1;
+ }
+ cur = cur->prev;
+ (*pool_head)->prev = NULL;
+ mgr->used = 0;
+ }
+ while (cur) {
+ struct ast_string_field_pool *prev = cur->prev;
+ ast_free(cur);
+ cur = prev;
+ }
return 0;
}
More information about the asterisk-commits
mailing list