[Asterisk-code-review] vector: Traversal, retrieval, insert and locking enhancements (asterisk[13])
George Joseph
asteriskteam at digium.com
Sat May 2 15:49:03 CDT 2015
George Joseph has uploaded a new change for review.
https://gerrit.asterisk.org/339
Change subject: vector: Traversal, retrieval, insert and locking enhancements
......................................................................
vector: Traversal, retrieval, insert and locking enhancements
Renamed AST_VECTOR_INSERT to AST_VECTOR_REPLACE because it really
does replace not insert. The few users of AST_VECTOR_INSERT were
refactored. Because these are macros, there should be no ABI
compatibility issues.
Added AST_VECTOR_INSERT that actually inserts an element into the
vector at a specific index pushing existing elements to the right.
Added AST_VECTOR_GET_CMP that can retrieve from the vector based
on a user-provided compare function.
Added AST_VECTOR_CALLBACK* functions that mirror the ao2_callback
and ao2_callback_data functions. This should allow easy migration
to a vector where a container might be too heavy.
Create R/W lock versions of macros.
Added unit tests.
ASTERISK-25045 #close
Change-Id: I2e07ecc709d2f5f91bcab8904e5e9340609b00e0
---
M include/asterisk/vector.h
M main/format_cap.c
M main/rtp_engine.c
M tests/test_message.c
A tests/test_vector.c
5 files changed, 603 insertions(+), 18 deletions(-)
git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/39/339/1
diff --git a/include/asterisk/vector.h b/include/asterisk/vector.h
index 0053d8a..a344e6e 100644
--- a/include/asterisk/vector.h
+++ b/include/asterisk/vector.h
@@ -47,6 +47,55 @@
}
/*!
+ * \brief Define a vector structure with a read/write lock
+ *
+ * \param name Optional vector struct name.
+ * \param type Vector element type.
+ */
+#define AST_RWVECTOR(name, type) \
+ struct name { \
+ type *elems; \
+ size_t max; \
+ size_t current; \
+ ast_rwlock_t lock; \
+ }
+
+/*!
+ * \brief Write locks a vector.
+ * \param vec This is a pointer to the vector structure
+ *
+ * This macro attempts to place an exclusive write lock in the
+ * vector.
+ * \retval 0 on success
+ * \retval non-zero on failure
+ */
+#define AST_RWVECTOR_WRLOCK(vec) \
+ ast_rwlock_wrlock(&(vec)->lock)
+
+/*!
+ * \brief Read locks a vector.
+ * \param vec This is a pointer to the vector structure
+ *
+ * This macro attempts to place a read lock in the
+ * vector.
+ * \retval 0 on success
+ * \retval non-zero on failure
+ */
+#define AST_RWVECTOR_RDLOCK(vec) \
+ ast_rwlock_rdlock(&(vec)->lock)
+
+/*!
+ * \brief Attempts to unlock a read/write based vector.
+ * \param vec This is a pointer to the vector structure
+ *
+ * This macro attempts to remove a read or write lock from the
+ * vector. If the vector was not locked by this thread, this
+ * macro has no effect.
+ */
+#define AST_RWVECTOR_UNLOCK(vec) \
+ ast_rwlock_unlock(&(vec)->lock)
+
+/*!
* \brief Initialize a vector
*
* If \a size is 0, then no space will be allocated until the vector is
@@ -71,6 +120,14 @@
(alloc_size == 0 || (vec)->elems != NULL) ? 0 : -1; \
})
+#define AST_RWVECTOR_INIT(vec, size) ({ \
+ int res = -1; \
+ if (AST_VECTOR_INIT(vec, size) == 0) { \
+ res = ast_rwlock_init(&(vec)->lock); \
+ } \
+ res; \
+})
+
/*!
* \brief Deallocates this vector.
*
@@ -85,6 +142,11 @@
(vec)->max = 0; \
(vec)->current = 0; \
} while (0)
+
+#define AST_RWVECTOR_FREE(vec) do { \
+ AST_VECTOR_FREE(vec); \
+ ast_rwlock_destroy(&(vec)->lock); \
+} while(0)
/*!
* \brief Append an element to a vector, growing the vector if needed.
@@ -115,12 +177,21 @@
res; \
})
+#define AST_RWVECTOR_APPEND(vec, elem) ({ \
+ int res = -1; \
+ if (!AST_RWVECTOR_WRLOCK(vec)) { \
+ res = AST_VECTOR_APPEND(vec, elem); \
+ AST_RWVECTOR_UNLOCK(vec); \
+ } \
+ res; \
+})
+
/*!
- * \brief Insert an element at a specific position in a vector, growing the vector if needed.
+ * \brief Replace an element at a specific position in a vector, growing the vector if needed.
*
- * \param vec Vector to insert into.
- * \param idx Position to insert at.
- * \param elem Element to insert.
+ * \param vec Vector to replace into.
+ * \param idx Position to replace.
+ * \param elem Element to replace.
*
* \return 0 on success.
* \return Non-zero on failure.
@@ -131,7 +202,7 @@
* index means you can not use the UNORDERED assortment of macros. These macros alter the ordering
* of the vector itself.
*/
-#define AST_VECTOR_INSERT(vec, idx, elem) ({ \
+#define AST_VECTOR_REPLACE(vec, idx, elem) ({ \
int res = 0; \
do { \
if (((idx) + 1) > (vec)->max) { \
@@ -157,6 +228,64 @@
res; \
})
+#define AST_RWVECTOR_REPLACE(vec, idx, elem) ({ \
+ int res = -1; \
+ if (!AST_RWVECTOR_WRLOCK(vec)) { \
+ res = AST_VECTOR_REPLACE(vec, idx, elem); \
+ AST_RWVECTOR_UNLOCK(vec); \
+ } \
+ res; \
+})
+
+/*!
+ * \brief Insert an element at a specific position in a vector, growing the vector if needed.
+ *
+ * \param vec Vector to insert into.
+ * \param idx Position to insert at.
+ * \param elem Element to insert.
+ *
+ * \return 0 on success.
+ * \return Non-zero on failure.
+ *
+ * \warning This macro will shift existing elements right to make room for the new element.
+ *
+ * \warning Use of this macro with the expectation that the element will remain at the provided
+ * index means you can not use the UNORDERED assortment of macros. These macros alter the ordering
+ * of the vector itself.
+ */
+#define AST_VECTOR_INSERT(vec, idx, elem) ({ \
+ int res = 0; \
+ size_t __move; \
+ do { \
+ if ((vec)->current + 1 > (vec)->max) { \
+ size_t new_max = (vec)->max ? 2 * (vec)->max : 1; \
+ typeof((vec)->elems) new_elems = ast_realloc( \
+ (vec)->elems, new_max * sizeof(*new_elems)); \
+ if (new_elems) { \
+ (vec)->elems = new_elems; \
+ (vec)->max = new_max; \
+ } else { \
+ res = -1; \
+ break; \
+ } \
+ } \
+ __move = ((vec)->current - 1) * sizeof(typeof((vec)->elems[0])); \
+ memmove(&(vec)->elems[(idx) + 1], &(vec)->elems[(idx)], __move); \
+ (vec)->elems[(idx)] = (elem); \
+ (vec)->current++; \
+ } while (0); \
+ res; \
+})
+
+#define AST_RWVECTOR_INSERT(vec, idx, elem) ({ \
+ int res = -1; \
+ if (!AST_RWVECTOR_WRLOCK(vec)) { \
+ res = AST_VECTOR_INSERT(vec, idx, elem); \
+ AST_RWVECTOR_UNLOCK(vec); \
+ } \
+ res; \
+})
+
/*!
* \brief Remove an element from a vector by index.
*
@@ -174,6 +303,15 @@
res = (vec)->elems[__idx]; \
(vec)->elems[__idx] = (vec)->elems[--(vec)->current]; \
res; \
+})
+
+#define AST_RWVECTOR_REMOVE_UNORDERED(vec, idx) ({ \
+ int res = -1; \
+ if (!AST_RWVECTOR_WRLOCK(vec)) { \
+ res = AST_VECTOR_REMOVE_UNORDERED(vec, idx); \
+ AST_RWVECTOR_UNLOCK(vec); \
+ } \
+ res; \
})
/*!
@@ -195,6 +333,14 @@
res; \
})
+#define AST_RWVECTOR_REMOVE_ORDERED(vec, idx) ({ \
+ int res = -1; \
+ if (!AST_RWVECTOR_WRLOCK(vec)) { \
+ res = AST_VECTOR_REMOVE_ORDERED(vec, idx); \
+ AST_RWVECTOR_UNLOCK(vec); \
+ } \
+ res; \
+})
/*!
* \brief Remove an element from a vector that matches the given comparison
@@ -222,6 +368,15 @@
res; \
})
+#define AST_RWVECTOR_REMOVE_CMP_UNORDERED(vec, value, cmp, cleanup) ({ \
+ int res = -1; \
+ if (!AST_RWVECTOR_WRLOCK(vec)) { \
+ res = AST_VECTOR_REMOVE_CMP_UNORDERED(vec, value, cmp, cleanup); \
+ AST_RWVECTOR_UNLOCK(vec); \
+ } \
+ res; \
+})
+
/*!
* \brief Remove an element from a vector that matches the given comparison while maintaining order
*
@@ -246,6 +401,15 @@
} \
} \
res; \
+})
+
+#define AST_RWVECTOR_REMOVE_CMP_ORDERED(vec, value, cmp, cleanup) ({ \
+ int res = -1; \
+ if (!AST_RWVECTOR_WRLOCK(vec)) { \
+ res = AST_VECTOR_REMOVE_CMP_ORDERED(vec, value, cmp, cleanup); \
+ AST_RWVECTOR_UNLOCK(vec); \
+ } \
+ res; \
})
/*!
@@ -283,6 +447,11 @@
AST_VECTOR_ELEM_DEFAULT_CMP, cleanup); \
})
+#define AST_RWVECTOR_REMOVE_ELEM_UNORDERED(vec, elem, cleanup) ({ \
+ AST_RWVECTOR_REMOVE_CMP_UNORDERED((vec), (elem), \
+ AST_VECTOR_ELEM_DEFAULT_CMP, cleanup); \
+})
+
/*!
* \brief Remove an element from a vector while maintaining order.
*
@@ -296,6 +465,11 @@
#define AST_VECTOR_REMOVE_ELEM_ORDERED(vec, elem, cleanup) ({ \
AST_VECTOR_REMOVE_CMP_ORDERED((vec), (elem), \
AST_VECTOR_ELEM_DEFAULT_CMP, cleanup); \
+})
+
+#define AST_RWVECTOR_REMOVE_ELEM_ORDERED(vec, elem, cleanup) ({ \
+ AST_RWVECTOR_REMOVE_CMP_ORDERED((vec), (elem), \
+ AST_VECTOR_ELEM_DEFAULT_CMP, cleanup); \
})
/*!
@@ -330,4 +504,149 @@
(vec)->elems[__idx]; \
})
+/*!
+ * \brief Get an element from a vector that matches the given comparison
+ *
+ * \param vec Vector to get from.
+ * \param value Value to pass into comparator.
+ * \param cmp Comparator function/macros (called as \c cmp(elem, value))
+ *
+ * \return a pointer to the element that was found or NULL
+ */
+#define AST_VECTOR_GET_CMP(vec, value, cmp) ({ \
+ void *res = NULL; \
+ size_t idx; \
+ typeof(value) __value = (value); \
+ for (idx = 0; idx < (vec)->current; ++idx) { \
+ if (cmp((vec)->elems[idx], __value)) { \
+ res = &(vec)->elems[idx]; \
+ break; \
+ } \
+ } \
+ res; \
+})
+
+/*!
+ * \brief Execute a callback on every element in a vector
+ *
+ * \param vec Vector to operate on.
+ * \param callback The ao2_callback style function to execute
+ * \param arg Any argument
+ *
+ * \return the number of elements visited before the end of the vector
+ * was reached or CMP_STOP was returned.
+ */
+#define AST_VECTOR_CALLBACK(vec, callback, arg) ({ \
+ size_t idx; \
+ for (idx = 0; idx < (vec)->current; idx++) { \
+ int rc = callback((vec)->elems[idx], arg, 0); \
+ if (rc == CMP_STOP) { \
+ idx++; \
+ break; \
+ }\
+ } \
+ idx; \
+})
+
+/*!
+ * \brief Execute a callback on every element in a vector while holding a read lock
+ *
+ * \param vec Vector to operate on.
+ * \param callback The ao2_callback style function to execute
+ * \param arg Any argument
+ *
+ * \return the number of elements visited before the end of the vector
+ * was reached or CMP_STOP was returned.
+ */
+#define AST_RWVECTOR_CALLBACK_RDLOCK(vec, callback, arg) ({ \
+ int res = -1; \
+ if (AST_RWVECTOR_RDLOCK(vec) == 0) { \
+ res = AST_VECTOR_CALLBACK(vec, callback, arg); \
+ AST_RWVECTOR_UNLOCK(vec); \
+ } \
+ res; \
+})
+
+/*!
+ * \brief Execute a callback on every element in a vector while holding a write lock
+ *
+ * \param vec Vector to operate on.
+ * \param callback The ao2_callback style function to execute
+ * \param arg Any argument
+ *
+ * \return the number of elements visited before the end of the vector
+ * was reached or CMP_STOP was returned.
+ */
+#define AST_RWVECTOR_CALLBACK_WRLOCK(vec, callback, arg) ({ \
+ int res = -1; \
+ if (AST_RWVECTOR_WRLOCK(vec) == 0) { \
+ res = AST_VECTOR_CALLBACK(vec, callback, arg); \
+ AST_RWVECTOR_UNLOCK(vec); \
+ } \
+ res; \
+})
+
+/*!
+ * \brief Execute a callback on every element in a vector
+ *
+ * \param vec Vector to operate on.
+ * \param callback The ao2_callback_data style function to execute
+ * \param arg Any argument
+ * \param data Any data
+ *
+ * \return the number of elements visited before the end of the vector
+ * was reached or CMP_STOP was returned.
+ */
+#define AST_VECTOR_CALLBACK_DATA(vec, callback, arg, data) ({ \
+ size_t idx; \
+ for (idx = 0; idx < (vec)->current; idx++) { \
+ int rc = callback((vec)->elems[idx], arg, data, 0); \
+ if (rc == CMP_STOP) { \
+ idx++; \
+ break; \
+ }\
+ } \
+ idx; \
+})
+
+/*!
+ * \brief Execute a callback on every element in a vector while holding a read lock
+ *
+ * \param vec Vector to operate on.
+ * \param callback The ao2_callback_data style function to execute
+ * \param arg Any argument
+ * \param data Any data
+ *
+ * \return the number of elements visited before the end of the vector
+ * was reached or CMP_STOP was returned.
+ */
+#define AST_RWVECTOR_CALLBACK_DATA_RDLOCK(vec, callback, arg, data) ({ \
+ int res = -1; \
+ if (AST_RWVECTOR_RDLOCK(vec) == 0) { \
+ res = AST_VECTOR_CALLBACK_DATA(vec, callback, arg, data); \
+ AST_RWVECTOR_UNLOCK(vec); \
+ } \
+ res; \
+})
+
+/*!
+ * \brief Execute a callback on every element in a vector while holding a write lock
+ *
+ * \param vec Vector to operate on.
+ * \param callback The ao2_callback_data style function to execute
+ * \param arg Any argument
+ * \param data Any data
+ *
+ * \return the number of elements visited before the end of the vector
+ * was reached or CMP_STOP was returned.
+ */
+#define AST_RWVECTOR_CALLBACK_DATA_WRLOCK(vec, callback, arg, data) ({ \
+ int res = -1; \
+ if (AST_RWVECTOR_WRLOCK(vec) == 0) { \
+ res = AST_VECTOR_CALLBACK_DATA(vec, callback, arg, data); \
+ AST_RWVECTOR_UNLOCK(vec); \
+ } \
+ res; \
+})
+
#endif /* _ASTERISK_VECTOR_H */
diff --git a/main/format_cap.c b/main/format_cap.c
index 177652e..cefad13 100644
--- a/main/format_cap.c
+++ b/main/format_cap.c
@@ -151,7 +151,7 @@
framed->framing = framing;
if (ast_format_get_codec_id(format) >= AST_VECTOR_SIZE(&cap->formats)) {
- if (AST_VECTOR_INSERT(&cap->formats, ast_format_get_codec_id(format), format_cap_framed_list_empty)) {
+ if (AST_VECTOR_REPLACE(&cap->formats, ast_format_get_codec_id(format), format_cap_framed_list_empty)) {
ao2_ref(framed, -1);
return -1;
}
diff --git a/main/rtp_engine.c b/main/rtp_engine.c
index 4120ba5..e09dcb8 100644
--- a/main/rtp_engine.c
+++ b/main/rtp_engine.c
@@ -625,7 +625,7 @@
}
ast_debug(2, "Copying payload %d (%p) from %p to %p\n", i, type, src, dest);
ao2_bump(type);
- AST_VECTOR_INSERT(&dest->payloads, i, type);
+ AST_VECTOR_REPLACE(&dest->payloads, i, type);
if (instance && instance->engine && instance->engine->payload_set) {
instance->engine->payload_set(instance, i, type->asterisk_format, type->format, type->rtp_code);
@@ -662,7 +662,7 @@
new_type->format = ao2_bump(static_RTP_PT[payload].format);
ast_debug(1, "Setting payload %d (%p) based on m type on %p\n", payload, new_type, codecs);
- AST_VECTOR_INSERT(&codecs->payloads, payload, new_type);
+ AST_VECTOR_REPLACE(&codecs->payloads, payload, new_type);
if (instance && instance->engine && instance->engine->payload_set) {
instance->engine->payload_set(instance, payload, new_type->asterisk_format, new_type->format, new_type->rtp_code);
@@ -727,7 +727,7 @@
} else {
new_type->format = ao2_bump(t->payload_type.format);
}
- AST_VECTOR_INSERT(&codecs->payloads, pt, new_type);
+ AST_VECTOR_REPLACE(&codecs->payloads, pt, new_type);
if (instance && instance->engine && instance->engine->payload_set) {
instance->engine->payload_set(instance, pt, new_type->asterisk_format, new_type->format, new_type->rtp_code);
@@ -760,7 +760,7 @@
if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
type = AST_VECTOR_GET(&codecs->payloads, payload);
ao2_cleanup(type);
- AST_VECTOR_INSERT(&codecs->payloads, payload, NULL);
+ AST_VECTOR_REPLACE(&codecs->payloads, payload, NULL);
}
if (instance && instance->engine && instance->engine->payload_set) {
diff --git a/tests/test_message.c b/tests/test_message.c
index 13e24a8..5db0ed0 100644
--- a/tests/test_message.c
+++ b/tests/test_message.c
@@ -160,7 +160,7 @@
bad_headers_head = AST_VECTOR_GET(&bad_headers, user_event);
}
ast_variable_list_append(&bad_headers_head, bad_header);
- AST_VECTOR_INSERT(&bad_headers, user_event, bad_headers_head);
+ AST_VECTOR_REPLACE(&bad_headers, user_event, bad_headers_head);
}
regfree(®exbuf);
return -1;
@@ -492,28 +492,28 @@
ast_variable_list_append(&expected_response, expected);
expected = ast_variable_new("Value","^foo$", __FILE__);
ast_variable_list_append(&expected_response, expected);
- AST_VECTOR_INSERT(&expected_user_event_fields, 0, expected_response);
+ AST_VECTOR_REPLACE(&expected_user_event_fields, 0, expected_response);
expected_response = NULL;
expected = ast_variable_new("Verify", "^From$", __FILE__);
ast_variable_list_append(&expected_response, expected);
expected = ast_variable_new("Value","^bar$", __FILE__);
ast_variable_list_append(&expected_response, expected);
- AST_VECTOR_INSERT(&expected_user_event_fields, 1, expected_response);
+ AST_VECTOR_REPLACE(&expected_user_event_fields, 1, expected_response);
expected_response = NULL;
expected = ast_variable_new("Verify", "^Body$", __FILE__);
ast_variable_list_append(&expected_response, expected);
expected = ast_variable_new("Value", "^a body$", __FILE__);
ast_variable_list_append(&expected_response, expected);
- AST_VECTOR_INSERT(&expected_user_event_fields, 2, expected_response);
+ AST_VECTOR_REPLACE(&expected_user_event_fields, 2, expected_response);
expected_response = NULL;
expected = ast_variable_new("Verify", "^Custom$", __FILE__);
ast_variable_list_append(&expected_response, expected);
expected = ast_variable_new("Value", "^field$", __FILE__);
ast_variable_list_append(&expected_response, expected);
- AST_VECTOR_INSERT(&expected_user_event_fields, 3, expected_response);
+ AST_VECTOR_REPLACE(&expected_user_event_fields, 3, expected_response);
ast_msg_set_to(msg, "foo");
ast_msg_set_from(msg, "bar");
@@ -609,21 +609,21 @@
ast_variable_list_append(&expected_response, expected);
expected = ast_variable_new("Value","^foo$", __FILE__);
ast_variable_list_append(&expected_response, expected);
- AST_VECTOR_INSERT(&expected_user_event_fields, 0, expected_response);
+ AST_VECTOR_REPLACE(&expected_user_event_fields, 0, expected_response);
expected_response = NULL;
expected = ast_variable_new("Verify", "^From$", __FILE__);
ast_variable_list_append(&expected_response, expected);
expected = ast_variable_new("Value","^bar$", __FILE__);
ast_variable_list_append(&expected_response, expected);
- AST_VECTOR_INSERT(&expected_user_event_fields, 1, expected_response);
+ AST_VECTOR_REPLACE(&expected_user_event_fields, 1, expected_response);
expected_response = NULL;
expected = ast_variable_new("Verify", "^Body$", __FILE__);
ast_variable_list_append(&expected_response, expected);
expected = ast_variable_new("Value", "^a body$", __FILE__);
ast_variable_list_append(&expected_response, expected);
- AST_VECTOR_INSERT(&expected_user_event_fields, 2, expected_response);
+ AST_VECTOR_REPLACE(&expected_user_event_fields, 2, expected_response);
ast_msg_set_to(msg, "foo");
ast_msg_set_from(msg, "bar");
diff --git a/tests/test_vector.c b/tests/test_vector.c
new file mode 100644
index 0000000..327db61
--- /dev/null
+++ b/tests/test_vector.c
@@ -0,0 +1,266 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2015, Fairview 5 Engineering, LLC
+ *
+ * George Joseph <george.joseph at fairview5.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \brief Vector tests
+ *
+ * \author George Joseph <george.joseph at fairview5.com>
+ *
+ * This module will run some vector tests.
+ *
+ * \ingroup tests
+ */
+
+/*** MODULEINFO
+ <depend>TEST_FRAMEWORK</depend>
+ <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_REGISTER_FILE()
+
+#include "asterisk/test.h"
+#include "asterisk/utils.h"
+#include "asterisk/strings.h"
+#include "asterisk/module.h"
+#include "asterisk/vector.h"
+
+#define test_validate_cleanup(condition) ({ \
+ if (!(condition)) { \
+ ast_test_status_update((test), "%s: %s\n", "Condition failed", #condition); \
+ rc = AST_TEST_FAIL; \
+ goto cleanup; \
+ } \
+})
+
+static int cleanup_count;
+
+static void cleanup(char *element)
+{
+ cleanup_count++;
+}
+
+AST_TEST_DEFINE(basic_ops)
+{
+ AST_VECTOR(test_struct, char *) sv1;
+ int rc = AST_TEST_PASS;
+
+ char *AAA = "AAA";
+ char *BBB = "BBB";
+ char *CCC = "CCC";
+ char *ZZZ = "ZZZ";
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "basic";
+ info->category = "/main/vector/";
+ info->summary = "Test vector basic ops";
+ info->description = "Test vector basic ops";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ ast_test_validate(test, AST_VECTOR_INIT(&sv1, 3) == 0);
+ test_validate_cleanup(sv1.max == 3);
+ test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 0);
+
+ test_validate_cleanup(AST_VECTOR_APPEND(&sv1, AAA) == 0);
+ test_validate_cleanup(AST_VECTOR_APPEND(&sv1, BBB) == 0);
+ test_validate_cleanup(AST_VECTOR_APPEND(&sv1, CCC) == 0);
+ test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 3);
+ test_validate_cleanup(sv1.max == 3);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == AAA);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == BBB);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 2) == CCC);
+
+ test_validate_cleanup(AST_VECTOR_INSERT(&sv1, 1, ZZZ) == 0);
+ test_validate_cleanup(sv1.max >= 4);
+ test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 4);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == AAA);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == ZZZ);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 2) == BBB);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 3) == CCC);
+
+ test_validate_cleanup(*(char **)AST_VECTOR_GET_CMP(&sv1, "AAA", 0 == strcmp) == AAA);
+ test_validate_cleanup(*(char **)AST_VECTOR_GET_CMP(&sv1, "ZZZ", 0 == strcmp) == ZZZ);
+
+ AST_VECTOR_FREE(&sv1);
+ ast_test_validate(test, sv1.elems == NULL);
+ ast_test_validate(test, sv1.current == 0);
+ ast_test_validate(test, sv1.max == 0);
+
+ ast_test_validate(test, AST_VECTOR_INIT(&sv1, 0) == 0);
+ test_validate_cleanup(sv1.max == 0);
+ test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 0);
+
+ test_validate_cleanup(AST_VECTOR_APPEND(&sv1, AAA) == 0);
+ test_validate_cleanup(AST_VECTOR_APPEND(&sv1, BBB) == 0);
+ test_validate_cleanup(AST_VECTOR_APPEND(&sv1, CCC) == 0);
+ test_validate_cleanup(sv1.max >= 3);
+ test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 3);
+
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == AAA);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == BBB);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 2) == CCC);
+
+ /* Overwrite index 1 */
+ test_validate_cleanup(AST_VECTOR_REPLACE(&sv1, 1, ZZZ) == 0);
+ test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 3);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == AAA);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == ZZZ);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 2) == CCC);
+
+ /* Remove index 0 and bring the last entry into it's empty slot */
+ test_validate_cleanup(AST_VECTOR_REMOVE_UNORDERED(&sv1, 0) == AAA);
+ test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 2);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == CCC);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == ZZZ);
+
+ /* Replace 0 and 2 leaving 1 alone */
+ test_validate_cleanup(AST_VECTOR_REPLACE(&sv1, 0, AAA) == 0);
+ test_validate_cleanup(AST_VECTOR_REPLACE(&sv1, 2, CCC) == 0);
+ test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 3);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == AAA);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == ZZZ);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 2) == CCC);
+
+ /* Remove 1 and compact preserving order */
+ test_validate_cleanup(AST_VECTOR_REMOVE_ORDERED(&sv1, 1) == ZZZ);
+ test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 2);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == AAA);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == CCC);
+
+ /* Equivalent of APPEND */
+ test_validate_cleanup(AST_VECTOR_REPLACE(&sv1, 2, ZZZ) == 0);
+ test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 3);
+
+ /* This should fail because comparison is by pointer */
+ test_validate_cleanup(AST_VECTOR_REMOVE_ELEM_ORDERED(&sv1, "ZZZ", cleanup) != 0);
+
+ /* This should work because we passing in the specific object to be removed */
+ cleanup_count = 0;
+ test_validate_cleanup(AST_VECTOR_REMOVE_ELEM_ORDERED(&sv1, ZZZ, cleanup) == 0);
+ test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 2);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == AAA);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == CCC);
+ test_validate_cleanup(cleanup_count == 1);
+
+ /* If we want a comparison by value, we need to pass in a comparison
+ * function. The comparison looks weird but that's what it takes.
+ */
+ cleanup_count = 0;
+ test_validate_cleanup(AST_VECTOR_REMOVE_CMP_ORDERED(&sv1, "AAA", 0 == strcmp, cleanup) == 0);
+ test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 1);
+ test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == CCC);
+ test_validate_cleanup(cleanup_count == 1);
+
+ /* This element is gone so we shouldn't be able to find it or delete it again. */
+ test_validate_cleanup(AST_VECTOR_GET_CMP(&sv1, "AAA", 0 == strcmp) == NULL);
+ test_validate_cleanup(AST_VECTOR_REMOVE_CMP_ORDERED(&sv1, "AAA", 0 == strcmp, cleanup) != 0);
+
+ /* CCC should still be there though */
+ test_validate_cleanup(*(char **)AST_VECTOR_GET_CMP(&sv1, "CCC", 0 == strcmp) == CCC);
+
+cleanup:
+ AST_VECTOR_FREE(&sv1);
+ return rc;
+}
+
+static int cb(void *obj, void *arg, void *data, int flags)
+{
+ return strcmp(arg, "ARG") == 0 ? 0 : CMP_STOP;
+}
+
+static int cb_first(void *obj, void *arg, void *data, int flags)
+{
+ return data == arg ? CMP_STOP : 0;
+}
+
+AST_TEST_DEFINE(callbacks)
+{
+ AST_VECTOR(, char *) sv1;
+ AST_RWVECTOR(, char *) sv2;
+ int rc = AST_TEST_PASS;
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "callbacks";
+ info->category = "/main/vector/";
+ info->summary = "Test vector callback ops";
+ info->description = "Test vector callback ops";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ /* We're not actually checking that locking works, just the the APIs work. */
+
+ AST_VECTOR_INIT(&sv1, 32);
+ AST_RWVECTOR_INIT(&sv2, 32);
+
+ AST_VECTOR_APPEND(&sv1, "AAA");
+ AST_VECTOR_APPEND(&sv1, "BBB");
+ AST_VECTOR_APPEND(&sv1, "CCC");
+
+ AST_VECTOR_APPEND(&sv2, "AAA2");
+ AST_VECTOR_APPEND(&sv2, "BBB2");
+ AST_VECTOR_APPEND(&sv2, "CCC2");
+
+ test_validate_cleanup(AST_VECTOR_CALLBACK_DATA(&sv1, cb, "ARG", test) == 3);
+
+ test_validate_cleanup(AST_VECTOR_CALLBACK_DATA(&sv1, cb_first, test, test) == 1);
+
+ test_validate_cleanup(AST_VECTOR_CALLBACK_DATA(&sv2, cb, "ARG", test) == 3);
+
+ test_validate_cleanup(AST_VECTOR_CALLBACK_DATA(&sv2, cb_first, test, test) == 1);
+
+ test_validate_cleanup(AST_RWVECTOR_CALLBACK_DATA_RDLOCK(&sv2, cb, "ARG", test) == 3);
+
+ test_validate_cleanup(AST_RWVECTOR_CALLBACK_DATA_RDLOCK(&sv2, cb_first, test, test) == 1);
+
+ test_validate_cleanup(AST_RWVECTOR_CALLBACK_DATA_WRLOCK(&sv2, cb, "ARG", test) == 3);
+
+ test_validate_cleanup(AST_RWVECTOR_CALLBACK_DATA_WRLOCK(&sv2, cb_first, test, test) == 1);
+
+cleanup:
+ AST_VECTOR_FREE(&sv1);
+ AST_RWVECTOR_FREE(&sv2);
+
+ return rc;
+}
+
+static int unload_module(void)
+{
+ AST_TEST_UNREGISTER(callbacks);
+ AST_TEST_UNREGISTER(basic_ops);
+
+ return 0;
+}
+
+static int load_module(void)
+{
+ AST_TEST_REGISTER(callbacks);
+ AST_TEST_REGISTER(basic_ops);
+
+ return AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Vector test module");
--
To view, visit https://gerrit.asterisk.org/339
To unsubscribe, visit https://gerrit.asterisk.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I2e07ecc709d2f5f91bcab8904e5e9340609b00e0
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: George Joseph <george.joseph at fairview5.com>
More information about the asterisk-code-review
mailing list