leptonica-wrap.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (C) 2020-2023 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "leptonica-wrap.h"
  23. #ifdef HAVE_LEPTONICA
  24. #include "allheaders.h"
  25. /* When we build with our own leptonica, we want to intercept malloc/free etc.
  26. * Unfortunately we have to use a nasty global here. */
  27. static fz_context *leptonica_mem = NULL;
  28. void *leptonica_malloc(size_t size)
  29. {
  30. void *ret = Memento_label(fz_malloc_no_throw(leptonica_mem, size), "leptonica_malloc");
  31. #ifdef DEBUG_ALLOCS
  32. printf("%d LEPTONICA_MALLOC(%p) %d -> %p\n", event++, leptonica_mem, (int)size, ret);
  33. fflush(stdout);
  34. #endif
  35. return ret;
  36. }
  37. void leptonica_free(void *ptr)
  38. {
  39. #ifdef DEBUG_ALLOCS
  40. printf("%d LEPTONICA_FREE(%p) %p\n", event++, leptonica_mem, ptr);
  41. fflush(stdout);
  42. #endif
  43. fz_free(leptonica_mem, ptr);
  44. }
  45. void *leptonica_calloc(size_t numelm, size_t elemsize)
  46. {
  47. void *ret = leptonica_malloc(numelm * elemsize);
  48. if (ret)
  49. memset(ret, 0, numelm * elemsize);
  50. #ifdef DEBUG_ALLOCS
  51. printf("%d LEPTONICA_CALLOC %d,%d -> %p\n", event++, (int)numelm, (int)elemsize, ret);
  52. fflush(stdout);
  53. #endif
  54. return ret;
  55. }
  56. /* Not currently actually used */
  57. void *leptonica_realloc(void *ptr, size_t blocksize)
  58. {
  59. void *ret = fz_realloc_no_throw(leptonica_mem, ptr, blocksize);
  60. #ifdef DEBUG_ALLOCS
  61. printf("%d LEPTONICA_REALLOC %p,%d -> %p\n", event++, ptr, (int)blocksize, ret);
  62. fflush(stdout);
  63. #endif
  64. return ret;
  65. }
  66. void
  67. fz_set_leptonica_mem(fz_context *ctx)
  68. {
  69. int die = 0;
  70. fz_lock(ctx, FZ_LOCK_ALLOC);
  71. die = (leptonica_mem != NULL);
  72. if (!die)
  73. leptonica_mem = ctx;
  74. fz_unlock(ctx, FZ_LOCK_ALLOC);
  75. if (die)
  76. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Attempt to use Leptonica from 2 threads at once!");
  77. setPixMemoryManager(leptonica_malloc, leptonica_free);
  78. }
  79. void
  80. fz_clear_leptonica_mem(fz_context *ctx)
  81. {
  82. int die = 0;
  83. fz_lock(ctx, FZ_LOCK_ALLOC);
  84. die = (leptonica_mem == NULL);
  85. if (!die)
  86. leptonica_mem = NULL;
  87. fz_unlock(ctx, FZ_LOCK_ALLOC);
  88. if (die)
  89. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Attempt to use Leptonica from 2 threads at once!");
  90. setPixMemoryManager(malloc, free);
  91. }
  92. #endif /* HAVE_LEPTONICA */