vector: Uppercase API to follow C convention.

C does not support templates like C++.
........

Merged revisions 402438 from http://svn.asterisk.org/svn/asterisk/branches/12


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@402439 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Richard Mudgett
2013-11-02 04:30:49 +00:00
parent 629a5fc39b
commit 7d2f2d6ef8
3 changed files with 47 additions and 47 deletions

View File

@@ -39,7 +39,7 @@
* \param name Optional vector struct name. * \param name Optional vector struct name.
* \param type Vector element type. * \param type Vector element type.
*/ */
#define ast_vector(name, type) \ #define AST_VECTOR(name, type) \
struct name { \ struct name { \
type *elems; \ type *elems; \
size_t max; \ size_t max; \
@@ -58,7 +58,7 @@
* \return 0 on success. * \return 0 on success.
* \return Non-zero on failure. * \return Non-zero on failure.
*/ */
#define ast_vector_init(vec, size) ({ \ #define AST_VECTOR_INIT(vec, size) ({ \
size_t __size = (size); \ size_t __size = (size); \
size_t alloc_size = __size * sizeof(*((vec)->elems)); \ size_t alloc_size = __size * sizeof(*((vec)->elems)); \
(vec)->elems = alloc_size ? ast_malloc(alloc_size) : NULL; \ (vec)->elems = alloc_size ? ast_malloc(alloc_size) : NULL; \
@@ -79,7 +79,7 @@
* *
* \param vec Vector to deallocate. * \param vec Vector to deallocate.
*/ */
#define ast_vector_free(vec) do { \ #define AST_VECTOR_FREE(vec) do { \
ast_free((vec)->elems); \ ast_free((vec)->elems); \
(vec)->elems = NULL; \ (vec)->elems = NULL; \
(vec)->max = 0; \ (vec)->max = 0; \
@@ -95,7 +95,7 @@
* \return 0 on success. * \return 0 on success.
* \return Non-zero on failure. * \return Non-zero on failure.
*/ */
#define ast_vector_append(vec, elem) ({ \ #define AST_VECTOR_APPEND(vec, elem) ({ \
int res = 0; \ int res = 0; \
do { \ do { \
if ((vec)->current + 1 > (vec)->max) { \ if ((vec)->current + 1 > (vec)->max) { \
@@ -125,7 +125,7 @@
* \param idx Index of the element to remove. * \param idx Index of the element to remove.
* \return The element that was removed. * \return The element that was removed.
*/ */
#define ast_vector_remove_unordered(vec, idx) ({ \ #define AST_VECTOR_REMOVE_UNORDERED(vec, idx) ({ \
typeof((vec)->elems[0]) res; \ typeof((vec)->elems[0]) res; \
size_t __idx = (idx); \ size_t __idx = (idx); \
ast_assert(__idx < (vec)->current); \ ast_assert(__idx < (vec)->current); \
@@ -146,14 +146,14 @@
* \return 0 if element was removed. * \return 0 if element was removed.
* \return Non-zero if element was not in the vector. * \return Non-zero if element was not in the vector.
*/ */
#define ast_vector_remove_cmp_unordered(vec, value, cmp, cleanup) ({ \ #define AST_VECTOR_REMOVE_CMP_UNORDERED(vec, value, cmp, cleanup) ({ \
int res = -1; \ int res = -1; \
size_t idx; \ size_t idx; \
typeof(value) __value = (value); \ typeof(value) __value = (value); \
for (idx = 0; idx < (vec)->current; ++idx) { \ for (idx = 0; idx < (vec)->current; ++idx) { \
if (cmp((vec)->elems[idx], __value)) { \ if (cmp((vec)->elems[idx], __value)) { \
cleanup((vec)->elems[idx]); \ cleanup((vec)->elems[idx]); \
ast_vector_remove_unordered((vec), idx); \ AST_VECTOR_REMOVE_UNORDERED((vec), idx); \
res = 0; \ res = 0; \
break; \ break; \
} \ } \
@@ -162,7 +162,7 @@
}) })
/*! /*!
* \brief Default comparator for ast_vector_remove_elem_unordered() * \brief Default comparator for AST_VECTOR_REMOVE_ELEM_UNORDERED()
* *
* \param elem Element to compare against * \param elem Element to compare against
* \param value Value to compare with the vector element. * \param value Value to compare with the vector element.
@@ -191,8 +191,8 @@
* \return 0 if element was removed. * \return 0 if element was removed.
* \return Non-zero if element was not in the vector. * \return Non-zero if element was not in the vector.
*/ */
#define ast_vector_remove_elem_unordered(vec, elem, cleanup) ({ \ #define AST_VECTOR_REMOVE_ELEM_UNORDERED(vec, elem, cleanup) ({ \
ast_vector_remove_cmp_unordered((vec), (elem), \ AST_VECTOR_REMOVE_CMP_UNORDERED((vec), (elem), \
AST_VECTOR_ELEM_DEFAULT_CMP, cleanup); \ AST_VECTOR_ELEM_DEFAULT_CMP, cleanup); \
}) })
@@ -202,7 +202,7 @@
* \param vec Vector to query. * \param vec Vector to query.
* \return Number of elements in the vector. * \return Number of elements in the vector.
*/ */
#define ast_vector_size(vec) (vec)->current #define AST_VECTOR_SIZE(vec) (vec)->current
/*! /*!
* \brief Get an address of element in a vector. * \brief Get an address of element in a vector.
@@ -210,7 +210,7 @@
* \param vec Vector to query. * \param vec Vector to query.
* \param idx Index of the element to get address of. * \param idx Index of the element to get address of.
*/ */
#define ast_vector_get_addr(vec, idx) ({ \ #define AST_VECTOR_GET_ADDR(vec, idx) ({ \
size_t __idx = (idx); \ size_t __idx = (idx); \
ast_assert(__idx < (vec)->current); \ ast_assert(__idx < (vec)->current); \
&(vec)->elems[__idx]; \ &(vec)->elems[__idx]; \
@@ -222,7 +222,7 @@
* \param vec Vector to query. * \param vec Vector to query.
* \param idx Index of the element to get. * \param idx Index of the element to get.
*/ */
#define ast_vector_get(vec, idx) ({ \ #define AST_VECTOR_GET(vec, idx) ({ \
size_t __idx = (idx); \ size_t __idx = (idx); \
ast_assert(__idx < (vec)->current); \ ast_assert(__idx < (vec)->current); \
(vec)->elems[__idx]; \ (vec)->elems[__idx]; \

