Merge Trond
authorBrian Aker <brian@gaz>
Sun, 14 Jun 2009 15:24:05 +0000 (08:24 -0700)
committerBrian Aker <brian@gaz>
Sun, 14 Jun 2009 15:24:05 +0000 (08:24 -0700)
20 files changed:
.bzrignore
ChangeLog
docs/Makefile.am
docs/libmemcached.pod
docs/memcached_callback.pod
docs/memcached_memory_allocators.pod [new file with mode: 0644]
docs/memcached_user_data.pod [new file with mode: 0644]
libmemcached/Makefile.am
libmemcached/memcached.c
libmemcached/memcached.h
libmemcached/memcached_allocators.c [new file with mode: 0644]
libmemcached/memcached_analyze.c
libmemcached/memcached_hosts.c
libmemcached/memcached_internal.h [new file with mode: 0644]
libmemcached/memcached_result.c
libmemcached/memcached_server.c
libmemcached/memcached_stats.c
libmemcached/memcached_string.c
libmemcached/memcached_types.h
tests/function.c

index 7cb02a8e50b61c6785f92a97e870e6a1f4b9b02f..4b8966e156676baddbf3da7be4120f04b7244168 100644 (file)
@@ -95,6 +95,10 @@ docs/memflush.1
 docs/memrm.1
 docs/memslap.1
 docs/memstat.1
+docs/memcached_set_memory_allocators.3
+docs/memcached_get_memory_allocators.3
+docs/memcached_set_user_data.3
+docs/memcached_get_user_data.3
 libmemcached/.deps
 libmemcached/.libs
 libmemcached/Makefile
