Changeset 489

Show
Ignore:
Timestamp:
04/08/07 15:27:38 (2 years ago)
Author:
plindner
Message:

Add cleanup patch from "Tim Yardley" <liquid@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.

Files:

Legend:

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

    r484 r489  
     12007-04-08  Paul Lindner  <lindner@inuus.com> 
     2 
     3        * Add cleanup patch from "Tim Yardley" <liquid@haveheart.com> to 
     4          clean up source spacing issues, fix -Wall warnings, add some 
     5          null checks, adds asserts at the top of each function for any 
     6          use of conn *c without checking to see if c is NULL first. 
     7 
    182007-04-04  Paul Lindner  <lindner@inuus.com> 
    29 
  • trunk/server/items.c

    r476 r489  
    3838void item_init(void) { 
    3939    int i; 
    40     for(i=0; i<LARGEST_ID; i++) { 
    41         heads[i]=NULL; 
    42         tails[i]=NULL; 
    43         sizes[i]=0; 
     40    for(i = 0; i < LARGEST_ID; i++) { 
     41        heads[i] = NULL; 
     42        tails[i] = NULL; 
     43        sizes[i] = 0; 
    4444    } 
    4545} 
     
    6868item *item_alloc(char *key, const size_t nkey, const int flags, const rel_time_t exptime, const int nbytes) { 
    6969    uint8_t nsuffix; 
    70     size_t ntotal; 
    7170    item *it; 
    72     unsigned int id; 
    7371    char suffix[40]; 
    74  
    75     ntotal = item_make_header(nkey + 1, flags, nbytes, suffix, &nsuffix); 
    76   
    77     id = slabs_clsid(ntotal); 
     72    size_t ntotal = item_make_header(nkey + 1, flags, nbytes, suffix, &nsuffix); 
     73 
     74    unsigned int id = slabs_clsid(ntotal); 
    7875    if (id == 0) 
    7976        return 0; 
     
    9895 
    9996        if (id > LARGEST_ID) return NULL; 
    100         if (tails[id]==0) return NULL; 
    101  
    102         for (search = tails[id]; tries>0 && search; tries--, search=search->prev) { 
    103             if (search->refcount==0) { 
     97        if (tails[id] == 0) return NULL; 
     98 
     99        for (search = tails[id]; tries > 0 && search; tries--, search=search->prev) { 
     100            if (search->refcount == 0) { 
    104101               if (search->exptime > current_time) 
    105102                       stats.evictions++; 
     
    109106        } 
    110107        it = slabs_alloc(ntotal); 
    111         if (it==0) return NULL; 
     108        if (it == 0) return NULL; 
    112109    } 
    113110 
     
    125122    strcpy(ITEM_key(it), key); 
    126123    it->exptime = exptime; 
    127     memcpy(ITEM_suffix(it), suffix, (size_t) nsuffix); 
     124    memcpy(ITEM_suffix(it), suffix, (size_t)nsuffix); 
    128125    it->nsuffix = nsuffix; 
    129126    return it; 
     
    251248/*@null@*/ 
    252249char *item_cachedump(const unsigned int slabs_clsid, const unsigned int limit, unsigned int *bytes) { 
    253     int memlimit = 2*1024*1024; 
     250    int memlimit = 2097152; /* 2097152: (2 * 1024 * 1024) */ 
    254251    char *buffer; 
    255252    unsigned int bufcurr; 
     
    266263    bufcurr = 0; 
    267264 
    268     while (it != NULL && (limit==0 || shown < limit)) { 
     265    while (it != NULL && (limit == 0 || shown < limit)) { 
    269266        len = snprintf(temp, 512, "ITEM %s [%d b; %lu s]\r\n", ITEM_key(it), it->nbytes - 2, it->time + stats.started); 
    270267        if (bufcurr + len + 6 > memlimit)  /* 6 is END\r\n\0 */ 
    271268            break; 
    272269        strcpy(buffer + bufcurr, temp); 
    273         bufcurr+=len; 
     270        bufcurr += len; 
    274271        shown++; 
    275272        it = it->next; 
    276273    } 
    277274 
    278     strcpy(buffer+bufcurr, "END\r\n"); 
    279     bufcurr+=5; 
     275    strcpy(buffer + bufcurr, "END\r\n"); 
     276    bufcurr += 5; 
    280277 
    281278    *bytes = bufcurr; 
     
    293290    } 
    294291 
    295     for (i=0; i<LARGEST_ID; i++) { 
     292    for (i = 0; i < LARGEST_ID; i++) { 
    296293        if (tails[i]) 
    297294            bufcurr += snprintf(bufcurr, (size_t)buflen, "STAT items:%d:number %u\r\nSTAT items:%d:age %u\r\n", 
     
    306303char* item_stats_sizes(int *bytes) { 
    307304    const int num_buckets = 32768;   /* max 1MB object, divided into 32 bytes size buckets */ 
    308     unsigned int *histogram = (unsigned int*) malloc((size_t)num_buckets * sizeof(int)); 
    309     char *buf = (char*) malloc(1024*1024*2*sizeof(char)); 
     305    unsigned int *histogram = (unsigned int *)malloc((size_t)num_buckets * sizeof(int)); 
     306    char *buf = (char *)malloc(2097152 * sizeof(char)); /* 2097152: 2 * 1024 * 1024 */ 
    310307    int i; 
    311308 
     
    317314 
    318315    /* build the histogram */ 
    319     memset(histogram, 0, (size_t) num_buckets * sizeof(int)); 
    320     for (i=0; i<LARGEST_ID; i++) { 
     316    memset(histogram, 0, (size_t)num_buckets * sizeof(int)); 
     317    for (i = 0; i < LARGEST_ID; i++) { 
    321318        item *iter = heads[i]; 
    322319        while (iter) { 
     
    331328    /* write the buffer */ 
    332329    *bytes = 0; 
    333     for (i=0; i<num_buckets; i++) { 
     330    for (i = 0; i < num_buckets; i++) { 
    334331        if (histogram[i] != 0) { 
    335             *bytes += sprintf(&buf[*bytes], "%d %u\r\n", i*32, histogram[i]); 
     332            *bytes += sprintf(&buf[*bytes], "%d %u\r\n", i * 32, histogram[i]); 
    336333        } 
    337334    } 
  • trunk/server/memcached.c

    r482 r489  
    105105 
    106106void pre_gdb(void); 
     107static void conn_free(conn *c); 
    107108 
    108109/** exported globals **/ 
     
    142143           really expiring never */ 
    143144        if (exptime <= stats.started) 
    144             return (rel_time_t) 1; 
    145         return (rel_time_t) (exptime - stats.started); 
     145            return (rel_time_t)1; 
     146        return (rel_time_t)(exptime - stats.started); 
    146147    } else { 
    147         return (rel_time_t) (exptime + current_time); 
     148        return (rel_time_t)(exptime + current_time); 
    148149    } 
    149150} 
     
    171172    settings.udpport = 0; 
    172173    settings.interface.s_addr = htonl(INADDR_ANY); 
    173     settings.maxbytes = 64*1024*1024; /* default is 64MB */ 
     174    settings.maxbytes = 67108864; /* default is 64MB: (64 * 1024 * 1024) */ 
    174175    settings.maxconns = 1024;         /* to limit connections-related memory to about 5MB */ 
    175176    settings.verbose = 0; 
     
    229230    struct msghdr *msg; 
    230231 
     232    assert(c != NULL); 
     233 
    231234    if (c->msgsize == c->msgused) { 
    232235        msg = realloc(c->msglist, c->msgsize * 2 * sizeof(struct msghdr)); 
     
    265268    freetotal = 200; 
    266269    freecurr = 0; 
    267     freeconns = (conn **)malloc(sizeof (conn *)*freetotal); 
    268     /** TODO check for NULL **/ 
     270    if (!(freeconns = (conn **)malloc(sizeof(conn *) * freetotal))) { 
     271        perror("malloc()"); 
     272    } 
    269273    return; 
    270274} 
     
    296300        c->hdrsize = 0; 
    297301 
    298         c->rbuf = (char *) malloc((size_t)c->rsize); 
    299         c->wbuf = (char *) malloc((size_t)c->wsize); 
    300         c->ilist = (item **) malloc(sizeof(item *) * c->isize); 
    301         c->iov = (struct iovec *) malloc(sizeof(struct iovec) * c->iovsize); 
    302         c->msglist = (struct msghdr *) malloc(sizeof(struct msghdr) * c->msgsize); 
     302        c->rbuf = (char *)malloc((size_t)c->rsize); 
     303        c->wbuf = (char *)malloc((size_t)c->wsize); 
     304        c->ilist = (item **)malloc(sizeof(item *) * c->isize); 
     305        c->iov = (struct iovec *)malloc(sizeof(struct iovec) * c->iovsize); 
     306        c->msglist = (struct msghdr *)malloc(sizeof(struct msghdr) * c->msgsize); 
    303307 
    304308        if (c->rbuf == 0 || c->wbuf == 0 || c->ilist == 0 || c->iov == 0 || 
     
    372376 
    373377static void conn_cleanup(conn *c) { 
     378    assert(c != NULL); 
     379 
    374380    if (c->item) { 
    375381        item_free(c->item); 
     
    411417 
    412418static void conn_close(conn *c) { 
     419    assert(c != NULL); 
     420 
    413421    /* delete the event, the socket and the conn */ 
    414422    event_del(&c->event); 
     
    429437    } else { 
    430438        /* try to enlarge free connections array */ 
    431         conn **new_freeconns = realloc((void*)freeconns, sizeof(conn *)*freetotal*2); 
     439        conn **new_freeconns = realloc((void *)freeconns, sizeof(conn *) * freetotal * 2); 
    432440        if (new_freeconns) { 
    433441            freetotal *= 2; 
     
    454462 */ 
    455463static void conn_shrink(conn *c) { 
     464    assert(c != NULL); 
     465 
    456466    if (c->udp) 
    457467        return; 
     
    459469    if (c->rsize > READ_BUFFER_HIGHWAT && c->rbytes < DATA_BUFFER_SIZE) { 
    460470        if (c->rcurr != c->rbuf) 
    461             memmove(c->rbuf, c->rcurr, (size_t) c->rbytes); 
    462  
    463         char *newbuf = (char*) realloc((void*)c->rbuf, DATA_BUFFER_SIZE); 
     471            memmove(c->rbuf, c->rcurr, (size_t)c->rbytes); 
     472 
     473        char *newbuf = (char *)realloc((void *)c->rbuf, DATA_BUFFER_SIZE); 
    464474 
    465475        if (newbuf) { 
     
    472482 
    473483    if (c->isize > ITEM_LIST_HIGHWAT) { 
    474         item **newbuf = (item**) realloc((void*)c->ilist, ITEM_LIST_INITIAL * sizeof(c->ilist[0])); 
     484        item **newbuf = (item**) realloc((void *)c->ilist, ITEM_LIST_INITIAL * sizeof(c->ilist[0])); 
    475485        if (newbuf) { 
    476486            c->ilist = newbuf; 
     
    481491 
    482492    if (c->msgsize > MSG_LIST_HIGHWAT) { 
    483         struct msghdr *newbuf = (struct msghdr*) realloc((void*)c->msglist, MSG_LIST_INITIAL * sizeof(c->msglist[0])); 
     493        struct msghdr *newbuf = (struct msghdr *) realloc((void *)c->msglist, MSG_LIST_INITIAL * sizeof(c->msglist[0])); 
    484494        if (newbuf) { 
    485495            c->msglist = newbuf; 
     
    490500 
    491501    if (c->iovsize > IOV_LIST_HIGHWAT) { 
    492         struct iovec* newbuf = (struct iovec *) realloc((void*)c->iov, IOV_LIST_INITIAL * sizeof(c->iov[0])); 
     502        struct iovec *newbuf = (struct iovec *) realloc((void *)c->iov, IOV_LIST_INITIAL * sizeof(c->iov[0])); 
    493503        if (newbuf) { 
    494504            c->iov = newbuf; 
     
    505515 */ 
    506516static void conn_set_state(conn *c, int state) { 
     517    assert(c != NULL); 
     518 
    507519    if (state != c->state) { 
    508520        if (state == conn_read) { 
     
    522534 */ 
    523535static int ensure_iov_space(conn *c) { 
     536    assert(c != NULL); 
     537 
    524538    if (c->iovused >= c->iovsize) { 
    525539        int i, iovnum; 
    526         struct iovec *new_iov = (struct iovec *) realloc(c->iov, 
     540        struct iovec *new_iov = (struct iovec *)realloc(c->iov, 
    527541                                (c->iovsize * 2) * sizeof(struct iovec)); 
    528542        if (! new_iov) 
     
    554568    bool limit_to_mtu; 
    555569 
     570    assert(c != NULL); 
     571 
    556572    do { 
    557573        m = &c->msglist[c->msgused - 1]; 
     
    582598 
    583599        m = &c->msglist[c->msgused - 1]; 
    584         m->msg_iov[m->msg_iovlen].iov_base = (void*) buf; 
     600        m->msg_iov[m->msg_iovlen].iov_base = (void *)buf; 
    585601        m->msg_iov[m->msg_iovlen].iov_len = len; 
    586602 
     
    603619    int i; 
    604620    unsigned char *hdr; 
     621 
     622    assert(c != NULL); 
    605623 
    606624    if (c->msgused > c->hdrsize) { 
     
    612630        if (! new_hdrbuf) 
    613631            return -1; 
    614         c->hdrbuf = (unsigned char *) new_hdrbuf; 
     632        c->hdrbuf = (unsigned char *)new_hdrbuf; 
    615633        c->hdrsize = c->msgused * 2; 
    616634    } 
     
    628646        *hdr++ = 0; 
    629647        *hdr++ = 0; 
    630         assert((void*) hdr == (void*) c->msglist[i].msg_iov[0].iov_base + UDP_HEADER_SIZE); 
     648        assert((void *) hdr == (void *)c->msglist[i].msg_iov[0].iov_base + UDP_HEADER_SIZE); 
    631649    } 
    632650 
     
    638656    int len; 
    639657 
     658    assert(c != NULL); 
     659 
    640660    if (settings.verbose > 1) 
    641661        fprintf(stderr, ">%d %s\n", c->sfd, str); 
    642662 
    643663    len = strlen(str); 
    644     if (len + 2 > c->wsize) { 
     664    if ((len + 2) > c->wsize) { 
    645665        /* ought to be always enough. just fail for simplicity */ 
    646666        str = "SERVER_ERROR output line too long"; 
     
    664684 
    665685static void complete_nread(conn *c) { 
     686    assert(c != NULL); 
     687 
    666688    item *it = c->item; 
    667689    int comm = c->item_comm; 
     
    719741 
    720742typedef struct token_s { 
    721     char* value; 
     743    char *value; 
    722744    size_t length; 
    723745} token_t; 
     
    747769 *   } 
    748770 */ 
    749 static size_t tokenize_command(char* command, token_t* tokens, const size_t max_tokens)  { 
    750     char* cp; 
    751     char* value = NULL; 
     771static size_t tokenize_command(char *command, token_t *tokens, const size_t max_tokens)  { 
     772    char *cp; 
     773    char *value = NULL; 
    752774    size_t length = 0; 
    753775    size_t ntokens = 0; 
     
    793815} 
    794816 
    795 static void process_stat(conn *c, token_t* tokens, const size_t ntokens) { 
     817static void process_stat(conn *c, token_t *tokens, const size_t ntokens) { 
    796818    rel_time_t now = current_time; 
    797     char* command; 
    798     char* subcommand; 
     819    char *command; 
     820    char *subcommand; 
     821 
     822    assert(c != NULL); 
    799823 
    800824    if(ntokens < 2) { 
     
    817841        pos += sprintf(pos, "STAT time %ld\r\n", now + stats.started); 
    818842        pos += sprintf(pos, "STAT version " VERSION "\r\n"); 
    819         pos += sprintf(pos, "STAT pointer_size %d\r\n", 8 * sizeof(void*)); 
     843        pos += sprintf(pos, "STAT pointer_size %d\r\n", 8 * sizeof(void *)); 
    820844        pos += sprintf(pos, "STAT rusage_user %ld.%06ld\r\n", usage.ru_utime.tv_sec, usage.ru_utime.tv_usec); 
    821845        pos += sprintf(pos, "STAT rusage_system %ld.%06ld\r\n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec); 
     
    833857        pos += sprintf(pos, "STAT bytes_read %llu\r\n", stats.bytes_read); 
    834858        pos += sprintf(pos, "STAT bytes_written %llu\r\n", stats.bytes_written); 
    835         pos += sprintf(pos, "STAT limit_maxbytes %llu\r\n", (unsigned long long) settings.maxbytes); 
     859        pos += sprintf(pos, "STAT limit_maxbytes %llu\r\n", (unsigned long long)settings.maxbytes); 
    836860        pos += sprintf(pos, "END"); 
    837861        out_string(c, temp); 
     
    877901        int res; 
    878902 
    879         wbuf = (char *)malloc(wsize); 
    880         if (wbuf == 0) { 
     903        if (!(wbuf = (char *)malloc(wsize))) { 
    881904            out_string(c, "SERVER_ERROR out of memory"); 
    882905            return; 
     
    902925        } 
    903926        strcpy(wbuf + res, "END\r\n"); 
    904         c->write_and_free=wbuf; 
    905         c->wcurr=wbuf; 
     927        c->write_and_free = wbuf; 
     928        c->wcurr = wbuf; 
    906929        c->wbytes = res + 5; // Don't write the terminal '\0'  
    907930        conn_set_state(c, conn_write); 
     
    943966    } 
    944967 
    945     if (strcmp(subcommand, "slabs")==0) { 
     968    if (strcmp(subcommand, "slabs") == 0) { 
    946969        int bytes = 0; 
    947970        char *buf = slabs_stats(&bytes); 
     
    958981    } 
    959982 
    960     if (strcmp(subcommand, "items")==0) { 
     983    if (strcmp(subcommand, "items") == 0) { 
    961984        char buffer[4096]; 
    962985        item_stats(buffer, 4096); 
     
    965988    } 
    966989 
    967     if (strcmp(subcommand, "sizes")==0) { 
     990    if (strcmp(subcommand, "sizes") == 0) { 
    968991        int bytes = 0; 
    969992        char *buf = item_stats_sizes(&bytes); 
     
    9851008 
    9861009/* ntokens is overwritten here... shrug.. */ 
    987 static inline void process_get_command(conn *c, token_t* tokens, size_t ntokens) { 
     1010static inline void process_get_command(conn *c, token_t *tokens, size_t ntokens) { 
    9881011    char *key; 
    9891012    size_t nkey; 
    9901013    int i = 0; 
    9911014    item *it; 
    992     token_t* key_token = &tokens[KEY_TOKEN]; 
     1015    token_t *key_token = &tokens[KEY_TOKEN]; 
     1016 
     1017    assert(c != NULL); 
    9931018 
    9941019    if (settings.managed) { 
     
    10201045            if (it) { 
    10211046                if (i >= c->isize) { 
    1022                     item **new_list = realloc(c->ilist, sizeof(item *)*c->isize*2); 
     1047                    item **new_list = realloc(c->ilist, sizeof(item *) * c->isize * 2); 
    10231048                    if (new_list) { 
    10241049                        c->isize *= 2; 
     
    10821107} 
    10831108 
    1084 static void process_update_command(conn *c, token_t* tokens, const size_t ntokens, int comm) { 
     1109static void process_update_command(conn *c, token_t *tokens, const size_t ntokens, int comm) { 
    10851110    char *key; 
    10861111    size_t nkey; 
     
    10891114    int vlen; 
    10901115    item *it; 
     1116 
     1117    assert(c != NULL); 
    10911118 
    10921119    if (tokens[KEY_TOKEN].length > KEY_MAX_LENGTH) { 
     
    11291156        /* swallow the data line */ 
    11301157        c->write_and_go = conn_swallow; 
    1131         c->sbytes = vlen+2; 
     1158        c->sbytes = vlen + 2; 
    11321159        return; 
    11331160    } 
     
    11411168} 
    11421169 
    1143 static void process_arithmetic_command(conn *c, token_t* tokens, const size_t ntokens, const int incr) { 
     1170static void process_arithmetic_command(conn *c, token_t *tokens, const size_t ntokens, const int incr) { 
    11441171    char temp[32]; 
    11451172    unsigned int value; 
     
    11501177    int res; 
    11511178    char *ptr; 
     1179 
     1180    assert(c != NULL); 
    11521181     
    11531182    if(tokens[KEY_TOKEN].length > KEY_MAX_LENGTH) {  
     
    11861215 
    11871216    ptr = ITEM_data(it); 
    1188     while ((*ptr != '\0') && (*ptr<'0' && *ptr>'9')) ptr++;    // BUG: can't be true 
     1217    while ((*ptr != '\0') && (*ptr < '0' && *ptr > '9')) ptr++;    // BUG: can't be true 
    11891218         
    11901219    value = strtol(ptr, NULL, 10); 
     
    11961225     
    11971226    if (incr != 0) 
    1198         value+=delta; 
     1227        value += delta; 
    11991228    else { 
    12001229        if (delta >= value) value = 0; 
    1201         else value-=delta; 
     1230        else value -= delta; 
    12021231    } 
    12031232    snprintf(temp, 32, "%u", value); 
     
    12151244    } else { /* replace in-place */ 
    12161245        memcpy(ITEM_data(it), temp, res); 
    1217         memset(ITEM_data(it) + res, ' ', it->nbytes-res-2); 
     1246        memset(ITEM_data(it) + res, ' ', it->nbytes - res - 2); 
    12181247    } 
    12191248    out_string(c, temp); 
     
    12211250} 
    12221251 
    1223 static void process_delete_command(conn *c, token_t* tokens, const size_t ntokens) { 
     1252static void process_delete_command(conn *c, token_t *tokens, const size_t ntokens) { 
    12241253    char *key; 
    12251254    size_t nkey; 
     
    12271256    time_t exptime = 0; 
    12281257     
     1258    assert(c != NULL); 
     1259 
    12291260    if (settings.managed) { 
    12301261        int bucket = c->bucket; 
     
    12981329    int comm; 
    12991330 
     1331    assert(c != NULL); 
     1332 
    13001333    if (settings.verbose > 1) 
    13011334        fprintf(stderr, "<%d %s\n", c->sfd, command); 
     
    13871420            return; 
    13881421        } 
    1389         if (sscanf(tokens[1].value, "%u:%u", &bucket,&gen) == 2) { 
     1422        if (sscanf(tokens[1].value, "%u:%u", &bucket, &gen) == 2) { 
    13901423            /* we never write anything back, even if input's wrong */ 
    1391             if ((bucket < 0) || (bucket >= MAX_BUCKETS) || (gen<=0)) { 
     1424            if ((bucket < 0) || (bucket >= MAX_BUCKETS) || (gen <= 0)) { 
    13921425                /* do nothing, bad input */ 
    13931426            } else { 
     
    14791512    char *el, *cont; 
    14801513 
    1481     assert(c->rcurr <= c->rbuf + c->rsize); 
     1514    assert(c != NULL); 
     1515    assert(c->rcurr <= (c->rbuf + c->rsize)); 
    14821516 
    14831517    if (c->rbytes == 0) 
     
    14871521        return 0; 
    14881522    cont = el + 1; 
    1489     if (el - c->rcurr > 1 && *(el - 1) == '\r') { 
     1523    if ((el - c->rcurr) > 1 && *(el - 1) == '\r') { 
    14901524        el--; 
    14911525    } 
    14921526    *el = '\0'; 
    14931527 
    1494     assert(cont <= c->rcurr + c->rbytes); 
     1528    assert(cont <= (c->rcurr + c->rbytes)); 
    14951529 
    14961530    process_command(c, c->rcurr); 
     
    14991533    c->rcurr = cont; 
    15001534 
    1501     assert(c->rcurr <= c->rbuf + c->rsize); 
     1535    assert(c->rcurr <= (c->rbuf + c->rsize)); 
    15021536 
    15031537    return 1; 
     
    15101544static int try_read_udp(conn *c) { 
    15111545    int res; 
     1546 
     1547    assert(c != NULL); 
    15121548 
    15131549    c->request_addr_size = sizeof(c->request_addr); 
     
    15491585    int res; 
    15501586 
     1587    assert(c != NULL); 
     1588 
    15511589    if (c->rcurr != c->rbuf) { 
    15521590        if (c->rbytes != 0) /* otherwise there's nothing to copy */ 
     
    15571595    while (1) { 
    15581596        if (c->rbytes >= c->rsize) { 
    1559             char *new_rbuf = realloc(c->rbuf, c->rsize*2); 
     1597            char *new_rbuf = realloc(c->rbuf, c->rsize * 2); 
    15601598            if (!new_rbuf) { 
    15611599                if (settings.verbose > 0) 
     
    15661604                return 1; 
    15671605            } 
    1568             c->rcurr = c->rbuf = new_rbuf; 
     1606            c->rcurr = c->rbuf = new_rbuf; 
    15691607            c->rsize *= 2; 
    15701608        } 
     
    16001638 
    16011639static bool update_event(conn *c, const int new_flags) { 
     1640    assert(c != NULL); 
     1641 
    16021642    if (c->ev_flags == new_flags) 
    16031643        return true; 
     
    16401680    int res; 
    16411681 
     1682    assert(c != NULL); 
     1683 
    16421684    if (c->msgcurr < c->msgused && 
    16431685            c->msglist[c->msgcurr].msg_iovlen == 0) { 
     
    16961738    socklen_t addrlen; 
    16971739    struct sockaddr addr; 
    1698     conn *newc; 
    16991740    int res; 
     1741 
     1742    assert(c != NULL); 
    17001743 
    17011744    while (!stop) { 
     
    17221765                break; 
    17231766            } 
    1724             newc = conn_new(sfd, conn_read, EV_READ | EV_PERSIST, 
     1767            conn *newc = conn_new(sfd, conn_read, EV_READ | EV_PERSIST, 
    17251768                            DATA_BUFFER_SIZE, false); 
    17261769            if (!newc) { 
     
    19091952 
    19101953    c = (conn *)arg; 
     1954    assert(c != NULL); 
     1955 
    19111956    c->which = which; 
    19121957 
     
    19662011 
    19672012    while (min <= max) { 
    1968         avg = ((unsigned int) min + max) / 2; 
     2013        avg = ((unsigned int)(min + max)) / 2; 
    19692014        if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &avg, intsize) == 0) { 
    19702015            last_good = avg; 
     
    20082053    addr.sin_port = htons(port); 
    20092054    addr.sin_addr = settings.interface; 
    2010     if (bind(sfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) { 
     2055    if (bind(sfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { 
    20112056        perror("bind()"); 
    20122057        close(sfd); 
     
    20742119    addr.sun_family = AF_UNIX; 
    20752120    strcpy(addr.sun_path, path); 
    2076     if (bind(sfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) { 
     2121    if (bind(sfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { 
    20772122        perror("bind()"); 
    20782123        close(sfd); 
     
    20882133 
    20892134/* listening socket */ 
    2090 static int l_socket=0; 
     2135static int l_socket = 0; 
    20912136 
    20922137/* udp socket */ 
    2093 static int u_socket=-1; 
     2138static int u_socket = -1; 
    20942139 
    20952140/* invoke right before gdb is called, on assert */ 
    20962141void pre_gdb(void) { 
    2097     int i = 0
     2142    int i
    20982143    if (l_socket > -1) close(l_socket); 
    20992144    if (u_socket > -1) close(u_socket); 
    2100     for (i=3; i<=500; i++) close(i); /* so lame */ 
     2145    for (i = 3; i <= 500; i++) close(i); /* so lame */ 
    21012146    kill(getpid(), SIGABRT); 
    21022147} 
     
    22682313        return; 
    22692314 
    2270     if (!(fp = fopen(pid_file,"w"))) { 
    2271         fprintf(stderr,"Could not open the pid file %s for writing\n",pid_file); 
     2315    if (!(fp = fopen(pid_file, "w"))) { 
     2316        fprintf(stderr, "Could not open the pid file %s for writing\n", pid_file); 
    22722317        return; 
    22732318    } 
    22742319 
    2275     fprintf(fp,"%ld\n",(long) pid); 
     2320    fprintf(fp,"%ld\n", (long)pid); 
    22762321    if (fclose(fp) == -1) { 
    2277         fprintf(stderr,"Could not close the pid file %s.\n",pid_file); 
     2322        fprintf(stderr, "Could not close the pid file %s.\n", pid_file); 
    22782323        return; 
    22792324    } 
     
    22852330 
    22862331  if (unlink(pid_file) != 0) { 
    2287       fprintf(stderr,"Could not remove the pid file %s.\n",pid_file); 
     2332      fprintf(stderr, "Could not remove the pid file %s.\n", pid_file); 
    22882333  } 
    22892334 
     
    23342379            break; 
    23352380        case 'm': 
    2336             settings.maxbytes = ((size_t)atoi(optarg))*1024*1024; 
     2381            settings.maxbytes = ((size_t)atoi(optarg)) * 1024 * 1024; 
    23372382            break; 
    23382383        case 'M': 
     
    24002445         * the soft limit to the hard. 
    24012446         */ 
    2402         if (getrlimit(RLIMIT_CORE, &rlim)==0) { 
     2447        if (getrlimit(RLIMIT_CORE, &rlim) == 0) { 
    24032448            rlim_new.rlim_cur = rlim_new.rlim_max = RLIM_INFINITY; 
    2404             if (setrlimit(RLIMIT_CORE, &rlim_new)!=0) { 
     2449            if (setrlimit(RLIMIT_CORE, &rlim_new)!= 0) { 
    24052450                /* failed. try raising just to the old max */ 
    2406                 rlim_new.rlim_cur = rlim_new.rlim_max = 
    2407                     rlim.rlim_max; 
    2408                 (void) setrlimit(RLIMIT_CORE, &rlim_new); 
     2451                rlim_new.rlim_cur = rlim_new.rlim_max = rlim.rlim_max; 
     2452                (void)setrlimit(RLIMIT_CORE, &rlim_new); 
    24092453            } 
    24102454        } 
     
    24152459         */ 
    24162460 
    2417         if ((getrlimit(RLIMIT_CORE, &rlim)!=0) || rlim.rlim_cur==0) { 
     2461        if ((getrlimit(RLIMIT_CORE, &rlim) != 0) || rlim.rlim_cur == 0) { 
    24182462            fprintf(stderr, "failed to ensure corefile creation\n"); 
    24192463            exit(EXIT_FAILURE); 
     
    24672511 
    24682512    /* lose root privileges if we have them */ 
    2469     if (getuid()== 0 || geteuid()==0) { 
    2470         if (username==0 || *username=='\0') { 
     2513    if (getuid() == 0 || geteuid() == 0) { 
     2514        if (username == 0 || *username == '\0') { 
    24712515            fprintf(stderr, "can't run as root without the -u switch\n"); 
    24722516            return 1; 
     
    24762520            return 1; 
    24772521        } 
    2478         if (setgid(pw->pw_gid)<0 || setuid(pw->pw_uid)<0) { 
     2522        if (setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) { 
    24792523            fprintf(stderr, "failed to assume identity of user %s\n", username); 
    24802524            return 1; 
     
    25132557    /* managed instance? alloc and zero a bucket array */ 
    25142558    if (settings.managed) { 
    2515         buckets = malloc(sizeof(int)*MAX_BUCKETS); 
     2559        buckets = malloc(sizeof(int) * MAX_BUCKETS); 
    25162560        if (buckets == 0) { 
    25172561            fprintf(stderr, "failed to allocate the bucket array"); 
    25182562            exit(EXIT_FAILURE); 
    25192563        } 
    2520         memset(buckets, 0, sizeof(int)*MAX_BUCKETS); 
     2564        memset(buckets, 0, sizeof(int) * MAX_BUCKETS); 
    25212565    } 
    25222566 
     
    25532597    } 
    25542598    /* initialise clock event */ 
    2555     clock_handler(0,0,0); 
     2599    clock_handler(0, 0, 0); 
    25562600    /* initialise deletion array and timer event */ 
    2557     deltotal = 200; delcurr = 0; 
    2558     todelete = malloc(sizeof(item *)*deltotal); 
    2559     delete_handler(0,0,0); /* sets up the event */ 
     2601    deltotal = 200; 
     2602    delcurr = 0; 
     2603    todelete = malloc(sizeof(item *) * deltotal); 
     2604    delete_handler(0, 0, 0); /* sets up the event */ 
    25602605    /* save the PID in if we're a daemon */ 
    25612606    if (daemonize) 
    2562         save_pid(getpid(),pid_file); 
     2607        save_pid(getpid(), pid_file); 
    25632608    /* enter the loop */ 
    25642609    event_loop(0); 
  • trunk/server/slabs.c

    r468 r489  
    5555} slabclass_t; 
    5656 
    57 static slabclass_t slabclass[POWER_LARGEST+1]; 
     57static slabclass_t slabclass[POWER_LARGEST + 1]; 
    5858static size_t mem_limit = 0; 
    5959static size_t mem_malloced = 0; 
     
    6565static int slabs_newslab(const unsigned int id); 
    6666 
     67#ifndef DONT_PREALLOC_SLABS 
    6768/* Preallocate as many slab pages as possible (called from slabs_init) 
    6869   on start-up, so users don't get confused out-of-memory errors when 
     
    7273   smaller ones will be made.  */ 
    7374static void slabs_preallocate (const unsigned int maxslabs); 
    74  
     75#endif 
    7576 
    7677/* 
     
    8586    int res = POWER_SMALLEST; 
    8687 
    87     if(size==0) 
     88    if(size == 0) 
    8889        return 0; 
    8990    while (size > slabclass[res].size) 
     
    130131        char *t_initial_malloc = getenv("T_MEMD_INITIAL_MALLOC"); 
    131132        if (t_initial_malloc) { 
    132             mem_malloced = (size_t) atol(t_initial_malloc); 
     133            mem_malloced = (size_t)atol(t_initial_malloc); 
    133134        } 
    134135 
     
    146147} 
    147148 
     149#ifndef DONT_PREALLOC_SLABS 
    148150static void slabs_preallocate (const unsigned int maxslabs) { 
    149151    int i; 
     
    156158       these three lines.  */ 
    157159 
    158     for(i=POWER_SMALLEST; i<=POWER_LARGEST; i++) { 
     160    for (i = POWER_SMALLEST; i <= POWER_LARGEST; i++) { 
    159161        if (++prealloc > maxslabs) 
    160162            return; 
     
    163165 
    164166} 
     167#endif 
    165168 
    166169static int grow_slab_list (const unsigned int id) { 
     
    168171    if (p->slabs == p->list_size) { 
    169172        size_t new_size =  (p->list_size != 0) ? p->list_size * 2 : 16; 
    170         void *new_list = realloc(p->slab_list, new_size*sizeof(void*)); 
     173        void *new_list = realloc(p->slab_list, new_size * sizeof(void *)); 
    171174        if (new_list == 0) return 0; 
    172175        p->list_size = new_size; 
     
    211214 
    212215    p = &slabclass[id]; 
    213     assert(p->sl_curr == 0 || ((item*)p->slots[p->sl_curr-1])->slabs_clsid == 0); 
     216    assert(p->sl_curr == 0 || ((item *)p->slots[p->sl_curr - 1])->slabs_clsid == 0); 
    214217 
    215218#ifdef USE_SYSTEM_MALLOC 
     
    222225    /* fail unless we have space at the end of a recently allocated page, 
    223226       we have something on our freelist, or we could allocate a new page */ 
    224     if (! (p->end_page_ptr != 0 || p->sl_curr != 0 || slabs_newslab(id) !=0)) 
     227    if (! (p->end_page_ptr != 0 || p->sl_curr != 0 || slabs_newslab(id) != 0)) 
    225228        return 0; 
    226229 
     
    247250    slabclass_t *p; 
    248251 
    249     assert(((item *)ptr)->slabs_clsid==0); 
     252    assert(((item *)ptr)->slabs_clsid == 0); 
    250253    assert(id >= POWER_SMALLEST && id <= power_largest); 
    251254    if (id < POWER_SMALLEST || id > power_largest) 
     
    261264 
    262265    if (p->sl_curr == p->sl_total) { /* need more space on the free list */ 
    263         int new_size = (p->sl_total != 0) ? p->sl_total*2 : 16;  /* 16 is arbitrary */ 
    264         void **new_slots = realloc(p->slots, new_size*sizeof(void *)); 
     266        int new_size = (p->sl_total != 0) ? p->sl_total * 2 : 16;  /* 16 is arbitrary */ 
     267        void **new_slots = realloc(p->slots, new_size * sizeof(void *)); 
    265268        if (new_slots == 0) 
    266269            return; 
     
    275278char* slabs_stats(int *buflen) { 
    276279    int i, total; 
    277     char *buf = (char*) malloc(power_largest * 200 + 100); 
     280    char *buf = (char *)malloc(power_largest * 200 + 100); 
    278281    char *bufcurr = buf; 
    279282 
     
    300303        } 
    301304    } 
    302     bufcurr += sprintf(bufcurr, "STAT active_slabs %d\r\nSTAT total_malloced %llu\r\n", total, (unsigned long long) mem_malloced); 
     305    bufcurr += sprintf(bufcurr, "STAT active_slabs %d\r\nSTAT total_malloced %llu\r\n", total, (unsigned long long)mem_malloced); 
    303306    bufcurr += sprintf(bufcurr, "END\r\n"); 
    304307    *buflen = bufcurr - buf; 
     
    338341    if (p->killing == 0) p->killing = 1; 
    339342 
    340     slab = p->slab_list[p->killing-1]; 
     343    slab = p->slab_list[p->killing - 1]; 
    341344    slab_end = slab + POWER_BLOCK; 
    342345 
    343     for (iter=slab; iter<slab_end; iter+=p->size) { 
    344         item *it = (item *) iter; 
     346    for (iter = slab; iter < slab_end; iter += p->size) { 
     347        item *it = (item *)iter; 
    345348        if (it->slabs_clsid) { 
    346349            if (it->refcount) was_busy = 1; 
     
    352355    { 
    353356        int fi; 
    354         for (fi=p->sl_curr-1; fi>=0; fi--) { 
     357        for (fi = p->sl_curr - 1; fi >= 0; fi--) { 
    355358            if (p->slots[fi] >= slab && p->slots[fi] < slab_end) { 
    356359                p->sl_curr--; 
     
    363366 
    364367    /* if good, now move it to the dst slab class */ 
    365     p->slab_list[p->killing-1] = p->slab_list[p->slabs-1]; 
     368    p->slab_list[p->killing - 1] = p->slab_list[p->slabs - 1]; 
    366369    p->slabs--; 
    367370    p->killing = 0; 
     
    371374    /* this isn't too critical, but other parts of the code do asserts to 
    372375       make sure this field is always 0.  */ 
    373     for (iter=slab; iter<slab_end; iter+=dp->size) { 
     376    for (iter = slab; iter < slab_end; iter += dp->size) { 
    374377        ((item *)iter)->slabs_clsid = 0; 
    375378    } 
  • trunk/server/t/stats.t

    r476 r489  
    22 
    33use strict; 
    4 use Test::More tests => 16
     4use Test::More tests => 17
    55use FindBin qw($Bin); 
    66use lib "$Bin/lib";