Skip to content

Commit 420aa2d

Browse files
bradfitzdustin
authored andcommitted
start of the incr fix, rearranges a bunch, adds util, tests, etc
1 parent 9791d32 commit 420aa2d

7 files changed

+78
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ memcached-*.tar.gz
3333
doc/protocol-binary-range.txt
3434
doc/protocol-binary.txt
3535
/sizes
36+
/internal_tests

Makefile.am

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
bin_PROGRAMS = memcached memcached-debug sizes
1+
bin_PROGRAMS = memcached memcached-debug sizes internal_tests
22
pkginclude_HEADERS = protocol_binary.h
33

44
BUILT_SOURCES=
@@ -10,6 +10,7 @@ memcached_SOURCES = memcached.c memcached.h \
1010
assoc.c assoc.h \
1111
thread.c daemon.c \
1212
stats.c stats.h \
13+
util.c util.h \
1314
trace.h
1415

1516
if BUILD_SOLARIS_PRIVS
@@ -26,6 +27,8 @@ memcached_DEPENDENCIES =
2627
memcached_debug_DEPENDENCIES =
2728
CLEANFILES=
2829

30+
internal_tests_SOURCES = internal_tests.c util.c
31+
2932
if BUILD_DTRACE
3033
BUILT_SOURCES += memcached_dtrace.h
3134
CLEANFILES += memcached_dtrace.h
@@ -58,8 +61,9 @@ EXTRA_DIST = doc scripts TODO t memcached.spec memcached_dtrace.d
5861

5962
MOSTLYCLEANFILES = *.gcov *.gcno *.gcda *.tcov
6063

61-
test: memcached-debug sizes
64+
test: memcached-debug internal_tests sizes
6265
$(srcdir)/sizes
66+
$(srcdir)/internal_tests
6367
prove $(srcdir)/t
6468
@if test `basename $(PROFILER)` = "gcov"; then \
6569
for file in memcached_debug-*.gc??; do \

globals.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "memcached.h"
2+
3+
/*
4+
* This file contains global variables shared across the rest of the
5+
* memcached codebase. These were originally in memcached.c but had
6+
* to be removed to make the rest of the object files linkable into
7+
* the test infrastructure.
8+
*
9+
*/
10+
11+
/*
12+
* We keep the current time of day in a global variable that's updated by a
13+
* timer event. This saves us a bunch of time() system calls (we really only
14+
* need to get the time once a second, whereas there can be tens of thousands
15+
* of requests a second) and allows us to use server-start-relative timestamps
16+
* rather than absolute UNIX timestamps, a space savings on systems where
17+
* sizeof(time_t) > sizeof(unsigned int).
18+
*/
19+
volatile rel_time_t current_time;
20+
21+
/** exported globals **/
22+
struct stats stats;
23+
struct settings settings;

internal_tests.c

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2+
3+
#include <assert.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
7+
#include "memcached.h"
8+
9+
int main(int argc, char **argv) {
10+
unsigned long long ull;
11+
assert(safe_strtoull("123", &ull));
12+
assert(ull == 123);
13+
14+
// Empty:
15+
assert(!safe_strtoull("", &ull));
16+
17+
// Bogus:
18+
assert(!safe_strtoull("123BOGUS", &ull));
19+
20+
printf("OK.\n");
21+
return 0;
22+
}

memcached.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#define UDP_MAX_PAYLOAD_SIZE 1400
2525
#define UDP_HEADER_SIZE 8
2626
#define MAX_SENDBUF_SIZE (256 * 1024 * 1024)
27-
/* I'm told the max legnth of a 64-bit num converted to string is 20 bytes.
27+
/* I'm told the max length of a 64-bit num converted to string is 20 bytes.
2828
* Plus a few for spaces, \r\n, \0 */
2929
#define SUFFIX_SIZE 24
3030

@@ -337,7 +337,7 @@ extern int daemonize(int nochdir, int noclose);
337337
#include "items.h"
338338
#include "trace.h"
339339
#include "hash.h"
340-
340+
#include "util.h"
341341

342342
/*
343343
* Functions such as the libevent-related calls that need to do cross-thread

util.c

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <stdlib.h>
2+
#include <assert.h>
3+
4+
#include "memcached.h"
5+
6+
bool safe_strtoull(const char *str, unsigned long long *out) {
7+
assert(out != NULL);
8+
*out = 0;
9+
char *endptr;
10+
unsigned long long ull = strtoull(str, &endptr, 10);
11+
if (*endptr == '\0' && endptr != str) {
12+
*out = ull;
13+
return true;
14+
}
15+
return false;
16+
}

util.h

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
* str a NULL-terminated base decimal 10 unsigned integer
3+
* out out parameter, if conversion succeeded
4+
*
5+
* returns true if conversion succeeded.
6+
*/
7+
bool safe_strtoull(const char *str, unsigned long long *out);
8+

0 commit comments

Comments
 (0)