[memcached] plindner, r490: Add cleanup patch from "Tim Yardley" <li...

commits at code.sixapart.com commits at code.sixapart.com
Sun Apr 8 15:29:07 UTC 2007


Add cleanup patch from "Tim Yardley" <liquid at haveheart.com> to
clean up source spacing issues, fix -Wall warnings, add some
null checks, adds asserts at the top of each function for any
use of conn *c without checking to see if c is NULL first.

(merges in rev 488 from trunk too..)


U   branches/multithreaded/server/ChangeLog
U   branches/multithreaded/server/items.c
U   branches/multithreaded/server/memcached.c
U   branches/multithreaded/server/slabs.c
U   branches/multithreaded/server/t/stats.t


Modified: branches/multithreaded/server/ChangeLog
===================================================================
--- branches/multithreaded/server/ChangeLog	2007-04-08 15:27:38 UTC (rev 489)
+++ branches/multithreaded/server/ChangeLog	2007-04-08 15:29:03 UTC (rev 490)
@@ -1,3 +1,10 @@
+2007-04-08  Paul Lindner  <lindner at inuus.com>
+
+	* Add cleanup patch from "Tim Yardley" <liquid at haveheart.com> to
+	  clean up source spacing issues, fix -Wall warnings, add some
+	  null checks, adds asserts at the top of each function for any
+	  use of conn *c without checking to see if c is NULL first.
+
 2007-04-04  Paul Lindner  <lindner at inuus.com>
 
 	* Add clarification of flush_all in the protocol docs

