<div dir="ltr">Excellent!<div><br>Thanks for this,</div><div><br>Peter</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On 24 October 2013 12:20, Charles Chance <span dir="ltr"><<a href="mailto:charles.chance@sipcentric.com" target="_blank">charles.chance@sipcentric.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Module: sip-router<br>
Branch: master<br>
Commit: adfa299a1a01aba1c69c1129d78170056d50db42<br>
URL:    <a href="http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=adfa299a1a01aba1c69c1129d78170056d50db42" target="_blank">http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=adfa299a1a01aba1c69c1129d78170056d50db42</a><br>

<br>
Author: Charles Chance <<a href="mailto:charles.chance@sipcentric.com">charles.chance@sipcentric.com</a>><br>
Committer: Charles Chance <<a href="mailto:charles.chance@sipcentric.com">charles.chance@sipcentric.com</a>><br>
Date:   Thu Oct 24 12:14:38 2013 +0100<br>
<br>
memcached: added alternate memory management wrappers for backwards compatibility with older libmemcached versions and added preprocessor check for the correct ones to use based on installed version.<br>
<br>
---<br>
<br>
 modules/memcached/memcached.c |   82 +++++++++++++++++++++++++++++++++++------<br>
 1 files changed, 70 insertions(+), 12 deletions(-)<br>
