summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrad Fitzpatrick <brad@danga.com>2008-10-14 21:03:05 -0700
committerDustin Sallings <dustin@spy.net>2009-03-19 01:52:53 -0700
commit420aa2d992093b78c1bba6cbb3577d5b53a19ec8 (patch)
treeb465c4262e27303f5178350f1a7d8239b864c1eb
parent9791d32edaa99d2cb3825c2e526b066a6cbe2e4c (diff)
downloadmemcached-420aa2d992093b78c1bba6cbb3577d5b53a19ec8.tar.gz
start of the incr fix, rearranges a bunch, adds util, tests, etc
-rw-r--r--.gitignore1
-rw-r--r--Makefile.am8
-rw-r--r--globals.c23
-rw-r--r--internal_tests.c22
-rw-r--r--memcached.h4
-rw-r--r--util.c16
-rw-r--r--util.h8
7 files changed, 78 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore
index affe663..195b246 100644
--- a/.gitignore
+++ b/.gitignore
@@ -33,3 +33,4 @@ memcached-*.tar.gz
doc/protocol-binary-range.txt
doc/protocol-binary.txt
/sizes
+/internal_tests \ No newline at end of file
diff --git a/Makefile.am b/Makefile.am
index ab4c26c..d263d07 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,4 +1,4 @@
-bin_PROGRAMS = memcached memcached-debug sizes
+bin_PROGRAMS = memcached memcached-debug sizes internal_tests
pkginclude_HEADERS = protocol_binary.h
BUILT_SOURCES=
@@ -10,6 +10,7 @@ memcached_SOURCES = memcached.c memcached.h \
assoc.c assoc.h \
thread.c daemon.c \
stats.c stats.h \
+ util.c util.h \
trace.h
if BUILD_SOLARIS_PRIVS
@@ -26,6 +27,8 @@ memcached_DEPENDENCIES =
memcached_debug_DEPENDENCIES =
CLEANFILES=
+internal_tests_SOURCES = internal_tests.c util.c
+
if BUILD_DTRACE
BUILT_SOURCES += memcached_dtrace.h
CLEANFILES += memcached_dtrace.h
@@ -58,8 +61,9 @@ EXTRA_DIST = doc scripts TODO t memcached.spec memcached_dtrace.d
MOSTLYCLEANFILES = *.gcov *.gcno *.gcda *.tcov
-test: memcached-debug sizes
+test: memcached-debug internal_tests sizes
$(srcdir)/sizes
+ $(srcdir)/internal_tests
prove $(srcdir)/t
@if test `basename $(PROFILER)` = "gcov"; then \
for file in memcached_debug-*.gc??; do \
diff --git a/globals.c b/globals.c
new file mode 100644
index 0000000..7d7b2a3
--- /dev/null
+++ b/globals.c
@@ -0,0 +1,23 @@
+#include "memcached.h"
+
+/*
+ * This file contains global variables shared across the rest of the
+ * memcached codebase. These were originally in memcached.c but had
+ * to be removed to make the rest of the object files linkable into
+ * the test infrastructure.
+ *
+ */
+
+/*
+ * We keep the current time of day in a global variable that's updated by a
+ * timer event. This saves us a bunch of time() system calls (we really only
+ * need to get the time once a second, whereas there can be tens of thousands
+ * of requests a second) and allows us to use server-start-relative timestamps
+ * rather than absolute UNIX timestamps, a space savings on systems where
+ * sizeof(time_t) > sizeof(unsigned int).
+ */
+volatile rel_time_t current_time;
+
+/** exported globals **/
+struct stats stats;
+struct settings settings;
diff --git a/internal_tests.c b/internal_tests.c
new file mode 100644
index 0000000..836b012
--- /dev/null
+++ b/internal_tests.c
@@ -0,0 +1,22 @@
+/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "memcached.h"
+
+int main(int argc, char **argv) {
+ unsigned long long ull;
+ assert(safe_strtoull("123", &ull));
+ assert(ull == 123);
+
+ // Empty:
+ assert(!safe_strtoull("", &ull));
+
+ // Bogus:
+ assert(!safe_strtoull("123BOGUS", &ull));
+
+ printf("OK.\n");
+ return 0;
+}
diff --git a/memcached.h b/memcached.h
index 2a606a8..0b2f0df 100644
--- a/memcached.h
+++ b/memcached.h
@@ -24,7 +24,7 @@
#define UDP_MAX_PAYLOAD_SIZE 1400
#define UDP_HEADER_SIZE 8
#define MAX_SENDBUF_SIZE (256 * 1024 * 1024)
-/* I'm told the max legnth of a 64-bit num converted to string is 20 bytes.
+/* I'm told the max length of a 64-bit num converted to string is 20 bytes.
* Plus a few for spaces, \r\n, \0 */
#define SUFFIX_SIZE 24
@@ -337,7 +337,7 @@ extern int daemonize(int nochdir, int noclose);
#include "items.h"
#include "trace.h"
#include "hash.h"
-
+#include "util.h"
/*
* Functions such as the libevent-related calls that need to do cross-thread
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..dbd62a1
--- /dev/null
+++ b/util.c
@@ -0,0 +1,16 @@
+#include <stdlib.h>
+#include <assert.h>
+
+#include "memcached.h"
+
+bool safe_strtoull(const char *str, unsigned long long *out) {
+ assert(out != NULL);
+ *out = 0;
+ char *endptr;
+ unsigned long long ull = strtoull(str, &endptr, 10);
+ if (*endptr == '\0' && endptr != str) {
+ *out = ull;
+ return true;
+ }
+ return false;
+}
diff --git a/util.h b/util.h
new file mode 100644
index 0000000..24b0da6
--- /dev/null
+++ b/util.h
@@ -0,0 +1,8 @@
+/*
+ * str a NULL-terminated base decimal 10 unsigned integer
+ * out out parameter, if conversion succeeded
+ *
+ * returns true if conversion succeeded.
+ */
+bool safe_strtoull(const char *str, unsigned long long *out);
+