Abstraction (which will save us merge hell with 1.2).
[awesomized/libmemcached] / libmemcached / stats.cc
index 434949485620a095834847f82353b1234b48e7a7..89d5be52382bb57387c006e06ee4172ade1802e3 100644 (file)
@@ -227,12 +227,23 @@ static memcached_return_t set_data(memcached_stat_st *memc_stat, const char *key
   return MEMCACHED_SUCCESS;
 }
 
-char *memcached_stat_get_value(const memcached_st *ptr, memcached_stat_st *memc_stat,
+char *memcached_stat_get_value(const memcached_st *, memcached_stat_st *memc_stat,
                                const char *key, memcached_return_t *error)
 {
+  memcached_return_t not_used;
+  if (error == NULL)
+  {
+    error= &not_used;
+  }
+
+  if (memc_stat == NULL)
+  {
+    *error= MEMCACHED_INVALID_ARGUMENTS;
+    return NULL;
+  }
+
   char buffer[SMALL_STRING_LEN];
   int length;
-  char *ret;
 
   *error= MEMCACHED_SUCCESS;
 
@@ -336,7 +347,8 @@ char *memcached_stat_get_value(const memcached_st *ptr, memcached_stat_st *memc_
     return NULL;
   }
 
-  ret= static_cast<char *>(libmemcached_malloc(ptr, (size_t) (length + 1)));
+  // User is responsible for free() memory, so use malloc()
+  char *ret= static_cast<char *>(malloc(size_t(length +1)));
   memcpy(ret, buffer, (size_t) length);
   ret[length]= '\0';
 
@@ -502,8 +514,9 @@ static memcached_return_t ascii_stats_fetch(memcached_stat_st *memc_stat,
   return rc;
 }
 
-memcached_stat_st *memcached_stat(memcached_st *self, char *args, memcached_return_t *error)
+memcached_stat_st *memcached_stat(memcached_st *shell, char *args, memcached_return_t *error)
 {
+  Memcached* self= memcached2Memcached(shell);
   memcached_return_t unused;
   if (error == NULL)
   {
@@ -640,10 +653,11 @@ memcached_return_t memcached_stat_servername(memcached_stat_st *memc_stat, char
   We make a copy of the keys since at some point in the not so distant future
   we will add support for "found" keys.
 */
-char ** memcached_stat_get_keys(memcached_st *memc,
+char ** memcached_stat_get_keys(memcached_st *shell,
                                 memcached_stat_st *,
                                 memcached_return_t *error)
 {
+  Memcached* memc= memcached2Memcached(shell);
   if (memc)
   {
     char **list= static_cast<char **>(libmemcached_malloc(memc, sizeof(memcached_stat_keys)));
@@ -700,8 +714,9 @@ static memcached_return_t call_stat_fn(memcached_st *memc,
   return MEMCACHED_INVALID_ARGUMENTS;
 }
 
-memcached_return_t memcached_stat_execute(memcached_st *memc, const char *args,  memcached_stat_fn func, void *context)
+memcached_return_t memcached_stat_execute(memcached_st *shell, const char *args,  memcached_stat_fn func, void *context)
 {
+  Memcached* memc= memcached2Memcached(shell);
   if (memcached_fatal(memcached_version(memc)))
   {
     return memcached_last_error(memc);