<br>
diff --git a/modules/memcached/memcached.c b/modules/memcached/memcached.c<br>
index 431cf4e..0815d63 100644<br>
--- a/modules/memcached/memcached.c<br>
+++ b/modules/memcached/memcached.c<br>
@@ -106,7 +106,7 @@ struct module_exports exports = {<br>
<br>
<br>
 /*!<br>
- * \brief Wrapper functions around our internal memory management for libmemcache callback<br>
+ * \brief Wrapper functions around our internal memory management for libmemcached (version >= 0.38) callback<br>
  * \param mem freed memory<br>
  * \note pkg_free does not allow NULL pointer as standard free, therefore we check it here<br>
  * \see pkg_free<br>
@@ -116,9 +116,20 @@ static inline void mcd_free(memcached_st *ptr, void *mem, void *context) {<br>
                pkg_free(mem);<br>
 }<br>
<br>
+/*!<br>
+ * \brief Wrapper functions around our internal memory management for libmemcached (version < 0.38) callback<br>
+ * \param mem freed memory<br>
+ * \note pkg_free does not allow NULL pointer as standard free, therefore we check it here<br>
+ * \see pkg_free<br>
+ */<br>
+ static inline void mcd_free_compat(memcached_st *ptr, void *mem) {<br>
+        if (mem)<br>
+                pkg_free(mem);<br>
+}<br>
+<br>
<br>
 /*!<br>
- * \brief Wrapper functions around our internal memory management for libmemcache callback<br>
+ * \brief Wrapper functions around our internal memory management for libmemcached (version >= 0.38) callback<br>
  * \param size allocated size<br>
  * \return allocated memory, or NULL on failure<br>
  * \see pkg_malloc<br>
@@ -127,9 +138,19 @@ static inline void* mcd_malloc(memcached_st *ptr, const size_t size, void *conte<br>
        return pkg_malloc(size);<br>
 }<br>
<br>
+/*!<br>
+ * \brief Wrapper functions around our internal memory management for libmemcached (version < 0.38) callback<br>
+ * \param size allocated size<br>
+ * \return allocated memory, or NULL on failure<br>
+ * \see pkg_malloc<br>
+ */<br>
+ static inline void* mcd_malloc_compat(memcached_st *ptr, const size_t size) {<br>
+        return pkg_malloc(size);<br>
+}<br>
+<br>
<br>
 /*!<br>
- * \brief Wrapper functions around our internal memory management for libmemcache callback<br>
+ * \brief Wrapper functions around our internal memory management for libmemcached (version >= 0.38) callback<br>
  * \param mem pointer to allocated memory<br>
  * \param size new size of memory area<br>
  * \return allocated memory, or NULL on failure<br>
@@ -139,9 +160,20 @@ static inline void* mcd_realloc(memcached_st *ptr, void *mem, const size_t size,<br>
        return pkg_realloc(mem, size);<br>
 }<br>
<br>
+/*!<br>
+ * \brief Wrapper functions around our internal memory management for libmemcached (version < 0.38) callback<br>
+ * \param mem pointer to allocated memory<br>
+ * \param size new size of memory area<br>
+ * \return allocated memory, or NULL on failure<br>
+ * \see pkg_realloc<br>
+ */<br>
+static inline void* mcd_realloc_compat(memcached_st *ptr, void *mem, const size_t size) {<br>
+        return pkg_realloc(mem, size);<br>
+}<br>
+<br>
<br>
 /*!<br>
- * \brief Wrapper functions around our internal memory management for libmemcache callback<br>
+ * \brief Wrapper functions around our internal memory management for libmemcached (version >= 0.38) callback<br>
  * \param mem pointer to allocated memory<br>
  * \param size new size of memory area<br>
  * \return allocated memory, or NULL on failure<br>
@@ -157,6 +189,24 @@ static inline void * mcd_calloc(memcached_st *ptr, size_t nelem, const size_t el<br>
        return tmp;<br>
 }<br>
<br>
+/*!<br>
+ * \brief Wrapper functions around our internal memory management for libmemcached (version < 0.38) callback<br>
+ * \param mem pointer to allocated memory<br>
+ * \param size new size of memory area<br>
+ * \return allocated memory, or NULL on failure<br>
+ * \see pkg_malloc<br>
+ * \todo this is not optimal,  use internal calloc implemention which is not exported yet<br>
+ */<br>
+static inline void * mcd_calloc_compat(memcached_st *ptr, size_t nelem, const size_t elsize) {<br>
+        void* tmp = NULL;<br>
+        tmp = pkg_malloc(nelem * elsize);<br>
+        if (tmp != NULL) {<br>
+                memset(tmp, 0, nelem * elsize);<br>
+        }<br>
+        return tmp;<br>
+}<br>
+<br>
+<br>
 /**<br>
  * \brief Callback to check if we could connect successfully to a server<br>
  * \param ptr memcached handler<br>
@@ -206,25 +256,33 @@ static int mod_init(void) {<br>
        }<br>
        LM_DBG("allocated new server handle at %p", memcached_h);<br>
<br>
-       if (mcd_memory == 1) {<br>
-               LM_INFO("Use internal kamailio memory manager for memcached client library");<br>
-               rc = memcached_set_memory_allocators(memcached_h, (memcached_malloc_fn)mcd_malloc,<br>
-                                            (memcached_free_fn)mcd_free, (memcached_realloc_fn)mcd_realloc,<br>
-                                            (memcached_calloc_fn)mcd_calloc, NULL);<br>
+        if (mcd_memory == 1) {<br>
+                LM_INFO("Use internal kamailio memory manager for memcached client library\n");<br>
+<br>
+#if LIBMEMCACHED_VERSION_HEX >= 0x00038000<br>
+                rc = memcached_set_memory_allocators(memcached_h, (memcached_malloc_fn)mcd_malloc,<br>
+                                             (memcached_free_fn)mcd_free, (memcached_realloc_fn)mcd_realloc,<br>
+                                             (memcached_calloc_fn)mcd_calloc, NULL);<br>
+#else<br>
+                rc = memcached_set_memory_allocators(memcached_h, (memcached_malloc_function)mcd_malloc_compat,<br>
+                                             (memcached_free_function)mcd_free_compat, (memcached_realloc_function)mcd_realloc_compat,<br>
+                                             (memcached_calloc_function)mcd_calloc_compat);<br>
+#endif<br>
+<br>
                if (rc == MEMCACHED_SUCCESS) {<br>
-                       LM_DBG("memory manager callbacks set");<br>
+                       LM_DBG("memory manager callbacks set\n");<br>
                } else {<br>
                        LM_ERR("memory manager callbacks not set, returned %s.\n", memcached_strerror(memcached_h, rc));<br>
                        return -1;<br>
                }<br>
        } else {<br>
-               LM_INFO("Use system memory manager for memcached client library");<br>
+               LM_INFO("Use system memory manager for memcached client library\n");<br>
        }<br>
<br>
         servers = memcached_server_list_append(servers, server, atoi(port), &rc);<br>
<br>
        if (memcached_behavior_set(memcached_h, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, mcd_timeout) != MEMCACHED_SUCCESS) {<br>
-               LM_ERR("could not set server connection timeout");<br>
+               LM_ERR("could not set server connection timeout\n");<br>
                return -1;<br>
        }<br>
        rc = memcached_server_push(memcached_h, servers);<br>
<br>
<br>
_______________________________________________<br>
sr-dev mailing list<br>
<a href="mailto:sr-dev@lists.sip-router.org">sr-dev@lists.sip-router.org</a><br>
<a href="http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-dev" target="_blank">http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-dev</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div dir="ltr"><div><font face="courier new, monospace">Peter Dunkley</font></div><div><font face="courier new, monospace">Technical Director</font></div><div>
<font face="courier new, monospace">Crocodile RCS Ltd</font></div></div>
</div>