Modified: branches/multithreaded/server/items.c
===================================================================
--- branches/multithreaded/server/items.c	2007-04-08 15:27:38 UTC (rev 489)
+++ branches/multithreaded/server/items.c	2007-04-08 15:29:03 UTC (rev 490)
@@ -32,10 +32,10 @@
 
 void item_init(void) {
     int i;
-    for(i=0; i<LARGEST_ID; i++) {
-        heads[i]=NULL;
-        tails[i]=NULL;
-        sizes[i]=0;
+    for(i = 0; i < LARGEST_ID; i++) {
+        heads[i] = NULL;
+        tails[i] = NULL;
+        sizes[i] = 0;
     }
 }
 
@@ -73,14 +73,11 @@
 /*@null@*/
 item *do_item_alloc(char *key, const size_t nkey, const int flags, const rel_time_t exptime, const int nbytes) {
     uint8_t nsuffix;
-    size_t ntotal;
     item *it;
-    unsigned int id;
     char suffix[40];
+    size_t ntotal = item_make_header(nkey + 1, flags, nbytes, suffix, &nsuffix);
 
-    ntotal = item_make_header(nkey + 1, flags, nbytes, suffix, &nsuffix);
- 
-    id = slabs_clsid(ntotal);
+    unsigned int id = slabs_clsid(ntotal);
     if (id == 0)
         return 0;
 
@@ -103,10 +100,10 @@
          */
 
         if (id > LARGEST_ID) return NULL;
-        if (tails[id]==0) return NULL;
+        if (tails[id] == 0) return NULL;
 
-        for (search = tails[id]; tries>0 && search; tries--, search=search->prev) {
-            if (search->refcount==0) {
+        for (search = tails[id]; tries > 0 && search; tries--, search=search->prev) {
+            if (search->refcount == 0) {
                if (search->exptime > current_time) {
                        STATS_LOCK();
                        stats.evictions++;
@@ -117,7 +114,7 @@
             }
         }
         it = slabs_alloc(ntotal);
-        if (it==0) return NULL;
+        if (it == 0) return NULL;
     }
 
     assert(it->slabs_clsid == 0);
@@ -134,7 +131,7 @@
     it->nbytes = nbytes;
     strcpy(ITEM_key(it), key);
     it->exptime = exptime;
-    memcpy(ITEM_suffix(it), suffix, (size_t) nsuffix);
+    memcpy(ITEM_suffix(it), suffix, (size_t)nsuffix);
     it->nsuffix = nsuffix;
     return it;
 }
@@ -270,7 +267,7 @@
 
 /*@null@*/
 char *item_cachedump(const unsigned int slabs_clsid, const unsigned int limit, unsigned int *bytes) {
-    int memlimit = 2*1024*1024;
+    int memlimit = 2097152; /* 2097152: (2 * 1024 * 1024) */
     char *buffer;
     unsigned int bufcurr;
     item *it;
@@ -285,18 +282,18 @@
     if (buffer == 0) return NULL;
     bufcurr = 0;
 
-    while (it != NULL && (limit==0 || shown < limit)) {
+    while (it != NULL && (limit == 0 || shown < limit)) {
         len = snprintf(temp, 512, "ITEM %s [%d b; %lu s]\r\n", ITEM_key(it), it->nbytes - 2, it->time + stats.started);
         if (bufcurr + len + 6 > memlimit)  /* 6 is END\r\n\0 */
             break;
         strcpy(buffer + bufcurr, temp);
-        bufcurr+=len;
+        bufcurr += len;
         shown++;
         it = it->next;
     }
 
-    strcpy(buffer+bufcurr, "END\r\n");
-    bufcurr+=5;
+    strcpy(buffer + bufcurr, "END\r\n");
+    bufcurr += 5;
 
     *bytes = bufcurr;
     return buffer;
@@ -312,7 +309,7 @@
         return;
     }
 
-    for (i=0; i<LARGEST_ID; i++) {
+    for (i = 0; i < LARGEST_ID; i++) {
         if (tails[i])
             bufcurr += snprintf(bufcurr, (size_t)buflen, "STAT items:%d:number %u\r\nSTAT items:%d:age %u\r\n",
                                i, sizes[i], i, now - tails[i]->time);
@@ -325,8 +322,8 @@
 /*@null@*/
 char* item_stats_sizes(int *bytes) {
     const int num_buckets = 32768;   /* max 1MB object, divided into 32 bytes size buckets */
-    unsigned int *histogram = (unsigned int*) malloc((size_t)num_buckets * sizeof(int));
-    char *buf = (char*) malloc(1024*1024*2*sizeof(char));
+    unsigned int *histogram = (unsigned int *)malloc((size_t)num_buckets * sizeof(int));
+    char *buf = (char *)malloc(2097152 * sizeof(char)); /* 2097152: 2 * 1024 * 1024 */
     int i;
 
     if (histogram == 0 || buf == 0) {
@@ -336,8 +333,8 @@
     }
 
     /* build the histogram */
-    memset(histogram, 0, (size_t) num_buckets * sizeof(int));
-    for (i=0; i<LARGEST_ID; i++) {
+    memset(histogram, 0, (size_t)num_buckets * sizeof(int));
+    for (i = 0; i < LARGEST_ID; i++) {
         item *iter = heads[i];
         while (iter) {
             int ntotal = ITEM_ntotal(iter);
@@ -350,9 +347,9 @@
 
     /* write the buffer */
     *bytes = 0;
-    for (i=0; i<num_buckets; i++) {
+    for (i = 0; i < num_buckets; i++) {
         if (histogram[i] != 0) {
-            *bytes += sprintf(&buf[*bytes], "%d %u\r\n", i*32, histogram[i]);
+            *bytes += sprintf(&buf[*bytes], "%d %u\r\n", i * 32, histogram[i]);
         }
     }
     *bytes += sprintf(&buf[*bytes], "END\r\n");

Modified: branches/multithreaded/server/memcached.c
===================================================================
--- branches/multithreaded/server/memcached.c	2007-04-08 15:27:38 UTC (rev 489)
+++ branches/multithreaded/server/memcached.c	2007-04-08 15:29:03 UTC (rev 490)
@@ -97,6 +97,7 @@
                               (to avoid 64 bit time_t) */
 
 void pre_gdb(void);
+void conn_free(conn *c);
 
 /** exported globals **/
 struct stats stats;
@@ -116,8 +117,6 @@
 
 static int *buckets = 0; /* bucket->generation array for a managed instance */
 
-void conn_free(conn *c);
-
 #define REALTIME_MAXDELTA 60*60*24*30
 /*
  * given time value that's either unix time or delta from current unix time, return
@@ -137,10 +136,10 @@
            future, effectively making items expiring in the past
            really expiring never */
         if (exptime <= stats.started)
-            return (rel_time_t) 1;
-        return (rel_time_t) (exptime - stats.started);
+            return (rel_time_t)1;
+        return (rel_time_t)(exptime - stats.started);
     } else {
-        return (rel_time_t) (exptime + current_time);
+        return (rel_time_t)(exptime + current_time);
     }
 }
 
@@ -170,7 +169,7 @@
     settings.port = 11211;
     settings.udpport = 0;
     settings.interface.s_addr = htonl(INADDR_ANY);
-    settings.maxbytes = 64*1024*1024; /* default is 64MB */
+    settings.maxbytes = 67108864; /* default is 64MB: (64 * 1024 * 1024) */
     settings.maxconns = 1024;         /* to limit connections-related memory to about 5MB */
     settings.verbose = 0;
     settings.oldest_live = 0;
@@ -204,6 +203,8 @@
 {
     struct msghdr *msg;
 
+    assert(c != NULL);
+
     if (c->msgsize == c->msgused) {
         msg = realloc(c->msglist, c->msgsize * 2 * sizeof(struct msghdr));
         if (! msg)
@@ -246,8 +247,9 @@
 static void conn_init(void) {
     freetotal = 200;
     freecurr = 0;
-    freeconns = (conn **)malloc(sizeof (conn *)*freetotal);
-    /** TODO check for NULL **/
+    if (!(freeconns = (conn **)malloc(sizeof(conn *) * freetotal))) {
+        perror("malloc()");
+    }
     return;
 }
 
@@ -277,7 +279,7 @@
         return 0;
     } else {
         /* try to enlarge free connections array */
-        conn **new_freeconns = realloc(freeconns, sizeof(conn *)*freetotal*2);
+        conn **new_freeconns = realloc(freeconns, sizeof(conn *) * freetotal * 2);
         if (new_freeconns) {
             freetotal *= 2;
             freeconns = new_freeconns;
@@ -310,11 +312,11 @@
         c->msgsize = MSG_LIST_INITIAL;
         c->hdrsize = 0;
 
-        c->rbuf = (char *) malloc((size_t)c->rsize);
-        c->wbuf = (char *) malloc((size_t)c->wsize);
-        c->ilist = (item **) malloc(sizeof(item *) * c->isize);
-        c->iov = (struct iovec *) malloc(sizeof(struct iovec) * c->iovsize);
-        c->msglist = (struct msghdr *) malloc(sizeof(struct msghdr) * c->msgsize);
+        c->rbuf = (char *)malloc((size_t)c->rsize);
+        c->wbuf = (char *)malloc((size_t)c->wsize);
+        c->ilist = (item **)malloc(sizeof(item *) * c->isize);
+        c->iov = (struct iovec *)malloc(sizeof(struct iovec) * c->iovsize);
+        c->msglist = (struct msghdr *)malloc(sizeof(struct msghdr) * c->msgsize);
 
         if (c->rbuf == 0 || c->wbuf == 0 || c->ilist == 0 || c->iov == 0 ||
                 c->msglist == 0) {
@@ -382,6 +384,8 @@
 }
 
 static void conn_cleanup(conn *c) {
+    assert(c != NULL);
+
     if (c->item) {
         item_remove(c->item);
         c->item = 0;
@@ -421,6 +425,8 @@
 }
 
 static void conn_close(conn *c) {
+    assert(c != NULL);
+
     /* delete the event, the socket and the conn */
     event_del(&c->event);
 
@@ -453,14 +459,16 @@
  * buffers!
  */
 static void conn_shrink(conn *c) {
+    assert(c != NULL);
+
     if (c->udp)
         return;
 
     if (c->rsize > READ_BUFFER_HIGHWAT && c->rbytes < DATA_BUFFER_SIZE) {
         if (c->rcurr != c->rbuf)
-            memmove(c->rbuf, c->rcurr, (size_t) c->rbytes);
+            memmove(c->rbuf, c->rcurr, (size_t)c->rbytes);
 
-        char *newbuf = (char*) realloc((void*)c->rbuf, DATA_BUFFER_SIZE);
+        char *newbuf = (char *)realloc((void *)c->rbuf, DATA_BUFFER_SIZE);
 
         if (newbuf) {
             c->rbuf = newbuf;
@@ -471,7 +479,7 @@
     }
 
     if (c->isize > ITEM_LIST_HIGHWAT) {
-        item **newbuf = (item**) realloc((void*)c->ilist, ITEM_LIST_INITIAL * sizeof(c->ilist[0]));
+        item **newbuf = (item**) realloc((void *)c->ilist, ITEM_LIST_INITIAL * sizeof(c->ilist[0]));
         if (newbuf) {
             c->ilist = newbuf;
             c->isize = ITEM_LIST_INITIAL;
@@ -480,7 +488,7 @@
     }
 
     if (c->msgsize > MSG_LIST_HIGHWAT) {
-        struct msghdr *newbuf = (struct msghdr*) realloc((void*)c->msglist, MSG_LIST_INITIAL * sizeof(c->msglist[0]));
+        struct msghdr *newbuf = (struct msghdr *) realloc((void *)c->msglist, MSG_LIST_INITIAL * sizeof(c->msglist[0]));
         if (newbuf) {
             c->msglist = newbuf;
             c->msgsize = MSG_LIST_INITIAL;
@@ -489,7 +497,7 @@
     }
 
     if (c->iovsize > IOV_LIST_HIGHWAT) {
-        struct iovec* newbuf = (struct iovec *) realloc((void*)c->iov, IOV_LIST_INITIAL * sizeof(c->iov[0]));
+        struct iovec *newbuf = (struct iovec *) realloc((void *)c->iov, IOV_LIST_INITIAL * sizeof(c->iov[0]));
         if (newbuf) {
             c->iov = newbuf;
             c->iovsize = IOV_LIST_INITIAL;
@@ -504,6 +512,8 @@
  * happen here.
  */
 static void conn_set_state(conn *c, int state) {
+    assert(c != NULL);
+
     if (state != c->state) {
         if (state == conn_read) {
             conn_shrink(c);
@@ -521,9 +531,11 @@
  * Returns 0 on success, -1 on out-of-memory.
  */
 static int ensure_iov_space(conn *c) {
+    assert(c != NULL);
+
     if (c->iovused >= c->iovsize) {
         int i, iovnum;
-        struct iovec *new_iov = (struct iovec *) realloc(c->iov,
+        struct iovec *new_iov = (struct iovec *)realloc(c->iov,
                                 (c->iovsize * 2) * sizeof(struct iovec));
         if (! new_iov)
             return -1;
@@ -553,6 +565,8 @@
     int leftover;
     bool limit_to_mtu;
 
+    assert(c != NULL);
+
     do {
         m = &c->msglist[c->msgused - 1];
 
@@ -581,7 +595,7 @@
         }
 
         m = &c->msglist[c->msgused - 1];
-        m->msg_iov[m->msg_iovlen].iov_base = (void*) buf;
+        m->msg_iov[m->msg_iovlen].iov_base = (void *)buf;
         m->msg_iov[m->msg_iovlen].iov_len = len;
 
         c->msgbytes += len;
@@ -603,6 +617,8 @@
     int i;
     unsigned char *hdr;
 
+    assert(c != NULL);
+
     if (c->msgused > c->hdrsize) {
         void *new_hdrbuf;
         if (c->hdrbuf)
@@ -611,7 +627,7 @@
             new_hdrbuf = malloc(c->msgused * 2 * UDP_HEADER_SIZE);
         if (! new_hdrbuf)
             return -1;
-        c->hdrbuf = (unsigned char *) new_hdrbuf;
+        c->hdrbuf = (unsigned char *)new_hdrbuf;
         c->hdrsize = c->msgused * 2;
     }
 
@@ -627,7 +643,7 @@
         *hdr++ = c->msgused % 256;
         *hdr++ = 0;
         *hdr++ = 0;
-        assert((void*) hdr == (void*) c->msglist[i].msg_iov[0].iov_base + UDP_HEADER_SIZE);
+        assert((void *) hdr == (void *)c->msglist[i].msg_iov[0].iov_base + UDP_HEADER_SIZE);
     }
 
     return 0;
@@ -637,11 +653,13 @@
 static void out_string(conn *c, const char *str) {
     int len;
 
+    assert(c != NULL);
+
     if (settings.verbose > 1)
         fprintf(stderr, ">%d %s\n", c->sfd, str);
 
     len = strlen(str);
-    if (len + 2 > c->wsize) {
+    if ((len + 2) > c->wsize) {
         /* ought to be always enough. just fail for simplicity */
         str = "SERVER_ERROR output line too long";
         len = strlen(str);
@@ -663,6 +681,8 @@
  */
 
 static void complete_nread(conn *c) {
+    assert(c != NULL);
+
     item *it = c->item;
     int comm = c->item_comm;
 
@@ -725,7 +745,7 @@
 }
 
 typedef struct token_s {
-    char* value;
+    char *value;
     size_t length;
 } token_t;
 
@@ -753,9 +773,9 @@
  *      command  = tokens[ix].value;
  *   }
  */
-static size_t tokenize_command(char* command, token_t* tokens, const size_t max_tokens)  {
-    char* cp;
-    char* value = NULL;
+static size_t tokenize_command(char *command, token_t *tokens, const size_t max_tokens)  {
+    char *cp;
+    char *value = NULL;
     size_t length = 0;
     size_t ntokens = 0;
 
@@ -800,6 +820,8 @@
 }
 
 inline void process_stats_detail(conn *c, const char *command) {
+    assert(c != NULL);
+
     if (strcmp(command, "on") == 0) {
         settings.detail_enabled = 1;
         out_string(c, "OK");
@@ -827,11 +849,13 @@
     }
 }
 
-static void process_stat(conn *c, token_t* tokens, const size_t ntokens) {
+static void process_stat(conn *c, token_t *tokens, const size_t ntokens) {
     rel_time_t now = current_time;
-    char* command;
-    char* subcommand;
+    char *command;
+    char *subcommand;
 
+    assert(c != NULL);
+
     if(ntokens < 2) {
         out_string(c, "CLIENT_ERROR bad command line");
         return;
@@ -852,7 +876,7 @@
         pos += sprintf(pos, "STAT uptime %u\r\n", now);
         pos += sprintf(pos, "STAT time %ld\r\n", now + stats.started);
         pos += sprintf(pos, "STAT version " VERSION "\r\n");
-        pos += sprintf(pos, "STAT pointer_size %d\r\n", 8 * sizeof(void*));
+        pos += sprintf(pos, "STAT pointer_size %d\r\n", 8 * sizeof(void *));
         pos += sprintf(pos, "STAT rusage_user %ld.%06ld\r\n", usage.ru_utime.tv_sec, usage.ru_utime.tv_usec);
         pos += sprintf(pos, "STAT rusage_system %ld.%06ld\r\n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);
         pos += sprintf(pos, "STAT curr_items %u\r\n", stats.curr_items);
@@ -868,7 +892,7 @@
         pos += sprintf(pos, "STAT evictions %llu\r\n", stats.evictions);
         pos += sprintf(pos, "STAT bytes_read %llu\r\n", stats.bytes_read);
         pos += sprintf(pos, "STAT bytes_written %llu\r\n", stats.bytes_written);
-        pos += sprintf(pos, "STAT limit_maxbytes %llu\r\n", (unsigned long long) settings.maxbytes);
+        pos += sprintf(pos, "STAT limit_maxbytes %llu\r\n", (unsigned long long)settings.maxbytes);
         pos += sprintf(pos, "STAT threads %u\r\n", settings.num_threads);
         pos += sprintf(pos, "END");
         STATS_UNLOCK();
@@ -914,8 +938,7 @@
         int fd;
         int res;
 
-        wbuf = (char *)malloc(wsize);
-        if (wbuf == 0) {
+        if (!(wbuf = (char *)malloc(wsize))) {
             out_string(c, "SERVER_ERROR out of memory");
             return;
         }
@@ -939,8 +962,8 @@
             return;
         }
         strcpy(wbuf + res, "END\r\n");
-        c->write_and_free=wbuf;
-        c->wcurr=wbuf;
+        c->write_and_free = wbuf;
+        c->wcurr = wbuf;
         c->wbytes = res + 5; // Don't write the terminal '\0' 
         conn_set_state(c, conn_write);
         c->write_and_go = conn_read;
@@ -980,7 +1003,7 @@
         return;
     }
 
-    if (strcmp(subcommand, "slabs")==0) {
+    if (strcmp(subcommand, "slabs") == 0) {
         int bytes = 0;
         char *buf = slabs_stats(&bytes);
         if (!buf) {
@@ -995,14 +1018,14 @@
         return;
     }
 
-    if (strcmp(subcommand, "items")==0) {
+    if (strcmp(subcommand, "items") == 0) {
         char buffer[4096];
         item_stats(buffer, 4096);
         out_string(c, buffer);
         return;
     }
 
-    if (strcmp(subcommand, "detail")==0) {
+    if (strcmp(subcommand, "detail") == 0) {
         if (ntokens < 4)
             process_stats_detail(c, "");  /* outputs the error message */
         else
@@ -1010,7 +1033,7 @@
         return;
     }
 
-    if (strcmp(subcommand, "sizes")==0) {
+    if (strcmp(subcommand, "sizes") == 0) {
         int bytes = 0;
         char *buf = item_stats_sizes(&bytes);
         if (! buf) {
@@ -1030,13 +1053,15 @@
 }
 
 /* ntokens is overwritten here... shrug.. */
-static inline void process_get_command(conn *c, token_t* tokens, size_t ntokens) {
+static inline void process_get_command(conn *c, token_t *tokens, size_t ntokens) {
     char *key;
     size_t nkey;
     int i = 0;
     item *it;
-    token_t* key_token = &tokens[KEY_TOKEN];
+    token_t *key_token = &tokens[KEY_TOKEN];
 
+    assert(c != NULL);
+
     if (settings.managed) {
         int bucket = c->bucket;
         if (bucket == -1) {
@@ -1070,7 +1095,7 @@
             }
             if (it) {
                 if (i >= c->isize) {
-                    item **new_list = realloc(c->ilist, sizeof(item *)*c->isize*2);
+                    item **new_list = realloc(c->ilist, sizeof(item *) * c->isize * 2);
                     if (new_list) {
                         c->isize *= 2;
                         c->ilist = new_list;
@@ -1138,7 +1163,7 @@
     return;
 }
 
-static void process_update_command(conn *c, token_t* tokens, const size_t ntokens, int comm) {
+static void process_update_command(conn *c, token_t *tokens, const size_t ntokens, int comm) {
     char *key;
     size_t nkey;
     int flags;
@@ -1146,6 +1171,8 @@
     int vlen;
     item *it;
 
+    assert(c != NULL);
+
     if (tokens[KEY_TOKEN].length > KEY_MAX_LENGTH) {
         out_string(c, "CLIENT_ERROR bad command line format");
         return;
@@ -1189,7 +1216,7 @@
             out_string(c, "SERVER_ERROR out of memory");
         /* swallow the data line */
         c->write_and_go = conn_swallow;
-        c->sbytes = vlen+2;
+        c->sbytes = vlen + 2;
         return;
     }
     
@@ -1200,14 +1227,15 @@
     conn_set_state(c, conn_nread);
 }
 
-static void process_arithmetic_command(conn *c, token_t* tokens, const size_t ntokens, const int incr) {
+static void process_arithmetic_command(conn *c, token_t *tokens, const size_t ntokens, const int incr) {
     char temp[32];
     item *it;
     unsigned int delta;
     char *key;
     size_t nkey;
-    char *msg;
     
+    assert(c != NULL);
+
     if(tokens[KEY_TOKEN].length > KEY_MAX_LENGTH) { 
         out_string(c, "CLIENT_ERROR bad command line format");
         return;
@@ -1262,7 +1290,7 @@
     int res;
 
     ptr = ITEM_data(it);
-    while ((*ptr != '\0') && (*ptr<'0' && *ptr>'9')) ptr++;    // BUG: can't be true
+    while ((*ptr != '\0') && (*ptr < '0' && *ptr > '9')) ptr++;    // BUG: can't be true
         
     value = strtol(ptr, NULL, 10);
 
@@ -1271,10 +1299,10 @@
     }
 
     if (incr != 0)
-        value+=delta;
+        value += delta;
     else {
         if (delta >= value) value = 0;
-        else value-=delta;
+        else value -= delta;
     }
     snprintf(buf, 32, "%u", value);
     res = strlen(buf);
@@ -1290,18 +1318,20 @@
         do_item_remove(new_it);       /* release our reference */
     } else { /* replace in-place */
         memcpy(ITEM_data(it), buf, res);
-        memset(ITEM_data(it) + res, ' ', it->nbytes-res-2);
+        memset(ITEM_data(it) + res, ' ', it->nbytes - res - 2);
     }
 
     return buf;
 }
 
-static void process_delete_command(conn *c, token_t* tokens, const size_t ntokens) {
+static void process_delete_command(conn *c, token_t *tokens, const size_t ntokens) {
     char *key;
     size_t nkey;
     item *it;
     time_t exptime = 0;
     
+    assert(c != NULL);
+
     if (settings.managed) {
         int bucket = c->bucket;
         if (bucket == -1) {
@@ -1387,6 +1417,8 @@
     size_t ntokens;
     int comm;
 
+    assert(c != NULL);
+
     if (settings.verbose > 1)
         fprintf(stderr, "<%d %s\n", c->sfd, command);
 
@@ -1476,9 +1508,9 @@
             out_string(c, "CLIENT_ERROR not a managed instance");
             return;
         }
-        if (sscanf(tokens[1].value, "%u:%u", &bucket,&gen) == 2) {
+        if (sscanf(tokens[1].value, "%u:%u", &bucket, &gen) == 2) {
             /* we never write anything back, even if input's wrong */
-            if ((bucket < 0) || (bucket >= MAX_BUCKETS) || (gen<=0)) {
+            if ((bucket < 0) || (bucket >= MAX_BUCKETS) || (gen <= 0)) {
                 /* do nothing, bad input */
             } else {
                 c->bucket = bucket;
@@ -1568,7 +1600,8 @@
 static int try_read_command(conn *c) {
     char *el, *cont;
 
-    assert(c->rcurr <= c->rbuf + c->rsize);
+    assert(c != NULL);
+    assert(c->rcurr <= (c->rbuf + c->rsize));
 
     if (c->rbytes == 0)
         return 0;
@@ -1576,19 +1609,19 @@
     if (!el)
         return 0;
     cont = el + 1;
-    if (el - c->rcurr > 1 && *(el - 1) == '\r') {
+    if ((el - c->rcurr) > 1 && *(el - 1) == '\r') {
         el--;
     }
     *el = '\0';
 
-    assert(cont <= c->rcurr + c->rbytes);
+    assert(cont <= (c->rcurr + c->rbytes));
 
     process_command(c, c->rcurr);
 
     c->rbytes -= (cont - c->rcurr);
     c->rcurr = cont;
 
-    assert(c->rcurr <= c->rbuf + c->rsize);
+    assert(c->rcurr <= (c->rbuf + c->rsize));
 
     return 1;
 }
@@ -1600,6 +1633,8 @@
 static int try_read_udp(conn *c) {
     int res;
 
+    assert(c != NULL);
+
     c->request_addr_size = sizeof(c->request_addr);
     res = recvfrom(c->sfd, c->rbuf, c->rsize,
                    0, &c->request_addr, &c->request_addr_size);
@@ -1640,6 +1675,8 @@
     int gotdata = 0;
     int res;
 
+    assert(c != NULL);
+
     if (c->rcurr != c->rbuf) {
         if (c->rbytes != 0) /* otherwise there's nothing to copy */
             memmove(c->rbuf, c->rcurr, c->rbytes);
@@ -1648,7 +1685,7 @@
 
     while (1) {
         if (c->rbytes >= c->rsize) {
-            char *new_rbuf = realloc(c->rbuf, c->rsize*2);
+            char *new_rbuf = realloc(c->rbuf, c->rsize * 2);
             if (!new_rbuf) {
                 if (settings.verbose > 0)
                     fprintf(stderr, "Couldn't realloc input buffer\n");
@@ -1657,7 +1694,7 @@
                 c->write_and_go = conn_closing;
                 return 1;
             }
-            c->rcurr  = c->rbuf = new_rbuf;
+            c->rcurr = c->rbuf = new_rbuf;
             c->rsize *= 2;
         }
 
@@ -1693,6 +1730,8 @@
 }
 
 static bool update_event(conn *c, const int new_flags) {
+    assert(c != NULL);
+
     struct event_base *base = c->event.ev_base;
     if (c->ev_flags == new_flags)
         return true;
@@ -1737,6 +1776,8 @@
 static int transmit(conn *c) {
     int res;
 
+    assert(c != NULL);
+
     if (c->msgcurr < c->msgused &&
             c->msglist[c->msgcurr].msg_iovlen == 0) {
         /* Finished writing the current msg; advance to the next. */
@@ -1795,9 +1836,10 @@
     int sfd, flags = 1;
     socklen_t addrlen;
     struct sockaddr addr;
-    conn *newc;
     int res;
 
+    assert(c != NULL);
+
     while (!stop) {
         switch(c->state) {
         case conn_listening:
@@ -2006,6 +2048,8 @@
     conn *c;
 
     c = (conn *)arg;
+    assert(c != NULL);
+
     c->which = which;
 
     /* sanity */
@@ -2062,7 +2106,7 @@
     max = MAX_SENDBUF_SIZE;
 
     while (min <= max) {
-        avg = ((unsigned int) min + max) / 2;
+        avg = ((unsigned int)(min + max)) / 2;
         if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &avg, intsize) == 0) {
             last_good = avg;
             min = avg + 1;
@@ -2104,7 +2148,7 @@
     addr.sin_family = AF_INET;
     addr.sin_port = htons(port);
     addr.sin_addr = settings.interface;
-    if (bind(sfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
+    if (bind(sfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
         perror("bind()");
         close(sfd);
         return -1;
@@ -2170,7 +2214,7 @@
 
     addr.sun_family = AF_UNIX;
     strcpy(addr.sun_path, path);
-    if (bind(sfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
+    if (bind(sfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
         perror("bind()");
         close(sfd);
         return -1;
@@ -2184,17 +2228,17 @@
 }
 
 /* listening socket */
-static int l_socket=0;
+static int l_socket = 0;
 
 /* udp socket */
-static int u_socket=-1;
+static int u_socket = -1;
 
 /* invoke right before gdb is called, on assert */
 void pre_gdb(void) {
-    int i = 0;
+    int i;
     if (l_socket > -1) close(l_socket);
     if (u_socket > -1) close(u_socket);
-    for (i=3; i<=500; i++) close(i); /* so lame */
+    for (i = 3; i <= 500; i++) close(i); /* so lame */
     kill(getpid(), SIGABRT);
 }
 
@@ -2255,9 +2299,9 @@
 /* Call run_deferred_deletes instead of this. */
 void do_run_deferred_deletes(void)
 {
-    int i=0, j=0;
+    int i, j = 0;
 
-    for (i=0; i<delcurr; i++) {
+    for (i = 0; i < delcurr; i++) {
         item *it = todelete[i];
         if (item_delete_lock_over(it)) {
             assert(it->refcount > 0);
@@ -2374,14 +2418,14 @@
     if (!pid_file)
         return;
 
-    if (!(fp = fopen(pid_file,"w"))) {
-        fprintf(stderr,"Could not open the pid file %s for writing\n",pid_file);
+    if (!(fp = fopen(pid_file, "w"))) {
+        fprintf(stderr, "Could not open the pid file %s for writing\n", pid_file);
         return;
     }
 
-    fprintf(fp,"%ld\n",(long) pid);
+    fprintf(fp,"%ld\n", (long)pid);
     if (fclose(fp) == -1) {
-        fprintf(stderr,"Could not close the pid file %s.\n",pid_file);
+        fprintf(stderr, "Could not close the pid file %s.\n", pid_file);
         return;
     }
 }
@@ -2391,7 +2435,7 @@
       return;
 
   if (unlink(pid_file) != 0) {
-      fprintf(stderr,"Could not remove the pid file %s.\n",pid_file);
+      fprintf(stderr, "Could not remove the pid file %s.\n", pid_file);
   }
 
 }
@@ -2404,7 +2448,6 @@
 
 int main (int argc, char **argv) {
     int c;
-    conn *u_conn;
     struct in_addr addr;
     bool lock_memory = false;
     bool daemonize = false;
@@ -2440,7 +2483,7 @@
             settings.socketpath = optarg;
             break;
         case 'm':
-            settings.maxbytes = ((size_t)atoi(optarg))*1024*1024;
+            settings.maxbytes = ((size_t)atoi(optarg)) * 1024 * 1024;
             break;
         case 'M':
             settings.evict_to_free = 0;
@@ -2521,13 +2564,12 @@
          * First try raising to infinity; if that fails, try bringing
          * the soft limit to the hard.
          */
-        if (getrlimit(RLIMIT_CORE, &rlim)==0) {
+        if (getrlimit(RLIMIT_CORE, &rlim) == 0) {
             rlim_new.rlim_cur = rlim_new.rlim_max = RLIM_INFINITY;
-            if (setrlimit(RLIMIT_CORE, &rlim_new)!=0) {
+            if (setrlimit(RLIMIT_CORE, &rlim_new)!= 0) {
                 /* failed. try raising just to the old max */
-                rlim_new.rlim_cur = rlim_new.rlim_max =
-                    rlim.rlim_max;
-                (void) setrlimit(RLIMIT_CORE, &rlim_new);
+                rlim_new.rlim_cur = rlim_new.rlim_max = rlim.rlim_max;
+                (void)setrlimit(RLIMIT_CORE, &rlim_new);
             }
         }
         /*
@@ -2536,7 +2578,7 @@
          * created at all.
          */
 
-        if ((getrlimit(RLIMIT_CORE, &rlim)!=0) || rlim.rlim_cur==0) {
+        if ((getrlimit(RLIMIT_CORE, &rlim) != 0) || rlim.rlim_cur == 0) {
             fprintf(stderr, "failed to ensure corefile creation\n");
             exit(EXIT_FAILURE);
         }
@@ -2588,8 +2630,8 @@
     }
 
     /* lose root privileges if we have them */
-    if (getuid()== 0 || geteuid()==0) {
-        if (username==0 || *username=='\0') {
+    if (getuid() == 0 || geteuid() == 0) {
+        if (username == 0 || *username == '\0') {
             fprintf(stderr, "can't run as root without the -u switch\n");
             return 1;
         }
@@ -2597,7 +2639,7 @@
             fprintf(stderr, "can't find the user %s to switch to\n", username);
             return 1;
         }
-        if (setgid(pw->pw_gid)<0 || setuid(pw->pw_uid)<0) {
+        if (setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) {
             fprintf(stderr, "failed to assume identity of user %s\n", username);
             return 1;
         }
@@ -2635,12 +2677,12 @@
 
     /* managed instance? alloc and zero a bucket array */
     if (settings.managed) {
-        buckets = malloc(sizeof(int)*MAX_BUCKETS);
+        buckets = malloc(sizeof(int) * MAX_BUCKETS);
         if (buckets == 0) {
             fprintf(stderr, "failed to allocate the bucket array");
             exit(EXIT_FAILURE);
         }
-        memset(buckets, 0, sizeof(int)*MAX_BUCKETS);
+        memset(buckets, 0, sizeof(int) * MAX_BUCKETS);
     }
 
     /* lock paged memory if needed */
@@ -2671,15 +2713,16 @@
     }
     /* save the PID in if we're a daemon */
     if (daemonize)
-        save_pid(getpid(),pid_file);
+        save_pid(getpid(), pid_file);
     /* start up worker threads if MT mode */
     thread_init(settings.num_threads, main_base);
     /* initialise clock event */
-    clock_handler(0,0,0);
+    clock_handler(0, 0, 0);
     /* initialise deletion array and timer event */
-    deltotal = 200; delcurr = 0;
-    todelete = malloc(sizeof(item *)*deltotal);
-    delete_handler(0,0,0); /* sets up the event */
+    deltotal = 200;
+    delcurr = 0;
+    todelete = malloc(sizeof(item *) * deltotal);
+    delete_handler(0, 0, 0); /* sets up the event */
     /* create the initial listening udp connection, monitored on all threads */
     if (u_socket > -1) {
         for (c = 0; c < settings.num_threads; c++) {

Modified: branches/multithreaded/server/slabs.c
===================================================================
--- branches/multithreaded/server/slabs.c	2007-04-08 15:27:38 UTC (rev 489)
+++ branches/multithreaded/server/slabs.c	2007-04-08 15:29:03 UTC (rev 490)
@@ -49,7 +49,7 @@
     unsigned int killing;  /* index+1 of dying slab, or zero if none */
 } slabclass_t;
 
-static slabclass_t slabclass[POWER_LARGEST+1];
+static slabclass_t slabclass[POWER_LARGEST + 1];
 static size_t mem_limit = 0;
 static size_t mem_malloced = 0;
 static int power_largest;
@@ -59,6 +59,7 @@
  */
 static int do_slabs_newslab(const unsigned int id);
 
+#ifndef DONT_PREALLOC_SLABS
 /* Preallocate as many slab pages as possible (called from slabs_init)
    on start-up, so users don't get confused out-of-memory errors when
    they do have free (in-slab) space, but no space to make new slabs.
@@ -66,8 +67,8 @@
    slab types can be made.  if max memory is less than 18 MB, only the
    smaller ones will be made.  */
 static void slabs_preallocate (const unsigned int maxslabs);
+#endif
 
-
 /*
  * Figures out which slab class (chunk size) is required to store an item of
  * a given size.
@@ -79,7 +80,7 @@
 unsigned int slabs_clsid(const size_t size) {
     int res = POWER_SMALLEST;
 
-    if(size==0)
+    if(size == 0)
         return 0;
     while (size > slabclass[res].size)
         if (res++ == power_largest)     /* won't fit in the biggest slab */
@@ -124,7 +125,7 @@
     {
         char *t_initial_malloc = getenv("T_MEMD_INITIAL_MALLOC");
         if (t_initial_malloc) {
-            mem_malloced = (size_t) atol(t_initial_malloc);
+            mem_malloced = (size_t)atol(t_initial_malloc);
         }
 
     }
@@ -140,6 +141,7 @@
 #endif
 }
 
+#ifndef DONT_PREALLOC_SLABS
 static void slabs_preallocate (const unsigned int maxslabs) {
     int i;
     unsigned int prealloc = 0;
@@ -150,19 +152,20 @@
        list.  if you really don't want this, you can rebuild without
        these three lines.  */
 
-    for(i=POWER_SMALLEST; i<=POWER_LARGEST; i++) {
+    for(i = POWER_SMALLEST; i <= POWER_LARGEST; i++) {
         if (++prealloc > maxslabs)
             return;
         do_slabs_newslab(i);
     }
 
 }
+#endif
 
 static int grow_slab_list (const unsigned int id) {
     slabclass_t *p = &slabclass[id];
     if (p->slabs == p->list_size) {
         size_t new_size =  (p->list_size != 0) ? p->list_size * 2 : 16;
-        void *new_list = realloc(p->slab_list, new_size*sizeof(void*));
+        void *new_list = realloc(p->slab_list, new_size * sizeof(void *));
         if (new_list == 0) return 0;
         p->list_size = new_size;
         p->slab_list = new_list;
@@ -205,7 +208,7 @@
         return NULL;
 
     p = &slabclass[id];
-    assert(p->sl_curr == 0 || ((item*)p->slots[p->sl_curr-1])->slabs_clsid == 0);
+    assert(p->sl_curr == 0 || ((item *)p->slots[p->sl_curr - 1])->slabs_clsid == 0);
 
 #ifdef USE_SYSTEM_MALLOC
     if (mem_limit && mem_malloced + size > mem_limit)
@@ -216,7 +219,7 @@
 
     /* fail unless we have space at the end of a recently allocated page,
        we have something on our freelist, or we could allocate a new page */
-    if (! (p->end_page_ptr != 0 || p->sl_curr != 0 || do_slabs_newslab(id) !=0))
+    if (! (p->end_page_ptr != 0 || p->sl_curr != 0 || do_slabs_newslab(id) != 0))
         return 0;
 
     /* return off our freelist, if we have one */
@@ -241,7 +244,7 @@
     unsigned char id = slabs_clsid(size);
     slabclass_t *p;
 
-    assert(((item *)ptr)->slabs_clsid==0);
+    assert(((item *)ptr)->slabs_clsid == 0);
     assert(id >= POWER_SMALLEST && id <= power_largest);
     if (id < POWER_SMALLEST || id > power_largest)
         return;
@@ -255,8 +258,8 @@
 #endif
 
     if (p->sl_curr == p->sl_total) { /* need more space on the free list */
-        int new_size = (p->sl_total != 0) ? p->sl_total*2 : 16;  /* 16 is arbitrary */
-        void **new_slots = realloc(p->slots, new_size*sizeof(void *));
+        int new_size = (p->sl_total != 0) ? p->sl_total * 2 : 16;  /* 16 is arbitrary */
+        void **new_slots = realloc(p->slots, new_size * sizeof(void *));
         if (new_slots == 0)
             return;
         p->slots = new_slots;
@@ -269,7 +272,7 @@
 /*@null@*/
 char* do_slabs_stats(int *buflen) {
     int i, total;
-    char *buf = (char*) malloc(power_largest * 200 + 100);
+    char *buf = (char *)malloc(power_largest * 200 + 100);
     char *bufcurr = buf;
 
     *buflen = 0;
@@ -294,7 +297,7 @@
             total++;
         }
     }
-    bufcurr += sprintf(bufcurr, "STAT active_slabs %d\r\nSTAT total_malloced %llu\r\n", total, (unsigned long long) mem_malloced);
+    bufcurr += sprintf(bufcurr, "STAT active_slabs %d\r\nSTAT total_malloced %llu\r\n", total, (unsigned long long)mem_malloced);
     bufcurr += sprintf(bufcurr, "END\r\n");
     *buflen = bufcurr - buf;
     return buf;
@@ -332,11 +335,11 @@
 
     if (p->killing == 0) p->killing = 1;
 
-    slab = p->slab_list[p->killing-1];
+    slab = p->slab_list[p->killing - 1];
     slab_end = slab + POWER_BLOCK;
 
-    for (iter=slab; iter<slab_end; iter+=p->size) {
-        item *it = (item *) iter;
+    for (iter = slab; iter < slab_end; iter += p->size) {
+        item *it = (item *)iter;
         if (it->slabs_clsid) {
             if (it->refcount) was_busy = 1;
             item_unlink(it);
@@ -346,7 +349,7 @@
     /* go through free list and discard items that are no longer part of this slab */
     {
         int fi;
-        for (fi=p->sl_curr-1; fi>=0; fi--) {
+        for (fi = p->sl_curr - 1; fi >= 0; fi--) {
             if (p->slots[fi] >= slab && p->slots[fi] < slab_end) {
                 p->sl_curr--;
                 if (p->sl_curr > fi) p->slots[fi] = p->slots[p->sl_curr];
@@ -357,7 +360,7 @@
     if (was_busy) return -1;
 
     /* if good, now move it to the dst slab class */
-    p->slab_list[p->killing-1] = p->slab_list[p->slabs-1];
+    p->slab_list[p->killing - 1] = p->slab_list[p->slabs - 1];
     p->slabs--;
     p->killing = 0;
     dp->slab_list[dp->slabs++] = slab;
@@ -365,7 +368,7 @@
     dp->end_page_free = dp->perslab;
     /* this isn't too critical, but other parts of the code do asserts to
        make sure this field is always 0.  */
-    for (iter=slab; iter<slab_end; iter+=dp->size) {
+    for (iter = slab; iter < slab_end; iter += dp->size) {
         ((item *)iter)->slabs_clsid = 0;
     }
     return 1;

Modified: branches/multithreaded/server/t/stats.t
===================================================================
--- branches/multithreaded/server/t/stats.t	2007-04-08 15:27:38 UTC (rev 489)
+++ branches/multithreaded/server/t/stats.t	2007-04-08 15:29:03 UTC (rev 490)
@@ -1,7 +1,7 @@
 #!/usr/bin/perl
 
 use strict;
-use Test::More tests => 16;
+use Test::More tests => 17;
 use FindBin qw($Bin);
 use lib "$Bin/lib";
 use MemcachedTest;




More information about the memcached-commits mailing list