jpeg-7-maxmem_sysconf.patch 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Make a reasonable guess about memory limits using sysconf().
  2. # includes 5% slop factor as suggested in documentation.
  3. --- jpeg-7/jmemansi.c
  4. +++ jpeg-7/jmemansi.c
  5. @@ -12,6 +12,15 @@
  6. * is shoved onto the user.
  7. */
  8. +#include <unistd.h>
  9. +
  10. +#ifdef __FreeBSD__
  11. +# include <sys/types.h>
  12. +# include <sys/sysctl.h>
  13. +# include <sys/vmmeter.h>
  14. +# include <vm/vm_param.h>
  15. +#endif
  16. +
  17. #define JPEG_INTERNALS
  18. #include "jinclude.h"
  19. #include "jpeglib.h"
  20. @@ -157,7 +166,26 @@
  21. GLOBAL(long)
  22. jpeg_mem_init (j_common_ptr cinfo)
  23. {
  24. - return DEFAULT_MAX_MEM; /* default for max_memory_to_use */
  25. +#ifdef _SC_AVPHYS_PAGES
  26. + long phys_size;
  27. +
  28. + if ((phys_size = sysconf(_SC_AVPHYS_PAGES)) == -1)
  29. + return DEFAULT_MAX_MEM; /* default for max_memory_to_use */
  30. + if ((phys_size *= sysconf(_SC_PAGESIZE)) < 0)
  31. + return DEFAULT_MAX_MEM;
  32. + return (long) (phys_size * 0.95);
  33. +#elif defined(HAVE_SYSCTL) && defined(HW_PHYSMEM)
  34. + /* This works on *bsd and darwin. */
  35. + unsigned int physmem;
  36. + size_t len = sizeof physmem;
  37. + static int mib[2] = { CTL_HW, HW_PHYSMEM };
  38. +
  39. + if (sysctl (mib, ARRAY_SIZE (mib), &physmem, &len, NULL, 0) == 0
  40. + && len == sizeof (physmem))
  41. + return (long) (physmem * 0.95);
  42. +#endif
  43. +
  44. + return DEFAULT_MAX_MEM;
  45. }
  46. GLOBAL(void)