Changeset 490

Show
Ignore:
Timestamp:
04/08/07 15:29:03 (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.

(merges in rev 488 from trunk too..)

Files:

Legend:

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

    r487 r490  
     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 
  • branches/multithreaded/server/items.c

    r487 r490  
    3333void item_init(void) { 
    3434    int i; 
    35     for(i=0; i<LARGEST_ID; i++) { 
    36         heads[i]=NULL; 
    37         tails[i]=NULL; 
    38         sizes[i]=0; 
     35    for(i = 0; i < LARGEST_ID; i++) { 
     36        heads[i] = NULL; 
     37        tails[i] = NULL; 
     38        sizes[i] = 0; 
    3939    } 
    4040} 
     
    7474item *do_item_alloc(char *key, const size_t nkey, const int flags, const rel_time_t exptime, const int nbytes) { 
    7575    uint8_t nsuffix; 
    76     size_t ntotal; 
    7776    item *it; 
    78     unsigned int id; 
    7977    char suffix[40]; 
    80  
    81     ntotal = item_make_header(nkey + 1, flags, nbytes, suffix, &nsuffix); 
    82   
    83     id = slabs_clsid(ntotal); 
     78    size_t ntotal = item_make_header(nkey + 1, flags, nbytes, suffix, &nsuffix); 
     79 
     80    unsigned int id = slabs_clsid(ntotal); 
    8481    if (id == 0) 
    8582        return 0; 
     
    104101 
    105102        if (id > LARGEST_ID) return NULL; 
    106         if (tails[id]==0) return NULL; 
    107  
    108         for (search = tails[id]; tries>0 && search; tries--, search=search->prev) { 
    109             if (search->refcount==0) { 
     103        if (tails[id] == 0) return NULL; 
     104 
     105        for (search = tails[id]; tries > 0 && search; tries--, search=search->prev) { 
     106            if (search->refcount == 0) { 
    110107               if (search->exptime > current_time) { 
    111108                       STATS_LOCK(); 
     
    118115        } 
    119116        it = slabs_alloc(ntotal); 
    120         if (it==0) return NULL; 
     117        if (it == 0) return NULL; 
    121118    } 
    122119 
     
    135132    strcpy(ITEM_key(it), key); 
    136133    it->exptime = exptime; 
    137     memcpy(ITEM_suffix(it), suffix, (size_t) nsuffix); 
     134    memcpy(ITEM_suffix(it), suffix, (size_t)nsuffix); 
    138135    it->nsuffix = nsuffix; 
    139136    return it; 
     
    271268/*@null@*/ 
    272269char *item_cachedump(const unsigned int slabs_clsid, const unsigned int limit, unsigned int *bytes) { 
    273     int memlimit = 2*1024*1024; 
     270    int memlimit = 2097152; /* 2097152: (2 * 1024 * 1024) */ 
    274271    char *buffer; 
    275272    unsigned int bufcurr; 
     
    286283    bufcurr = 0; 
    287284 
    288     while (it != NULL && (limit==0 || shown < limit)) { 
     285    while (it != NULL && (limit == 0 || shown < limit)) { 
    289286        len = snprintf(temp, 512, "ITEM %s [%d b; %lu s]\r\n", ITEM_key(it), it->nbytes - 2, it->time + stats.started); 
    290287        if (bufcurr + len + 6 > memlimit)  /* 6 is END\r\n\0 */ 
    291288            break; 
    292289        strcpy(buffer + bufcurr, temp); 
    293         bufcurr+=len; 
     290        bufcurr += len; 
    294291        shown++; 
    295292        it = it->next; 
    296293    } 
    297294 
    298     strcpy(buffer+bufcurr, "END\r\n"); 
    299     bufcurr+=5; 
     295    strcpy(buffer + bufcurr, "END\r\n"); 
     296    bufcurr += 5; 
    300297 
    301298    *bytes = bufcurr; 
     
    313310    } 
    314311 
    315     for (i=0; i<LARGEST_ID; i++) { 
     312    for (i = 0; i < LARGEST_ID; i++) { 
    316313        if (tails[i]) 
    317314            bufcurr += snprintf(bufcurr, (size_t)buflen, "STAT items:%d:number %u\r\nSTAT items:%d:age %u\r\n", 
     
    326323char* item_stats_sizes(int *bytes) { 
    327324    const int num_buckets = 32768;   /* max 1MB object, divided into 32 bytes size buckets */ 
    328     unsigned int *histogram = (unsigned int*) malloc((size_t)num_buckets * sizeof(int)); 
    329     char *buf = (char*) malloc(1024*1024*2*sizeof(char)); 
     325    unsigned int *histogram = (unsigned int *)malloc((size_t)num_buckets * sizeof(int)); 
     326    char *buf = (char *)malloc(2097152 * sizeof(char)); /* 2097152: 2 * 1024 * 1024 */ 
    330327    int i; 
    331328 
     
    337334 
    338335    /* build the histogram */ 
    339     memset(histogram, 0, (size_t) num_buckets * sizeof(int)); 
    340     for (i=0; i<LARGEST_ID; i++) { 
     336    memset(histogram, 0, (size_t)num_buckets * sizeof(int)); 
     337    for (i = 0; i < LARGEST_ID; i++) { 
    341338        item *iter = heads[i]; 
    342339        while (iter) { 
     
    351348    /* write the buffer */ 
    352349    *bytes = 0; 
    353     for (i=0; i<num_buckets; i++) { 
     350    for (i = 0; i < num_buckets; i++) { 
    354351        if (histogram[i] != 0) { 
    355             *bytes += sprintf(&buf[*bytes], "%d %u\r\n", i*32, histogram[i]); 
     352            *bytes += sprintf(&buf[*bytes], "%d %u\r\n", i * 32, histogram[i]); 
    356353        } 
    357354    } 
  • branches/multithreaded/server/memcached.c

    r487 r490  
    9898 
    9999void pre_gdb(void); 
     100void conn_free(conn *c); 
    100101 
    101102/** exported globals **/ 
     
    116117 
    117118static int *buckets = 0; /* bucket->generation array for a managed instance */ 
    118  
    119 void conn_free(conn *c); 
    120119 
    121120#define REALTIME_MAXDELTA 60*60*24*30 
     
    138137           really expiring never */ 
    139138        if (exptime <= stats.started) 
    140             return (rel_time_t) 1; 
    141         return (rel_time_t) (exptime - stats.started); 
     139            return (rel_time_t)1; 
     140        return (rel_time_t)(exptime - stats.started); 
    142141    } else { 
    143         return (rel_time_t) (exptime + current_time); 
     142        return (rel_time_t)(exptime + current_time); 
    144143    } 
    145144} 
     
    171170    settings.udpport = 0; 
    172171    settings.interface.s_addr = htonl(INADDR_ANY); 
    173     settings.maxbytes = 64*1024*1024; /* default is 64MB */ 
     172    settings.maxbytes = 67108864; /* default is 64MB: (64 * 1024 * 1024) */ 
    174173    settings.maxconns = 1024;         /* to limit connections-related memory to about 5MB */ 
    175174    settings.verbose = 0; 
     
    205204    struct msghdr *msg; 
    206205 
     206    assert(c != NULL); 
     207 
    207208    if (c->msgsize == c->msgused) { 
    208209        msg = realloc(c->msglist, c->msgsize * 2 * sizeof(struct msghdr)); 
     
    247248    freetotal = 200; 
    248249    freecurr = 0; 
    249     freeconns = (conn **)malloc(sizeof (conn *)*freetotal); 
    250     /** TODO check for NULL **/ 
     250    if (!(freeconns = (conn **)malloc(sizeof(conn *) * freetotal))) { 
     251        perror("malloc()"); 
     252    } 
    251253    return; 
    252254} 
     
    278280    } else { 
    279281        /* try to enlarge free connections array */ 
    280         conn **new_freeconns = realloc(freeconns, sizeof(conn *)*freetotal*2); 
     282        conn **new_freeconns = realloc(freeconns, sizeof(conn *) * freetotal * 2); 
    281283        if (new_freeconns) { 
    282284            freetotal *= 2; 
     
    311313        c->hdrsize = 0; 
    312314 
    313         c->rbuf = (char *) malloc((size_t)c->rsize); 
    314         c->wbuf = (char *) malloc((size_t)c->wsize); 
    315         c->ilist = (item **) malloc(sizeof(item *) * c->isize); 
    316         c->iov = (struct iovec *) malloc(sizeof(struct iovec) * c->iovsize); 
    317         c->msglist = (struct msghdr *) malloc(sizeof(struct msghdr) * c->msgsize); 
     315        c->rbuf = (char *)malloc((size_t)c->rsize); 
     316        c->wbuf = (char *)malloc((size_t)c->wsize); 
     317        c->ilist = (item **)malloc(sizeof(item *) * c->isize); 
     318        c->iov = (struct iovec *)malloc(sizeof(struct iovec) * c->iovsize); 
     319        c->msglist = (struct msghdr *)malloc(sizeof(struct msghdr) * c->msgsize); 
    318320 
    319321        if (c->rbuf == 0 || c->wbuf == 0 || c->ilist == 0 || c->iov == 0 || 
     
    383385 
    384386static void conn_cleanup(conn *c) { 
     387    assert(c != NULL); 
     388 
    385389    if (c->item) { 
    386390        item_remove(c->item); 
     
    422426 
    423427static void conn_close(conn *c) { 
     428    assert(c != NULL); 
     429 
    424430    /* delete the event, the socket and the conn */ 
    425431    event_del(&c->event); 
     
    454460 */ 
    455461static void conn_shrink(conn *c) { 
     462    assert(c != NULL); 
     463 
    456464    if (c->udp) 
    457465        return; 
     
    459467    if (c->rsize > READ_BUFFER_HIGHWAT && c->rbytes < DATA_BUFFER_SIZE) { 
    460468        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); 
     469            memmove(c->rbuf, c->rcurr, (size_t)c->rbytes); 
     470 
     471        char *newbuf = (char *)realloc((void *)c->rbuf, DATA_BUFFER_SIZE); 
    464472 
    465473        if (newbuf) { 
     
    472480 
    473481    if (c->isize > ITEM_LIST_HIGHWAT) { 
    474         item **newbuf = (item**) realloc((void*)c->ilist, ITEM_LIST_INITIAL * sizeof(c->ilist[0])); 
     482        item **newbuf = (item**) realloc((void *)c->ilist, ITEM_LIST_INITIAL * sizeof(c->ilist[0])); 
    475483        if (newbuf) { 
    476484            c->ilist = newbuf; 
     
    481489 
    482490    if (c->msgsize > MSG_LIST_HIGHWAT) { 
    483         struct msghdr *newbuf = (struct msghdr*) realloc((void*)c->msglist, MSG_LIST_INITIAL * sizeof(c->msglist[0])); 
     491        struct msghdr *newbuf = (struct msghdr *) realloc((void *)c->msglist, MSG_LIST_INITIAL * sizeof(c->msglist[0])); 
    484492        if (newbuf) { 
    485493            c->msglist = newbuf; 
     
    490498 
    491499    if (c->iovsize > IOV_LIST_HIGHWAT) { 
    492         struct iovec* newbuf = (struct iovec *) realloc((void*)c->iov, IOV_LIST_INITIAL * sizeof(c->iov[0])); 
     500        struct iovec *newbuf = (struct iovec *) realloc((void *)c->iov, IOV_LIST_INITIAL * sizeof(c->iov[0])); 
    493501        if (newbuf) { 
    494502            c->iov = newbuf; 
     
    505513 */ 
    506514static void conn_set_state(conn *c, int state) { 
     515    assert(c != NULL); 
     516 
    507517    if (state != c->state) { 
    508518        if (state == conn_read) { 
     
    522532 */ 
    523533static int ensure_iov_space(conn *c) { 
     534    assert(c != NULL); 
     535 
    524536    if (c->iovused >= c->iovsize) { 
    525537        int i, iovnum; 
    526         struct iovec *new_iov = (struct iovec *) realloc(c->iov, 
     538        struct iovec *new_iov = (struct iovec *)realloc(c->iov, 
    527539                                (c->iovsize * 2) * sizeof(struct iovec)); 
    528540        if (! new_iov) 
     
    554566    bool limit_to_mtu; 
    555567 
     568    assert(c != NULL); 
     569 
    556570    do { 
    557571        m = &c->msglist[c->msgused - 1]; 
     
    582596 
    583597        m = &c->msglist[c->msgused - 1]; 
    584         m->msg_iov[m->msg_iovlen].iov_base = (void*) buf; 
     598        m->msg_iov[m->msg_iovlen].iov_base = (void *)buf; 
    585599        m->msg_iov[m->msg_iovlen].iov_len = len; 
    586600 
     
    603617    int i; 
    604618    unsigned char *hdr; 
     619 
     620    assert(c != NULL); 
    605621 
    606622    if (c->msgused > c->hdrsize) { 
     
    612628        if (! new_hdrbuf) 
    613629            return -1; 
    614         c->hdrbuf = (unsigned char *) new_hdrbuf; 
     630        c->hdrbuf = (unsigned char *)new_hdrbuf; 
    615631        c->hdrsize = c->msgused * 2; 
    616632    } 
     
    628644        *hdr++ = 0; 
    629645        *hdr++ = 0; 
    630         assert((void*) hdr == (void*) c->msglist[i].msg_iov[0].iov_base + UDP_HEADER_SIZE); 
     646        assert((void *) hdr == (void *)c->msglist[i].msg_iov[0].iov_base + UDP_HEADER_SIZE); 
    631647    } 
    632648 
     
    638654    int len; 
    639655 
     656    assert(c != NULL); 
     657 
    640658    if (settings.verbose > 1) 
    641659        fprintf(stderr, ">%d %s\n", c->sfd, str); 
    642660 
    643661    len = strlen(str); 
    644     if (len + 2 > c->wsize) { 
     662    if ((len + 2) > c->wsize) { 
    645663        /* ought to be always enough. just fail for simplicity */ 
    646664        str = "SERVER_ERROR output line too long"; 
     
    664682 
    665683static void complete_nread(conn *c) { 
     684    assert(c != NULL); 
     685 
    666686    item *it = c->item; 
    667687    int comm = c->item_comm; 
     
    726746 
    727747typedef struct token_s { 
    728     char* value; 
     748    char *value; 
    729749    size_t length; 
    730750} token_t; 
     
    754774 *   } 
    755775 */ 
    756 static size_t tokenize_command(char* command, token_t* tokens, const size_t max_tokens)  { 
    757     char* cp; 
    758     char* value = NULL; 
     776static size_t tokenize_command(char *command, token_t *tokens, const size_t max_tokens)  { 
     777    char *cp; 
     778    char *value = NULL; 
    759779    size_t length = 0; 
    760780    size_t ntokens = 0; 
     
    801821 
    802822inline void process_stats_detail(conn *c, const char *command) { 
     823    assert(c != NULL); 
     824 
    803825    if (strcmp(command, "on") == 0) { 
    804826        settings.detail_enabled = 1; 
     
    828850} 
    829851 
    830 static void process_stat(conn *c, token_t* tokens, const size_t ntokens) { 
     852static void process_stat(conn *c, token_t *tokens, const size_t ntokens) { 
    831853    rel_time_t now = current_time; 
    832     char* command; 
    833     char* subcommand; 
     854    char *command; 
     855    char *subcommand; 
     856 
     857    assert(c != NULL); 
    834858 
    835859    if(ntokens < 2) { 
     
    853877        pos += sprintf(pos, "STAT time %ld\r\n", now + stats.started); 
    854878        pos += sprintf(pos, "STAT version " VERSION "\r\n"); 
    855         pos += sprintf(pos, "STAT pointer_size %d\r\n", 8 * sizeof(void*)); 
     879        pos += sprintf(pos, "STAT pointer_size %d\r\n", 8 * sizeof(void *)); 
    856880        pos += sprintf(pos, "STAT rusage_user %ld.%06ld\r\n", usage.ru_utime.tv_sec, usage.ru_utime.tv_usec); 
    857881        pos += sprintf(pos, "STAT rusage_system %ld.%06ld\r\n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec); 
     
    869893        pos += sprintf(pos, "STAT bytes_read %llu\r\n", stats.bytes_read); 
    870894        pos += sprintf(pos, "STAT bytes_written %llu\r\n", stats.bytes_written); 
    871         pos += sprintf(pos, "STAT limit_maxbytes %llu\r\n", (unsigned long long) settings.maxbytes); 
     895        pos += sprintf(pos, "STAT limit_maxbytes %llu\r\n", (unsigned long long)settings.maxbytes); 
    872896        pos += sprintf(pos, "STAT threads %u\r\n", settings.num_threads); 
    873897        pos += sprintf(pos, "END"); 
     
    915939        int res; 
    916940 
    917         wbuf = (char *)malloc(wsize); 
    918         if (wbuf == 0) { 
     941        if (!(wbuf = (char *)malloc(wsize))) { 
    919942            out_string(c, "SERVER_ERROR out of memory"); 
    920943            return; 
     
    940963        } 
    941964        strcpy(wbuf + res, "END\r\n"); 
    942         c->write_and_free=wbuf; 
    943         c->wcurr=wbuf; 
     965        c->write_and_free = wbuf; 
     966        c->wcurr = wbuf; 
    944967        c->wbytes = res + 5; // Don't write the terminal '\0'  
    945968        conn_set_state(c, conn_write); 
     
    9811004    } 
    9821005 
    983     if (strcmp(subcommand, "slabs")==0) { 
     1006    if (strcmp(subcommand, "slabs") == 0) { 
    9841007        int bytes = 0; 
    9851008        char *buf = slabs_stats(&bytes); 
     
    9961019    } 
    9971020 
    998     if (strcmp(subcommand, "items")==0) { 
     1021    if (strcmp(subcommand, "items") == 0) { 
    9991022        char buffer[4096]; 
    10001023        item_stats(buffer, 4096); 
     
    10031026    } 
    10041027 
    1005     if (strcmp(subcommand, "detail")==0) { 
     1028    if (strcmp(subcommand, "detail") == 0) { 
    10061029        if (ntokens < 4) 
    10071030            process_stats_detail(c, "");  /* outputs the error message */ 
     
    10111034    } 
    10121035 
    1013     if (strcmp(subcommand, "sizes")==0) { 
     1036    if (strcmp(subcommand, "sizes") == 0) { 
    10141037        int bytes = 0; 
    10151038        char *buf = item_stats_sizes(&bytes); 
     
    10311054 
    10321055/* ntokens is overwritten here... shrug.. */ 
    1033 static inline void process_get_command(conn *c, token_t* tokens, size_t ntokens) { 
     1056static inline void process_get_command(conn *c, token_t *tokens, size_t ntokens) { 
    10341057    char *key; 
    10351058    size_t nkey; 
    10361059    int i = 0; 
    10371060    item *it; 
    1038     token_t* key_token = &tokens[KEY_TOKEN]; 
     1061    token_t *key_token = &tokens[KEY_TOKEN]; 
     1062 
     1063    assert(c != NULL); 
    10391064 
    10401065    if (settings.managed) { 
     
    10711096            if (it) { 
    10721097                if (i >= c->isize) { 
    1073                     item **new_list = realloc(c->ilist, sizeof(item *)*c->isize*2); 
     1098                    item **new_list = realloc(c->ilist, sizeof(item *) * c->isize * 2); 
    10741099                    if (new_list) { 
    10751100                        c->isize *= 2; 
     
    11391164} 
    11401165 
    1141 static void process_update_command(conn *c, token_t* tokens, const size_t ntokens, int comm) { 
     1166static void process_update_command(conn *c, token_t *tokens, const size_t ntokens, int comm) { 
    11421167    char *key; 
    11431168    size_t nkey; 
     
    11461171    int vlen; 
    11471172    item *it; 
     1173 
     1174    assert(c != NULL); 
    11481175 
    11491176    if (tokens[KEY_TOKEN].length > KEY_MAX_LENGTH) { 
     
    11901217        /* swallow the data line */ 
    11911218        c->write_and_go = conn_swallow; 
    1192         c->sbytes = vlen+2; 
     1219        c->sbytes = vlen + 2; 
    11931220        return; 
    11941221    } 
     
    12011228} 
    12021229 
    1203 static void process_arithmetic_command(conn *c, token_t* tokens, const size_t ntokens, const int incr) { 
     1230static void process_arithmetic_command(conn *c, token_t *tokens, const size_t ntokens, const int incr) { 
    12041231    char temp[32]; 
    12051232    item *it; 
     
    12071234    char *key; 
    12081235    size_t nkey; 
    1209     char *msg; 
    12101236     
     1237    assert(c != NULL); 
     1238 
    12111239    if(tokens[KEY_TOKEN].length > KEY_MAX_LENGTH) {  
    12121240        out_string(c, "CLIENT_ERROR bad command line format"); 
     
    12631291 
    12641292    ptr = ITEM_data(it); 
    1265     while ((*ptr != '\0') && (*ptr<'0' && *ptr>'9')) ptr++;    // BUG: can't be true 
     1293    while ((*ptr != '\0') && (*ptr < '0' && *ptr > '9')) ptr++;    // BUG: can't be true 
    12661294         
    12671295    value = strtol(ptr, NULL, 10); 
     
    12721300 
    12731301    if (incr != 0) 
    1274         value+=delta; 
     1302        value += delta; 
    12751303    else { 
    12761304        if (delta >= value) value = 0; 
    1277         else value-=delta; 
     1305        else value -= delta; 
    12781306    } 
    12791307    snprintf(buf, 32, "%u", value); 
     
    12911319    } else { /* replace in-place */ 
    12921320        memcpy(ITEM_data(it), buf, res); 
    1293         memset(ITEM_data(it) + res, ' ', it->nbytes-res-2); 
     1321        memset(ITEM_data(it) + res, ' ', it->nbytes - res - 2); 
    12941322    } 
    12951323 
     
    12971325} 
    12981326 
    1299 static void process_delete_command(conn *c, token_t* tokens, const size_t ntokens) { 
     1327static void process_delete_command(conn *c, token_t *tokens, const size_t ntokens) { 
    13001328    char *key; 
    13011329    size_t nkey; 
     
    13031331    time_t exptime = 0; 
    13041332     
     1333    assert(c != NULL); 
     1334 
    13051335    if (settings.managed) { 
    13061336        int bucket = c->bucket; 
     
    13881418    int comm; 
    13891419 
     1420    assert(c != NULL); 
     1421 
    13901422    if (settings.verbose > 1) 
    13911423        fprintf(stderr, "<%d %s\n", c->sfd, command); 
     
    14771509            return; 
    14781510        } 
    1479         if (sscanf(tokens[1].value, "%u:%u", &bucket,&gen) == 2) { 
     1511        if (sscanf(tokens[1].value, "%u:%u", &bucket, &gen) == 2) { 
    14801512            /* we never write anything back, even if input's wrong */ 
    1481             if ((bucket < 0) || (bucket >= MAX_BUCKETS) || (gen<=0)) { 
     1513            if ((bucket < 0) || (bucket >= MAX_BUCKETS) || (gen <= 0)) { 
    14821514                /* do nothing, bad input */ 
    14831515            } else { 
     
    15691601    char *el, *cont; 
    15701602 
    1571     assert(c->rcurr <= c->rbuf + c->rsize); 
     1603    assert(c != NULL); 
     1604    assert(c->rcurr <= (c->rbuf + c->rsize)); 
    15721605 
    15731606    if (c->rbytes == 0) 
     
    15771610        return 0; 
    15781611    cont = el + 1; 
    1579     if (el - c->rcurr > 1 && *(el - 1) == '\r') { 
     1612    if ((el - c->rcurr) > 1 && *(el - 1) == '\r') { 
    15801613        el--; 
    15811614    } 
    15821615    *el = '\0'; 
    15831616 
    1584     assert(cont <= c->rcurr + c->rbytes); 
     1617    assert(cont <= (c->rcurr + c->rbytes)); 
    15851618 
    15861619    process_command(c, c->rcurr); 
     
    15891622    c->rcurr = cont; 
    15901623 
    1591     assert(c->rcurr <= c->rbuf + c->rsize); 
     1624    assert(c->rcurr <= (c->rbuf + c->rsize)); 
    15921625 
    15931626    return 1; 
     
    16001633static int try_read_udp(conn *c) { 
    16011634    int res; 
     1635 
     1636    assert(c != NULL); 
    16021637 
    16031638    c->request_addr_size = sizeof(c->request_addr); 
     
    16411676    int res; 
    16421677 
     1678    assert(c != NULL); 
     1679 
    16431680    if (c->rcurr != c->rbuf) { 
    16441681        if (c->rbytes != 0) /* otherwise there's nothing to copy */ 
     
    16491686    while (1) { 
    16501687        if (c->rbytes >= c->rsize) { 
    1651             char *new_rbuf = realloc(c->rbuf, c->rsize*2); 
     1688            char *new_rbuf = realloc(c->rbuf, c->rsize * 2); 
    16521689            if (!new_rbuf) { 
    16531690                if (settings.verbose > 0) 
     
    16581695                return 1; 
    16591696            } 
    1660             c->rcurr = c->rbuf = new_rbuf; 
     1697            c->rcurr = c->rbuf = new_rbuf; 
    16611698            c->rsize *= 2; 
    16621699        } 
     
    16941731 
    16951732static bool update_event(conn *c, const int new_flags) { 
     1733    assert(c != NULL); 
     1734 
    16961735    struct event_base *base = c->event.ev_base; 
    16971736    if (c->ev_flags == new_flags) 
     
    17381777    int res; 
    17391778 
     1779    assert(c != NULL); 
     1780 
    17401781    if (c->msgcurr < c->msgused && 
    17411782            c->msglist[c->msgcurr].msg_iovlen == 0) { 
     
    17961837    socklen_t addrlen; 
    17971838    struct sockaddr addr; 
    1798     conn *newc; 
    17991839    int res; 
     1840 
     1841    assert(c != NULL); 
    18001842 
    18011843    while (!stop) { 
     
    20072049 
    20082050    c = (conn *)arg; 
     2051    assert(c != NULL); 
     2052 
    20092053    c->which = which; 
    20102054 
     
    20632107 
    20642108    while (min <= max) { 
    2065         avg = ((unsigned int) min + max) / 2; 
     2109        avg = ((unsigned int)(min + max)) / 2; 
    20662110        if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &avg, intsize) == 0) { 
    20672111            last_good = avg; 
     
    21052149    addr.sin_port = htons(port); 
    21062150    addr.sin_addr = settings.interface; 
    2107     if (bind(sfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) { 
     2151    if (bind(sfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { 
    21082152        perror("bind()"); 
    21092153        close(sfd); 
     
    21712215    addr.sun_family = AF_UNIX; 
    21722216    strcpy(addr.sun_path, path); 
    2173     if (bind(sfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) { 
     2217    if (bind(sfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { 
    21742218        perror("bind()"); 
    21752219        close(sfd); 
     
    21852229 
    21862230/* listening socket */ 
    2187 static int l_socket=0; 
     2231static int l_socket = 0; 
    21882232 
    21892233/* udp socket */ 
    2190 static int u_socket=-1; 
     2234static int u_socket = -1; 
    21912235 
    21922236/* invoke right before gdb is called, on assert */ 
    21932237void pre_gdb(void) { 
    2194     int i = 0
     2238    int i
    21952239    if (l_socket > -1) close(l_socket); 
    21962240    if (u_socket > -1) close(u_socket); 
    2197     for (i=3; i<=500; i++) close(i); /* so lame */ 
     2241    for (i = 3; i <= 500; i++) close(i); /* so lame */ 
    21982242    kill(getpid(), SIGABRT); 
    21992243} 
     
    22562300void do_run_deferred_deletes(void) 
    22572301{ 
    2258     int i=0, j=0; 
    2259  
    2260     for (i=0; i<delcurr; i++) { 
     2302    int i, j = 0; 
     2303 
     2304    for (i = 0; i < delcurr; i++) { 
    22612305        item *it = todelete[i]; 
    22622306        if (item_delete_lock_over(it)) { 
     
    23752419        return; 
    23762420 
    2377     if (!(fp = fopen(pid_file,"w"))) { 
    2378         fprintf(stderr,"Could not open the pid file %s for writing\n",pid_file); 
     2421    if (!(fp = fopen(pid_file, "w"))) { 
     2422        fprintf(stderr, "Could not open the pid file %s for writing\n", pid_file); 
    23792423        return; 
    23802424    } 
    23812425 
    2382     fprintf(fp,"%ld\n",(long) pid); 
     2426    fprintf(fp,"%ld\n", (long)pid); 
    23832427    if (fclose(fp) == -1) { 
    2384         fprintf(stderr,"Could not close the pid file %s.\n",pid_file); 
     2428        fprintf(stderr, "Could not close the pid file %s.\n", pid_file); 
    23852429        return; 
    23862430    } 
     
    23922436 
    23932437  if (unlink(pid_file) != 0) { 
    2394       fprintf(stderr,"Could not remove the pid file %s.\n",pid_file); 
     2438      fprintf(stderr, "Could not remove the pid file %s.\n", pid_file); 
    23952439  } 
    23962440 
     
    24052449int main (int argc, char **argv) { 
    24062450    int c; 
    2407     conn *u_conn; 
    24082451    struct in_addr addr; 
    24092452    bool lock_memory = false; 
     
    24412484            break; 
    24422485        case 'm': 
    2443             settings.maxbytes = ((size_t)atoi(optarg))*1024*1024; 
     2486            settings.maxbytes = ((size_t)atoi(optarg)) * 1024 * 1024; 
    24442487            break; 
    24452488        case 'M': 
     
    25222565         * the soft limit to the hard. 
    25232566         */ 
    2524         if (getrlimit(RLIMIT_CORE, &rlim)==0) { 
     2567        if (getrlimit(RLIMIT_CORE, &rlim) == 0) { 
    25252568            rlim_new.rlim_cur = rlim_new.rlim_max = RLIM_INFINITY; 
    2526             if (setrlimit(RLIMIT_CORE, &rlim_new)!=0) { 
     2569            if (setrlimit(RLIMIT_CORE, &rlim_new)!= 0) { 
    25272570                /* failed. try raising just to the old max */ 
    2528                 rlim_new.rlim_cur = rlim_new.rlim_max = 
    2529                     rlim.rlim_max; 
    2530                 (void) setrlimit(RLIMIT_CORE, &rlim_new); 
     2571                rlim_new.rlim_cur = rlim_new.rlim_max = rlim.rlim_max; 
     2572                (void)setrlimit(RLIMIT_CORE, &rlim_new); 
    25312573            } 
    25322574        } 
     
    25372579         */ 
    25382580 
    2539         if ((getrlimit(RLIMIT_CORE, &rlim)!=0) || rlim.rlim_cur==0) { 
     2581        if ((getrlimit(RLIMIT_CORE, &rlim) != 0) || rlim.rlim_cur == 0) { 
    25402582            fprintf(stderr, "failed to ensure corefile creation\n"); 
    25412583            exit(EXIT_FAILURE); 
     
    25892631 
    25902632    /* lose root privileges if we have them */ 
    2591     if (getuid()== 0 || geteuid()==0) { 
    2592         if (username==0 || *username=='\0') { 
     2633    if (getuid() == 0 || geteuid() == 0) { 
     2634        if (username == 0 || *username == '\0') { 
    25932635            fprintf(stderr, "can't run as root without the -u switch\n"); 
    25942636            return 1; 
     
    25982640            return 1; 
    25992641        } 
    2600         if (setgid(pw->pw_gid)<0 || setuid(pw->pw_uid)<0) { 
     2642        if (setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) { 
    26012643            fprintf(stderr, "failed to assume identity of user %s\n", username); 
    26022644            return 1; 
     
    26362678    /* managed instance? alloc and zero a bucket array */ 
    26372679    if (settings.managed) { 
    2638         buckets = malloc(sizeof(int)*MAX_BUCKETS); 
     2680        buckets = malloc(sizeof(int) * MAX_BUCKETS); 
    26392681        if (buckets == 0) { 
    26402682            fprintf(stderr, "failed to allocate the bucket array"); 
    26412683            exit(EXIT_FAILURE); 
    26422684        } 
    2643         memset(buckets, 0, sizeof(int)*MAX_BUCKETS); 
     2685        memset(buckets, 0, sizeof(int) * MAX_BUCKETS); 
    26442686    } 
    26452687 
     
    26722714    /* save the PID in if we're a daemon */ 
    26732715    if (daemonize) 
    2674         save_pid(getpid(),pid_file); 
     2716        save_pid(getpid(), pid_file); 
    26752717    /* start up worker threads if MT mode */ 
    26762718    thread_init(settings.num_threads, main_base); 
    26772719    /* initialise clock event */ 
    2678     clock_handler(0,0,0); 
     2720    clock_handler(0, 0, 0); 
    26792721    /* initialise deletion array and timer event */ 
    2680     deltotal = 200; delcurr = 0; 
    2681     todelete = malloc(sizeof(item *)*deltotal); 
    2682     delete_handler(0,0,0); /* sets up the event */ 
     2722    deltotal = 200; 
     2723    delcurr = 0; 
     2724    todelete = malloc(sizeof(item *) * deltotal); 
     2725    delete_handler(0, 0, 0); /* sets up the event */ 
    26832726    /* create the initial listening udp connection, monitored on all threads */ 
    26842727    if (u_socket > -1) { 
  • branches/multithreaded/server/slabs.c

    r469 r490  
    5050} slabclass_t; 
    5151 
    52 static slabclass_t slabclass[POWER_LARGEST+1]; 
     52static slabclass_t slabclass[POWER_LARGEST + 1]; 
    5353static size_t mem_limit = 0; 
    5454static size_t mem_malloced = 0; 
     
    6060static int do_slabs_newslab(const unsigned int id); 
    6161 
     62#ifndef DONT_PREALLOC_SLABS 
    6263/* Preallocate as many slab pages as possible (called from slabs_init) 
    6364   on start-up, so users don't get confused out-of-memory errors when 
     
    6768   smaller ones will be made.  */ 
    6869static void slabs_preallocate (const unsigned int maxslabs); 
    69  
     70#endif 
    7071 
    7172/* 
     
    8081    int res = POWER_SMALLEST; 
    8182 
    82     if(size==0) 
     83    if(size == 0) 
    8384        return 0; 
    8485    while (size > slabclass[res].size) 
     
    125126        char *t_initial_malloc = getenv("T_MEMD_INITIAL_MALLOC"); 
    126127        if (t_initial_malloc) { 
    127             mem_malloced = (size_t) atol(t_initial_malloc); 
     128            mem_malloced = (size_t)atol(t_initial_malloc); 
    128129        } 
    129130 
     
    141142} 
    142143 
     144#ifndef DONT_PREALLOC_SLABS 
    143145static void slabs_preallocate (const unsigned int maxslabs) { 
    144146    int i; 
     
    151153       these three lines.  */ 
    152154 
    153     for(i=POWER_SMALLEST; i<=POWER_LARGEST; i++) { 
     155    for(i = POWER_SMALLEST; i <= POWER_LARGEST; i++) { 
    154156        if (++prealloc > maxslabs) 
    155157            return; 
     
    158160 
    159161} 
     162#endif 
    160163 
    161164static int grow_slab_list (const unsigned int id) { 
     
    163166    if (p->slabs == p->list_size) { 
    164167        size_t new_size =  (p->list_size != 0) ? p->list_size * 2 : 16; 
    165         void *new_list = realloc(p->slab_list, new_size*sizeof(void*)); 
     168        void *new_list = realloc(p->slab_list, new_size * sizeof(void *)); 
    166169        if (new_list == 0) return 0; 
    167170        p->list_size = new_size; 
     
    206209 
    207210    p = &slabclass[id]; 
    208     assert(p->sl_curr == 0 || ((item*)p->slots[p->sl_curr-1])->slabs_clsid == 0); 
     211    assert(p->sl_curr == 0 || ((item *)p->slots[p->sl_curr - 1])->slabs_clsid == 0); 
    209212 
    210213#ifdef USE_SYSTEM_MALLOC 
     
    217220    /* fail unless we have space at the end of a recently allocated page, 
    218221       we have something on our freelist, or we could allocate a new page */ 
    219     if (! (p->end_page_ptr != 0 || p->sl_curr != 0 || do_slabs_newslab(id) !=0)) 
     222    if (! (p->end_page_ptr != 0 || p->sl_curr != 0 || do_slabs_newslab(id) != 0)) 
    220223        return 0; 
    221224 
     
    242245    slabclass_t *p; 
    243246 
    244     assert(((item *)ptr)->slabs_clsid==0); 
     247    assert(((item *)ptr)->slabs_clsid == 0); 
    245248    assert(id >= POWER_SMALLEST && id <= power_largest); 
    246249    if (id < POWER_SMALLEST || id > power_largest) 
     
    256259 
    257260    if (p->sl_curr == p->sl_total) { /* need more space on the free list */ 
    258         int new_size = (p->sl_total != 0) ? p->sl_total*2 : 16;  /* 16 is arbitrary */ 
    259         void **new_slots = realloc(p->slots, new_size*sizeof(void *)); 
     261        int new_size = (p->sl_total != 0) ? p->sl_total * 2 : 16;  /* 16 is arbitrary */ 
     262        void **new_slots = realloc(p->slots, new_size * sizeof(void *)); 
    260263        if (new_slots == 0) 
    261264            return; 
     
    270273char* do_slabs_stats(int *buflen) { 
    271274    int i, total; 
    272     char *buf = (char*) malloc(power_largest * 200 + 100); 
     275    char *buf = (char *)malloc(power_largest * 200 + 100); 
    273276    char *bufcurr = buf; 
    274277 
     
    295298        } 
    296299    } 
    297     bufcurr += sprintf(bufcurr, "STAT active_slabs %d\r\nSTAT total_malloced %llu\r\n", total, (unsigned long long) mem_malloced); 
     300    bufcurr += sprintf(bufcurr, "STAT active_slabs %d\r\nSTAT total_malloced %llu\r\n", total, (unsigned long long)mem_malloced); 
    298301    bufcurr += sprintf(bufcurr, "END\r\n"); 
    299302    *buflen = bufcurr - buf; 
     
    333336    if (p->killing == 0) p->killing = 1; 
    334337 
    335     slab = p->slab_list[p->killing-1]; 
     338    slab = p->slab_list[p->killing - 1]; 
    336339    slab_end = slab + POWER_BLOCK; 
    337340 
    338     for (iter=slab; iter<slab_end; iter+=p->size) { 
    339         item *it = (item *) iter; 
     341    for (iter = slab; iter < slab_end; iter += p->size) { 
     342        item *it = (item *)iter; 
    340343        if (it->slabs_clsid) { 
    341344            if (it->refcount) was_busy = 1; 
     
    347350    { 
    348351        int fi; 
    349         for (fi=p->sl_curr-1; fi>=0; fi--) { 
     352        for (fi = p->sl_curr - 1; fi >= 0; fi--) { 
    350353            if (p->slots[fi] >= slab && p->slots[fi] < slab_end) { 
    351354                p->sl_curr--; 
     
    358361 
    359362    /* if good, now move it to the dst slab class */ 
    360     p->slab_list[p->killing-1] = p->slab_list[p->slabs-1]; 
     363    p->slab_list[p->killing - 1] = p->slab_list[p->slabs - 1]; 
    361364    p->slabs--; 
    362365    p->killing = 0; 
     
    366369    /* this isn't too critical, but other parts of the code do asserts to 
    367370       make sure this field is always 0.  */ 
    368     for (iter=slab; iter<slab_end; iter+=dp->size) { 
     371    for (iter = slab; iter < slab_end; iter += dp->size) { 
    369372        ((item *)iter)->slabs_clsid = 0; 
    370373    } 
  • branches/multithreaded/server/t/stats.t

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