[memcached] plindner, r512: first pass at pedantic ansi-c changes, u...

commits at code.sixapart.com commits at code.sixapart.com
Mon Apr 16 23:33:44 UTC 2007


first pass at pedantic ansi-c changes, use bools/consts.

U   trunk/server/items.c
U   trunk/server/items.h
U   trunk/server/memcached.c
U   trunk/server/memcached.h
U   trunk/server/stats.c
U   trunk/server/stats.h
U   trunk/server/thread.c


Modified: trunk/server/items.c
===================================================================
--- trunk/server/items.c	2007-04-16 22:22:33 UTC (rev 511)
+++ trunk/server/items.c	2007-04-16 23:33:43 UTC (rev 512)
@@ -360,47 +360,47 @@
 
 /* returns true if a deleted item's delete-locked-time is over, and it
    should be removed from the namespace */
-int item_delete_lock_over (item *it) {
+bool item_delete_lock_over (item *it) {
     assert(it->it_flags & ITEM_DELETED);
     return (current_time >= it->exptime);
 }
 
 /* wrapper around assoc_find which does the lazy expiration/deletion logic */
-item *do_item_get_notedeleted(char *key, size_t nkey, int *delete_locked) {
+item *do_item_get_notedeleted(const char *key, const size_t nkey, bool *delete_locked) {
     item *it = assoc_find(key, nkey);
-    if (delete_locked) *delete_locked = 0;
+    if (delete_locked) *delete_locked = false;
     if (it && (it->it_flags & ITEM_DELETED)) {
         /* it's flagged as delete-locked.  let's see if that condition
            is past due, and the 5-second delete_timer just hasn't
            gotten to it yet... */
-        if (! item_delete_lock_over(it)) {
-            if (delete_locked) *delete_locked = 1;
+        if (!item_delete_lock_over(it)) {
+            if (delete_locked) *delete_locked = true;
             it = 0;
         }
     }
-    if (it && settings.oldest_live && settings.oldest_live <= current_time &&
+    if (it != NULL && settings.oldest_live != 0 && settings.oldest_live <= current_time &&
         it->time <= settings.oldest_live) {
         do_item_unlink(it);           // MTSAFE - cache_lock held
         it = 0;
     }
-    if (it && it->exptime && it->exptime <= current_time) {
+    if (it != NULL && it->exptime != 0 && it->exptime <= current_time) {
         do_item_unlink(it);           // MTSAFE - cache_lock held
         it = 0;
     }
 
-    if (it) {
+    if (it != NULL) {
         it->refcount++;
         DEBUG_REFCNT(it, '+');
     }
     return it;
 }
 
-item *item_get(char *key, size_t nkey) {
+item *item_get(const char *key, const size_t nkey) {
     return item_get_notedeleted(key, nkey, 0);
 }
 
 /* returns an item whether or not it's delete-locked or expired. */
-item *do_item_get_nocheck(char *key, size_t nkey) {
+item *do_item_get_nocheck(const char *key, const size_t nkey) {
     item *it = assoc_find(key, nkey);
     if (it) {
         it->refcount++;

Modified: trunk/server/items.h
===================================================================
--- trunk/server/items.h	2007-04-16 22:22:33 UTC (rev 511)
+++ trunk/server/items.h	2007-04-16 23:33:43 UTC (rev 512)
@@ -18,7 +18,7 @@
 /*@null@*/
 char *item_stats_sizes(int *bytes);
 void do_item_flush_expired(void);
-item *item_get(char *key, size_t nkey);
+item *item_get(const char *key, const size_t nkey);
 
-item *do_item_get_notedeleted(char *key, size_t nkey, int *delete_locked);
-item *do_item_get_nocheck(char *key, size_t nkey);
+item *do_item_get_notedeleted(const char *key, const size_t nkey, bool *delete_locked);
+item *do_item_get_nocheck(const char *key, const size_t nkey);

Modified: trunk/server/memcached.c
===================================================================
--- trunk/server/memcached.c	2007-04-16 22:22:33 UTC (rev 511)
+++ trunk/server/memcached.c	2007-04-16 23:33:43 UTC (rev 512)
@@ -714,7 +714,7 @@
  */
 int do_store_item(item *it, int comm) {
     char *key = ITEM_key(it);
-    int delete_locked = 0;
+    bool delete_locked = false;
     item *old_it = do_item_get_notedeleted(key, it->nkey, &delete_locked);
     int stored = 0;
 

Modified: trunk/server/memcached.h
===================================================================
--- trunk/server/memcached.h	2007-04-16 22:22:33 UTC (rev 511)
+++ trunk/server/memcached.h	2007-04-16 23:33:43 UTC (rev 512)
@@ -251,7 +251,7 @@
 int   mt_is_listen_thread(void);
 item *mt_item_alloc(char *key, size_t nkey, int flags, rel_time_t exptime, int nbytes);
 void  mt_item_flush_expired(void);
-item *mt_item_get_notedeleted(char *key, size_t nkey, int *delete_locked);
+item *mt_item_get_notedeleted(char *key, size_t nkey, bool *delete_locked);
 item *mt_item_get_nocheck(char *key, size_t nkey);
 int   mt_item_link(item *it);
 void  mt_item_remove(item *it);

Modified: trunk/server/stats.c
===================================================================
--- trunk/server/stats.c	2007-04-16 22:22:33 UTC (rev 511)
+++ trunk/server/stats.c	2007-04-16 23:33:43 UTC (rev 512)
@@ -12,7 +12,9 @@
 #include "memcached.h"
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #include <string.h>
+#include <assert.h>
 
 /*
  * Stats are tracked on the basis of key prefixes. This is a simple
@@ -21,16 +23,17 @@
  */
 typedef struct _prefix_stats PREFIX_STATS;
 struct _prefix_stats {
-    char               *prefix;
-    int                prefix_len;
-    unsigned long long num_gets;
-    unsigned long long num_sets;
-    unsigned long long num_deletes;
-    unsigned long long num_hits;
-    PREFIX_STATS       *next;
+    char         *prefix;
+    size_t        prefix_len;
+    uint64_t      num_gets;
+    uint64_t      num_sets;
+    uint64_t      num_deletes;
+    uint64_t      num_hits;
+    PREFIX_STATS *next;
 };
 
 #define PREFIX_HASH_SIZE 256
+
 static PREFIX_STATS *prefix_stats[PREFIX_HASH_SIZE];
 static int num_prefixes = 0;
 static int total_prefix_size = 0;
@@ -45,9 +48,9 @@
  */
 void stats_prefix_clear() {
     int i;
-    PREFIX_STATS *cur, *next;
 
     for (i = 0; i < PREFIX_HASH_SIZE; i++) {
+        PREFIX_STATS *cur, *next;
         for (cur = prefix_stats[i]; cur != NULL; cur = next) {
             next = cur->next;
             free(cur->prefix);
@@ -63,11 +66,14 @@
  * Returns the stats structure for a prefix, creating it if it's not already
  * in the list.
  */
-static PREFIX_STATS *stats_prefix_find(char *key) {
+/*@null@*/
+static PREFIX_STATS *stats_prefix_find(const char *key) {
     PREFIX_STATS *pfs;
-    int hashval;
-    int length;
+    uint32_t hashval;
+    size_t length;
 
+    assert(key != NULL);
+
     for (length = 0; key[length] != '\0'; length++)
         if (key[length] == settings.prefix_delimiter)
             break;
@@ -75,7 +81,7 @@
     hashval = hash(key, length, 0) % PREFIX_HASH_SIZE;
 
     for (pfs = prefix_stats[hashval]; NULL != pfs; pfs = pfs->next) {
-        if (! strncmp(pfs->prefix, key, length))
+        if (strncmp(pfs->prefix, key, length) == 0)
             return pfs;
     }
 
@@ -108,7 +114,7 @@
 /*
  * Records a "get" of a key.
  */
-void stats_prefix_record_get(char *key, int is_hit) {
+void stats_prefix_record_get(const char *key, const bool is_hit) {
     PREFIX_STATS *pfs;
 
     STATS_LOCK();
@@ -125,7 +131,7 @@
 /*
  * Records a "delete" of a key.
  */
-void stats_prefix_record_delete(char *key) {
+void stats_prefix_record_delete(const char *key) {
     PREFIX_STATS *pfs;
 
     STATS_LOCK();
@@ -139,7 +145,7 @@
 /*
  * Records a "set" of a key.
  */
-void stats_prefix_record_set(char *key) {
+void stats_prefix_record_set(const char *key) {
     PREFIX_STATS *pfs;
 
     STATS_LOCK();
@@ -153,12 +159,13 @@
 /*
  * Returns stats in textual form suitable for writing to a client.
  */
+/*@null@*/
 char *stats_prefix_dump(int *length) {
-    char *format = "PREFIX %s get %llu hit %llu set %llu del %llu\r\n";
+    const char *format = "PREFIX %s get %llu hit %llu set %llu del %llu\r\n";
     PREFIX_STATS *pfs;
     char *buf;
     int i, pos;
-    int size;
+    size_t size;
 
     /*
      * Figure out how big the buffer needs to be. This is the sum of the
@@ -181,14 +188,14 @@
     pos = 0;
     for (i = 0; i < PREFIX_HASH_SIZE; i++) {
         for (pfs = prefix_stats[i]; NULL != pfs; pfs = pfs->next) {
-            pos += sprintf(buf + pos, format,
+            pos += snprintf(buf + pos, size-pos, format,
                            pfs->prefix, pfs->num_gets, pfs->num_hits,
                            pfs->num_sets, pfs->num_deletes);
         }
     }
 
     STATS_UNLOCK();
-    strcpy(buf + pos, "END\r\n");
+    memcpy(buf + pos, "END\r\n", 6);
 
     *length = pos + 5;
     return buf;
@@ -212,7 +219,7 @@
 static void test_equals_int(char *what, int a, int b) { test_count++; if (a != b) fail(what); }
 static void test_equals_ptr(char *what, void *a, void *b) { test_count++; if (a != b) fail(what); }
 static void test_equals_str(char *what, const char *a, const char *b) { test_count++; if (strcmp(a, b)) fail(what); }
-static void test_equals_ull(char *what, unsigned long long a, unsigned long long b) { test_count++; if (a != b) fail(what); }
+static void test_equals_ull(char *what, uint64_t a, uint64_t b) { test_count++; if (a != b) fail(what); }
 static void test_notequals_ptr(char *what, void *a, void *b) { test_count++; if (a == b) fail(what); }
 static void test_notnull_ptr(char *what, void *a) { test_count++; if (NULL == a) fail(what); }
 

Modified: trunk/server/stats.h
===================================================================
--- trunk/server/stats.h	2007-04-16 22:22:33 UTC (rev 511)
+++ trunk/server/stats.h	2007-04-16 23:33:43 UTC (rev 512)
@@ -1,7 +1,8 @@
 /* stats */
 void stats_prefix_init(void);
 void stats_prefix_clear(void);
-void stats_prefix_record_get(char *key, int is_hit);
-void stats_prefix_record_delete(char *key);
-void stats_prefix_record_set(char *key);
+void stats_prefix_record_get(const char *key, const bool is_hit);
+void stats_prefix_record_delete(const char *key);
+void stats_prefix_record_set(const char *key);
+/*@null@*/
 char *stats_prefix_dump(int *length);

Modified: trunk/server/thread.c
===================================================================
--- trunk/server/thread.c	2007-04-16 22:22:33 UTC (rev 511)
+++ trunk/server/thread.c	2007-04-16 23:33:43 UTC (rev 512)
@@ -381,7 +381,7 @@
  * Returns an item if it hasn't been marked as expired or deleted,
  * lazy-expiring as needed.
  */
-item *mt_item_get_notedeleted(char *key, size_t nkey, int *delete_locked) {
+item *mt_item_get_notedeleted(char *key, size_t nkey, bool *delete_locked) {
     item *it;
     pthread_mutex_lock(&cache_lock);
     it = do_item_get_notedeleted(key, nkey, delete_locked);




More information about the memcached-commits mailing list