Skip to content

Commit

Permalink
start of the incr fix, rearranges a bunch, adds util, tests, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
bradfitz authored and dustin committed Mar 19, 2009
1 parent 9791d32 commit 420aa2d
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -33,3 +33,4 @@ memcached-*.tar.gz
doc/protocol-binary-range.txt
doc/protocol-binary.txt
/sizes
/internal_tests
8 changes: 6 additions & 2 deletions 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=
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 \
Expand Down
23 changes: 23 additions & 0 deletions 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;
22 changes: 22 additions & 0 deletions 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;
}
4 changes: 2 additions & 2 deletions memcached.h
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions 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;
}
8 changes: 8 additions & 0 deletions 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);

0 comments on commit 420aa2d

Please sign in to comment.