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

commits at code.sixapart.com commits at code.sixapart.com
Sun Apr 8 15:27:39 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.



U   trunk/server/ChangeLog
U   trunk/server/items.c
U   trunk/server/memcached.c
U   trunk/server/slabs.c
U   trunk/server/t/stats.t


Modified: trunk/server/ChangeLog
===================================================================
--- trunk/server/ChangeLog	2007-04-08 15:21:45 UTC (rev 488)
+++ trunk/server/ChangeLog	2007-04-08 15:27:38 UTC (rev 489)
@@ -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: trunk/server/items.c
===================================================================
--- trunk/server/items.c	2007-04-08 15:21:45 UTC (rev 488)
+++ trunk/server/items.c	2007-04-08 15:27:38 UTC (rev 489)
@@ -37,10 +37,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;
     }
 }
 
@@ -67,14 +67,11 @@
 /*@null@*/
 item *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;
 
@@ -97,10 +94,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.evictions++;
                 item_unlink(search);
@@ -108,7 +105,7 @@
             }
         }
         it = slabs_alloc(ntotal);
-        if (it==0) return NULL;
+        if (it == 0) return NULL;
     }
 
     assert(it->slabs_clsid == 0);
@@ -124,7 +121,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;
 }
@@ -250,7 +247,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;
@@ -265,18 +262,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;
@@ -292,7 +289,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);
@@ -305,8 +302,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) {
@@ -316,8 +313,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);
@@ -330,9 +327,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: trunk/server/memcached.c
===================================================================
--- trunk/server/memcached.c	2007-04-08 15:21:45 UTC (rev 488)
+++ trunk/server/memcached.c	2007-04-08 15:27:38 UTC (rev 489)
@@ -104,6 +104,7 @@
                               (to avoid 64 bit time_t) */
 
 void pre_gdb(void);
+static void conn_free(conn *c);
 
 /** exported globals **/
 struct stats stats;
@@ -141,10 +142,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 +171,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;
@@ -228,6 +229,8 @@
 {
     struct msghdr *msg;
 
+    assert(c != NULL);
+
     if (c->msgsize == c->msgused) {
         msg = realloc(c->msglist, c->msgsize * 2 * sizeof(struct msghdr));
         if (! msg)
@@ -264,8 +267,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;
 }
 
@@ -295,11 +299,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) {
@@ -371,6 +375,8 @@
 }
 
 static void conn_cleanup(conn *c) {
+    assert(c != NULL);
+
     if (c->item) {
         item_free(c->item);
         c->item = 0;
@@ -410,6 +416,8 @@
 }
 
 static void conn_close(conn *c) {
+    assert(c != NULL);
+
     /* delete the event, the socket and the conn */
     event_del(&c->event);
 
@@ -428,7 +436,7 @@
         freeconns[freecurr++] = c;
     } else {
         /* try to enlarge free connections array */
-        conn **new_freeconns = realloc((void*)freeconns, sizeof(conn *)*freetotal*2);
+        conn **new_freeconns = realloc((void *)freeconns, sizeof(conn *) * freetotal * 2);
         if (new_freeconns) {
             freetotal *= 2;
             freeconns = new_freeconns;
@@ -453,14 +461,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 +481,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 +490,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 +499,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 +514,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 +533,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 +567,8 @@
     int leftover;
     bool limit_to_mtu;
 
+    assert(c != NULL);
+
     do {
         m = &c->msglist[c->msgused - 1];
 
@@ -581,7 +597,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 +619,8 @@
     int i;
     unsigned char *hdr;
 
+    assert(c != NULL);
+
     if (c->msgused > c->hdrsize) {
         void *new_hdrbuf;
         if (c->hdrbuf)
@@ -611,7 +629,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 +645,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 +655,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 +683,8 @@
  */
 
 static void complete_nread(conn *c) {
+    assert(c != NULL);
+
     item *it = c->item;
     int comm = c->item_comm;
     item *old_it;
@@ -718,7 +740,7 @@
 }
 
 typedef struct token_s {
-    char* value;
+    char *value;
     size_t length;
 } token_t;
 
@@ -746,9 +768,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;
 
@@ -792,11 +814,13 @@
     return ntokens;
 }
 
-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;
@@ -816,7 +840,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);
@@ -832,7 +856,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, "END");
         out_string(c, temp);
         return;
@@ -876,8 +900,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;
         }
@@ -901,8 +924,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;
@@ -942,7 +965,7 @@
         return;
     }
 
-    if (strcmp(subcommand, "slabs")==0) {
+    if (strcmp(subcommand, "slabs") == 0) {
         int bytes = 0;
         char *buf = slabs_stats(&bytes);
         if (!buf) {
@@ -957,14 +980,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, "sizes")==0) {
+    if (strcmp(subcommand, "sizes") == 0) {
         int bytes = 0;
         char *buf = item_stats_sizes(&bytes);
         if (! buf) {
@@ -984,13 +1007,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) {
@@ -1019,7 +1044,7 @@
             it = get_item(key, nkey);
             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;
@@ -1081,7 +1106,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;
@@ -1089,6 +1114,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;
@@ -1128,7 +1155,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;
     }
     
@@ -1140,7 +1167,7 @@
     return;
 }
 
-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];
     unsigned int value;
     item *it;
@@ -1149,6 +1176,8 @@
     size_t nkey;
     int res;
     char *ptr;
+
+    assert(c != NULL);
     
     if(tokens[KEY_TOKEN].length > KEY_MAX_LENGTH) { 
         out_string(c, "CLIENT_ERROR bad command line format");
@@ -1185,7 +1214,7 @@
     }
 
     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);
 
