Permalink
Browse files

Move find_tube and find_or_make_tube to tube.c

And renamed to have tube_ prefix
  • Loading branch information...
1 parent 63b663d commit 81a343fc6ee8979c000bbb3699caf42bc90a0f4c @gbarr gbarr committed Nov 4, 2008
Showing with 48 additions and 41 deletions.
  1. +4 −41 prot.c
  2. +40 −0 tube.c
  3. +4 −0 tube.h
View
@@ -222,7 +222,6 @@ static unsigned int ready_ct = 0;
static struct stats global_stat = {0, 0, 0, 0, 0};
static tube default_tube;
-static struct ms tubes;
static int drain_mode = 0;
static time_t start_time;
@@ -973,48 +972,12 @@ name_is_ok(const char *name, size_t max)
strspn(name, NAME_CHARS) == len && name[0] != '-';
}
-static tube
-find_tube(const char *name)
-{
- tube t;
- size_t i;
-
- for (i = 0; i < tubes.used; i++) {
- t = tubes.items[i];
- if (strncmp(t->name, name, MAX_TUBE_NAME_LEN) == 0) return t;
- }
- return NULL;
-}
-
void
prot_remove_tube(tube t)
{
ms_remove(&tubes, t);
}
-static tube
-make_and_insert_tube(const char *name)
-{
- int r;
- tube t = NULL;
-
- t = make_tube(name);
- if (!t) return NULL;
-
- /* We want this global tube list to behave like "weak" refs, so don't
- * increment the ref count. */
- r = ms_append(&tubes, t);
- if (!r) return tube_dref(t), NULL;
-
- return t;
-}
-
-static tube
-find_or_make_tube(const char *name)
-{
- return find_tube(name) ? : make_and_insert_tube(name);
-}
-
static void
dispatch_cmd(conn c)
{
@@ -1259,7 +1222,7 @@ dispatch_cmd(conn c)
op_ct[type]++;
- t = find_tube(name);
+ t = tube_find(name);
if (!t) return reply_msg(c, MSG_NOTFOUND);
do_stats(c, (fmt_fn) fmt_stats_tube, t);
@@ -1297,7 +1260,7 @@ dispatch_cmd(conn c)
if (!name_is_ok(name, 200)) return reply_msg(c, MSG_BAD_FORMAT);
op_ct[type]++;
- TUBE_ASSIGN(t, find_or_make_tube(name));
+ TUBE_ASSIGN(t, tube_find_or_make(name));
if (!t) return reply_serr(c, MSG_OUT_OF_MEMORY);
c->use->using_ct--;
@@ -1312,7 +1275,7 @@ dispatch_cmd(conn c)
if (!name_is_ok(name, 200)) return reply_msg(c, MSG_BAD_FORMAT);
op_ct[type]++;
- TUBE_ASSIGN(t, find_or_make_tube(name));
+ TUBE_ASSIGN(t, tube_find_or_make(name));
if (!t) return reply_serr(c, MSG_OUT_OF_MEMORY);
r = 1;
@@ -1607,6 +1570,6 @@ prot_init()
ms_init(&tubes, NULL, NULL);
- TUBE_ASSIGN(default_tube, find_or_make_tube("default"));
+ TUBE_ASSIGN(default_tube, tube_find_or_make("default"));
if (!default_tube) twarnx("Out of memory during startup!");
}
View
@@ -19,11 +19,14 @@
#include <stdlib.h>
#include <string.h>
+#include "ms.h"
#include "stat.h"
#include "tube.h"
#include "prot.h"
#include "util.h"
+struct ms tubes;
+
tube
make_tube(const char *name)
{
@@ -74,3 +77,40 @@ tube_iref(tube t)
if (!t) return;
++t->refs;
}
+
+static tube
+make_and_insert_tube(const char *name)
+{
+ int r;
+ tube t = NULL;
+
+ t = make_tube(name);
+ if (!t) return NULL;
+
+ /* We want this global tube list to behave like "weak" refs, so don't
+ * increment the ref count. */
+ r = ms_append(&tubes, t);
+ if (!r) return tube_dref(t), NULL;
+
+ return t;
+}
+
+tube
+tube_find(const char *name)
+{
+ tube t;
+ size_t i;
+
+ for (i = 0; i < tubes.used; i++) {
+ t = tubes.items[i];
+ if (strncmp(t->name, name, MAX_TUBE_NAME_LEN) == 0) return t;
+ }
+ return NULL;
+}
+
+tube
+tube_find_or_make(const char *name)
+{
+ return tube_find(name) ? : make_and_insert_tube(name);
+}
+
View
@@ -42,9 +42,13 @@ struct tube {
unsigned int watching_ct;
};
+extern struct ms tubes;
+
tube make_tube(const char *name);
void tube_dref(tube t);
void tube_iref(tube t);
+tube tube_find(const char *name);
+tube tube_find_or_make(const char *name);
#define TUBE_ASSIGN(a,b) (tube_dref(a), (a) = (b), tube_iref(a))
#endif /*tube_h*/

0 comments on commit 81a343f

Please sign in to comment.