Re: gwmem patches

"Nikos Balkanas" <[email protected]>
Newsgroups gmane.comp.mobile.kannel.devel
Message-ID <001a01c9b695$a1015050$02b2a8c0@tardis>
Hi,

Here it goes. gw_calloc + gw_strdup.

BR,
Nikos
  ----- Original Message ----- 
  From: Alexander Malysh 
  To: Andreas Fink 
  Cc: Nikos Balkanas ; [email protected] 
  Sent: Monday, April 06, 2009 10:26 AM
  Subject: Re: gwmem patches


  Hi Nikos,


  we have at least one +1 so please repost your patch.


  Thanks,
  Alex


  Am 03.04.2009 um 17:37 schrieb Andreas Fink:


    I'm +1 on gw_calloc. There's a few times I would have used it in the past so its nice to have it in gwlib.


    I see very little optimisation of strdup except we do a strlen twice (once explicit in the malloc line an done implicity in the strcpy) but it is never bad to save a few microseconds.. (who knows one might call this one a gazillion times...). so +1 for that one too.




    On 02.04.2009, at 23:21, Alexander Malysh wrote:


      Hi,


      thanks for your patch but:


      1) thread.h patch was wrong. I fixed it in CVS.
      2) gw_strdup optimisation looks OK, please submit as extra patch
      3) gw_calloc, hmm... I don't really see any advantage of this one. because
      x = gw_malloc(count*size);
      memset(x, 0);
          do the same. why do we need this?


      Thanks,
      Alex


      Am 02.04.2009 um 19:09 schrieb Nikos Balkanas:


        Hi,

        An assortment of small patches to make check_memory_leaks work better:

        1) Added support for gw_calloc, which is #undefed but not defined
        2) Replaced strcpy with memcpy in gw_strdup for better efficiency
        3) Added function prototype in thread.h for mutex_make_measured so that MUTEX_STATS compile correctly.

        Please decide and vote.

        BR,
        Nikos<kannel.diff>
kannel.diff (application/octet-stream, 4.6 KB)
Index: gwlib/gwmem.h
===================================================================
RCS file: /home/cvs/gateway/gwlib/gwmem.h,v
retrieving revision 1.28
diff -a -u -r1.28 gwmem.h
--- gwlib/gwmem.h	12 Jan 2009 16:46:53 -0000	1.28
+++ gwlib/gwmem.h	6 Apr 2009 08:53:46 -0000
@@ -76,6 +76,7 @@
 void gw_native_init(void);
 void gw_native_check_leaks(void);
 void *gw_native_malloc(size_t size);
+void *gw_native_calloc(int nmemb, size_t size);
 void *gw_native_realloc(void *ptr, size_t size);
 void gw_native_free(void *ptr);
 char *gw_native_strdup(const char *str);
@@ -86,6 +87,8 @@
 void gw_check_check_leaks(void);
 void *gw_check_malloc(size_t size, 
 	const char *filename, long line, const char *function);
