Hi,

let's see if I get it right this time. libev maps allocation, reallocation, and freeing of memory to calls of one single function, set by ev_set_allocator(), or the default ev_realloc_emul(). The libev API specification mandates that the allocator function fulfils the requirements of realloc() as defined by C89.

Unfortunately, that is not enough to avoid memory leaks, for a call of the form realloc(ptr, 0) is not required to have the same semantics as a call to free(ptr). A C89-conformant implementation may return a unique non-NULL pointer which may be safely passed to free(). Therefore, the implementation is required to keep track of these unique pointers internally, whereas it is permitted to have forgotten pointers passed to free() once free() has returned.

The attached patch changes ev_realloc_emul() so that ev_realloc_emul(ptr, 0) has free(ptr) semantics in all cases.

Best regards,
Alexander

--
Dr. Alexander Klauer
Competence Centre for High Performance Computing

Fraunhofer-Institut für Techno-
und Wirtschaftsmathematik ITWM
Fraunhofer-Platz 1
67663 Kaiserslautern
Tel.: +49 631 31600-4335
Fax : +49 631 31600-5335
Email: [email protected]

--- libev-4.11-old/ev.c	2012-02-11 00:02:12.000000000 +0100
+++ libev-4.11/ev.c	2013-02-20 16:53:54.332938227 +0100
@@ -1134,9 +1134,6 @@
 static void *
 ev_realloc_emul (void *ptr, long size)
 {
-#if __GLIBC__
-  return realloc (ptr, size);
-#else
   /* some systems, notably openbsd and darwin, fail to properly
    * implement realloc (x, 0) (as required by both ansi c-89 and
    * the single unix specification, so work around them here.
@@ -1147,7 +1144,6 @@
 
   free (ptr);
   return 0;
-#endif
 }
 
 static void *(*alloc)(void *ptr, long size) = ev_realloc_emul;
_______________________________________________
libev mailing list
[email protected]
http://lists.schmorp.de/cgi-bin/mailman/listinfo/libev

Reply via email to