[LIBART] art_alloc/art_free macros



Hi,

While fiddeling around with the Mozilla SVG buildsystem, I've encountered a
problem with libart's memory management macros art_alloc, art_free, etc,
when using libart as a DLL.
In Mozilla SVG we frequently take BPaths and other objects that were
allocated by some libart function, and free them with a call to art_free.
(Conversely, we also allocate paths using art_malloc and pass them into
libart functions to be freed there). Since art_malloc/art_free are macros,
what actually gets executed when we call them from Mozilla SVG code, are the
versions of malloc/free that are compiled into Mozilla. When executed from
libart, the versions compiled into libart get called.
The problem is that Visual C++ links an executable or DLL to different
versions of the C runtime library, depending on whether they are compiled as
'debug' or 'release'. The two runtime libaries have different
implementations of malloc/free. This means that if e.g. a 'debug' libart is
matched up with 'release' Mozilla build, we get heap corruption. This is not
a very good situation, of course. We shouldn't need to match up compile
settings (or even compilers!) between a library and client programs.

As an ugly (but low-impact) fix to this problem, can I suggest adding memory
management *functions* to libart. These will always call versions of
malloc/free compiled into libart, no matter where they are called from.
In this way, programs that link libart statically can use the macros and
dynamically linking programs can use the safe versions (with whatever
performance-penalty that might incur) :

Index: art_misc.h
===================================================================
RCS file: /cvs/gnome/libart_lgpl/art_misc.h,v
retrieving revision 1.8
diff -u -r1.8 art_misc.h
--- art_misc.h	2001/10/02 00:20:15	1.8
+++ art_misc.h	2001/10/04 07:23:59
@@ -78,6 +78,10 @@
 extern "C" {
 #endif

+void *art_alloc_safe(size_t size);
+void *art_realloc_safe(void *memblock, size_t size);
+void art_free_safe(void *memblock);
+
 void ART_GNUC_NORETURN
 art_die (const char *fmt, ...) ART_GNUC_PRINTF (1, 2);

Index: art_misc.c
===================================================================
RCS file: /cvs/gnome/libart_lgpl/art_misc.c,v
retrieving revision 1.5
diff -u -r1.5 art_misc.c
--- art_misc.c	2001/02/25 16:27:08	1.5
+++ art_misc.c	2001/10/04 07:23:59
@@ -27,6 +27,23 @@
 #include <stdarg.h>
 #include "art_misc.h"

+
+void *art_alloc_safe(size_t size)
+{
+  return art_alloc(size);
+}
+
+void *art_realloc_safe(void *memblock, size_t size)
+{
+  return art_realloc(memblock, size);
+}
+
+void art_free_safe(void *memblock)
+{
+  art_free(memblock);
+}
+
+
 /**
  * art_die: Print the error message to stderr and exit with a return code
of 1.
  * @fmt: The printf-style format for the error message.


Any chance of getting something like this into the libart tree?

Alex


--------------------------------------------------------------
 alex fritze
 crocodile clips ltd
 11 randolph place        [e] alex fritze crocodile-clips com
 edinburgh                [t] +44 (0) 131 226 3263
 eh3 7ta                  [f] +44 (0) 131 226 1522
 scotland
--------------------------------------------------------------





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]