Gents,

Part 2 of the changes fed back from my experiments with making
Ghotscript work with lcms v2.

As far as I can see, there exists no way within the current API to
avoid the memory functions using malloc and free (unless you are
implementing a plugin, which we are not).

Lcms v2 appears to have gained some changes in this area over v1 that
are, I think, intended to allow different plugins to use different
families of allocation functions.

As far as I can see this code still ends up redirecting through a set
of static variables that hold function pointers. As such attempting
to use a single library instantiation simultaneously from multiple
different plugins will fail?

This is, currently at least, not a problem for us, however as we
statically link against a library and are hence sure that we will be
the only callers at a time.

The attached patch is a simple change that follows the approach we
took for lcms v1. If we define 'CMS_USER_ALLOC' when the lib is
built, we get to define cms{Malloc,Realloc,Free}DefaultFn, and the
library will link and use those.

This is working locally, and seems a relatively non-invasive change
to your library.

Thinking about again however, while writing this email, I wonder if a
better solution would not be to simply expose
_cmsRegisterMemHandlerPlugin (or something like it) to the outside
world. This would enable non-plugin callers to provide their own
malloc/realloc/free functions.

It would leave the library depending on malloc/realloc/free, but
they'd never be called. I suspect this is unlikely to ever be a
serious issue.

Thanks for your time and effort in creating a great library.

Robin
-- 

diff --git a/include/lcms2.h b/include/lcms2.h
index 16c1b46..6bb6055 100644
--- a/include/lcms2.h
+++ b/include/lcms2.h
@@ -52,6 +52,11 @@
 // require "KEYWORD" on undefined identifiers, keep it comented out unless 
needed
 // #define CMS_STRICT_CGATS  1
 
+// Uncomment this line if you plan to provide the default cms malloc/realloc/
+// free functions outside of lcms.
+
+// #define CMS_USER_ALLOC 1
+
 // ********** End of configuration toggles ******************************
 
 // Needed for streams
@@ -1728,6 +1733,21 @@ CMSAPI cmsBool          CMSEXPORT 
cmsDesaturateLab(cmsCIELab* Lab,
                                                    double amax, double amin,
                                                    double bmax, double bmin);
 
+// Default memory handling  
----------------------------------------------------------------------------------------
+
+#ifdef CMS_USER_ALLOC
+
+// If CMS_USER_ALLOC is defined, then the libraries environment will provide
+// the following 3 functions.
+
+void* cmsMallocDefaultFn(cmsContext ContextID, cmsUInt32Number size);
+
+void cmsFreeDefaultFn(cmsContext ContextID, void *Ptr);
+
+void* cmsReallocDefaultFn(cmsContext ContextID, void* Ptr, cmsUInt32Number 
size);
+
+#endif
+
 #ifndef CMS_USE_CPP_API
 #   ifdef __cplusplus
     }
diff --git a/src/cmserr.c b/src/cmserr.c
index caa17e5..552af36 100644
--- a/src/cmserr.c
+++ b/src/cmserr.c
@@ -78,6 +78,15 @@ cmsBool   _cmsRegisterMemHandlerPlugin(cmsPluginBase* 
Plugin);
 
 // 
*********************************************************************************
 
+#ifdef CMS_USER_ALLOC
+
+// The following 3 functions will be provided by the libraries environment
+#define _cmsMallocDefaultFn cmsMallocDefaultFn
+#define _cmsFreeDefaultFn cmsFreeDefaultFn
+#define _cmsReallocDefaultFn cmsReallocDefaultFn
+
+#else
+
 // This is the default memory allocation function. It does a very coarse 
 // check of amout of memory, just to prevent exploits
 static
@@ -90,18 +99,6 @@ void* _cmsMallocDefaultFn(cmsContext ContextID, 
cmsUInt32Number size)
     cmsUNUSED_PARAMETER(ContextID);
 }
 
-// Generic allocate & zero
-static
-void* _cmsMallocZeroDefaultFn(cmsContext ContextID, cmsUInt32Number size)
-{
-    void *pt = _cmsMalloc(ContextID, size);
-    if (pt == NULL) return NULL;
-
-    memset(pt, 0, size);
-    return pt;
-}
-
-
 // The default free function. The only check proformed is against NULL pointers
 static
 void _cmsFreeDefaultFn(cmsContext ContextID, void *Ptr)
@@ -126,6 +123,19 @@ void* _cmsReallocDefaultFn(cmsContext ContextID, void* 
Ptr, cmsUInt32Number size
 
     cmsUNUSED_PARAMETER(ContextID);
 }
+#endif
+
+
+// Generic allocate & zero
+static
+void* _cmsMallocZeroDefaultFn(cmsContext ContextID, cmsUInt32Number size)
+{
+    void *pt = _cmsMalloc(ContextID, size);
+    if (pt == NULL) return NULL;
+
+    memset(pt, 0, size);
+    return pt;
+}
 
 
 // The default calloc function. Allocates an array of num elements, each one 
of size bytes

------------------------------------------------------------------------------
Xperia(TM) PLAY
It's a major breakthrough. An authentic gaming
smartphone on the nation's most reliable network.
And it wants your games.
http://p.sf.net/sfu/verizon-sfdev
_______________________________________________
Lcms-user mailing list
Lcms-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/lcms-user

Reply via email to