Skip to content
Snippets Groups Projects
Commit b4dd374b authored by zuul's avatar zuul Committed by Gerrit Code Review
Browse files

Merge "stringfields: Update extended string fields for master only."

parents cc9b7220 caa416d5
No related branches found
No related tags found
No related merge requests found
...@@ -220,23 +220,14 @@ struct ast_string_field_pool { ...@@ -220,23 +220,14 @@ struct ast_string_field_pool {
*/ */
AST_VECTOR(ast_string_field_vector, const char **); AST_VECTOR(ast_string_field_vector, const char **);
/*!
\internal
\brief Structure used to hold a pointer to the embedded pool and the field vector
\since 13.9.0
*/
struct ast_string_field_header {
struct ast_string_field_pool *embedded_pool; /*!< pointer to the embedded pool, if any */
struct ast_string_field_vector string_fields; /*!< field vector for compare and copy */
};
/*! /*!
\internal \internal
\brief Structure used to manage the storage for a set of string fields. \brief Structure used to manage the storage for a set of string fields.
*/ */
struct ast_string_field_mgr { struct ast_string_field_mgr {
ast_string_field last_alloc; /*!< the last field allocated */ ast_string_field last_alloc; /*!< the last field allocated */
struct ast_string_field_header *header; /*!< pointer to the header */ struct ast_string_field_pool *embedded_pool; /*!< pointer to the embedded pool, if any */
struct ast_string_field_vector string_fields; /*!< field vector for compare and copy */
#if defined(__AST_DEBUG_MALLOC) #if defined(__AST_DEBUG_MALLOC)
const char *owner_file; /*!< filename of owner */ const char *owner_file; /*!< filename of owner */
const char *owner_func; /*!< function name of owner */ const char *owner_func; /*!< function name of owner */
...@@ -407,10 +398,10 @@ int __ast_string_field_free_memory(struct ast_string_field_mgr *mgr, ...@@ -407,10 +398,10 @@ int __ast_string_field_free_memory(struct ast_string_field_mgr *mgr,
#define ast_string_field_init_extended(x, field) \ #define ast_string_field_init_extended(x, field) \
({ \ ({ \
int __res__ = -1; \ int __res__ = -1; \
if (((void *)(x)) != NULL && (x)->__field_mgr.header != NULL) { \ if (((void *)(x)) != NULL) { \
ast_string_field *non_const = (ast_string_field *)&(x)->field; \ ast_string_field *non_const = (ast_string_field *)&(x)->field; \
*non_const = __ast_string_field_empty; \ *non_const = __ast_string_field_empty; \
__res__ = AST_VECTOR_APPEND(&(x)->__field_mgr.header->string_fields, non_const); \ __res__ = AST_VECTOR_APPEND(&(x)->__field_mgr.string_fields, non_const); \
} \ } \
__res__; \ __res__; \
}) })
...@@ -613,8 +604,8 @@ void __ast_string_field_release_active(struct ast_string_field_pool *pool_head, ...@@ -613,8 +604,8 @@ void __ast_string_field_release_active(struct ast_string_field_pool *pool_head,
({ \ ({ \
int __res__ = -1; \ int __res__ = -1; \
if (((void *)(instance1)) != NULL && ((void *)(instance2)) != NULL) { \ if (((void *)(instance1)) != NULL && ((void *)(instance2)) != NULL) { \
__res__ = __ast_string_fields_cmp(&(instance1)->__field_mgr.header->string_fields, \ __res__ = __ast_string_fields_cmp(&(instance1)->__field_mgr.string_fields, \
&(instance2)->__field_mgr.header->string_fields); \ &(instance2)->__field_mgr.string_fields); \
} \ } \
__res__; \ __res__; \
}) })
......
...@@ -118,29 +118,23 @@ int __ast_string_field_free_memory(struct ast_string_field_mgr *mgr, ...@@ -118,29 +118,23 @@ int __ast_string_field_free_memory(struct ast_string_field_mgr *mgr,
struct ast_string_field_pool *cur = NULL; struct ast_string_field_pool *cur = NULL;
struct ast_string_field_pool *preserve = NULL; struct ast_string_field_pool *preserve = NULL;
if (!mgr->header) {
return -1;
}
/* reset all the fields regardless of cleanup type */ /* reset all the fields regardless of cleanup type */
AST_VECTOR_CALLBACK_VOID(&mgr->header->string_fields, reset_field); AST_VECTOR_CALLBACK_VOID(&mgr->string_fields, reset_field);
switch (cleanup_type) { switch (cleanup_type) {
case AST_STRINGFIELD_DESTROY: case AST_STRINGFIELD_DESTROY:
AST_VECTOR_FREE(&mgr->header->string_fields); AST_VECTOR_FREE(&mgr->string_fields);
if (mgr->header->embedded_pool) { /* ALWAYS preserve the embedded pool if there is one */ if (mgr->embedded_pool) { /* ALWAYS preserve the embedded pool if there is one */
preserve = mgr->header->embedded_pool; preserve = mgr->embedded_pool;
preserve->used = preserve->active = 0; preserve->used = preserve->active = 0;
} }
ast_free(mgr->header);
mgr->header = NULL;
break; break;
case AST_STRINGFIELD_RESET: case AST_STRINGFIELD_RESET:
/* Preserve the embedded pool if there is one, otherwise the last pool */ /* Preserve the embedded pool if there is one, otherwise the last pool */
if (mgr->header->embedded_pool) { if (mgr->embedded_pool) {
preserve = mgr->header->embedded_pool; preserve = mgr->embedded_pool;
} else { } else {
if (*pool_head == NULL) { if (*pool_head == NULL) {
ast_log(LOG_WARNING, "trying to reset empty pool\n"); ast_log(LOG_WARNING, "trying to reset empty pool\n");
...@@ -202,27 +196,19 @@ int __ast_string_field_init(struct ast_string_field_mgr *mgr, struct ast_string_ ...@@ -202,27 +196,19 @@ int __ast_string_field_init(struct ast_string_field_mgr *mgr, struct ast_string_
mgr->owner_line = lineno; mgr->owner_line = lineno;
#endif #endif
if (!(mgr->header = calloc_wrapper(1, sizeof(*mgr->header), file, lineno, func))) { if (AST_VECTOR_INIT(&mgr->string_fields, initial_vector_size)) {
return -1;
}
if (AST_VECTOR_INIT(&mgr->header->string_fields, initial_vector_size)) {
ast_free(mgr->header);
mgr->header = NULL;
return -1; return -1;
} }
while ((struct ast_string_field_mgr *) p != mgr) { while ((struct ast_string_field_mgr *) p != mgr) {
AST_VECTOR_APPEND(&mgr->header->string_fields, p); AST_VECTOR_APPEND(&mgr->string_fields, p);
*p++ = __ast_string_field_empty; *p++ = __ast_string_field_empty;
} }
*pool_head = NULL; *pool_head = NULL;
mgr->header->embedded_pool = NULL; mgr->embedded_pool = NULL;
if (add_string_pool(mgr, pool_head, needed, file, lineno, func)) { if (add_string_pool(mgr, pool_head, needed, file, lineno, func)) {
AST_VECTOR_FREE(&mgr->header->string_fields); AST_VECTOR_FREE(&mgr->string_fields);
ast_free(mgr->header);
mgr->header = NULL;
return -1; return -1;
} }
...@@ -428,33 +414,22 @@ void *__ast_calloc_with_stringfields(unsigned int num_structs, size_t struct_siz ...@@ -428,33 +414,22 @@ void *__ast_calloc_with_stringfields(unsigned int num_structs, size_t struct_siz
mgr = allocation + field_mgr_offset; mgr = allocation + field_mgr_offset;
/*
* The header is calloced in __ast_string_field_init so it also gets calloced here
* so __ast_string_fields_free_memory can always just free mgr->header.
*/
mgr->header = calloc_wrapper(1, sizeof(*mgr->header), file, lineno, func);
if (!mgr->header) {
ast_free(allocation);
return NULL;
}
pool = allocation + struct_size; pool = allocation + struct_size;
pool_head = allocation + field_mgr_pool_offset; pool_head = allocation + field_mgr_pool_offset;
p = (const char **) pool_head + 1; p = (const char **) pool_head + 1;
initial_vector_size = ((size_t) (((char *)mgr) - ((char *)p))) / sizeof(*p); initial_vector_size = ((size_t) (((char *)mgr) - ((char *)p))) / sizeof(*p);
if (AST_VECTOR_INIT(&mgr->header->string_fields, initial_vector_size)) { if (AST_VECTOR_INIT(&mgr->string_fields, initial_vector_size)) {
ast_free(mgr->header);
ast_free(allocation); ast_free(allocation);
return NULL; return NULL;
} }
while ((struct ast_string_field_mgr *) p != mgr) { while ((struct ast_string_field_mgr *) p != mgr) {
AST_VECTOR_APPEND(&mgr->header->string_fields, p); AST_VECTOR_APPEND(&mgr->string_fields, p);
*p++ = __ast_string_field_empty; *p++ = __ast_string_field_empty;
} }
mgr->header->embedded_pool = pool; mgr->embedded_pool = pool;
*pool_head = pool; *pool_head = pool;
pool->size = size_to_alloc - struct_size - sizeof(*pool); pool->size = size_to_alloc - struct_size - sizeof(*pool);
#if defined(__AST_DEBUG_MALLOC) #if defined(__AST_DEBUG_MALLOC)
...@@ -487,8 +462,8 @@ int __ast_string_fields_copy(struct ast_string_field_pool *copy_pool, ...@@ -487,8 +462,8 @@ 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) struct ast_string_field_mgr *copy_mgr, struct ast_string_field_mgr *orig_mgr)
{ {
int i; int i;
struct ast_string_field_vector *dest = &(copy_mgr->header->string_fields); struct ast_string_field_vector *dest = &(copy_mgr->string_fields);
struct ast_string_field_vector *src = &(orig_mgr->header->string_fields); struct ast_string_field_vector *src = &(orig_mgr->string_fields);
ast_assert(AST_VECTOR_SIZE(dest) == AST_VECTOR_SIZE(src)); ast_assert(AST_VECTOR_SIZE(dest) == AST_VECTOR_SIZE(src));
......
...@@ -369,7 +369,7 @@ AST_TEST_DEFINE(string_field_aggregate_test) ...@@ -369,7 +369,7 @@ AST_TEST_DEFINE(string_field_aggregate_test)
ast_string_field_ptr_set_by_fields(inst2->__field_mgr_pool, inst2->__field_mgr, &inst2->string1, "foo"); ast_string_field_ptr_set_by_fields(inst2->__field_mgr_pool, inst2->__field_mgr, &inst2->string1, "foo");
inst2->foo = 2; inst2->foo = 2;
if (inst3->__field_mgr.header->embedded_pool->prev) { if (inst3->__field_mgr.embedded_pool->prev) {
ast_test_status_update(test, "Structure 3 embedded pool should not have a previous pool!\n"); ast_test_status_update(test, "Structure 3 embedded pool should not have a previous pool!\n");
res = AST_TEST_FAIL; res = AST_TEST_FAIL;
goto error; goto error;
...@@ -377,13 +377,13 @@ AST_TEST_DEFINE(string_field_aggregate_test) ...@@ -377,13 +377,13 @@ AST_TEST_DEFINE(string_field_aggregate_test)
ast_string_field_set(inst3, string1, "foo"); ast_string_field_set(inst3, string1, "foo");
if (inst3->__field_mgr.header->embedded_pool != inst3->__field_mgr_pool) { if (inst3->__field_mgr.embedded_pool != inst3->__field_mgr_pool) {
ast_test_status_update(test, "Structure 3 embedded pool should have been the current pool!\n"); ast_test_status_update(test, "Structure 3 embedded pool should have been the current pool!\n");
res = AST_TEST_FAIL; res = AST_TEST_FAIL;
goto error; goto error;
} }
if (inst3->__field_mgr.header->embedded_pool->prev) { if (inst3->__field_mgr.embedded_pool->prev) {
ast_test_status_update(test, "Structure 3 embedded pool should not have a previous pool!\n"); ast_test_status_update(test, "Structure 3 embedded pool should not have a previous pool!\n");
res = AST_TEST_FAIL; res = AST_TEST_FAIL;
goto error; goto error;
...@@ -395,13 +395,13 @@ AST_TEST_DEFINE(string_field_aggregate_test) ...@@ -395,13 +395,13 @@ AST_TEST_DEFINE(string_field_aggregate_test)
ast_string_field_set(inst3, string2, "baz 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890"); ast_string_field_set(inst3, string2, "baz 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890");
inst3->foo = 3; inst3->foo = 3;
if (inst3->__field_mgr_pool == inst3->__field_mgr.header->embedded_pool) { if (inst3->__field_mgr_pool == inst3->__field_mgr.embedded_pool) {
ast_test_status_update(test, "Structure 3 embedded pool should not have been the current pool!\n"); ast_test_status_update(test, "Structure 3 embedded pool should not have been the current pool!\n");
res = AST_TEST_FAIL; res = AST_TEST_FAIL;
goto error; goto error;
} }
if (inst3->__field_mgr.header->embedded_pool != inst3->__field_mgr_pool->prev) { if (inst3->__field_mgr.embedded_pool != inst3->__field_mgr_pool->prev) {
ast_test_status_update(test, "Structure 3 embedded pool should be the current pool's previous!\n"); ast_test_status_update(test, "Structure 3 embedded pool should be the current pool's previous!\n");
res = AST_TEST_FAIL; res = AST_TEST_FAIL;
goto error; goto error;
...@@ -484,7 +484,7 @@ AST_TEST_DEFINE(string_field_aggregate_test) ...@@ -484,7 +484,7 @@ AST_TEST_DEFINE(string_field_aggregate_test)
ast_test_status_update(test, "Structures 1/2 are the same (empty) as expected.\n"); ast_test_status_update(test, "Structures 1/2 are the same (empty) as expected.\n");
} }
if (inst4->__field_mgr.header->embedded_pool != inst4->__field_mgr_pool) { if (inst4->__field_mgr.embedded_pool != inst4->__field_mgr_pool) {
ast_test_status_update(test, "Structure 4 embedded pool should have been the current pool!\n"); ast_test_status_update(test, "Structure 4 embedded pool should have been the current pool!\n");
res = AST_TEST_FAIL; res = AST_TEST_FAIL;
goto error; goto error;
...@@ -492,7 +492,7 @@ AST_TEST_DEFINE(string_field_aggregate_test) ...@@ -492,7 +492,7 @@ AST_TEST_DEFINE(string_field_aggregate_test)
ast_test_status_update(test, "Structure 4 embedded pool is the current pool as expected.\n"); ast_test_status_update(test, "Structure 4 embedded pool is the current pool as expected.\n");
} }
if (inst4->__field_mgr.header->embedded_pool->prev) { if (inst4->__field_mgr.embedded_pool->prev) {
ast_test_status_update(test, "Structure 4 embedded pool should not have a previous pool!\n"); ast_test_status_update(test, "Structure 4 embedded pool should not have a previous pool!\n");
res = AST_TEST_FAIL; res = AST_TEST_FAIL;
goto error; goto error;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment