GraphicsMagick: Call jas_conf_set_allocator() and pass the maxim...

GraphicsMagick Commits <[email protected]>
Newsgroups gmane.comp.video.graphicsmagick.cvs
Message-ID <mailman.43763.1640305232.1346.graphicsmagick-commit@lists.sourceforge.net>
changeset 3b613b68681b in /hg/GraphicsMagick
details: http://hg.GraphicsMagick.org/hg/GraphicsMagick?cmd=changeset;node=3b613b68681b
summary: Call jas_conf_set_allocator() and pass the maximum amount of memory it should be allocated to use.  This seems to avoid a malfunction.  Re-enable use of jas_initialize().

diffstat:

 ChangeLog          |   3 +++
 coders/jp2.c       |  48 ++++++++++++++++++++++++++++++++++++++++++++----
 www/Changelog.html |   5 ++++-
 3 files changed, 51 insertions(+), 5 deletions(-)

diffs (123 lines):

diff -r 033b61f33d55 -r 3b613b68681b ChangeLog
--- a/ChangeLog	Thu Dec 23 16:59:53 2021 -0600
+++ b/ChangeLog	Thu Dec 23 18:20:21 2021 -0600
@@ -2,6 +2,9 @@
 
         * coders/jp2.c: Properly support passing JasPer options for
         decoder and encoder, including the 'debug' option.
+        (initialize_jasper): Call jas_conf_set_allocator() and pass the
+        maximum amount of memory it should be allocated to use.  This
+        seems to avoid a malfunction.  Re-enable use of jas_initialize().
 
 2021-12-22  Bob Friesenhahn  <[email protected]>
 
diff -r 033b61f33d55 -r 3b613b68681b coders/jp2.c
--- a/coders/jp2.c	Thu Dec 23 16:59:53 2021 -0600
+++ b/coders/jp2.c	Thu Dec 23 18:20:21 2021 -0600
@@ -92,10 +92,12 @@
 #    undef HAVE_PGX_DECODE
 #  endif
 
+#if 0
 /* Development JasPer 3.0.0 jas_initialize() is not yet ready for our purposes */
 #if !(defined(MAGICK_ENABLE_JAS_INITIALIZE) && MAGICK_ENABLE_JAS_INITIALIZE)
 #undef HAVE_JAS_INITIALIZE
 #endif /* if !defined(ENABLE_JAS_INITIALIZE) */
+#endif
 
 
 /*
@@ -428,20 +430,27 @@
 #if HAVE_JAS_INITIALIZE
 static void *alloc_rlm(struct jas_allocator_s *allocator, size_t size)
 {
+  char *p;
   (void) allocator;
   /* JasPer expects its allocator to return non-null for zero size */
-  return _MagickAllocateResourceLimitedMemory(size == 0 ? 1 : size);
+  p=_MagickAllocateResourceLimitedMemory(size == 0 ? 1 : size);
+  /* fprintf(stderr,"alloc_rlm(%p, %zu) -> %p\n", allocator, size, p); */
+  return p;
 }
 static void free_rlm(struct jas_allocator_s *allocator, void *pointer)
 {
   (void) allocator;
+  /* fprintf(stderr,"free_rlm(%p, %p\n", allocator, pointer); */
   _MagickFreeResourceLimitedMemory(pointer);
 }
 static void *realloc_rlm(struct jas_allocator_s *allocator, void *pointer,
                          size_t new_size)
 {
+  char *p;
   (void) allocator;
-  return _MagickReallocateResourceLimitedMemory(pointer,1,new_size,0);
+  p =_MagickReallocateResourceLimitedMemory(pointer,1,new_size,0);
+  /* fprintf(stderr,"realloc_rlm(%p, %p, %zu) -> %p\n", allocator, pointer, new_size, p); */
+  return p;
 }
 #endif /* if HAVE_JAS_INITIALIZE */
 static void initialize_jasper(void)
@@ -455,15 +464,46 @@
         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
                               "Initializing JasPer...");
         jas_conf_clear();
-        /* Use our own resource-limited memory allocation functions */
+        /*
+          Provide our own resource-limited memory allocation
+          functions.
+
+          See src/libjasper/include/jasper/jas_malloc.h
+        */
+
+        /*
+          Function to clean up the allocator when no longer needed.
+          The allocator cannot be used after the clean-up operation is performed.
+          This function pointer may be null, in which case the clean-up operation
+          is treated as a no-op.
+        */
         allocator.cleanup = 0;
+
+        /*
+          Function to allocate memory.
+          This function should have behavior similar to malloc.
+        */
         allocator.alloc = alloc_rlm;
+
+        /*
+          Function to deallocate memory.
+          This function should have behavior similar to free.
+        */
         allocator.free = free_rlm;
+
+        /*
+          Function to reallocate memory.
+          This function should have behavior similar to realloc.
+        */
         allocator.realloc = realloc_rlm;
         /* jas_std_allocator_init(&allocator); */ /* Uses JasPer allocators */
         jas_conf_set_allocator(&allocator); /* Assigns jas_allocator_t to jas_conf.allocator in library */
         /* jas_conf_set_debug_level(cmdopts->debug); */
-        /* jas_conf_set_max_mem(cmdopts->max_mem); */ /* Only pertains to JasPer allocators */
+
+        /*
+          Tell JasPer how much memory it could ever be allowed to use.
+        */
+        jas_conf_set_max_mem((size_t) GetMagickResourceLimit(MemoryResource));
 
         if (jas_initialize() == 0)
           {
diff -r 033b61f33d55 -r 3b613b68681b www/Changelog.html
--- a/www/Changelog.html	Thu Dec 23 16:59:53 2021 -0600
+++ b/www/Changelog.html	Thu Dec 23 18:20:21 2021 -0600
@@ -38,7 +38,10 @@
 <p>2021-12-23  Bob Friesenhahn  &lt;<a class="reference external" href="mailto:bfriesen&#37;&#52;&#48;simple&#46;dallas&#46;tx&#46;us">bfriesen<span>&#64;</span>simple<span>&#46;</span>dallas<span>&#46;</span>tx<span>&#46;</span>us</a>&gt;</p>
 <blockquote>
 * coders/jp2.c: Properly support passing JasPer options for
-decoder and encoder, including the 'debug' option.</blockquote>
+decoder and encoder, including the 'debug' option.
+(initialize_jasper): Call jas_conf_set_allocator() and pass the
+maximum amount of memory it should be allocated to use.  This
+seems to avoid a malfunction.  Re-enable use of jas_initialize().</blockquote>
 <p>2021-12-22  Bob Friesenhahn  &lt;<a class="reference external" href="mailto:bfriesen&#37;&#52;&#48;simple&#46;dallas&#46;tx&#46;us">bfriesen<span>&#64;</span>simple<span>&#46;</span>dallas<span>&#46;</span>tx<span>&#46;</span>us</a>&gt;</p>
 <blockquote>
 <p>* coders/jp2.c (realloc_rlm): JasPer wants its custom memory
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.