index 70cddaff32d3d2a5611c2e5df5ff676b12492b87..a972319da5ac2e425337c349758243155a8cc3f1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,6 @@
+  * Added support for memcached_set_memory_allocators().
+  * Fixed bug in configure.ac for have_htoll.
+
 0.30 Sun May 31 17:31:52 PDT 2009
   * Added memcachd_dump command (and framework for memdump tool).
   * Realigned all structures to remove padding (and line up important bits for 64bit caches.
index 1ac9b2fae2cc3aff3922dd6a63461029162319e8..fa2d338325f8ac70e0be49a217a5a447788477a9 100644 (file)
@@ -31,7 +31,9 @@ EXTRA_DIST = libmemcached.pod\
        memdump.pod\
        memcached_flush_buffers.pod\
         memcached_analyze.pod\
-        memcached_generate_hash_value.pod
+        memcached_generate_hash_value.pod\
+        memcached_memory_allocators.pod\
+        memory_user_data.pod
 
 man_MANS = libmemcached.3\
        libmemcached_examples.3\
@@ -95,7 +97,11 @@ man_MANS = libmemcached.3\
        memcached_version.3\
         memcached_flush_buffers.3\
         memcached_dump.3\
-        memcached_generate_hash_value.3
+        memcached_generate_hash_value.3\
+        memcached_set_memory_allocators.3\
+        memcached_get_memory_allocators.3\
+        memcached_set_user_data.3\
+        memcached_get_user_data.3
 
 if BUILD_LIBMEMCACHEDUTIL
 man_MANS+= libmemcachedutil.3 \
@@ -307,6 +313,18 @@ memcached_analyze.3: memcached_analyze.pod
 memcached_generate_hash_value.3: memcached_generate_hash_value.pod
        @POD2MAN@ -c "libmemcached" -r "" -s 3 ${top_srcdir}/docs/memcached_generate_hash_value.pod > memcached_generate_hash_value.3
 
+memcached_get_memory_allocators.3: memcached_memory_allocators.pod
+       @POD2MAN@ -c "libmemcached" -r "" -s 3 ${top_srcdir}/docs/memcached_memory_allocators.pod > memcached_get_memory_allocators.3
+
+memcached_set_memory_allocators.3: memcached_memory_allocators.pod
+       @POD2MAN@ -c "libmemcached" -r "" -s 3 ${top_srcdir}/docs/memcached_memory_allocators.pod > memcached_set_memory_allocators.3
+
+memcached_get_user_data.3: memcached_user_data.pod
+       @POD2MAN@ -c "libmemcached" -r "" -s 3 ${top_srcdir}/docs/memcached_user_data.pod > memcached_get_user_data.3
+
+memcached_set_user_data.3: memcached_user_data.pod
+       @POD2MAN@ -c "libmemcached" -r "" -s 3 ${top_srcdir}/docs/memcached_user_data.pod > memcached_set_user_data.3
+
 memcached_pool_create.3: memcached_pool.pod 
        @POD2MAN@ -c "libmemcachedutil" -r "" -s 3 ${top_srcdir}/docs/memcached_pool.pod > memcached_pool_create.3
 
@@ -370,6 +388,8 @@ test:
        podchecker $(top_srcdir)/docs/memflush.pod
        podchecker $(top_srcdir)/docs/memcached_flush_buffers.pod
        podchecker $(top_srcdir)/docs/memcached_pool.pod
+       podchecker $(top_srcdir)/docs/memcached_memory_allocators.pod
+       podchecker $(top_srcdir)/docs/memcached_user_data.pod
 
 html-local:
        pod2htmltree "/libmemcached" .
index 2ee593a8c8fb23d3caaa29e4fd1a6d0a69d27d59..e1a95a73eebdd34bfcf69a8b5e355c37f75437be 100644 (file)
@@ -121,7 +121,7 @@ Brian Aker, E<lt>brian@tangent.orgE<gt>
 
 =head1 SEE ALSO
 
-memcached(1) libmemcached_examples(3) libmemcached(1) memcat(1) memcp(1) memflush(1) memrm(1) memslap(1) memstat(1) memcached_fetch(3) memcached_replace(3) memcached_server_list_free(3) libmemcached_examples(3) memcached_clone(3) memcached_free(3) memcached_server_add(3) memcached_server_push(3) memcached_add(3) memcached_get(3) memcached_server_count(3) memcached_servers_parse(3) memcached_create(3) memcached_increment(3) memcached_server_list(3) memcached_set(3) memcached_decrement(3) memcached_mget(3) memcached_server_list_append(3) memcached_strerror(3) memcached_delete(3) memcached_quit(3) memcached_server_list_count(3) memcached_verbosity(3) memcached_server_add_unix_socket(3) memcached_result_create(3)  memcached_result_free(3)  memcached_result_key_value(3)  memcached_result_key_length(3)  memcached_result_value(3)  memcached_result_length(3)  memcached_result_flags(3)  memcached_result_cas(3) memcached_result_st(3) memcached_append(3) memcached_prepend(3) memcached_fetch_result(3) memerror(1) memcached_get_by_key(3) memcached_mget_by_key(3) memcached_delete_by_key(3) memcached_fetch_execute(3) memcached_callback_get(3) memcached_callback_set(3) memcached_version(3) memcached_lib_version(3) memcached_result_set_value(3) memcached_dump(3) memdump(1)
+memcached(1) libmemcached_examples(3) libmemcached(1) memcat(1) memcp(1) memflush(1) memrm(1) memslap(1) memstat(1) memcached_fetch(3) memcached_replace(3) memcached_server_list_free(3) libmemcached_examples(3) memcached_clone(3) memcached_free(3) memcached_server_add(3) memcached_server_push(3) memcached_add(3) memcached_get(3) memcached_server_count(3) memcached_servers_parse(3) memcached_create(3) memcached_increment(3) memcached_server_list(3) memcached_set(3) memcached_decrement(3) memcached_mget(3) memcached_server_list_append(3) memcached_strerror(3) memcached_delete(3) memcached_quit(3) memcached_server_list_count(3) memcached_verbosity(3) memcached_server_add_unix_socket(3) memcached_result_create(3)  memcached_result_free(3)  memcached_result_key_value(3)  memcached_result_key_length(3)  memcached_result_value(3)  memcached_result_length(3)  memcached_result_flags(3)  memcached_result_cas(3) memcached_result_st(3) memcached_append(3) memcached_prepend(3) memcached_fetch_result(3) memerror(1) memcached_get_by_key(3) memcached_mget_by_key(3) memcached_delete_by_key(3) memcached_fetch_execute(3) memcached_callback_get(3) memcached_callback_set(3) memcached_version(3) memcached_lib_version(3) memcached_result_set_value(3) memcached_dump(3) memdump(1) memcached_set_memory_allocators(3) memcached_get_memory_allocators(3) memcached_get_user_data(3) memcached_set_user_data(3)
 
 =cut
 
index 100aff189305f6d0f75457e3f747e4c41dd10aaf..6eba4f3263080ed910bba2ebeb00324c2dca7a63 100644 (file)
@@ -64,25 +64,15 @@ will copy the pointer to the clone.
 
 =item  MEMCACHED_CALLBACK_MALLOC_FUNCTION
 
-This alllows yout to pass in a customized version of malloc that will be used instead of the builtin malloc(3) call.
-Your malloc must zero all memory.
-The prototype for this is:
-
-void *(*memcached_malloc_function)(memcached_st *ptr, const size_t size);
+DEPRECATED: use memcached_set_memory_allocators instead.
 
 =item  MEMCACHED_CALLBACK_REALLOC_FUNCTION
 
-This alllows yout to pass in a customized version of realloc that will be used instead of the builtin realloc(3) call.
-The prototype for this is:
-
-void *(*memcached_realloc_function)(memcached_st *ptr, void *mem, const size_t size);
+DEPRECATED: use memcached_set_memory_allocators instead.
 
 =item  MEMCACHED_CALLBACK_FREE_FUNCTION
 
-This alllows yout to pass in a customized version of realloc that will be used instead of the builtin free(3) call.
-The prototype for this is:
-
-typedef void (*memcached_free_function)(memcached_st *ptr, void *mem);
+DEPRECATED: use memcached_set_memory_allocators instead.
 
 =item  MEMCACHED_CALLBACK_GET_FAILURE
 
diff --git a/docs/memcached_memory_allocators.pod b/docs/memcached_memory_allocators.pod
new file mode 100644 (file)
index 0000000..5d6d00a
--- /dev/null
@@ -0,0 +1,73 @@
+=head1 NAME
+
+memcached_set_memory_allocators, memcached_get_memory_allocators
+
+=head1 LIBRARY
+
+C Client Library for memcached (libmemcached, -lmemcached)
+
+=head1 SYNOPSIS
+
+  #include <memcached.h>
+
+  memcached_return 
+    memcached_set_memory_allocators(memcached_st *ptr,
+                                    memcached_malloc_function mem_malloc,
+                                    memcached_free_function mem_free,
+                                    memcached_realloc_function mem_realloc,
+                                    memcached_calloc_function mem_calloc);
+
+  void memcached_get_memory_allocators(memcached_st *ptr,
+                                       memcached_malloc_function *mem_malloc,
+                                       memcached_free_function *mem_free,
+                                       memcached_realloc_function *mem_realloc,
+                                       memcached_calloc_function *mem_calloc);
+
+  void *(*memcached_malloc_function)(memcached_st *ptr, const size_t size);
+  void *(*memcached_realloc_function)(memcached_st *ptr, void *mem, 
+                                      const size_t size);
+  void  (*memcached_free_function)(memcached_st *ptr, void *mem);
+  void *(*memcached_calloc_function)(memcached_st *ptr, size_t nelem, 
+                                     const size_t elsize);
+
+
+=head1 DESCRIPTION
+
+libmemcached(3) allows you to specify your own memory allocators optimized
+for your application.
+
+memcached_set_memory_allocators() is used to set the memory allocators used
+by the memcached instance specified by ptr. Please note that you cannot
+override only one of the memory allocators, you have to specify a complete
+new set if you want to override one of them. All of the memory allocation
+functions should behave as specified in the C99 standard. Specify NULL as
+all functions to reset them to the default values.
+
+memcached_get_memory_allocators() is used to get the currently used memory
+allocators by a mamcached handle.
+
+The first argument to the memory allocator functions is a pointer to a
+memcached structure, and you may use the memcached_set_user_data() and
+memcached_get_user_data() to store a user-specific value to each memcached
+structure.
+
+=head1 RETURN
+
+memcached_set_memory_allocators() return MEMCACHED_SUCCESS upon success, 
+and MEMCACHED_FAILURE if you don't pass a complete set of function pointers.
+
+=head1 HOME
+
+To find out more information please check:
+L<http://tangent.org/552/libmemcached.html>
+
+=head1 AUTHOR
+
+Trond Norbye, E<lt>trond.norbye@sun.comE<gt>
+
+=head1 SEE ALSO
+
+memcached(1) libmemcached(3) memcached_get_user_data(3) memcached_set_user_data(3)
+
+=cut
+
diff --git a/docs/memcached_user_data.pod b/docs/memcached_user_data.pod
new file mode 100644 (file)
index 0000000..d5161a1
--- /dev/null
@@ -0,0 +1,49 @@
+=head1 NAME
+
+memcached_set_user_data, memcached_get_user_data
+
+=head1 LIBRARY
+
+C Client Library for memcached (libmemcached, -lmemcached)
+
+=head1 SYNOPSIS
+
+  #include <memcached.h>
+
+  void *memcached_get_user_data(memcached_st *ptr);
+  void *memcached_set_user_data(memcached_st *ptr, void *data);
+
+=head1 DESCRIPTION
+
+libmemcached(3) allows you to store a pointer to a user specific data inside
+the memcached_st structure. 
+
+memcached_set_user_data() is used to set the user specific data in the
+memcached_st structure.
+
+memcached_get_user_data() is used to retrieve the user specific data in
+the memcached_st structure.
+
+=head1 RETURN
+
+memcached_set_user_data() returns the previous value of the user specific 
+data.
+
+memcached_get_user_data() returns the current value uf the user specific
+data.
+
+=head1 HOME
+
+To find out more information please check:
+L<http://tangent.org/552/libmemcached.html>
+
+=head1 AUTHOR
+
+Trond Norbye, E<lt>trond.norbye@sun.comE<gt>
+
+=head1 SEE ALSO
+
+memcached(1) libmemcached(3)
+
+=cut
+
index be314700e3e333ba0f2685cdc34051e2bc57c320..1e3224c08c565b812943526c814ab862e3bd2ec5 100644 (file)
@@ -9,6 +9,7 @@ DTRACEFLAGS =           @DTRACEFLAGS@
 noinst_HEADERS = libmemcached_probes.h \
                  libmemcached_config.h \
                 memcached_io.h \
+                 memcached_internal.h \
                  common.h \
                  memcached/protocol_binary.h @DTRACE_HEADER@
 
@@ -61,7 +62,8 @@ libmemcached_la_SOURCES = crc.c \
                          memcached_verbosity.c \
                          memcached_version.c \
                          murmur_hash.c \
-                         jenkins_hash.c
+                         jenkins_hash.c \
+                          memcached_allocators.c
 
 if INCLUDE_HSIEH_SRC
 libmemcached_la_SOURCES += hsieh_hash.c
index a93bef7d651512ac7d49c4cf92e260fb200fb866..5991aabb46ced3c685ed7a26fcf0f9a4ce40786e 100644 (file)
@@ -20,6 +20,9 @@ memcached_st *memcached_create(memcached_st *ptr)
   {
     memset(ptr, 0, sizeof(memcached_st));
   }
+
+  memcached_set_memory_allocators(ptr, NULL, NULL, NULL, NULL);
+
   result_ptr= memcached_result_create(ptr, &ptr->result);
   WATCHPOINT_ASSERT(result_ptr);
   ptr->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT;
@@ -45,20 +48,10 @@ void memcached_free(memcached_st *ptr)
     ptr->on_cleanup(ptr);
 
   if (ptr->continuum)
-  {
-    if (ptr->call_free)
-      ptr->call_free(ptr, ptr->continuum);
-    else
-      free(ptr->continuum);
-  }
+    ptr->call_free(ptr, ptr->continuum);
 
   if (ptr->is_allocated)
-  {
-    if (ptr->call_free)
-      ptr->call_free(ptr, ptr);
-    else
-      free(ptr);
-  }
+    ptr->call_free(ptr, ptr);
   else
     memset(ptr, 0, sizeof(memcached_st));
 }
