comparison mupdf-source/source/fitz/pool.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) 2004-2025 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 "mupdf/fitz.h"
24
25 #include <string.h>
26 #include <stdio.h>
27
28 typedef struct fz_pool_node
29 {
30 struct fz_pool_node *next;
31 char mem[FZ_FLEXIBLE_ARRAY];
32 } fz_pool_node;
33
34 #define POOL_SIZE (4<<10) /* default size of pool blocks */
35 #define POOL_SELF (1<<10) /* size where allocs are put into their own blocks */
36
37 struct fz_pool
38 {
39 size_t size;
40 fz_pool_node *head, *tail;
41 char *pos, *end;
42 };
43
44
45 fz_pool *fz_new_pool(fz_context *ctx)
46 {
47 fz_pool *pool;
48 fz_pool_node *node = NULL;
49
50 pool = fz_malloc_struct(ctx, fz_pool);
51 fz_try(ctx)
52 {
53 node = Memento_label(fz_calloc(ctx, offsetof(fz_pool_node, mem) + POOL_SIZE, 1), "fz_pool_block");
54 pool->head = pool->tail = node;
55 pool->pos = node->mem;
56 pool->end = node->mem + POOL_SIZE;
57 }
58 fz_catch(ctx)
59 {
60 fz_free(ctx, pool);
61 fz_rethrow(ctx);
62 }
63
64 return pool;
65 }
66
67 static void *fz_pool_alloc_oversize(fz_context *ctx, fz_pool *pool, size_t size)
68 {
69 fz_pool_node *node;
70
71 /* link in memory at the head of the list */
72 node = Memento_label(fz_calloc(ctx, offsetof(fz_pool_node, mem) + size, 1), "fz_pool_oversize");
73 node->next = pool->head;
74 pool->head = node;
75 pool->size += offsetof(fz_pool_node, mem) + size;
76
77 return node->mem;
78 }
79
80 void *fz_pool_alloc(fz_context *ctx, fz_pool *pool, size_t size)
81 {
82 char *ptr;
83
84 if (size >= POOL_SELF)
85 return fz_pool_alloc_oversize(ctx, pool, size);
86
87 /* round size to pointer alignment (we don't expect to use doubles) */
88 size = (size + FZ_POINTER_ALIGN_MOD - 1) & ~(FZ_POINTER_ALIGN_MOD-1);
89
90 if (pool->pos + size > pool->end)
91 {
92 fz_pool_node *node = Memento_label(fz_calloc(ctx, offsetof(fz_pool_node, mem) + POOL_SIZE, 1), "fz_pool_block");
93 pool->tail = pool->tail->next = node;
94 pool->pos = node->mem;
95 pool->end = node->mem + POOL_SIZE;
96 pool->size += offsetof(fz_pool_node, mem) + POOL_SIZE;
97 }
98 ptr = pool->pos;
99 pool->pos += size;
100 return ptr;
101 }
102
103 char *fz_pool_strdup(fz_context *ctx, fz_pool *pool, const char *s)
104 {
105 size_t n = strlen(s) + 1;
106 char *p = fz_pool_alloc(ctx, pool, n);
107 memcpy(p, s, n);
108 return p;
109 }
110
111 size_t fz_pool_size(fz_context *ctx, fz_pool *pool)
112 {
113 return pool ? pool->size : 0;
114 }
115
116 void fz_drop_pool(fz_context *ctx, fz_pool *pool)
117 {
118 fz_pool_node *node;
119
120 if (!pool)
121 return;
122
123 node = pool->head;
124 while (node)
125 {
126 fz_pool_node *next = node->next;
127 fz_free(ctx, node);
128 node = next;
129 }
130 fz_free(ctx, pool);
131 }