gwmem patches

"Nikos Balkanas" <[email protected]>
Newsgroups gmane.comp.mobile.kannel.devel
Message-ID <003001c9b3b5$c050e7f0$02b2a8c0@tardis>
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 (application/octet-stream, 5.2 KB)
Index: gwlib/thread.h
===================================================================
RCS file: /home/cvs/gateway/gwlib/thread.h,v
retrieving revision 1.27
diff -a -u -r1.27 thread.h
--- gwlib/thread.h	12 Jan 2009 16:46:52 -0000	1.27
+++ gwlib/thread.h	2 Apr 2009 17:05:26 -0000
@@ -110,6 +110,7 @@
  * "protected".
  */
 #ifdef MUTEX_STATS
+    Mutex *mutex_make_measured(Mutex *mutex, unsigned char *filename, int lineno);
 #define mutex_init_static(mutex) \
     mutex_make_measured(mutex_init_static_real(mutex), __FILE__, __LINE__)
 #else
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	2 Apr 2009 17:05:26 -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	2 Apr 2009 17:05:26 -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	2 Apr 2009 17:05:27 -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.