@@ -105,6 +98,7 @@ memcached_st *memcached_clone(memcached_st *clone, memcached_st *source)
   new_clone->call_free= source->call_free;
   new_clone->call_malloc= source->call_malloc;
   new_clone->call_realloc= source->call_realloc;
+  new_clone->call_calloc= source->call_calloc;
   new_clone->get_key_failure= source->get_key_failure;
   new_clone->delete_trigger= source->delete_trigger;
   new_clone->server_failure_limit= source->server_failure_limit;
@@ -142,3 +136,14 @@ memcached_st *memcached_clone(memcached_st *clone, memcached_st *source)
 
   return new_clone;
 }
+void *memcached_get_user_data(memcached_st *ptr)
+{
+  return ptr->user_data;
+}
+
+void *memcached_set_user_data(memcached_st *ptr, void *data)
+{
+  void *ret= ptr->user_data;
+  ptr->user_data= data;
+  return ret;
+}
index 7f2b9a3449ab9f532342d6ca77bdda8d1115b97c..607fb4b6000d7fdbc66fa00a89af47165aef0454 100644 (file)
@@ -116,6 +116,7 @@ struct memcached_st {
   memcached_free_function call_free;
   memcached_malloc_function call_malloc;
   memcached_realloc_function call_realloc;
+  memcached_calloc_function call_calloc;
   memcached_trigger_key get_key_failure;
   memcached_trigger_delete_key delete_trigger;
   char prefix_key[MEMCACHED_PREFIX_KEY_MAX_SIZE];
@@ -230,10 +231,29 @@ void *memcached_callback_get(memcached_st *ptr,
 memcached_return memcached_dump(memcached_st *ptr, memcached_dump_func *function, void *context, uint32_t number_of_callbacks);
 
 
+memcached_return memcached_set_memory_allocators(memcached_st *ptr,
+                                                 memcached_malloc_function mem_malloc,
+                                                 memcached_free_function mem_free,
+                                                 memcached_realloc_function mem_realloc,
+                                                 memcached_calloc_function mem_calloc);
+
+void memcached_get_memory_allocators(memcached_st *ptr,
+                                     memcached_malloc_function *mem_malloc,
+                                     memcached_free_function *mem_free,
+                                     memcached_realloc_function *mem_realloc,
+                                     memcached_calloc_function *mem_calloc);
+
+void *memcached_get_user_data(memcached_st *ptr);
+void *memcached_set_user_data(memcached_st *ptr, void *data);
+
 #ifdef __cplusplus
 }
 #endif
 
 #include <libmemcached/memcached_storage.h>
 
+#ifdef MEMCACHED_INTERNAL
+#include <libmemcached/memcached_internal.h>
+#endif
+
 #endif /* __MEMCACHED_H__ */
diff --git a/libmemcached/memcached_allocators.c b/libmemcached/memcached_allocators.c
new file mode 100644 (file)
index 0000000..3192f9e
--- /dev/null
@@ -0,0 +1,72 @@
+#include "common.h"
+
+void libmemcached_free(memcached_st *ptr __attribute__((unused)), void *mem)
+{
+  free(mem);
+}
+
+void *libmemcached_malloc(memcached_st *ptr __attribute__((unused)), 
+                          size_t size)
+{
+  return malloc(size);
+}
+
+void *libmemcached_realloc(memcached_st *ptr __attribute__((unused)), 
+                           void *mem, size_t size)
+{
+  return realloc(mem, size);
+}
+
+void *libmemcached_calloc(memcached_st *ptr __attribute__((unused)), 
+                           size_t nelem, size_t size)
+{
+  if (ptr->call_malloc != libmemcached_malloc)
+  {
+     void *ret = libmemcached_malloc(ptr, nelem * size);
+     if (ret != NULL) 
+       memset(ret, 0, nelem * size);
+
+     return ret;
+  }
+
+  return calloc(nelem, size);
+}
+
+memcached_return memcached_set_memory_allocators(memcached_st *ptr,
+                                                 memcached_malloc_function mem_malloc,
+                                                 memcached_free_function mem_free,
+                                                 memcached_realloc_function mem_realloc,
+                                                 memcached_calloc_function mem_calloc)
+{
+  /* All should be set, or none should be set */
+  if (mem_malloc == NULL && mem_free == NULL && mem_realloc == NULL && mem_calloc == NULL) 
+  {
+    ptr->call_malloc= libmemcached_malloc;
+    ptr->call_free= libmemcached_free;
+    ptr->call_realloc= libmemcached_realloc;
+    ptr->call_calloc= libmemcached_calloc;
+  }
+  else if (mem_malloc == NULL || mem_free == NULL || mem_realloc == NULL || mem_calloc == NULL)
+    return MEMCACHED_FAILURE;
+  else
+  {
+    ptr->call_malloc= mem_malloc;
+    ptr->call_free= mem_free;
+    ptr->call_realloc= mem_realloc;
+    ptr->call_calloc= mem_calloc;
+  }
+
+  return MEMCACHED_SUCCESS;
+}
+
+void memcached_get_memory_allocators(memcached_st *ptr,
+                                     memcached_malloc_function *mem_malloc,
+                                     memcached_free_function *mem_free,
+                                     memcached_realloc_function *mem_realloc,
+                                     memcached_calloc_function *mem_calloc)
+{
+   *mem_malloc= ptr->call_malloc;
+   *mem_free= ptr->call_free;
+   *mem_realloc= ptr->call_realloc;
+   *mem_calloc= ptr->call_calloc;
+}
index 583d828f012986cbdea937a6da75f34377c077b3..e2c17fda25ef3316958300f5d1bf4d4a48e701fa 100644 (file)
@@ -70,8 +70,9 @@ memcached_analysis_st *memcached_analyze(memcached_st *memc,
   
   *error= MEMCACHED_SUCCESS;
   server_count= memcached_server_count(memc);
-  result= (memcached_analysis_st*)calloc(1, sizeof(memcached_analysis_st)
-                                         * (memc->number_of_hosts));
+  result= (memcached_analysis_st*)calloc(memc->number_of_hosts,
+                                         sizeof(memcached_analysis_st));
+
   if (!result)
   {
     *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
index 88256aedcf0f185cf4200086da701e586a6c6fce..3e8b122bd660960b6aefe77df39e79816faa9c1c 100644 (file)
@@ -68,7 +68,7 @@ void server_list_free(memcached_st *ptr, memcached_server_st *servers)
       servers[x].address_info= NULL;
     }
 
-  if (ptr && ptr->call_free)
+  if (ptr)
     ptr->call_free(ptr, servers);
   else
     free(servers);
@@ -155,12 +155,8 @@ memcached_return update_continuum(memcached_st *ptr)
   {
     memcached_continuum_item_st *new_ptr;
 
-    if (ptr->call_realloc)
-      new_ptr= (memcached_continuum_item_st *)ptr->call_realloc(ptr, ptr->continuum, 
-                                                                sizeof(memcached_continuum_item_st) * (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server);
-    else
-      new_ptr= (memcached_continuum_item_st *)realloc(ptr->continuum, 
-                                                      sizeof(memcached_continuum_item_st) * (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server);
+    new_ptr= ptr->call_realloc(ptr, ptr->continuum, 
+                               sizeof(memcached_continuum_item_st) * (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server);
 
     if (new_ptr == 0)
       return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
@@ -265,14 +261,8 @@ memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *l
     return MEMCACHED_SUCCESS;
 
   count= list[0].count;
-  if (ptr->call_realloc)
-    new_host_list= 
-      (memcached_server_st *)ptr->call_realloc(ptr, ptr->hosts, 
-                                               sizeof(memcached_server_st) * (count + ptr->number_of_hosts));
-  else
-    new_host_list= 
-      (memcached_server_st *)realloc(ptr->hosts, 
-                                     sizeof(memcached_server_st) * (count + ptr->number_of_hosts));
+  new_host_list= ptr->call_realloc(ptr, ptr->hosts, 
+                                   sizeof(memcached_server_st) * (count + ptr->number_of_hosts));
 
   if (!new_host_list)
     return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
@@ -367,12 +357,9 @@ static memcached_return server_add(memcached_st *ptr, const char *hostname,
       || ( (type == MEMCACHED_CONNECTION_UDP) && !(ptr->flags & MEM_USE_UDP) ) )
     return MEMCACHED_INVALID_HOST_PROTOCOL;
   
-  if (ptr->call_realloc)
-    new_host_list= (memcached_server_st *)ptr->call_realloc(ptr, ptr->hosts, 
-                                                            sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
-  else
-    new_host_list= (memcached_server_st *)realloc(ptr->hosts, 
-                                                  sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
+  new_host_list= ptr->call_realloc(ptr, ptr->hosts, 
+                                   sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
+
   if (new_host_list == NULL)
     return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
 
diff --git a/libmemcached/memcached_internal.h b/libmemcached/memcached_internal.h
new file mode 100644 (file)
index 0000000..cda67a2
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Summary: Internal functions used by the library. Not for public use!
+ * Copy: See Copyright for the status of this software.
+ *
+ * Author: Trond Norbye
+ */
+
+#if !defined(MEMCACHED_INTERNAL_H) && defined(MEMCACHED_INTERNAL)
+#define MEMCACHED_INTERNAL_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void libmemcached_free(memcached_st *ptr __attribute__((unused)), void *mem);
+void *libmemcached_malloc(memcached_st *ptr __attribute__((unused)), 
+                          const size_t size);
+void *libmemcached_realloc(memcached_st *ptr __attribute__((unused)), 
+                           void *mem, const size_t size);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* MEMCACHED_INTERNAL_H */
index fac3b8c554d7c794676059ae594d7b8e48747ec5..1462b89c3ef42e446162e44842931800bfbfcba7 100644 (file)
@@ -14,10 +14,7 @@ memcached_result_st *memcached_result_create(memcached_st *memc,
     memset(ptr, 0, sizeof(memcached_result_st));
   else
   {
-    if (memc->call_malloc)
-      ptr= (memcached_result_st *)memc->call_malloc(memc, sizeof(memcached_result_st));
-    else
-      ptr= (memcached_result_st *)calloc(1, sizeof(memcached_result_st));
+    ptr= memc->call_malloc(memc, sizeof(memcached_result_st));
 
     if (ptr == NULL)
       return NULL;
index 7fff6cdb4fdb96aa146bb8e7f6d32a6f9c9b2882..04364f3a3d31bad4b0c5fe607c353c8acb980c55 100644 (file)
@@ -60,12 +60,7 @@ void memcached_server_free(memcached_server_st *ptr)
   }
 
   if (ptr->is_allocated)
-  {
-    if (ptr->root && ptr->root->call_free)
-      ptr->root->call_free(ptr->root, ptr);
-    else
-      free(ptr);
-  }
+    ptr->root->call_free(ptr->root, ptr);
   else
     memset(ptr, 0, sizeof(memcached_server_st));
 }
index d7e3b8dde22dca8e80d97d8b834e896f7bbb0e2e..d52f781a641199d03fe857238cb40f7fa3555a4e 100644 (file)
@@ -216,10 +216,7 @@ char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *stat,
     return NULL;
   }
 
-  if (ptr->call_malloc)
-    ret= ptr->call_malloc(ptr, length + 1);
-  else
-    ret= malloc(length + 1);
+  ret= ptr->call_malloc(ptr, length + 1);
   memcpy(ret, buffer, length);
   ret[length]= '\0';
 
@@ -369,10 +366,7 @@ memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_retur
     return NULL;
   }
 
-  if (ptr->call_malloc)
-    stats= (memcached_stat_st *)ptr->call_malloc(ptr, sizeof(memcached_stat_st)*(ptr->number_of_hosts));
-  else
-    stats= (memcached_stat_st *)calloc(1, sizeof(memcached_stat_st)*(ptr->number_of_hosts));
+  stats= ptr->call_calloc(ptr, ptr->number_of_hosts, sizeof(memcached_stat_st));
 
   if (!stats)
   {
@@ -428,10 +422,7 @@ char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *stat __att
   char **list;
   size_t length= sizeof(memcached_stat_keys);
 
-  if (ptr->call_malloc)
-    list= (char **)ptr->call_malloc(ptr, length);
-  else
-    list= (char **)calloc(1, length);
+  list= ptr->call_malloc(ptr, length);
 
   if (!list)
   {
@@ -454,7 +445,7 @@ void memcached_stat_free(memcached_st *ptr, memcached_stat_st *stat)
     return;
   }
 
-  if (ptr && ptr->call_free)
+  if (ptr)
     ptr->call_free(ptr, stat);
   else
     free(stat);
index 3bac225afe2e5e80ae0aee780bb25aa1edd6e845..c190997b9d3a9273436e310e645805e722db9ac0 100644 (file)
@@ -18,10 +18,7 @@ memcached_return memcached_string_check(memcached_string_st *string, size_t need
     if (new_size < need)
       return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
 
-    if (string->root->call_realloc)
-      new_value= (char *)string->root->call_realloc(string->root, string->string, new_size);
-    else
-      new_value= (char *)realloc(string->string, new_size);
+    new_value= string->root->call_realloc(string->root, string->string, new_size);
 
     if (new_value == NULL)
       return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
@@ -44,10 +41,7 @@ memcached_string_st *memcached_string_create(memcached_st *ptr, memcached_string
     memset(string, 0, sizeof(memcached_string_st));
   else
   {
-    if (ptr->call_malloc)
-      string= (memcached_string_st *)ptr->call_malloc(ptr, sizeof(memcached_string_st));
-    else
-      string= (memcached_string_st *)calloc(1, sizeof(memcached_string_st));
+    string= ptr->call_calloc(ptr, 1, sizeof(memcached_string_st));
 
     if (string == NULL)
       return NULL;
@@ -59,11 +53,7 @@ memcached_string_st *memcached_string_create(memcached_st *ptr, memcached_string
   rc=  memcached_string_check(string, initial_size);
   if (rc != MEMCACHED_SUCCESS)
   {
-    if (ptr->call_free)
-      ptr->call_free(ptr, string);
-    else
-      free(string);
-
+    ptr->call_free(ptr, string);
     return NULL;
   }
 
@@ -115,10 +105,7 @@ char *memcached_string_c_copy(memcached_string_st *string)
   if (memcached_string_length(string) == 0)
     return NULL;
 
-  if (string->root->call_malloc)
-    c_ptr= (char *)string->root->call_malloc(string->root, (memcached_string_length(string)+1) * sizeof(char));
-  else
-    c_ptr= (char *)malloc((memcached_string_length(string)+1) * sizeof(char));
+  c_ptr= string->root->call_malloc(string->root, (memcached_string_length(string)+1) * sizeof(char));
 
   if (c_ptr == NULL)
     return NULL;
@@ -142,20 +129,10 @@ void memcached_string_free(memcached_string_st *ptr)
     return;
 
   if (ptr->string)
-  {
-    if (ptr->root->call_free)
-      ptr->root->call_free(ptr->root, ptr->string);
-    else
-      free(ptr->string);
-  }
+    ptr->root->call_free(ptr->root, ptr->string);
 
   if (ptr->is_allocated)
-  {
-    if (ptr->root->call_free)
-      ptr->root->call_free(ptr->root, ptr);
-    else
-      free(ptr);
-  }
+    ptr->root->call_free(ptr->root, ptr);
   else
     memset(ptr, 0, sizeof(memcached_string_st));
 }
index 538ab4bf15b0a09e9ccdc1f63be3d227d4f222f1..d7bb470ab915d9868df4133e8b7f26260f267294 100644 (file)
@@ -25,6 +25,7 @@ typedef memcached_return (*memcached_cleanup_func)(memcached_st *ptr);
 typedef void (*memcached_free_function)(memcached_st *ptr, void *mem);
 typedef void *(*memcached_malloc_function)(memcached_st *ptr, const size_t size);
 typedef void *(*memcached_realloc_function)(memcached_st *ptr, void *mem, const size_t size);
+typedef void *(*memcached_calloc_function)(memcached_st *ptr, size_t nelem, const size_t elsize);
 typedef memcached_return (*memcached_execute_function)(memcached_st *ptr, memcached_result_st *result, void *context);
 typedef memcached_return (*memcached_server_function)(memcached_st *ptr, memcached_server_st *server, void *context);
 typedef memcached_return (*memcached_trigger_key)(memcached_st *ptr,  
index d15c1a0ce16b5b4155a2d3621cee0d048947a341..99ad776aecc1bc8154c4dc58cca229b885fd925c 100644 (file)
@@ -215,6 +215,7 @@ static test_return  clone_test(memcached_st *memc)
     assert(clone->call_free == memc->call_free);
     assert(clone->call_malloc == memc->call_malloc);
     assert(clone->call_realloc == memc->call_realloc);
+    assert(clone->call_calloc == memc->call_calloc);
     assert(clone->connect_timeout == memc->connect_timeout);
     assert(clone->delete_trigger == memc->delete_trigger);
     assert(clone->distribution == memc->distribution);
@@ -262,6 +263,16 @@ static test_return  clone_test(memcached_st *memc)
   return 0;
 }
 
+static test_return userdata_test(memcached_st *memc)
+{
+  void* foo;
+  assert(memcached_set_user_data(memc, foo) == NULL);
+  assert(memcached_get_user_data(memc) == foo);
+  assert(memcached_set_user_data(memc, NULL) == foo);
+  
+  return TEST_SUCCESS;
+}
+
 static test_return  connection_test(memcached_st *memc)
 {
   memcached_return rc;
@@ -3043,7 +3054,11 @@ static void my_free(memcached_st *ptr __attribute__((unused)), void *mem)
 
 static void *my_malloc(memcached_st *ptr __attribute__((unused)), const size_t size)
 {
-  return calloc(1, size);
+  void *ret= malloc(size);
+  if (ret != NULL)
+    memset(ret, 0xff, size);
+
+  return ret;
 }
 
 static void *my_realloc(memcached_st *ptr __attribute__((unused)), void *mem, const size_t size)
@@ -3051,6 +3066,11 @@ static void *my_realloc(memcached_st *ptr __attribute__((unused)), void *mem, co
   return realloc(mem, size);
 }
 
+static void *my_calloc(memcached_st *ptr __attribute__((unused)), size_t nelem, const size_t size)
+{
+  return calloc(nelem, size);
+}
+
 static memcached_return set_prefix(memcached_st *memc)
 {
   memcached_return rc;
@@ -3118,7 +3138,7 @@ static memcached_return set_prefix(memcached_st *memc)
   return MEMCACHED_SUCCESS;
 }
 
-static memcached_return  set_memory_alloc(memcached_st *memc)
+static memcached_return deprecated_set_memory_alloc(memcached_st *memc)
 {
   void *test_ptr= NULL;
   void *cb_ptr= NULL;
@@ -3160,6 +3180,30 @@ static memcached_return  set_memory_alloc(memcached_st *memc)
     assert(rc == MEMCACHED_SUCCESS);
     assert(test_ptr == cb_ptr);
   }
+  return MEMCACHED_SUCCESS;
+}
+
+static memcached_return set_memory_alloc(memcached_st *memc)
+{
+  memcached_return rc;
+  rc= memcached_set_memory_allocators(memc, NULL, my_free, 
+                                      my_realloc, my_calloc);
+  assert(rc == MEMCACHED_FAILURE);
+
+  rc= memcached_set_memory_allocators(memc, my_malloc, my_free, 
+                                      my_realloc, my_calloc);
+  
+  memcached_malloc_function mem_malloc;
+  memcached_free_function mem_free;
+  memcached_realloc_function mem_realloc;
+  memcached_calloc_function mem_calloc;
+  memcached_get_memory_allocators(memc, &mem_malloc, &mem_free, 
+                                  &mem_realloc, &mem_calloc);   
+
+  assert(mem_malloc == my_malloc);
+  assert(mem_realloc == my_realloc);
+  assert(mem_calloc == my_calloc);
+  assert(mem_free == my_free);
 
   return MEMCACHED_SUCCESS;
 }
@@ -4097,6 +4141,7 @@ test_st tests[] ={
   {"connection_test", 0, connection_test},
   {"callback_test", 0, callback_test},
   {"behavior_test", 0, behavior_test},
+  {"userdata_test", 0, userdata_test},
   {"error", 0, error_test },
   {"set", 0, set_test },
   {"set2", 0, set_test2 },
@@ -4285,6 +4330,7 @@ collection_st collection[] ={
   {"poll_timeout", poll_timeout, 0, tests},
   {"gets", enable_cas, 0, tests},
   {"consistent", enable_consistent, 0, tests},
+  {"deprecated_memory_allocators", deprecated_set_memory_alloc, 0, tests},
   {"memory_allocators", set_memory_alloc, 0, tests},
   {"prefix", set_prefix, 0, tests},
   {"version_1_2_3", check_for_1_2_3, 0, version_1_2_3},