Changeset 468

Show
Ignore:
Timestamp:
03/06/07 16:28:53 (2 years ago)
Author:
plindner
Message:

multiple cleanups/refactoring, see ChangeLog

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/server/ChangeLog

    r465 r468  
    112007-03-05  Paul Lindner  <lindner@inuus.com> 
    2  
     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         
    312        * Avoid type-punning.  Do a more efficient realloc inside the 
    413          conn_shrink routine. 
     
    615        * Fix overflow bug where uninitialized access to slabclass caused 
    716          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. 
    824 
    9252006-12-23 
  • trunk/server/Makefile.am

    r379 r468  
    11bin_PROGRAMS = memcached memcached-debug 
    22 
    3 memcached_SOURCES = memcached.c slabs.c items.c assoc.c memcached.h 
     3memcached_SOURCES = memcached.c slabs.c slabs.h items.c items.h assoc.c assoc.h memcached.h  
    44memcached_debug_SOURCES = $(memcached_SOURCES) 
    55memcached_CPPFLAGS = -DNDEBUG 
  • trunk/server/assoc.c

    r450 r468  
    143143 
    144144#if HASH_LITTLE_ENDIAN == 1 
    145 uint32_t hash(  
     145static uint32_t hash(  
    146146  const void *key,       /* the key to hash */ 
    147147  size_t      length,    /* length of the key */ 
    148   uint32_t    initval)   /* initval */ 
     148  const uint32_t    initval)   /* initval */ 
    149149{ 
    150150  uint32_t a,b,c;                                          /* internal state */ 
     
    324324 * big-endian byte ordering.  
    325325 */ 
    326 uint32_t hash( const void *key, size_t length, uint32_t initval) 
     326static uint32_t hash( const void *key, size_t length, const uint32_t initval) 
    327327{ 
    328328  uint32_t a,b,c; 
     
    455455 
    456456/* how many powers of 2's worth of buckets we use */ 
    457 int hashpower = 16; 
     457static int hashpower = 16; 
    458458 
    459459#define hashsize(n) ((ub4)1<<(n)) 
     
    491491} 
    492492 
    493 item *assoc_find(const char *key, size_t nkey) { 
     493item *assoc_find(const char *key, const size_t nkey) { 
    494494    uint32_t hv = hash(key, nkey, 0); 
    495495    item *it; 
     
    517517   the item wasn't found */ 
    518518 
    519 static item** _hashitem_before (const char *key, size_t nkey) { 
     519static item** _hashitem_before (const char *key, const size_t nkey) { 
    520520    uint32_t hv = hash(key, nkey, 0); 
    521521    item **pos; 
     
    604604} 
    605605 
    606 void assoc_delete(const char *key, size_t nkey) { 
     606void assoc_delete(const char *key, const size_t nkey) { 
    607607    item **before = _hashitem_before(key, nkey); 
    608608 
  • trunk/server/configure.ac

    r453 r468  
    9898AC_CHECK_FUNC(daemon,AC_DEFINE([HAVE_DAEMON],,[Define this if you have daemon()]),[AC_LIBOBJ(daemon)]) 
    9999 
    100  
     100AC_HEADER_STDBOOL 
     101AC_C_CONST 
    101102AC_CHECK_HEADER(malloc.h, AC_DEFINE(HAVE_MALLOC_H,,[do we have malloc.h?])) 
    102103AC_CHECK_MEMBER([struct mallinfo.arena], [ 
  • trunk/server/items.c

    r450 r468  
    2020#include "memcached.h" 
    2121 
     22/* Forward Declarations */ 
     23static void item_link_q(item *it); 
     24static void item_unlink_q(item *it); 
     25 
    2226/* 
    2327 * We only reposition items in the LRU queue if they haven't been repositioned 
     
    3034static item *heads[LARGEST_ID]; 
    3135static item *tails[LARGEST_ID]; 
    32 unsigned int sizes[LARGEST_ID]; 
     36static unsigned int sizes[LARGEST_ID]; 
    3337 
    3438void item_init(void) { 
     
    5458 * Returns the total size of the header. 
    5559 */ 
    56 int item_make_header(char *key, uint8_t nkey, int flags, int nbytes, 
    57                      char *suffix, int *nsuffix) { 
    58     *nsuffix = sprintf(suffix, " %u %u\r\n", flags, nbytes - 2); 
     60static size_t item_make_header(char *key, const uint8_t nkey, const int flags, const int nbytes, 
     61                     char *suffix, uint8_t *nsuffix) { 
     62    /* suffix is defined at 40 chars elsewhere.. */ 
     63    *nsuffix = (uint8_t) snprintf(suffix, 40, " %d %d\r\n", flags, nbytes - 2); 
    5964    return sizeof(item) + nkey + *nsuffix + nbytes; 
    6065} 
    6166  
    62 item *item_alloc(char *key, size_t nkey, int flags, rel_time_t exptime, int nbytes) { 
    63     int nsuffix, ntotal; 
     67/*@null@*/ 
     68item *item_alloc(char *key, const size_t nkey, const int flags, const rel_time_t exptime, const int nbytes) { 
     69    uint8_t nsuffix; 
     70    size_t ntotal; 
    6471    item *it; 
    6572    unsigned int id; 
     
    8188         */ 
    8289 
    83         if (!settings.evict_to_free) return 0
     90        if (settings.evict_to_free == 0) return NULL
    8491 
    8592        /*  
     
    9097         */ 
    9198 
    92         if (id > LARGEST_ID) return 0
    93         if (tails[id]==0) return 0
     99        if (id > LARGEST_ID) return NULL
     100        if (tails[id]==0) return NULL
    94101 
    95102        for (search = tails[id]; tries>0 && search; tries--, search=search->prev) { 
     
    100107        } 
    101108        it = slabs_alloc(ntotal); 
    102         if (it==0) return 0
     109        if (it==0) return NULL
    103110    } 
    104111 
     
    116123    strcpy(ITEM_key(it), key); 
    117124    it->exptime = exptime; 
    118     memcpy(ITEM_suffix(it), suffix, nsuffix); 
     125    memcpy(ITEM_suffix(it), suffix, (size_t) nsuffix); 
    119126    it->nsuffix = nsuffix; 
    120127    return it; 
     
    122129 
    123130void item_free(item *it) { 
    124     unsigned int ntotal = ITEM_ntotal(it); 
     131    size_t ntotal = ITEM_ntotal(it); 
    125132    assert((it->it_flags & ITEM_LINKED) == 0); 
    126133    assert(it != heads[it->slabs_clsid]); 
     
    138145 * the maximum for a cache entry.) 
    139146 */ 
    140 int item_size_ok(char *key, size_t nkey, int flags, int nbytes) { 
     147bool item_size_ok(char *key, const size_t nkey, const int flags, const int nbytes) { 
    141148    char prefix[40]; 
    142     int nsuffix; 
     149    uint8_t nsuffix; 
    143150 
    144151    return slabs_clsid(item_make_header(key, nkey + 1, flags, nbytes, 
     
    146153} 
    147154 
    148 void item_link_q(item *it) { /* item is the new head */ 
     155static void item_link_q(item *it) { /* item is the new head */ 
    149156    item **head, **tail; 
    150157    /* always true, warns: assert(it->slabs_clsid <= LARGEST_ID); */ 
     
    164171} 
    165172 
    166 void item_unlink_q(item *it) { 
     173static void item_unlink_q(item *it) { 
    167174    item **head, **tail; 
    168175    /* always true, warns: assert(it->slabs_clsid <= LARGEST_ID); */ 
     
    204211 
    205212void item_unlink(item *it) { 
    206     if (it->it_flags & ITEM_LINKED) { 
     213    if ((it->it_flags & ITEM_LINKED) != 0) { 
    207214        it->it_flags &= ~ITEM_LINKED; 
    208215        stats.curr_bytes -= ITEM_ntotal(it); 
     
    216223void item_remove(item *it) { 
    217224    assert((it->it_flags & ITEM_SLABBED) == 0); 
    218     if (it->refcount) it->refcount--; 
    219     assert((it->it_flags & ITEM_DELETED) == 0 || it->refcount); 
     225    if (it->refcount != 0) it->refcount--; 
     226    assert((it->it_flags & ITEM_DELETED) == 0 || it->refcount != 0); 
    220227    if (it->refcount == 0 && (it->it_flags & ITEM_LINKED) == 0) { 
    221228        item_free(it); 
     
    240247} 
    241248 
    242 char *item_cachedump(unsigned int slabs_clsid, unsigned int limit, unsigned int *bytes) { 
    243  
     249/*@null@*/ 
     250char *item_cachedump(const unsigned int slabs_clsid, const unsigned int limit, unsigned int *bytes) { 
    244251    int memlimit = 2*1024*1024; 
    245252    char *buffer; 
    246     int bufcurr; 
     253    unsigned int bufcurr; 
    247254    item *it; 
    248255    int len; 
     
    250257    char temp[512]; 
    251258 
    252     if (slabs_clsid > LARGEST_ID) return 0
     259    if (slabs_clsid > LARGEST_ID) return NULL
    253260    it = heads[slabs_clsid]; 
    254261 
    255     buffer = malloc(memlimit); 
    256     if (buffer == 0) return 0
     262    buffer = malloc((size_t)memlimit); 
     263    if (buffer == 0) return NULL
    257264    bufcurr = 0; 
    258265 
    259     while (it && (!limit || shown < limit)) { 
    260         len = sprintf(temp, "ITEM %s [%u b; %lu s]\r\n", ITEM_key(it), it->nbytes - 2, it->time + stats.started); 
     266    while (it != NULL && (limit==0 || shown < limit)) { 
     267        len = snprintf(temp, 512, "ITEM %s [%d b; %lu s]\r\n", ITEM_key(it), it->nbytes - 2, it->time + stats.started); 
    261268        if (bufcurr + len + 6 > memlimit)  /* 6 is END\r\n\0 */ 
    262269            break; 
     
    274281} 
    275282 
    276 void item_stats(char *buffer, int buflen) { 
     283void item_stats(char *buffer, const int buflen) { 
    277284    int i; 
    278285    char *bufcurr = buffer; 
     
    286293    for (i=0; i<LARGEST_ID; i++) { 
    287294        if (tails[i]) 
    288             bufcurr += sprintf(bufcurr, "STAT items:%u:number %u\r\nSTAT items:%u:age %u\r\n", 
     295            bufcurr += snprintf(bufcurr, (size_t)buflen, "STAT items:%d:number %u\r\nSTAT items:%d:age %u\r\n", 
    289296                               i, sizes[i], i, now - tails[i]->time); 
    290297    } 
     
    294301 
    295302/* dumps out a list of objects of each size, with granularity of 32 bytes */ 
     303/*@null@*/ 
    296304char* item_stats_sizes(int *bytes) { 
    297     int num_buckets = 32768;   /* max 1MB object, divided into 32 bytes size buckets */ 
    298     unsigned int *histogram = (unsigned int*) malloc(num_buckets * sizeof(int)); 
     305    const int num_buckets = 32768;   /* max 1MB object, divided into 32 bytes size buckets */ 
     306    unsigned int *histogram = (unsigned int*) malloc((size_t)num_buckets * sizeof(int)); 
    299307    char *buf = (char*) malloc(1024*1024*2*sizeof(char)); 
    300308    int i; 
     
    303311        if (histogram) free(histogram); 
    304312        if (buf) free(buf); 
    305         return 0
     313        return NULL
    306314    } 
    307315 
    308316    /* build the histogram */ 
    309     memset(histogram, 0, num_buckets * sizeof(int)); 
     317    memset(histogram, 0, (size_t) num_buckets * sizeof(int)); 
    310318    for (i=0; i<LARGEST_ID; i++) { 
    311319        item *iter = heads[i]; 
     
    313321            int ntotal = ITEM_ntotal(iter); 
    314322            int bucket = ntotal / 32; 
    315             if (ntotal % 32) bucket++; 
     323            if ((ntotal % 32) != 0) bucket++; 
    316324            if (bucket < num_buckets) histogram[bucket]++; 
    317325            iter = iter->next; 
     
    322330    *bytes = 0; 
    323331    for (i=0; i<num_buckets; i++) { 
    324         if (histogram[i]) { 
    325             *bytes += sprintf(&buf[*bytes], "%u %u\r\n", i*32, histogram[i]); 
     332        if (histogram[i] != 0) { 
     333            *bytes += sprintf(&buf[*bytes], "%d %u\r\n", i*32, histogram[i]); 
    326334        } 
    327335    } 
     
    335343    int i; 
    336344    item *iter, *next; 
    337     if (! settings.oldest_live
     345    if (settings.oldest_live == 0
    338346        return; 
    339347    for (i = 0; i < LARGEST_ID; i++) { 
  • trunk/server/memcached.c

    r465 r468  
    6767#include "memcached.h" 
    6868 
     69/* 
     70 * forward declarations  
     71 */ 
     72static void drive_machine(conn *c); 
     73static int new_socket(const bool is_udp); 
     74static int server_socket(const int port, const bool is_udp); 
     75static int try_read_command(conn *c); 
     76static int try_read_network(conn *c); 
     77static int try_read_udp(conn *c); 
     78 
     79/* stats */ 
     80static void stats_reset(void); 
     81static void stats_init(void); 
     82 
     83/* defaults */ 
     84static void settings_init(void); 
     85 
     86/* event handling, network IO */ 
     87static void event_handler(const int fd, const short which, void *arg); 
     88static conn *conn_new(const int sfd, const int init_state, const int event_flags, const int read_buffer_size, const bool is_udp); 
     89static void conn_close(conn *c); 
     90static void conn_init(void); 
     91static void accept_new_conns(const bool do_accept); 
     92static bool update_event(conn *c, const int new_flags); 
     93static void complete_nread(conn *c); 
     94static void process_command(conn *c, char *command); 
     95static int transmit(conn *c); 
     96static int ensure_iov_space(conn *c); 
     97static int add_iov(conn *c, const void *buf, int len); 
     98static int add_msghdr(conn *c); 
     99 
     100 
     101/* time handling */ 
     102static void set_current_time(void);  /* update the global variable holding 
     103                              global 32-bit seconds-since-start time 
     104                              (to avoid 64 bit time_t) */ 
     105 
     106void pre_gdb(void); 
     107 
     108/** exported globals **/ 
    69109struct stats stats; 
    70110struct settings settings; 
    71111 
     112/** file scope variables **/ 
    72113static item **todelete = 0; 
    73114static int delcurr; 
     
    80121#define TRANSMIT_HARD_ERROR 3 
    81122 
    82 int *buckets = 0; /* bucket->generation array for a managed instance */ 
     123static int *buckets = 0; /* bucket->generation array for a managed instance */ 
    83124 
    84125#define REALTIME_MAXDELTA 60*60*24*30 
    85 rel_time_t realtime(time_t exptime) { 
     126/* 
     127 * given time value that's either unix time or delta from current unix time, return 
     128 * unix time. Use the fact that delta can't exceed one month (and real time value can't 
     129 * be that low). 
     130 */ 
     131static rel_time_t realtime(const time_t exptime) { 
    86132    /* no. of seconds in 30 days - largest possible delta exptime */ 
    87133 
     
    103149} 
    104150 
    105 void stats_init(void) { 
     151static void stats_init(void) { 
    106152    stats.curr_items = stats.total_items = stats.curr_conns = stats.total_conns = stats.conn_structs = 0; 
    107153    stats.get_cmds = stats.set_cmds = stats.get_hits = stats.get_misses = 0; 
     
    115161} 
    116162 
    117 void stats_reset(void) { 
     163static void stats_reset(void) { 
    118164    stats.total_items = stats.total_conns = 0; 
    119165    stats.get_cmds = stats.set_cmds = stats.get_hits = stats.get_misses = 0; 
     
    121167} 
    122168 
    123 void settings_init(void) { 
     169static void settings_init(void) { 
    124170    settings.port = 11211; 
    125171    settings.udpport = 0; 
     
    131177    settings.evict_to_free = 1;       /* push old items out of cache when memory runs out */ 
    132178    settings.socketpath = NULL;       /* by default, not using a unix socket */ 
    133     settings.managed = 0
     179    settings.managed = false
    134180    settings.factor = 1.25; 
    135181    settings.chunk_size = 48;         /* space for a modest key and value */ 
     
    138184/* returns true if a deleted item's delete-locked-time is over, and it 
    139185   should be removed from the namespace */ 
    140 int item_delete_lock_over (item *it) { 
     186static bool item_delete_lock_over (item *it) { 
    141187    assert(it->it_flags & ITEM_DELETED); 
    142188    return (current_time >= it->exptime); 
     
    144190 
    145191/* wrapper around assoc_find which does the lazy expiration/deletion logic */ 
    146 item *get_item_notedeleted(char *key, size_t nkey, int *delete_locked) { 
     192static item *get_item_notedeleted(const char *key, const size_t nkey, int *delete_locked) { 
    147193    item *it = assoc_find(key, nkey); 
     194 
    148195    if (delete_locked) *delete_locked = 0; 
    149     if (it && (it->it_flags & ITEM_DELETED)) { 
     196 
     197    if (it != NULL && (it->it_flags & ITEM_DELETED)) { 
    150198        /* it's flagged as delete-locked.  let's see if that condition 
    151199           is past due, and the 5-second delete_timer just hasn't 
     
    156204        } 
    157205    } 
    158     if (it && settings.oldest_live && settings.oldest_live <= current_time && 
     206    if (it != NULL && settings.oldest_live != 0 && settings.oldest_live <= current_time && 
    159207        it->time <= settings.oldest_live) { 
    160208        item_unlink(it); 
    161209        it = 0; 
    162210    } 
    163     if (it && it->exptime && it->exptime <= current_time) { 
     211    if (it != NULL && it->exptime != 0 && it->exptime <= current_time) { 
    164212        item_unlink(it); 
    165213        it = 0; 
     
    168216} 
    169217 
    170 item *get_item(char *key, size_t nkey) { 
     218static item *get_item(const char *key, const size_t nkey) { 
    171219    return get_item_notedeleted(key, nkey, 0); 
    172220} 
     
    177225 * Returns 0 on success, -1 on out-of-memory. 
    178226 */ 
    179 int add_msghdr(conn *c) 
     227static int add_msghdr(conn *c) 
    180228{ 
    181229    struct msghdr *msg; 
     
    210258} 
    211259 
    212 conn **freeconns; 
    213 int freetotal; 
    214 int freecurr; 
    215  
    216 void conn_init(void) { 
     260static conn **freeconns; 
     261static int freetotal; 
     262static int freecurr; 
     263 
     264static void conn_init(void) { 
    217265    freetotal = 200; 
    218266    freecurr = 0; 
    219267    freeconns = (conn **)malloc(sizeof (conn *)*freetotal); 
     268    /** TODO check for NULL **/ 
    220269    return; 
    221270} 
    222271 
    223 conn *conn_new(int sfd, int init_state, int event_flags, int read_buffer_size, 
    224                 int is_udp) { 
     272/*@null@*/ 
     273static conn *conn_new(const int sfd, const int init_state, const int event_flags,  
     274                      const int read_buffer_size, const bool is_udp) { 
    225275    conn *c; 
    226276 
     
    231281        if (!(c = (conn *)malloc(sizeof(conn)))) { 
    232282            perror("malloc()"); 
    233             return 0
     283            return NULL
    234284        } 
    235285        c->rbuf = c->wbuf = 0; 
     
    246296        c->hdrsize = 0; 
    247297 
    248         c->rbuf = (char *) malloc(c->rsize); 
    249         c->wbuf = (char *) malloc(c->wsize); 
     298        c->rbuf = (char *) malloc((size_t)c->rsize); 
     299        c->wbuf = (char *) malloc((size_t)c->wsize); 
    250300        c->ilist = (item **) malloc(sizeof(item *) * c->isize); 
    251301        c->iov = (struct iovec *) malloc(sizeof(struct iovec) * c->iovsize); 
     
    261311            free(c); 
    262312            perror("malloc()"); 
    263             return 0
     313            return NULL
    264314        } 
    265315 
     
    312362            free (c); 
    313363        } 
    314         return 0
     364        return NULL
    315365    } 
    316366 
     
    321371} 
    322372 
    323 void conn_cleanup(conn *c) { 
     373static void conn_cleanup(conn *c) { 
    324374    if (c->item) { 
    325375        item_free(c->item); 
     
    327377    } 
    328378 
    329     if (c->ileft) { 
     379    if (c->ileft != 0) { 
    330380        for (; c->ileft > 0; c->ileft--,c->icurr++) { 
    331381            item_remove(*(c->icurr)); 
     
    360410} 
    361411 
    362 void conn_close(conn *c) { 
     412static void conn_close(conn *c) { 
    363413    /* delete the event, the socket and the conn */ 
    364414    event_del(&c->event); 
     
    368418 
    369419    close(c->sfd); 
    370     accept_new_conns(1); 
     420    accept_new_conns(true); 
    371421    conn_cleanup(c); 
    372422 
     
    379429    } else { 
    380430        /* try to enlarge free connections array */ 
    381         conn **new_freeconns = realloc(freeconns, sizeof(conn *)*freetotal*2); 
     431        conn **new_freeconns = realloc((void*)freeconns, sizeof(conn *)*freetotal*2); 
    382432        if (new_freeconns) { 
    383433            freetotal *= 2; 
     
    409459    if (c->rsize > READ_BUFFER_HIGHWAT && c->rbytes < DATA_BUFFER_SIZE) { 
    410460        if (c->rcurr != c->rbuf) 
    411             memmove(c->rbuf, c->rcurr, c->rbytes); 
     461            memmove(c->rbuf, c->rcurr, (size_t) c->rbytes); 
    412462 
    413463        char *newbuf = (char*) realloc((void*)c->rbuf, DATA_BUFFER_SIZE); 
     464 
    414465        if (newbuf) { 
    415466            c->rbuf = newbuf; 
     
    453504 * happen here. 
    454505 */ 
    455 void conn_set_state(conn *c, int state) { 
     506static void conn_set_state(conn *c, int state) { 
    456507    if (state != c->state) { 
    457508        if (state == conn_read) { 
     
    470521 * Returns 0 on success, -1 on out-of-memory. 
    471522 */ 
    472 int ensure_iov_space(conn *c) { 
     523static int ensure_iov_space(conn *c) { 
    473524    if (c->iovused >= c->iovsize) { 
    474525        int i, iovnum; 
     
    498549 */ 
    499550 
    500 int add_iov(conn *c, const void *buf, int len) { 
     551static int add_iov(conn *c, const void *buf, int len) { 
    501552    struct msghdr *m; 
    502553    int leftover; 
    503     int limit_to_mtu; 
     554    bool limit_to_mtu; 
    504555 
    505556    do { 
     
    519570        } 
    520571 
    521         if (ensure_iov_space(c)
     572        if (ensure_iov_space(c) != 0
    522573            return -1; 
    523574 
     
    549600 * Constructs a set of UDP headers and attaches them to the outgoing messages. 
    550601 */ 
    551 int build_udp_headers(conn *c) { 
     602static int build_udp_headers(conn *c) { 
    552603    int i; 
    553604    unsigned char *hdr; 
     
    584635 
    585636 
    586 void out_string(conn *c, char *str) { 
     637static void out_string(conn *c, const char *str) { 
    587638    int len; 
    588639 
     
    612663 */ 
    613664 
    614 void complete_nread(conn *c) { 
     665static void complete_nread(conn *c) { 
    615666    item *it = c->item; 
    616667    int comm = c->item_comm; 
     
    628679    old_it = get_item_notedeleted(key, it->nkey, &delete_locked); 
    629680 
    630     if (old_it && comm == NREAD_ADD) { 
     681    if (old_it != NULL && comm == NREAD_ADD) { 
    631682        item_update(old_it);  /* touches item, promotes to head of LRU */ 
    632683        out_string(c, "NOT_STORED"); 
     
    634685    } 
    635686 
    636     if (!old_it && comm == NREAD_REPLACE) { 
     687    if (old_it == NULL && comm == NREAD_REPLACE) { 
    637688        out_string(c, "NOT_STORED"); 
    638689        goto err; 
    639690    } 
    640691 
    641     if (delete_locked) { 
     692    if (delete_locked != 0) { 
    642693        if (comm == NREAD_REPLACE || comm == NREAD_ADD) { 
    643694            out_string(c, "NOT_STORED"); 
     
    696747 *   } 
    697748 */ 
    698 size_t tokenize_command(char* command, token_t* tokens, size_t max_tokens)  { 
     749static size_t tokenize_command(char* command, token_t* tokens, const size_t max_tokens)  { 
    699750    char* cp; 
    700751    char* value = NULL; 
     
    742793} 
    743794 
    744 void process_stat(conn *c, token_t* tokens, size_t ntokens) { 
     795static void process_stat(conn *c, token_t* tokens, const size_t ntokens) { 
    745796    rel_time_t now = current_time; 
    746797    char* command; 
     
    932983} 
    933984 
    934 inline void process_get_command(conn *c, token_t* tokens, size_t ntokens) { 
     985/* ntokens is overwritten here... shrug.. */ 
     986static inline void process_get_command(conn *c, token_t* tokens, size_t ntokens) { 
    935987    char *key; 
    936988    size_t nkey; 
     
    9811033                 *   " " + flags + " " + data length + "\r\n" + data (with \r\n) 
    9821034                 */ 
    983                 if (add_iov(c, "VALUE ", 6) || 
    984                     add_iov(c, ITEM_key(it), it->nkey) || 
    985                     add_iov(c, ITEM_suffix(it), it->nsuffix + it->nbytes)
     1035                if (add_iov(c, "VALUE ", 6) != 0 || 
     1036                    add_iov(c, ITEM_key(it), it->nkey) != 0 || 
     1037                    add_iov(c, ITEM_suffix(it), it->nsuffix + it->nbytes) != 0
    9861038                    { 
    9871039                        break; 
     
    10191071    add_iov(c, "END\r\n", 5); 
    10201072         
    1021     if (c->udp && build_udp_headers(c)) { 
     1073    if (c->udp && build_udp_headers(c) != 0) { 
    10221074        out_string(c, "SERVER_ERROR out of memory"); 
    10231075    } 
     
    10291081} 
    10301082 
    1031 void process_update_command(conn *c, token_t* tokens, size_t ntokens, int comm) { 
     1083static void process_update_command(conn *c, token_t* tokens, const size_t ntokens, int comm) { 
    10321084    char *key; 
    10331085    size_t nkey; 
     
    10881140} 
    10891141 
    1090 void process_arithmetic_command(conn *c, token_t* tokens, size_t ntokens, int incr) { 
     1142static void process_arithmetic_command(conn *c, token_t* tokens, const size_t ntokens, const int incr) { 
    10911143    char temp[32]; 
    10921144    unsigned int value; 
     
    11331185 
    11341186    ptr = ITEM_data(it); 
    1135     while (*ptr && (*ptr<'0' && *ptr>'9')) ptr++;    // BUG: can't be true 
     1187    while ((*ptr != '\0') && (*ptr<'0' && *ptr>'9')) ptr++;    // BUG: can't be true 
    11361188         
    11371189    value = strtol(ptr, NULL, 10); 
     
    11421194    } 
    11431195     
    1144     if (incr
     1196    if (incr != 0
    11451197        value+=delta; 
    11461198    else { 
     
    11481200        else value-=delta; 
    11491201    } 
    1150     sprintf(temp, "%u", value); 
     1202    snprintf(temp, 32, "%u", value); 
    11511203    res = strlen(temp); 
    11521204    if (res + 2 > it->nbytes) { /* need to realloc */ 
     
    11681220} 
    11691221 
    1170 void process_delete_command(conn *c, token_t* tokens, size_t ntokens) { 
     1222static void process_delete_command(conn *c, token_t* tokens, const size_t ntokens) { 
    11711223    char *key; 
    11721224    size_t nkey; 
     
    12391291} 
    12401292 
    1241 void process_command(conn *c, char *command) { 
     1293static void process_command(conn *c, char *command) { 
    12421294     
    12431295    token_t tokens[MAX_TOKENS]; 
     
    12561308    c->msgused = 0; 
    12571309    c->iovused = 0; 
    1258     if (add_msghdr(c)) { 
     1310    if (add_msghdr(c) != 0) { 
    12591311        out_string(c, "SERVER_ERROR out of memory"); 
    12601312        return; 
     
    14231475 * if we have a complete line in the buffer, process it. 
    14241476 */ 
    1425 int try_read_command(conn *c) { 
     1477static int try_read_command(conn *c) { 
    14261478    char *el, *cont; 
    14271479 
    14281480    assert(c->rcurr <= c->rbuf + c->rsize); 
    14291481 
    1430     if (!c->rbytes
     1482    if (c->rbytes == 0
    14311483        return 0; 
    14321484    el = memchr(c->rcurr, '\n', c->rbytes); 
     
    14551507 * return 0 if there's nothing to read. 
    14561508 */ 
    1457 int try_read_udp(conn *c) { 
     1509static int try_read_udp(conn *c) { 
    14581510    int res; 
    14591511 
     
    14921544 * return 0 if there's nothing to read on the first read. 
    14931545 */ 
    1494 int try_read_network(conn *c) { 
     1546static int try_read_network(conn *c) { 
    14951547    int gotdata = 0; 
    14961548    int res; 
     
    15461598} 
    15471599 
    1548 int update_event(conn *c, int new_flags) { 
     1600static bool update_event(conn *c, const int new_flags) { 
    15491601    if (c->ev_flags == new_flags) 
    1550         return 1
    1551     if (event_del(&c->event) == -1) return 0
     1602        return true
     1603    if (event_del(&c->event) == -1) return false
    15521604    event_set(&c->event, c->sfd, new_flags, event_handler, (void *)c); 
    15531605    c->ev_flags = new_flags; 
    1554     if (event_add(&c->event, 0) == -1) return 0
    1555     return 1
     1606    if (event_add(&c->event, 0) == -1) return false
     1607    return true
    15561608} 
    15571609 
     
    15591611 * Sets whether we are listening for new connections or not. 
    15601612 */ 
    1561 void accept_new_conns(int do_accept) { 
     1613void accept_new_conns(const bool do_accept) { 
    15621614    if (do_accept) { 
    15631615        update_event(listen_conn, EV_READ | EV_PERSIST); 
    1564         if (listen(listen_conn->sfd, 1024)) { 
     1616        if (listen(listen_conn->sfd, 1024) != 0) { 
    15651617            perror("listen"); 
    15661618        } 
     
    15681620    else { 
    15691621        update_event(listen_conn, 0); 
    1570         if (listen(listen_conn->sfd, 0)) { 
     1622        if (listen(listen_conn->sfd, 0) != 0) { 
    15711623            perror("listen"); 
    15721624        } 
     
    15841636 *   TRANSMIT_HARD_ERROR Can't write (c->state is set to conn_closing) 
    15851637 */ 
    1586 int transmit(conn *c) { 
     1638static int transmit(conn *c) { 
    15871639    int res; 
    15881640 
     
    16381690} 
    16391691 
    1640 void drive_machine(conn *c) { 
    1641  
    1642     int stop = 0; 
     1692static void drive_machine(conn *c) { 
     1693    bool stop = false; 
    16431694    int sfd, flags = 1; 
    16441695    socklen_t addrlen; 
     
    16531704            if ((sfd = accept(c->sfd, &addr, &addrlen)) == -1) { 
    16541705                if (errno == EAGAIN || errno == EWOULDBLOCK) { 
    1655                     stop = 1
     1706                    stop = true
    16561707                    break; 
    16571708                } else if (errno == EMFILE) { 
    16581709                    if (settings.verbose > 0) 
    16591710                        fprintf(stderr, "Too many open connections\n"); 
    1660                     accept_new_conns(0); 
     1711                    accept_new_conns(false); 
    16611712                } else { 
    16621713                    perror("accept()"); 
     
    16711722            } 
    16721723            newc = conn_new(sfd, conn_read, EV_READ | EV_PERSIST, 
    1673                             DATA_BUFFER_SIZE, 0); 
     1724                            DATA_BUFFER_SIZE, false); 
    16741725            if (!newc) { 
    16751726                if (settings.verbose > 0) 
     
    16821733 
    16831734        case conn_read: 
    1684             if (try_read_command(c)) { 
     1735            if (try_read_command(c) != 0) { 
    16851736                continue; 
    16861737            } 
    1687             if (c->udp ? try_read_udp(c) : try_read_network(c)) { 
     1738            if ((c->udp ? try_read_udp(c) : try_read_network(c)) != 0) { 
    16881739                continue; 
    16891740            } 
     
    16951746                break; 
    16961747            } 
    1697             stop = 1
     1748            stop = true
    16981749            break; 
    16991750 
     
    17341785                    break; 
    17351786                } 
    1736                 stop = 1
     1787                stop = true
    17371788                break; 
    17381789            } 
     
    17771828                    break; 
    17781829                } 
    1779                 stop = 1
     1830                stop = true
    17801831                break; 
    17811832            } 
     
    17931844             */ 
    17941845            if (c->iovused == 0 || (c->udp && c->iovused == 1)) { 
    1795                 if (add_iov(c, c->wcurr, c->wbytes) || 
    1796                     (c->udp && build_udp_headers(c))) { 
     1846                if (add_iov(c, c->wcurr, c->wbytes) != 0 || 
     1847                    (c->udp && build_udp_headers(c) != 0)) { 
    17971848                    if (settings.verbose > 0) 
    17981849                        fprintf(stderr, "Couldn't build response\n"); 
     
    18341885 
    18351886            case TRANSMIT_SOFT_ERROR: 
    1836                 stop = 1
     1887                stop = true
    18371888                break; 
    18381889            } 
     
    18441895            else 
    18451896                conn_close(c); 
    1846             stop = 1
     1897            stop = true
    18471898            break; 
    18481899        } 
     
    18531904} 
    18541905 
    1855 void event_handler(int fd, short which, void *arg) { 
     1906void event_handler(const int fd, const short which, void *arg) { 
    18561907    conn *c; 
    18571908 
     
    18741925} 
    18751926 
    1876 int new_socket(int is_udp) { 
     1927static int new_socket(const bool is_udp) { 
    18771928    int sfd; 
    18781929    int flags; 
     
    18961947 * Sets a socket's send buffer size to the maximum allowed by the system. 
    18971948 */ 
    1898 void maximize_sndbuf(int sfd) { 
     1949static void maximize_sndbuf(const int sfd) { 
    18991950    socklen_t intsize = sizeof(int); 
    19001951    int last_good = 0; 
     
    19031954 
    19041955    /* Start with the default size. */ 
    1905     if (getsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &old_size, &intsize)) { 
     1956    if (getsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &old_size, &intsize) != 0) { 
    19061957        if (settings.verbose > 0) 
    19071958            perror("getsockopt(SO_SNDBUF)"); 
     
    19281979 
    19291980 
    1930 int server_socket(int port, int is_udp) { 
     1981static int server_socket(const int port, const bool is_udp) { 
    19311982    int sfd; 
    19321983    struct linger ling = {0, 0}; 
     
    19612012        return -1; 
    19622013    } 
    1963     if (! is_udp && listen(sfd, 1024) == -1) { 
     2014    if (!is_udp && listen(sfd, 1024) == -1) { 
    19642015        perror("listen()"); 
    19652016        close(sfd); 
     
    19692020} 
    19702021 
    1971 int new_socket_unix(void) { 
     2022static int new_socket_unix(void) { 
    19722023    int sfd; 
    19732024    int flags; 
     
    19872038} 
    19882039 
    1989 int server_socket_unix(char *path) { 
     2040static int server_socket_unix(char *path) { 
    19902041    int sfd; 
    19912042    struct linger ling = {0, 0}; 
     
    20052056     * Clean up a previous socket file if we left it around 
    20062057     */ 
    2007     if (!lstat(path, &tstat)) { 
     2058    if (lstat(path, &tstat) == 0) { 
    20082059        if (S_ISSOCK(tstat.st_mode)) 
    20092060            unlink(path); 
     
    20352086} 
    20362087 
     2088/* listening socket */ 
     2089static int l_socket=0; 
     2090 
     2091/* udp socket */ 
     2092static int u_socket=-1; 
    20372093 
    20382094/* invoke right before gdb is called, on assert */ 
    2039 void pre_gdb () { 
     2095void pre_gdb(void) { 
    20402096    int i = 0; 
    2041     if(l_socket) close(l_socket); 
    2042     if(u_socket > -1) close(u_socket); 
     2097    if (l_socket > -1) close(l_socket); 
     2098    if (u_socket > -1) close(u_socket); 
    20432099    for (i=3; i<=500; i++) close(i); /* so lame */ 
    20442100    kill(getpid(), SIGABRT); 
     
    20542110 */ 
    20552111volatile rel_time_t current_time; 
    2056 struct event clockevent; 
     2112static struct event clockevent; 
    20572113 
    20582114/* time-sensitive callers can call it by hand with this, outside the normal ever-1-second timer */ 
    2059 void set_current_time () { 
     2115static void set_current_time(void) { 
    20602116    current_time = (rel_time_t) (time(0) - stats.started); 
    20612117} 
    20622118 
    2063 void clock_handler(int fd, short which, void *arg) { 
    2064     struct timeval t
    2065     static int initialized = 0
     2119static void clock_handler(const int fd, const short which, void *arg) { 
     2120    struct timeval t = {.tv_sec = 1, .tv_usec = 0}