[asterisk-commits] nadi: branch nadi/trunk-cm r44557 -
/team/nadi/trunk-cm/res/res_csel.c
asterisk-commits at lists.digium.com
asterisk-commits at lists.digium.com
Fri Oct 6 03:30:44 MST 2006
Author: nadi
Date: Fri Oct 6 05:30:43 2006
New Revision: 44557
URL: http://svn.digium.com/view/asterisk?rev=44557&view=rev
Log:
new methods: standard and round_robin. standard is the fallback.
Modified:
team/nadi/trunk-cm/res/res_csel.c
Modified: team/nadi/trunk-cm/res/res_csel.c
URL: http://svn.digium.com/view/asterisk/team/nadi/trunk-cm/res/res_csel.c?rev=44557&r1=44556&r2=44557&view=diff
==============================================================================
--- team/nadi/trunk-cm/res/res_csel.c (original)
+++ team/nadi/trunk-cm/res/res_csel.c Fri Oct 6 05:30:43 2006
@@ -31,6 +31,7 @@
#include "asterisk/module.h"
#include "asterisk/logger.h"
#include "asterisk/lock.h"
+#include "asterisk/utils.h"
#include <stdlib.h>
#include <string.h>
@@ -58,9 +59,61 @@
#define LOCK(cs) ast_mutex_lock(&(cs)->lock)
#define UNLOCK(cs) ast_mutex_unlock(&(cs)->lock)
+/* STANDARD: begin */
+struct standard_data {
+ int size;
+ void **arr;
+};
+
+static int standard_init (struct csel *cs, const char *params)
+{
+ cs->data = calloc(1, sizeof(struct standard_data));
+ return cs->data ? 0 : 1;
+}
+
+static void standard_destroy (struct csel *cs)
+{
+ struct standard_data *data = cs->data;
+ int i = 0;
+
+ if (data) {
+ if (data->arr) {
+ if (cs->free)
+ for (; i < data->size; ++i)
+ cs->free(data->arr[i]);
+ free(data->arr);
+ }
+ free(data);
+ }
+}
+
+static int standard_add (struct csel *cs, void *priv)
+{
+ struct standard_data *data = cs->data;
+
+ ++data->size;
+ data->arr = realloc(data->arr, data->size * sizeof(void *));
+ data->arr[data->size - 1] = priv;
+
+ return 0;
+}
+
+static void * standard_get_next (struct csel *cs)
+{
+ struct standard_data *data = cs->data;
+ void *p;
+ int i = 0;
+
+ for (; i < data->size; ++i) {
+ if ((p = cs->occupy(data->arr[i])))
+ return p;
+ }
+
+ return NULL;
+}
+/* STANDARD: end */
+
/* RANDOM: begin */
-#include "asterisk/utils.h"
-
struct rand_data {
int size;
void **arr;
@@ -114,12 +167,74 @@
data->arr[i - 1] = data->arr[pos];
data->arr[pos] = p;
}
+
return NULL;
}
/* RANDOM: end */
+/* ROUND ROBIN: begin */
+struct rr_data {
+ int size;
+ int curr;
+ void **arr;
+};
+
+static int rr_init (struct csel *cs, const char *params)
+{
+ cs->data = calloc(1, sizeof(struct rr_data));
+ return cs->data ? 0 : 1;
+}
+
+static void rr_destroy (struct csel *cs)
+{
+ struct rr_data *data = cs->data;
+ int i = 0;
+
+ if (data) {
+ if (data->arr) {
+ if (cs->free)
+ for (; i < data->size; ++i)
+ cs->free(data->arr[i]);
+ free(data->arr);
+ }
+ free(data);
+ }
+}
+
+static int rr_add (struct csel *cs, void *priv)
+{
+ struct rr_data *data = cs->data;
+
+ ++data->size;
+ data->arr = realloc(data->arr, data->size * sizeof(void *));
+ data->arr[data->size - 1] = priv;
+
+ return 0;
+}
+
+static void * rr_get_next (struct csel *cs)
+{
+ struct rr_data *data = cs->data;
+ void *p;
+ int begin = (data->curr + 1) % data->size,
+ i = begin;
+
+ do {
+ if ((p = cs->occupy(data->arr[i]))) {
+ data->curr = i;
+ return p;
+ }
+ i = (i + 1) % data->size;
+ } while (i != begin);
+
+ return NULL;
+}
+/* ROUND ROBIN: end */
+
static struct method methods[] = {
+ { "standard", standard_init, standard_destroy, standard_add, standard_get_next, 0, 0 },
{ "random", rand_init, rand_destroy, rand_add, rand_get_next, 0, 0 },
+ { "round_robin", rr_init, rr_destroy, rr_add, rr_get_next, 0, 0 },
};
struct csel * csel_create (const char *method,
@@ -130,14 +245,15 @@
struct csel *cs;
int i = 0;
- for (; i < (sizeof(methods) / sizeof(struct method)); ++i) {
- if (!strcasecmp(methods[i].name, method))
- break;
- }
-
- if (i == (sizeof(methods) / sizeof(struct method))) {
- ast_log(LOG_WARNING, "csel: unknown method: %s, falling back to: %s\n", method, methods[0].name);
- i = 0;
+ if (method) {
+ for (; i < (sizeof(methods) / sizeof(struct method)); ++i) {
+ if (!strcasecmp(methods[i].name, method))
+ break;
+ }
+ if (i == (sizeof(methods) / sizeof(struct method))) {
+ ast_log(LOG_WARNING, "csel: unknown method: %s, falling back to: %s\n", method, methods[0].name);
+ i = 0;
+ }
}
cs = calloc(1, sizeof(struct csel));
More information about the asterisk-commits
mailing list