View File

@@ -140,10 +140,10 @@ STASIS_MESSAGE_TYPE_DEFN(stasis_subscription_change_type);
struct stasis_topic { struct stasis_topic {
char *name; char *name;
/*! Variable length array of the subscribers */ /*! Variable length array of the subscribers */
ast_vector(, struct stasis_subscription *) subscribers; AST_VECTOR(, struct stasis_subscription *) subscribers;
/*! Topics forwarding into this topic */ /*! Topics forwarding into this topic */
ast_vector(, struct stasis_topic *) upstream_topics; AST_VECTOR(, struct stasis_topic *) upstream_topics;
}; };
/* Forward declarations for the tightly-coupled subscription object */ /* Forward declarations for the tightly-coupled subscription object */
@@ -167,13 +167,13 @@ static void topic_dtor(void *obj)
/* Subscribers hold a reference to topics, so they should all be /* Subscribers hold a reference to topics, so they should all be
* unsubscribed before we get here. */ * unsubscribed before we get here. */
ast_assert(ast_vector_size(&topic->subscribers) == 0); ast_assert(AST_VECTOR_SIZE(&topic->subscribers) == 0);
ast_free(topic->name); ast_free(topic->name);
topic->name = NULL; topic->name = NULL;
ast_vector_free(&topic->subscribers); AST_VECTOR_FREE(&topic->subscribers);
ast_vector_free(&topic->upstream_topics); AST_VECTOR_FREE(&topic->upstream_topics);
} }
struct stasis_topic *stasis_topic_create(const char *name) struct stasis_topic *stasis_topic_create(const char *name)
@@ -192,8 +192,8 @@ struct stasis_topic *stasis_topic_create(const char *name)
return NULL; return NULL;
} }
res |= ast_vector_init(&topic->subscribers, INITIAL_SUBSCRIBERS_MAX); res |= AST_VECTOR_INIT(&topic->subscribers, INITIAL_SUBSCRIBERS_MAX);
res |= ast_vector_init(&topic->upstream_topics, 0); res |= AST_VECTOR_INIT(&topic->upstream_topics, 0);
if (res != 0) { if (res != 0) {
return NULL; return NULL;
@@ -428,8 +428,8 @@ int stasis_subscription_is_subscribed(const struct stasis_subscription *sub)
struct stasis_topic *topic = sub->topic; struct stasis_topic *topic = sub->topic;
SCOPED_AO2LOCK(lock_topic, topic); SCOPED_AO2LOCK(lock_topic, topic);
for (i = 0; i < ast_vector_size(&topic->subscribers); ++i) { for (i = 0; i < AST_VECTOR_SIZE(&topic->subscribers); ++i) {
if (ast_vector_get(&topic->subscribers, i) == sub) { if (AST_VECTOR_GET(&topic->subscribers, i) == sub) {
return 1; return 1;
} }
} }
@@ -480,11 +480,11 @@ static int topic_add_subscription(struct stasis_topic *topic, struct stasis_subs
* *
* If we bumped the refcount here, the owner would have to unsubscribe * If we bumped the refcount here, the owner would have to unsubscribe
* and cleanup, which is a bit awkward. */ * and cleanup, which is a bit awkward. */
ast_vector_append(&topic->subscribers, sub); AST_VECTOR_APPEND(&topic->subscribers, sub);
for (idx = 0; idx < ast_vector_size(&topic->upstream_topics); ++idx) { for (idx = 0; idx < AST_VECTOR_SIZE(&topic->upstream_topics); ++idx) {
topic_add_subscription( topic_add_subscription(
ast_vector_get(&topic->upstream_topics, idx), sub); AST_VECTOR_GET(&topic->upstream_topics, idx), sub);
} }
return 0; return 0;
@@ -495,12 +495,12 @@ static int topic_remove_subscription(struct stasis_topic *topic, struct stasis_s
size_t idx; size_t idx;
SCOPED_AO2LOCK(lock_topic, topic); SCOPED_AO2LOCK(lock_topic, topic);
for (idx = 0; idx < ast_vector_size(&topic->upstream_topics); ++idx) { for (idx = 0; idx < AST_VECTOR_SIZE(&topic->upstream_topics); ++idx) {
topic_remove_subscription( topic_remove_subscription(
ast_vector_get(&topic->upstream_topics, idx), sub); AST_VECTOR_GET(&topic->upstream_topics, idx), sub);
} }
return ast_vector_remove_elem_unordered(&topic->subscribers, sub, return AST_VECTOR_REMOVE_ELEM_UNORDERED(&topic->subscribers, sub,
AST_VECTOR_ELEM_CLEANUP_NOOP); AST_VECTOR_ELEM_CLEANUP_NOOP);
} }
@@ -549,8 +549,8 @@ void stasis_publish(struct stasis_topic *topic, struct stasis_message *message)
*/ */
ao2_ref(topic, +1); ao2_ref(topic, +1);
ao2_lock(topic); ao2_lock(topic);
for (i = 0; i < ast_vector_size(&topic->subscribers); ++i) { for (i = 0; i < AST_VECTOR_SIZE(&topic->subscribers); ++i) {
struct stasis_subscription *sub = ast_vector_get(&topic->subscribers, i); struct stasis_subscription *sub = AST_VECTOR_GET(&topic->subscribers, i);
ast_assert(sub != NULL); ast_assert(sub != NULL);
@@ -599,11 +599,11 @@ struct stasis_forward *stasis_forward_cancel(struct stasis_forward *forward)
to = forward->to_topic; to = forward->to_topic;
topic_lock_both(to, from); topic_lock_both(to, from);
ast_vector_remove_elem_unordered(&to->upstream_topics, from, AST_VECTOR_REMOVE_ELEM_UNORDERED(&to->upstream_topics, from,
AST_VECTOR_ELEM_CLEANUP_NOOP); AST_VECTOR_ELEM_CLEANUP_NOOP);
for (idx = 0; idx < ast_vector_size(&to->subscribers); ++idx) { for (idx = 0; idx < AST_VECTOR_SIZE(&to->subscribers); ++idx) {
topic_remove_subscription(from, ast_vector_get(&to->subscribers, idx)); topic_remove_subscription(from, AST_VECTOR_GET(&to->subscribers, idx));
} }
ao2_unlock(from); ao2_unlock(from);
ao2_unlock(to); ao2_unlock(to);
@@ -633,15 +633,15 @@ struct stasis_forward *stasis_forward_all(struct stasis_topic *from_topic,
forward->to_topic = ao2_bump(to_topic); forward->to_topic = ao2_bump(to_topic);
topic_lock_both(to_topic, from_topic); topic_lock_both(to_topic, from_topic);
res = ast_vector_append(&to_topic->upstream_topics, from_topic); res = AST_VECTOR_APPEND(&to_topic->upstream_topics, from_topic);
if (res != 0) { if (res != 0) {
ao2_unlock(from_topic); ao2_unlock(from_topic);
ao2_unlock(to_topic); ao2_unlock(to_topic);
return NULL; return NULL;
} }
for (idx = 0; idx < ast_vector_size(&to_topic->subscribers); ++idx) { for (idx = 0; idx < AST_VECTOR_SIZE(&to_topic->subscribers); ++idx) {
topic_add_subscription(from_topic, ast_vector_get(&to_topic->subscribers, idx)); topic_add_subscription(from_topic, AST_VECTOR_GET(&to_topic->subscribers, idx));
} }
ao2_unlock(from_topic); ao2_unlock(from_topic);
ao2_unlock(to_topic); ao2_unlock(to_topic);

View File

@@ -45,7 +45,7 @@ struct stasis_message_route {
void *data; void *data;
}; };
ast_vector(route_table, struct stasis_message_route); AST_VECTOR(route_table, struct stasis_message_route);
static struct stasis_message_route *route_table_find(struct route_table *table, static struct stasis_message_route *route_table_find(struct route_table *table,
struct stasis_message_type *message_type) struct stasis_message_type *message_type)
@@ -59,8 +59,8 @@ static struct stasis_message_route *route_table_find(struct route_table *table,
* tables, then we can look into containers with more efficient * tables, then we can look into containers with more efficient
* lookups. * lookups.
*/ */
for (idx = 0; idx < ast_vector_size(table); ++idx) { for (idx = 0; idx < AST_VECTOR_SIZE(table); ++idx) {
route = ast_vector_get_addr(table, idx); route = AST_VECTOR_GET_ADDR(table, idx);
if (route->message_type == message_type) { if (route->message_type == message_type) {
return route; return route;
} }
@@ -70,7 +70,7 @@ static struct stasis_message_route *route_table_find(struct route_table *table,
} }
/*! /*!
* \brief route_table comparator for ast_vector_remove_cmp_unordered() * \brief route_table comparator for AST_VECTOR_REMOVE_CMP_UNORDERED()
* *
* \param elem Element to compare against * \param elem Element to compare against
* \param value Value to compare with the vector element. * \param value Value to compare with the vector element.
@@ -92,7 +92,7 @@ static struct stasis_message_route *route_table_find(struct route_table *table,
static int route_table_remove(struct route_table *table, static int route_table_remove(struct route_table *table,
struct stasis_message_type *message_type) struct stasis_message_type *message_type)
{ {
return ast_vector_remove_cmp_unordered(table, message_type, ROUTE_TABLE_ELEM_CMP, return AST_VECTOR_REMOVE_CMP_UNORDERED(table, message_type, ROUTE_TABLE_ELEM_CMP,
ROUTE_TABLE_ELEM_CLEANUP); ROUTE_TABLE_ELEM_CLEANUP);
} }
@@ -110,7 +110,7 @@ static int route_table_add(struct route_table *table,
route.callback = callback; route.callback = callback;
route.data = data; route.data = data;
res = ast_vector_append(table, route); res = AST_VECTOR_APPEND(table, route);
if (res) { if (res) {
ROUTE_TABLE_ELEM_CLEANUP(route); ROUTE_TABLE_ELEM_CLEANUP(route);
} }
@@ -122,11 +122,11 @@ static void route_table_dtor(struct route_table *table)
size_t idx; size_t idx;
struct stasis_message_route *route; struct stasis_message_route *route;
for (idx = 0; idx < ast_vector_size(table); ++idx) { for (idx = 0; idx < AST_VECTOR_SIZE(table); ++idx) {
route = ast_vector_get_addr(table, idx); route = AST_VECTOR_GET_ADDR(table, idx);
ROUTE_TABLE_ELEM_CLEANUP(*route); ROUTE_TABLE_ELEM_CLEANUP(*route);
} }
ast_vector_free(table); AST_VECTOR_FREE(table);
} }
/*! \internal */ /*! \internal */
@@ -218,8 +218,8 @@ struct stasis_message_router *stasis_message_router_create(
} }
res = 0; res = 0;
res |= ast_vector_init(&router->routes, 0); res |= AST_VECTOR_INIT(&router->routes, 0);
res |= ast_vector_init(&router->cache_routes, 0); res |= AST_VECTOR_INIT(&router->cache_routes, 0);
if (res) { if (res) {
return NULL; return NULL;
} }