comparison mupdf-source/source/fitz/leptonica-wrap.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
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
23 #include "leptonica-wrap.h"
24
25 #ifdef HAVE_LEPTONICA
26
27 #include "allheaders.h"
28
29 /* When we build with our own leptonica, we want to intercept malloc/free etc.
30 * Unfortunately we have to use a nasty global here. */
31 static fz_context *leptonica_mem = NULL;
32
33 void *leptonica_malloc(size_t size)
34 {
35 void *ret = Memento_label(fz_malloc_no_throw(leptonica_mem, size), "leptonica_malloc");
36 #ifdef DEBUG_ALLOCS
37 printf("%d LEPTONICA_MALLOC(%p) %d -> %p\n", event++, leptonica_mem, (int)size, ret);
38 fflush(stdout);
39 #endif
40 return ret;
41 }
42
43 void leptonica_free(void *ptr)
44 {
45 #ifdef DEBUG_ALLOCS
46 printf("%d LEPTONICA_FREE(%p) %p\n", event++, leptonica_mem, ptr);
47 fflush(stdout);
48 #endif
49 fz_free(leptonica_mem, ptr);
50 }
51
52 void *leptonica_calloc(size_t numelm, size_t elemsize)
53 {
54 void *ret = leptonica_malloc(numelm * elemsize);
55
56 if (ret)
57 memset(ret, 0, numelm * elemsize);
58 #ifdef DEBUG_ALLOCS
59 printf("%d LEPTONICA_CALLOC %d,%d -> %p\n", event++, (int)numelm, (int)elemsize, ret);
60 fflush(stdout);
61 #endif
62 return ret;
63 }
64
65 /* Not currently actually used */
66 void *leptonica_realloc(void *ptr, size_t blocksize)
67 {
68 void *ret = fz_realloc_no_throw(leptonica_mem, ptr, blocksize);
69
70 #ifdef DEBUG_ALLOCS
71 printf("%d LEPTONICA_REALLOC %p,%d -> %p\n", event++, ptr, (int)blocksize, ret);
72 fflush(stdout);
73 #endif
74 return ret;
75 }
76
77 void
78 fz_set_leptonica_mem(fz_context *ctx)
79 {
80 int die = 0;
81
82 fz_lock(ctx, FZ_LOCK_ALLOC);
83 die = (leptonica_mem != NULL);
84 if (!die)
85 leptonica_mem = ctx;
86 fz_unlock(ctx, FZ_LOCK_ALLOC);
87 if (die)
88 fz_throw(ctx, FZ_ERROR_ARGUMENT, "Attempt to use Leptonica from 2 threads at once!");
89 setPixMemoryManager(leptonica_malloc, leptonica_free);
90 }
91
92 void
93 fz_clear_leptonica_mem(fz_context *ctx)
94 {
95 int die = 0;
96
97 fz_lock(ctx, FZ_LOCK_ALLOC);
98 die = (leptonica_mem == NULL);
99 if (!die)
100 leptonica_mem = NULL;
101 fz_unlock(ctx, FZ_LOCK_ALLOC);
102 if (die)
103 fz_throw(ctx, FZ_ERROR_ARGUMENT, "Attempt to use Leptonica from 2 threads at once!");
104 setPixMemoryManager(malloc, free);
105 }
106
107 #endif /* HAVE_LEPTONICA */