+void *gw_check_calloc(int nmemb, size_t size, 
+	const char *filename, long line, const char *function);
 void *gw_check_realloc(void *p, size_t size, 
 	const char *filename, long line, const char *function);
 void  gw_check_free(void *p, 
@@ -118,6 +121,7 @@
 #define gw_check_leaks()
 #define gw_malloc(size) (gw_native_malloc(size))
 #define gw_malloc_trace(size, file, line, func) (gw_native_malloc(size))
+#define gw_calloc(nmemb, size) (gw_native_calloc(nmemb, size))
 #define gw_realloc(ptr, size) (gw_native_realloc(ptr, size))
 #define gw_free(ptr) (gw_native_free(ptr))
 #define gw_strdup(str) (gw_native_strdup(str))
@@ -146,6 +150,8 @@
 	(gw_check_malloc(size, file, line, func))
 #define gw_malloc(size) \
 	(gw_check_malloc(size, __FILE__, __LINE__, __func__))
+#define gw_calloc(nmemb, size) \
+	(gw_check_malloc(nmemb, size, __FILE__, __LINE__, __func__))
 #define gw_realloc(ptr, size) \
 	(gw_check_realloc(ptr, size, __FILE__, __LINE__, __func__))
 #define gw_free(ptr) \
@@ -179,6 +185,7 @@
 #define calloc(a, b)	do_not_use_calloc
 #define realloc(p, n)	do_not_call_realloc_directly
 #define free(p)	    	do_not_call_free_directly
+#define strdup(p)	    	do_not_call_strdup_directly
 
 
 #endif
Index: gwlib/gwmem-native.c
===================================================================
RCS file: /home/cvs/gateway/gwlib/gwmem-native.c,v
retrieving revision 1.14
diff -a -u -r1.14 gwmem-native.c
--- gwlib/gwmem-native.c	12 Jan 2009 16:46:54 -0000	1.14
+++ gwlib/gwmem-native.c	6 Apr 2009 08:53:46 -0000
@@ -71,6 +71,7 @@
  * accident protectors. 
  */
 #undef malloc
+#undef calloc
 #undef realloc
 #undef free
 
@@ -90,6 +91,20 @@
     return ptr;
 }
 
+void *gw_native_calloc(int nmemb, size_t size)
+{
+    void *ptr;
+
+    /* ANSI C89 says malloc(0) is implementation-defined.  Avoid it. */
+    gw_assert(size > 0);
+    gw_assert(nmemb > 0);
+
+    ptr = calloc(nmemb, size);
+    if (ptr == NULL)
+        panic(errno, "Memory allocation failed");
+
+    return ptr;
+}
 
 void *gw_native_realloc(void *ptr, size_t size)
 {
@@ -114,10 +129,12 @@
 char *gw_native_strdup(const char *str)
 {
     char *copy;
+    int size;
 
     gw_assert(str != NULL);
+    size = strlen(str) + 1;
 
-    copy = gw_native_malloc(strlen(str) + 1);
-    strcpy(copy, str);
+    copy = gw_native_malloc(size);
+    memcpy(copy, str, size);
     return copy;
 }
Index: gwlib/gwmem-check.c
===================================================================
RCS file: /home/cvs/gateway/gwlib/gwmem-check.c,v
retrieving revision 1.34
diff -a -u -r1.34 gwmem-check.c
--- gwlib/gwmem-check.c	12 Jan 2009 16:46:53 -0000	1.34
+++ gwlib/gwmem-check.c	6 Apr 2009 08:53:48 -0000
@@ -103,6 +103,7 @@
  * accident protectors. */
 #undef malloc
 #undef realloc
+#undef calloc
 #undef free
 
 /* Freshly malloced space is filled with NEW_AREA_PATTERN, to break
@@ -533,6 +534,29 @@
     return p;
 }
 
+void *gw_check_calloc(int nmemb, size_t size, const char *filename, long lineno,
+                      const char *function)
+{
+    unsigned char *p;
+
+    gw_assert(initialized);
+
+    /* ANSI C89 says malloc(0) is implementation-defined.  Avoid it. */
+    gw_assert(size > 0);
+
+    p = calloc(1, (nmemb*size) + 2 * MARKER_SIZE);
+    if (p == NULL)
+        panic(errno, "Memory allocation of %ld bytes failed.", (long)size);
+
+    p += MARKER_SIZE;
+
+    lock();
+    record_allocation(p, size, filename, lineno, function);
+    unlock();
+
+    return p;
+}
+
 void *gw_check_realloc(void *p, size_t size, const char *filename,
                        long lineno, const char *function)
 {
@@ -615,12 +639,14 @@
                       const char *function)
 {
     char *copy;
+    int size;
 
     gw_assert(initialized);
     gw_assert(str != NULL);
 
-    copy = gw_check_malloc(strlen(str) + 1, filename, lineno, function);
-    strcpy(copy, str);
+    size = strlen(str) + 1;
+    copy = gw_check_malloc(size, filename, lineno, function);
+    memcpy(copy, str, size);
     return copy;
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.