@@ -1195,10 +1224,10 @@
     }
     
     if (incr != 0)
-        value+=delta;
+        value += delta;
     else {
         if (delta >= value) value = 0;
-        else value-=delta;
+        else value -= delta;
     }
     snprintf(temp, 32, "%u", value);
     res = strlen(temp);
@@ -1214,18 +1243,20 @@
         item_replace(it, new_it);
     } else { /* replace in-place */
         memcpy(ITEM_data(it), temp, res);
-        memset(ITEM_data(it) + res, ' ', it->nbytes-res-2);
+        memset(ITEM_data(it) + res, ' ', it->nbytes - res - 2);
     }
     out_string(c, temp);
     return;
 }
 
-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) {
@@ -1297,6 +1328,8 @@
     size_t ntokens;
     int comm;
 
+    assert(c != NULL);
+
     if (settings.verbose > 1)
         fprintf(stderr, "<%d %s\n", c->sfd, command);
 
@@ -1386,9 +1419,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;
@@ -1478,7 +1511,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;
@@ -1486,19 +1520,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;
 }
@@ -1510,6 +1544,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);
@@ -1548,6 +1584,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);
@@ -1556,7 +1594,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");
@@ -1565,7 +1603,7 @@
                 c->write_and_go = conn_closing;
                 return 1;
             }
-            c->rcurr  = c->rbuf = new_rbuf;
+            c->rcurr = c->rbuf = new_rbuf;
             c->rsize *= 2;
         }
 
@@ -1599,6 +1637,8 @@
 }
 
 static bool update_event(conn *c, const int new_flags) {
+    assert(c != NULL);
+
     if (c->ev_flags == new_flags)
         return true;
     if (event_del(&c->event) == -1) return false;
@@ -1639,6 +1679,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. */
@@ -1695,9 +1737,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:
@@ -1721,7 +1764,7 @@
                 close(sfd);
                 break;
             }
-            newc = conn_new(sfd, conn_read, EV_READ | EV_PERSIST,
+            conn *newc = conn_new(sfd, conn_read, EV_READ | EV_PERSIST,
                             DATA_BUFFER_SIZE, false);
             if (!newc) {
                 if (settings.verbose > 0)
@@ -1908,6 +1951,8 @@
     conn *c;
 
     c = (conn *)arg;
+    assert(c != NULL);
+
     c->which = which;
 
     /* sanity */
@@ -1965,7 +2010,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;
@@ -2007,7 +2052,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;
@@ -2073,7 +2118,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;
@@ -2087,17 +2132,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);
 }
 
@@ -2267,14 +2312,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;
     }
 }
@@ -2284,7 +2329,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);
   }
 
 }
@@ -2333,7 +2378,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;
@@ -2399,13 +2444,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);
             }
         }
         /*
@@ -2414,7 +2458,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);
         }
@@ -2466,8 +2510,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;
         }
@@ -2475,7 +2519,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;
         }
@@ -2512,12 +2556,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 */
@@ -2552,14 +2596,15 @@
         exit(EXIT_FAILURE);
     }
     /* 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 */
     /* save the PID in if we're a daemon */
     if (daemonize)
-        save_pid(getpid(),pid_file);
+        save_pid(getpid(), pid_file);
     /* enter the loop */
     event_loop(0);
     /* remove the PID file if we're a daemon */

Modified: trunk/server/slabs.c
===================================================================
--- trunk/server/slabs.c	2007-04-08 15:21:45 UTC (rev 488)
+++ trunk/server/slabs.c	2007-04-08 15:27:38 UTC (rev 489)
@@ -54,7 +54,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;
@@ -64,6 +64,7 @@
  */
 static int 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.
@@ -71,8 +72,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.
@@ -84,7 +85,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 */
@@ -129,7 +130,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);
         }
 
     }
@@ -145,6 +146,7 @@
 #endif
 }
 
+#ifndef DONT_PREALLOC_SLABS
 static void slabs_preallocate (const unsigned int maxslabs) {
     int i;
     unsigned int prealloc = 0;
@@ -155,19 +157,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;
         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;
@@ -210,7 +213,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)
@@ -221,7 +224,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 || slabs_newslab(id) !=0))
+    if (! (p->end_page_ptr != 0 || p->sl_curr != 0 || slabs_newslab(id)  != 0))
         return 0;
 
     /* return off our freelist, if we have one */
@@ -246,7 +249,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;
@@ -260,8 +263,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;
@@ -274,7 +277,7 @@
 /*@null@*/
 char* 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;
@@ -299,7 +302,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;
@@ -337,11 +340,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);
@@ -351,7 +354,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];
@@ -362,7 +365,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;
@@ -370,7 +373,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: trunk/server/t/stats.t
===================================================================
--- trunk/server/t/stats.t	2007-04-08 15:21:45 UTC (rev 488)
+++ trunk/server/t/stats.t	2007-04-08 15:27:38 UTC (rev 489)
@@ -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