Changeset 469

Show
Ignore:
Timestamp:
03/06/07 22:11:22 (2 years ago)
Author:
plindner
Message:

Merge r468 into multithreaded branch

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/multithreaded/server/ChangeLog

    r467 r469  
    112007-03-05  Paul Lindner  <lindner@inuus.com> 
     2        * Fix a number of places where (s)printf calls were using unsigned 
     3          or signed formats that did not match their arguments. 
     4 
     5        * Add support for stdbool.h and stdint.h to use the bool and 
     6          uint8_t types. 
     7 
     8        * Major refactoring - move API calls for assoc/items/slabs to 
     9          their own individual header files.  Add apropriate const and 
     10          static declarations as appropriate. 
     11         
    212        * Avoid type-punning.  Do a more efficient realloc inside the 
    313          conn_shrink routine. 
     
    515        * Fix overflow bug where uninitialized access to slabclass caused 
    616          size-0 mallocs during slab preallocation. 
     17 
     18        * Use EXIT_SUCCESS/EXIT_FAILURE constants. 
     19 
     20        * Convert some sprintf calls to snprintf to protect against 
     21          buffer overflows. 
     22 
     23        * Explicitly compare against NULL or zero in many places. 
    724 
    8252007-03-05 
  • branches/multithreaded/server/Makefile.am

    r462 r469  
    11bin_PROGRAMS = memcached memcached-debug 
    22 
    3 memcached_SOURCES = memcached.c slabs.c items.c memcached.h assoc.c thread.c stats.c 
     3memcached_SOURCES = memcached.c slabs.c slabs.h items.c items.h assoc.c assoc.h memcached.h thread.c stats.c stats.h 
    44memcached_debug_SOURCES = $(memcached_SOURCES) 
    55memcached_CPPFLAGS = -DNDEBUG 
  • branches/multithreaded/server/assoc.c

    r442 r469  
    141141  const void *key,       /* the key to hash */ 
    142142  size_t      length,    /* length of the key */ 
    143   uint32_t    initval)   /* initval */ 
     143  const uint32_t    initval)   /* initval */ 
    144144{ 
    145145  uint32_t a,b,c;                                          /* internal state */ 
     
    319319 * big-endian byte ordering.  
    320320 */ 
    321 uint32_t hash( const void *key, size_t length, uint32_t initval) 
     321uint32_t hash( const void *key, size_t length, const uint32_t initval) 
    322322{ 
    323323  uint32_t a,b,c; 
     
    450450 
    451451/* how many powers of 2's worth of buckets we use */ 
    452 int hashpower = 16; 
     452static int hashpower = 16; 
    453453 
    454454#define hashsize(n) ((ub4)1<<(n)) 
     
    486486} 
    487487 
    488 item *assoc_find(const char *key, size_t nkey) { 
     488item *assoc_find(const char *key, const size_t nkey) { 
    489489    uint32_t hv = hash(key, nkey, 0); 
    490490    item *it; 
     
    512512   the item wasn't found */ 
    513513 
    514 static item** _hashitem_before (const char *key, size_t nkey) { 
     514static item** _hashitem_before (const char *key, const size_t nkey) { 
    515515    uint32_t hv = hash(key, nkey, 0); 
    516516    item **pos; 
     
    601601} 
    602602 
    603 void assoc_delete(const char *key, size_t nkey) { 
     603void assoc_delete(const char *key, const size_t nkey) { 
    604604    item **before = _hashitem_before(key, nkey); 
    605605 
  • branches/multithreaded/server/assoc.h

    r468 r469  
    44int assoc_insert(item *item); 
    55void assoc_delete(const char *key, const size_t nkey); 
    6 void assoc_move_next_bucket(void); 
     6void do_assoc_move_next_bucket(void); 
     7uint32_t hash( const void *key, size_t length, const uint32_t initval); 
     8 
  • branches/multithreaded/server/configure.ac

    r467 r469  
    9999AC_CHECK_FUNC(daemon,AC_DEFINE([HAVE_DAEMON],,[Define this if you have daemon()]),[AC_LIBOBJ(daemon)]) 
    100100 
    101  
     101AC_HEADER_STDBOOL 
     102AC_C_CONST 
    102103AC_CHECK_HEADER(malloc.h, AC_DEFINE(HAVE_MALLOC_H,,[do we have malloc.h?])) 
    103104AC_CHECK_MEMBER([struct mallinfo.arena], [ 
  • branches/multithreaded/server/items.c

    r442 r469  
    1515#include <assert.h> 
    1616 
     17/* Forward Declarations */ 
     18static void item_link_q(item *it); 
     19static void item_unlink_q(item *it); 
     20 
    1721/* 
    1822 * We only reposition items in the LRU queue if they haven't been repositioned 
     
    2529static item *heads[LARGEST_ID]; 
    2630static item *tails[LARGEST_ID]; 
    27 unsigned int sizes[LARGEST_ID]; 
     31static unsigned int sizes[LARGEST_ID]; 
    2832 
    2933void item_init(void) { 
     
    6064 * Returns the total size of the header. 
    6165 */ 
    62 int item_make_header(char *key, uint8_t nkey, int flags, int nbytes, 
    63                      char *suffix, int *nsuffix) { 
    64     *nsuffix = sprintf(suffix, " %u %u\r\n", flags, nbytes - 2); 
     66static size_t item_make_header(char *key, const uint8_t nkey, const int flags, const int nbytes, 
     67                     char *suffix, uint8_t *nsuffix) { 
     68    /* suffix is defined at 40 chars elsewhere.. */ 
     69    *nsuffix = (uint8_t) snprintf(suffix, 40, " %d %d\r\n", flags, nbytes - 2); 
    6570    return sizeof(item) + nkey + *nsuffix + nbytes; 
    6671} 
    6772  
    68 item *do_item_alloc(char *key, size_t nkey, int flags, rel_time_t exptime, int nbytes) { 
    69     int nsuffix, ntotal; 
     73/*@null@*/ 
     74item *do_item_alloc(char *key, const size_t nkey, const int flags, const rel_time_t exptime, const int nbytes) { 
     75    uint8_t nsuffix; 
     76    size_t ntotal; 
    7077    item *it; 
    7178    unsigned int id; 
     
    8794         */ 
    8895 
    89         if (!settings.evict_to_free) return 0
     96        if (settings.evict_to_free == 0) return NULL
    9097 
    9198        /*  
     
    96103         */ 
    97104 
    98         if (id > LARGEST_ID) return 0
    99         if (tails[id]==0) return 0
     105        if (id > LARGEST_ID) return NULL
     106        if (tails[id]==0) return NULL
    100107 
    101108        for (search = tails[id]; tries>0 && search; tries--, search=search->prev) { 
     
    106113        } 
    107114        it = slabs_alloc(ntotal); 
    108         if (it==0) return 0
     115        if (it==0) return NULL
    109116    } 
    110117 
     
    123130    strcpy(ITEM_key(it), key); 
    124131    it->exptime = exptime; 
    125     memcpy(ITEM_suffix(it), suffix, nsuffix); 
     132    memcpy(ITEM_suffix(it), suffix, (size_t) nsuffix); 
    126133    it->nsuffix = nsuffix; 
    127134    return it; 
     
    129136 
    130137void item_free(item *it) { 
    131     unsigned int ntotal = ITEM_ntotal(it); 
     138    size_t ntotal = ITEM_ntotal(it); 
    132139    assert((it->it_flags & ITEM_LINKED) == 0); 
    133140    assert(it != heads[it->slabs_clsid]); 
     
    146153 * the maximum for a cache entry.) 
    147154 */ 
    148 int item_size_ok(char *key, size_t nkey, int flags, int nbytes) { 
     155bool item_size_ok(char *key, const size_t nkey, const int flags, const int nbytes) { 
    149156    char prefix[40]; 
    150     int nsuffix; 
     157    uint8_t nsuffix; 
    151158 
    152159    return slabs_clsid(item_make_header(key, nkey + 1, flags, nbytes, 
     
    154161} 
    155162 
    156 void item_link_q(item *it) { /* item is the new head */ 
     163static void item_link_q(item *it) { /* item is the new head */ 
    157164    item **head, **tail; 
    158165    /* always true, warns: assert(it->slabs_clsid <= LARGEST_ID); */ 
     
    172179} 
    173180 
    174 void item_unlink_q(item *it) { 
     181static void item_unlink_q(item *it) { 
    175182    item **head, **tail; 
    176183    /* always true, warns: assert(it->slabs_clsid <= LARGEST_ID); */ 
     
    214221 
    215222void do_item_unlink(item *it) { 
    216     if (it->it_flags & ITEM_LINKED) { 
     223    if ((it->it_flags & ITEM_LINKED) != 0) { 
    217224        it->it_flags &= ~ITEM_LINKED; 
    218225        STATS_LOCK(); 
     
    232239        DEBUG_REFCNT(it, '-'); 
    233240    } 
    234     assert((it->it_flags & ITEM_DELETED) == 0 || it->refcount); 
     241    assert((it->it_flags & ITEM_DELETED) == 0 || it->refcount != 0); 
    235242    if (it->refcount == 0 && (it->it_flags & ITEM_LINKED) == 0) { 
    236243        item_free(it); 
     
    257264} 
    258265 
    259 char *item_cachedump(unsigned int slabs_clsid, unsigned int limit, unsigned int *bytes) { 
    260  
     266/*@null@*/ 
     267char *item_cachedump(const unsigned int slabs_clsid, const unsigned int limit, unsigned int *bytes) { 
    261268    int memlimit = 2*1024*1024; 
    262269    char *buffer; 
    263     int bufcurr; 
     270    unsigned int bufcurr; 
    264271    item *it; 
    265272    int len; 
     
    267274    char temp[512]; 
    268275 
    269     if (slabs_clsid > LARGEST_ID) return 0
     276    if (slabs_clsid > LARGEST_ID) return NULL
    270277    it = heads[slabs_clsid]; 
    271278 
    272     buffer = malloc(memlimit); 
    273     if (buffer == 0) return 0
     279    buffer = malloc((size_t)memlimit); 
     280    if (buffer == 0) return NULL
    274281    bufcurr = 0; 
    275282 
    276     while (it && (!limit || shown < limit)) { 
    277         len = sprintf(temp, "ITEM %s [%u b; %lu s]\r\n", ITEM_key(it), it->nbytes - 2, it->time + stats.started); 
     283    while (it != NULL && (limit==0 || shown < limit)) { 
     284        len = snprintf(temp, 512, "ITEM %s [%d b; %lu s]\r\n", ITEM_key(it), it->nbytes - 2, it->time + stats.started); 
    278285        if (bufcurr + len + 6 > memlimit)  /* 6 is END\r\n\0 */ 
    279286            break; 
     
    291298} 
    292299 
    293 void item_stats(char *buffer, int buflen) { 
     300void item_stats(char *buffer, const int buflen) { 
    294301    int i; 
    295302    char *bufcurr = buffer; 
     
    303310    for (i=0; i<LARGEST_ID; i++) { 
    304311        if (tails[i]) 
    305             bufcurr += sprintf(bufcurr, "STAT items:%u:number %u\r\nSTAT items:%u:age %u\r\n", 
     312            bufcurr += snprintf(bufcurr, (size_t)buflen, "STAT items:%d:number %u\r\nSTAT items:%d:age %u\r\n", 
    306313                               i, sizes[i], i, now - tails[i]->time); 
    307314    } 
     
    311318 
    312319/* dumps out a list of objects of each size, with granularity of 32 bytes */ 
     320/*@null@*/ 
    313321char* item_stats_sizes(int *bytes) { 
    314     int num_buckets = 32768;   /* max 1MB object, divided into 32 bytes size buckets */ 
    315     unsigned int *histogram = (unsigned int*) malloc(num_buckets * sizeof(int)); 
     322    const int num_buckets = 32768;   /* max 1MB object, divided into 32 bytes size buckets */ 
     323    unsigned int *histogram = (unsigned int*) malloc((size_t)num_buckets * sizeof(int)); 
    316324    char *buf = (char*) malloc(1024*1024*2*sizeof(char)); 
    317325    int i; 
     
    320328        if (histogram) free(histogram); 
    321329        if (buf) free(buf); 
    322         return 0
     330        return NULL
    323331    } 
    324332 
    325333    /* build the histogram */ 
    326     memset(histogram, 0, num_buckets * sizeof(int)); 
     334    memset(histogram, 0, (size_t) num_buckets * sizeof(int)); 
    327335    for (i=0; i<LARGEST_ID; i++) { 
    328336        item *iter = heads[i]; 
     
    330338            int ntotal = ITEM_ntotal(iter); 
    331339            int bucket = ntotal / 32; 
    332             if (ntotal % 32) bucket++; 
     340            if ((ntotal % 32) != 0) bucket++; 
    333341            if (bucket < num_buckets) histogram[bucket]++; 
    334342            iter = iter->next; 
     
    339347    *bytes = 0; 
    340348    for (i=0; i<num_buckets; i++) { 
    341         if (histogram[i]) { 
    342             *bytes += sprintf(&buf[*bytes], "%u %u\r\n", i*32, histogram[i]); 
     349        if (histogram[i] != 0) { 
     350            *bytes += sprintf(&buf[*bytes], "%d %u\r\n", i*32, histogram[i]); 
    343351        } 
    344352    } 
     
    403411    int i; 
    404412    item *iter, *next; 
    405     if (! settings.oldest_live
     413    if (settings.oldest_live == 0
    406414        return; 
    407415    for (i = 0; i < LARGEST_ID; i++) { 
  • branches/multithreaded/server/items.h

    r468 r469  
    22void item_init(void); 
    33/*@null@*/ 
    4 item *item_alloc(char *key, const size_t nkey, const int flags, const rel_time_t exptime, const int nbytes); 
     4item *do_item_alloc(char *key, const size_t nkey, const int flags, const rel_time_t exptime, const int nbytes); 
    55void item_free(item *it); 
    66bool item_size_ok(char *key, const size_t nkey, const int flags, const int nbytes); 
    77 
    8 int item_link(item *it);    /* may fail if transgresses limits */ 
    9 void item_unlink(item *it); 
    10 void item_remove(item *it); 
    11 void item_update(item *it);   /* update LRU time to current and reposition */ 
    12 int item_replace(item *it, item *new_it); 
     8int do_item_link(item *it);    /* may fail if transgresses limits */ 
     9void do_item_unlink(item *it); 
     10void do_item_remove(item *it); 
     11void do_item_update(item *it);   /* update LRU time to current and reposition */ 
     12int do_item_replace(item *it, item *new_it); 
    1313 
    1414/*@null@*/ 
     
    1818/*@null@*/ 
    1919char *item_stats_sizes(int *bytes); 
    20 void item_flush_expired(void); 
     20void do_item_flush_expired(void); 
     21item *item_get(char *key, size_t nkey); 
     22 
     23item *do_item_get_notedeleted(char *key, size_t nkey, int *delete_locked); 
     24item *do_item_get_nocheck(char *key, size_t nkey); 
  • branches/multithreaded/server/memcached.c

    r467 r469  
    6161#endif 
    6262 
     63/* 
     64 * forward declarations  
     65 */ 
     66static void drive_machine(conn *c); 
     67static int new_socket(const bool is_udp); 
     68static int server_socket(const int port, const bool is_udp); 
     69static int try_read_command(conn *c); 
     70static int try_read_network(conn *c); 
     71static int try_read_udp(conn *c); 
     72 
     73/* stats */ 
     74static void stats_reset(void); 
     75static void stats_init(void); 
     76 
     77/* defaults */ 
     78static void settings_init(void); 
     79 
     80/* event handling, network IO */ 
     81static void event_handler(const int fd, const short which, void *arg); 
     82static void conn_close(conn *c); 
     83static void conn_init(void); 
     84static void accept_new_conns(const bool do_accept); 
     85static bool update_event(conn *c, const int new_flags); 
     86static void complete_nread(conn *c); 
     87static void process_command(conn *c, char *command); 
     88static int transmit(conn *c); 
     89static int ensure_iov_space(conn *c); 
     90static int add_iov(conn *c, const void *buf, int len); 
     91static int add_msghdr(conn *c); 
     92 
     93 
     94/* time handling */ 
     95static void set_current_time(void);  /* update the global variable holding 
     96                              global 32-bit seconds-since-start time 
     97                              (to avoid 64 bit time_t) */ 
     98 
     99void pre_gdb(void); 
     100 
     101/** exported globals **/ 
    63102struct stats stats; 
    64103struct settings settings; 
    65104 
     105/** file scope variables **/ 
    66106static item **todelete = 0; 
    67107static int delcurr; 
     
    75115#define TRANSMIT_HARD_ERROR 3 
    76116 
    77 int *buckets = 0; /* bucket->generation array for a managed instance */ 
     117static int *buckets = 0; /* bucket->generation array for a managed instance */ 
    78118 
    79119void conn_free(conn *c); 
    80120 
    81121#define REALTIME_MAXDELTA 60*60*24*30 
    82 rel_time_t realtime(time_t exptime) { 
     122/* 
     123 * given time value that's either unix time or delta from current unix time, return 
     124 * unix time. Use the fact that delta can't exceed one month (and real time value can't 
     125 * be that low). 
     126 */ 
     127static rel_time_t realtime(const time_t exptime) { 
    83128    /* no. of seconds in 30 days - largest possible delta exptime */ 
    84129 
     
    100145} 
    101146 
    102 void stats_init(void) { 
     147static void stats_init(void) { 
    103148    stats.curr_items = stats.total_items = stats.curr_conns = stats.total_conns = stats.conn_structs = 0; 
    104149    stats.get_cmds = stats.set_cmds = stats.get_hits = stats.get_misses = 0; 
     
    113158} 
    114159 
    115 void stats_reset(void) { 
     160static void stats_reset(void) { 
    116161    STATS_LOCK(); 
    117162    stats.total_items = stats.total_conns = 0; 
     
    122167} 
    123168 
    124 void settings_init(void) { 
     169static void settings_init(void) { 
    125170    settings.port = 11211; 
    126171    settings.udpport = 0; 
     
    132177    settings.evict_to_free = 1;       /* push old items out of cache when memory runs out */ 
    133178    settings.socketpath = NULL;       /* by default, not using a unix socket */ 
    134     settings.managed = 0
     179    settings.managed = false
    135180    settings.factor = 1.25; 
    136181    settings.chunk_size = 48;         /* space for a modest key and value */ 
     
    144189} 
    145190 
     191/* returns true if a deleted item's delete-locked-time is over, and it 
     192   should be removed from the namespace */ 
     193static bool item_delete_lock_over (item *it) { 
     194    assert(it->it_flags & ITEM_DELETED); 
     195    return (current_time >= it->exptime); 
     196} 
     197 
    146198/* 
    147199 * Adds a message header to a connection. 
     
    149201 * Returns 0 on success, -1 on out-of-memory. 
    150202 */ 
    151 int add_msghdr(conn *c) 
     203static int add_msghdr(conn *c) 
    152204{ 
    153205    struct msghdr *msg; 
     
    182234} 
    183235 
     236 
    184237/* 
    185238 * Free list management for connections. 
    186239 */ 
    187 conn **freeconns; 
    188 int freetotal; 
    189 int freecurr; 
    190  
    191 void conn_init(void) { 
     240 
     241static conn **freeconns; 
     242static int freetotal; 
     243static int freecurr; 
     244 
     245 
     246static void conn_init(void) { 
    192247    freetotal = 200; 
    193248    freecurr = 0; 
    194249    freeconns = (conn **)malloc(sizeof (conn *)*freetotal); 
     250    /** TODO check for NULL **/ 
    195251    return; 
    196252} 
     
    233289} 
    234290 
    235 conn *conn_new(int sfd, int init_state, int event_flags, 
    236                 int read_buffer_size, int is_udp, struct event_base *base) { 
     291conn *conn_new(const int sfd, const int init_state, const int event_flags, 
     292                const int read_buffer_size, const bool is_udp, struct event_base *base) { 
    237293    conn *c = conn_from_freelist(); 
    238294 
     
    240296        if (!(c = (conn *)malloc(sizeof(conn)))) { 
    241297            perror("malloc()"); 
    242             return 0
     298            return NULL
    243299        } 
    244300        c->rbuf = c->wbuf = 0; 
     
    255311        c->hdrsize = 0; 
    256312 
    257         c->rbuf = (char *) malloc(c->rsize); 
    258         c->wbuf = (char *) malloc(c->wsize); 
     313        c->rbuf = (char *) malloc((size_t)c->rsize); 
     314        c->wbuf = (char *) malloc((size_t)c->wsize); 
    259315        c->ilist = (item **) malloc(sizeof(item *) * c->isize); 
    260316        c->iov = (struct iovec *) malloc(sizeof(struct iovec) * c->iovsize); 
     
    270326            free(c); 
    271327            perror("malloc()"); 
    272             return 0
     328            return NULL
    273329        } 
    274330 
     
    315371            conn_free(c); 
    316372        } 
    317         return 0
     373        return NULL
    318374    } 
    319375 
     
    326382} 
    327383 
    328 void conn_cleanup(conn *c) { 
     384static void conn_cleanup(conn *c) { 
    329385    if (c->item) { 
    330386        item_remove(c->item); 
     
    332388    } 
    333389 
    334     if (c->ileft) { 
     390    if (c->ileft != 0) { 
    335391        for (; c->ileft > 0; c->ileft--,c->icurr++) { 
    336392            item_remove(*(c->icurr)); 
     
    365421} 
    366422 
    367 void conn_close(conn *c) { 
     423static void conn_close(conn *c) { 
    368424    /* delete the event, the socket and the conn */ 
    369425    event_del(&c->event); 
     
    373429 
    374430    close(c->sfd); 
    375     accept_new_conns(1); 
     431    accept_new_conns(true); 
    376432    conn_cleanup(c); 
    377433 
     
    403459    if (c->rsize > READ_BUFFER_HIGHWAT && c->rbytes < DATA_BUFFER_SIZE) { 
    404460        if (c->rcurr != c->rbuf) 
    405             memmove(c->rbuf, c->rcurr, c->rbytes); 
     461            memmove(c->rbuf, c->rcurr, (size_t) c->rbytes); 
    406462 
    407463        char *newbuf = (char*) realloc((void*)c->rbuf, DATA_BUFFER_SIZE); 
     464 
    408465        if (newbuf) { 
    409466            c->rbuf = newbuf; 
     
    447504 * happen here. 
    448505 */ 
    449 void conn_set_state(conn *c, int state) { 
     506static void conn_set_state(conn *c, int state) { 
    450507    if (state != c->state) { 
    451508        if (state == conn_read) { 
     
    464521 * Returns 0 on success, -1 on out-of-memory. 
    465522 */ 
    466 int ensure_iov_space(conn *c) { 
     523static int ensure_iov_space(conn *c) { 
    467524    if (c->iovused >= c->iovsize) { 
    468525        int i, iovnum; 
     
    492549 */ 
    493550 
    494 int add_iov(conn *c, const void *buf, int len) { 
     551static int add_iov(conn *c, const void *buf, int len) { 
    495552    struct msghdr *m; 
    496553    int leftover; 
    497     int limit_to_mtu; 
     554    bool limit_to_mtu; 
    498555 
    499556    do { 
     
    513570        } 
    514571 
    515         if (ensure_iov_space(c)
     572        if (ensure_iov_space(c) != 0
    516573            return -1; 
    517574 
     
    543600 * Constructs a set of UDP headers and attaches them to the outgoing messages. 
    544601 */ 
    545 int build_udp_headers(conn *c) { 
     602static int build_udp_headers(conn *c) { 
    546603    int i; 
    547604    unsigned char *hdr; 
     
    578635 
    579636 
    580 void out_string(conn *c, char *str) { 
     637static void out_string(conn *c, const char *str) { 
    581638    int len; 
    582639 
     
    606663 */ 
    607664 
    608 void complete_nread(conn *c) { 
     665static void complete_nread(conn *c) { 
    609666    item *it = c->item; 
    610667    int comm = c->item_comm; 
     
    640697    int stored = 0; 
    641698 
    642     if (old_it && comm == NREAD_ADD) { 
     699    if (old_it != NULL && comm == NREAD_ADD) { 
    643700        /* add only adds a nonexistent item, but promote to head of LRU */ 
    644701        do_item_update(old_it); 
     
    655712            old_it = do_item_get_nocheck(key, it->nkey); 
    656713 
    657         if (old_it
     714        if (old_it != NULL
    658715            do_item_replace(old_it, it); 
    659716        else 
     
    697754 *   } 
    698755 */ 
    699 size_t tokenize_command(char* command, token_t* tokens, size_t max_tokens)  { 
     756static size_t tokenize_command(char* command, token_t* tokens, const size_t max_tokens)  { 
    700757    char* cp; 
    701758    char* value = NULL; 
     
    771828} 
    772829 
    773 void process_stat(conn *c, token_t* tokens, size_t ntokens) { 
     830static void process_stat(conn *c, token_t* tokens, const size_t ntokens) { 
    774831    rel_time_t now = current_time; 
    775832    char* command; 
     
    9721029} 
    9731030 
    974 inline void process_get_command(conn *c, token_t* tokens, size_t ntokens) { 
     1031/* ntokens is overwritten here... shrug.. */ 
     1032static inline void process_get_command(conn *c, token_t* tokens, size_t ntokens) { 
    9751033    char *key; 
    9761034    size_t nkey; 
     
    10261084                 *   " " + flags + " " + data length + "\r\n" + data (with \r\n) 
    10271085                 */ 
    1028                 if (add_iov(c, "VALUE ", 6) || 
    1029                     add_iov(c, ITEM_key(it), it->nkey) || 
    1030                     add_iov(c, ITEM_suffix(it), it->nsuffix + it->nbytes)
     1086                if (add_iov(c, "VALUE ", 6) != 0 || 
     1087                    add_iov(c, ITEM_key(it), it->nkey) != 0 || 
     1088                    add_iov(c, ITEM_suffix(it), it->nsuffix + it->nbytes) != 0
    10311089                    { 
    10321090                        break; 
     
    10701128    add_iov(c, "END\r\n", 5); 
    10711129         
    1072     if (c->udp && build_udp_headers(c)) { 
     1130    if (c->udp && build_udp_headers(c) != 0) { 
    10731131        out_string(c, "SERVER_ERROR out of memory"); 
    10741132    } 
     
    10801138} 
    10811139 
    1082 void process_update_command(conn *c, token_t* tokens, size_t ntokens, int comm) { 
     1140static void process_update_command(conn *c, token_t* tokens, const size_t ntokens, int comm) { 
    10831141    char *key; 
    10841142    size_t nkey; 
     
    11421200} 
    11431201 
    1144 void process_arithmetic_command(conn *c, token_t* tokens, size_t ntokens, int incr) { 
     1202static void process_arithmetic_command(conn *c, token_t* tokens, const size_t ntokens, const int incr) { 
    11451203    char temp[32]; 
    11461204    item *it; 
     
    12041262 
    12051263    ptr = ITEM_data(it); 
    1206     while (*ptr && (*ptr<'0' && *ptr>'9')) ptr++;    // BUG: can't be true 
     1264    while ((*ptr != '\0') && (*ptr<'0' && *ptr>'9')) ptr++;    // BUG: can't be true 
    12071265         
    12081266    value = strtol(ptr, NULL, 10); 
     
    12121270    } 
    12131271 
    1214     if (incr
     1272    if (incr != 0
    12151273        value+=delta; 
    12161274    else { 
     
    12181276        else value-=delta; 
    12191277    } 
    1220     sprintf(buf, "%u", value); 
     1278    snprintf(buf, 32, "%u", value); 
    12211279    res = strlen(buf); 
    12221280    if (res + 2 > it->nbytes) { /* need to realloc */ 
     
    12381296} 
    12391297 
    1240 void process_delete_command(conn *c, token_t* tokens, size_t ntokens) { 
     1298static void process_delete_command(conn *c, token_t* tokens, const size_t ntokens) { 
    12411299    char *key; 
    12421300    size_t nkey; 
     
    13231381} 
    13241382 
    1325 void process_command(conn *c, char *command) { 
     1383static void process_command(conn *c, char *command) { 
    13261384     
    13271385    token_t tokens[MAX_TOKENS]; 
     
    13401398    c->msgused = 0; 
    13411399    c->iovused = 0; 
    1342     if (add_msghdr(c)) { 
     1400    if (add_msghdr(c) != 0) { 
    13431401        out_string(c, "SERVER_ERROR out of memory"); 
    13441402        return; 
     
    15071565 * if we have a complete line in the buffer, process it. 
    15081566 */ 
    1509 int try_read_command(conn *c) { 
     1567static int try_read_command(conn *c) { 
    15101568    char *el, *cont; 
    15111569 
    15121570    assert(c->rcurr <= c->rbuf + c->rsize); 
    15131571 
    1514     if (!c->rbytes
     1572    if (c->rbytes == 0
    15151573        return 0; 
    15161574    el = memchr(c->rcurr, '\n', c->rbytes); 
     
    15391597 * return 0 if there's nothing to read. 
    15401598 */ 
    1541 int try_read_udp(conn *c) { 
     1599static int try_read_udp(conn *c) { 
    15421600    int res; 
    15431601 
     
    15781636 * return 0 if there's nothing to read on the first read. 
    15791637 */ 
    1580 int try_read_network(conn *c) { 
     1638static int try_read_network(conn *c) { 
    15811639    int gotdata = 0; 
    15821640    int res; 
     
    16341692} 
    16351693 
    1636 int update_event(conn *c, int new_flags) { 
     1694static bool update_event(conn *c, const int new_flags) { 
    16371695    struct event_base *base = c->event.ev_base; 
    16381696    if (c->ev_flags == new_flags) 
    1639         return 1
    1640     if (event_del(&c->event) == -1) return 0
     1697        return true
     1698    if (event_del(&c->event) == -1) return false
    16411699    event_set(&c->event, c->sfd, new_flags, event_handler, (void *)c); 
    16421700    event_base_set(base, &c->event); 
    16431701    c->ev_flags = new_flags; 
    1644     if (event_add(&c->event, 0) == -1) return 0
    1645     return 1
     1702    if (event_add(&c->event, 0) == -1) return false
     1703    return true
    16461704} 
    16471705 
     
    16491707 * Sets whether we are listening for new connections or not. 
    16501708 */ 
    1651 void accept_new_conns(int do_accept) { 
     1709void accept_new_conns(const bool do_accept) { 
    16521710    if (! is_listen_thread()) 
    16531711        return; 
    16541712    if (do_accept) { 
    16551713        update_event(listen_conn, EV_READ | EV_PERSIST); 
    1656         if (listen(listen_conn->sfd, 1024)) { 
     1714        if (listen(listen_conn->sfd, 1024) != 0) { 
    16571715            perror("listen"); 
    16581716        } 
     
    16601718    else { 
    16611719        update_event(listen_conn, 0); 
    1662         if (listen(listen_conn->sfd, 0)) { 
     1720        if (listen(listen_conn->sfd, 0) != 0) { 
    16631721            perror("listen"); 
    16641722        } 
     
    16761734 *   TRANSMIT_HARD_ERROR Can't write (c->state is set to conn_closing) 
    16771735 */ 
    1678 int transmit(conn *c) { 
     1736static int transmit(conn *c) { 
    16791737    int res; 
    16801738 
     
    17321790} 
    17331791 
    1734 void drive_machine(conn *c) { 
    1735  
    1736     int stop = 0; 
     1792static void drive_machine(conn *c) { 
     1793    bool stop = false; 
    17371794    int sfd, flags = 1; 
    17381795    socklen_t addrlen; 
     
    17481805                if (errno == EAGAIN || errno == EWOULDBLOCK) { 
    17491806                    /* these are transient, so don't log anything */ 
    1750                     stop = 1
     1807                    stop = true
    17511808                } else if (errno == EMFILE) { 
    17521809                    if (settings.verbose > 0) 
    17531810                        fprintf(stderr, "Too many open connections\n"); 
    1754                     accept_new_conns(0); 
     1811                    accept_new_conns(false); 
    17551812                    stop = 1; 
    17561813                } else { 
     
    17671824            } 
    17681825            dispatch_conn_new(sfd, conn_read, EV_READ | EV_PERSIST, 
    1769                                      DATA_BUFFER_SIZE, 0); 
     1826                                     DATA_BUFFER_SIZE, false); 
    17701827            break; 
    17711828 
    17721829        case conn_read: 
    1773             if (try_read_command(c)) { 
     1830            if (try_read_command(c) != 0) { 
    17741831                continue; 
    17751832            } 
    1776             if (c->udp ? try_read_udp(c) : try_read_network(c)) { 
     1833            if ((c->udp ? try_read_udp(c) : try_read_network(c)) != 0) { 
    17771834                continue; 
    17781835            } 
     
    17841841                break; 
    17851842            } 
    1786             stop = 1
     1843            stop = true
    17871844            break; 
    17881845 
     
    18251882                    break; 
    18261883                } 
    1827                 stop = 1
     1884                stop = true
    18281885                break; 
    18291886            } 
     
    18701927                    break; 
    18711928                } 
    1872                 stop = 1
     1929                stop = true
    18731930                break; 
    18741931            } 
     
    18861943             */ 
    18871944            if (c->iovused == 0 || (c->udp && c->iovused == 1)) { 
    1888                 if (add_iov(c, c->wcurr, c->wbytes) || 
    1889                     (c->udp && build_udp_headers(c))) { 
     1945                if (add_iov(c, c->wcurr, c->wbytes) != 0 || 
     1946                    (c->udp && build_udp_headers(c) != 0)) { 
    18901947                    if (settings.verbose > 0) 
    18911948                        fprintf(stderr, "Couldn't build response\n"); 
     
    19271984 
    19281985            case TRANSMIT_SOFT_ERROR: 
    1929                 stop = 1
     1986                stop = true
    19301987                break; 
    19311988            } 
     
    19371994            else 
    19381995                conn_close(c); 
    1939             stop = 1
     1996            stop = true
    19401997            break; 
    19411998        } 
     
    19452002} 
    19462003 
    1947 void event_handler(int fd, short which, void *arg) { 
     2004void event_handler(const int fd, const short which, void *arg) { 
    19482005    conn *c; 
    19492006 
     
    19652022} 
    19662023 
    1967 int new_socket(int is_udp) { 
     2024static int new_socket(const bool is_udp) { 
    19682025    int sfd; 
    19692026    int flags; 
     
    19872044 * Sets a socket's send buffer size to the maximum allowed by the system. 
    19882045 */ 
    1989 void maximize_sndbuf(int sfd) { 
     2046static void maximize_sndbuf(const int sfd) { 
    19902047    socklen_t intsize = sizeof(int); 
    19912048    int last_good = 0; 
     
    19942051 
    19952052    /* Start with the default size. */ 
    1996     if (getsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &old_size, &intsize)) { 
     2053    if (getsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &old_size, &intsize) != 0) { 
    19972054        if (settings.verbose > 0) 
    19982055            perror("getsockopt(SO_SNDBUF)"); 
     
    20192076 
    20202077 
    2021 int server_socket(int port, int is_udp) { 
     2078static int server_socket(const int port, const bool is_udp) { 
    20222079    int sfd; 
    20232080    struct linger ling = {0, 0}; 
     
    20522109        return -1; 
    20532110    } 
    2054     if (! is_udp && listen(sfd, 1024) == -1) { 
     2111    if (!is_udp && listen(sfd, 1024) == -1) { 
    20552112        perror("listen()"); 
    20562113        close(sfd); 
     
    20602117} 
    20612118 
    2062 int new_socket_unix(void) { 
     2119static int new_socket_unix(void) { 
    20632120    int sfd; 
    20642121    int flags; 
     
    20782135} 
    20792136 
    2080 int server_socket_unix(char *path) { 
     2137static int server_socket_unix(char *path) { 
    20812138    int sfd; 
    20822139    struct linger ling = {0, 0}; 
     
    20962153     * Clean up a previous socket file if we left it around 
    20972154     */ 
    2098     if (!lstat(path, &tstat)) { 
     2155    if (lstat(path, &tstat) == 0) { 
    20992156        if (S_ISSOCK(tstat.st_mode)) 
    21002157            unlink(path); 
     
    21262183} 
    21272184 
     2185/* listening socket */ 
     2186static int l_socket=0; 
     2187 
     2188/* udp socket */ 
     2189static int u_socket=-1; 
    21282190 
    21292191/* invoke right before gdb is called, on assert */ 
    2130 void pre_gdb () { 
     2192void pre_gdb(void) { 
    21312193    int i = 0; 
    2132     if(l_socket) close(l_socket); 
    2133     if(u_socket > -1) close(u_socket); 
     2194    if (l_socket > -1) close(l_socket); 
     2195    if (u_socket > -1) close(u_socket); 
    21342196    for (i=3; i<=500; i++) close(i); /* so lame */ 
    21352197    kill(getpid(), SIGABRT); 
     
    21452207 */ 
    21462208volatile rel_time_t curre